Skip to content

Commit

Permalink
Supports fixed price fee (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
boqiu authored Apr 2, 2024
1 parent 1d09ec4 commit a5055ab
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
26 changes: 26 additions & 0 deletions contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package contract

import (
"fmt"
"math/big"

"github.com/0glabs/0g-storage-client/common/blockchain"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -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)
}
6 changes: 6 additions & 0 deletions transfer/uploader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package transfer

import (
"math/big"
"runtime"
"time"

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a5055ab

Please sign in to comment.