From a5055ab7b625153e575c992bf724ec37f72f0f4e Mon Sep 17 00:00:00 2001 From: Bo QIU <35757521+boqiu@users.noreply.github.com> Date: Tue, 2 Apr 2024 11:31:16 +0800 Subject: [PATCH] Supports fixed price fee (#6) --- contract/contract.go | 26 ++++++++++++++++++++++++++ transfer/uploader.go | 6 ++++++ 2 files changed, 32 insertions(+) diff --git a/contract/contract.go b/contract/contract.go index 638239f..5f1924e 100644 --- a/contract/contract.go +++ b/contract/contract.go @@ -2,6 +2,7 @@ package contract import ( "fmt" + "math/big" "github.com/0glabs/0g-storage-client/common/blockchain" "github.com/ethereum/go-ethereum/common" @@ -56,3 +57,28 @@ func (submission Submission) Root() common.Hash { return root } + +const ( + LIFETIME_MONTHES = 3 + BYTES_PER_SECTOR = 256 + ANNUAL_ZGS_TOKENS_PER_GB = 10 + GB = 1024 * 1024 * 1024 + MONTH_PER_YEAR = 12 +) + +var pricePerSector = new(big.Int).Div( + new(big.Int).Mul( + big.NewInt(LIFETIME_MONTHES*BYTES_PER_SECTOR*ANNUAL_ZGS_TOKENS_PER_GB/MONTH_PER_YEAR), + new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil), // ether: 10^18 + ), + big.NewInt(GB), +) + +func (submission Submission) Fee() *big.Int { + var sectors int64 + for _, node := range submission.Nodes { + sectors += 1 << node.Height.Int64() + } + + return big.NewInt(0).Mul(big.NewInt(sectors), pricePerSector) +} diff --git a/transfer/uploader.go b/transfer/uploader.go index 0ca6cef..8f5b483 100644 --- a/transfer/uploader.go +++ b/transfer/uploader.go @@ -1,6 +1,7 @@ package transfer import ( + "math/big" "runtime" "time" @@ -249,8 +250,13 @@ func (uploader *Uploader) SubmitLogEntry(datas []core.IterableData, tags [][]byt var tx *types.Transaction if len(datas) == 1 { + opts.Value = submissions[0].Fee() tx, err = uploader.flow.Submit(opts, submissions[0]) } else { + opts.Value = big.NewInt(0) + for _, v := range submissions { + opts.Value = new(big.Int).Add(opts.Value, v.Fee()) + } tx, err = uploader.flow.BatchSubmit(opts, submissions) } if err != nil {