Skip to content

Commit

Permalink
fixed estimate gas limit instead by hard code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tien Ngo committed Mar 21, 2024
1 parent 4e5cbb3 commit 3c7382f
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 55 deletions.
10 changes: 10 additions & 0 deletions contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

Expand All @@ -27,14 +28,23 @@ type Contract interface {
// Pack packs the method and arguments into a byte array representing
// the smart contract call data
Pack(
wallet *Wallet,
method string,
arguments json.RawMessage) ([]byte, error)

// Parse parses the arguments as JSON format into an array of smart
// contract function call arguments
Parse(
wallet *Wallet,
method string,
arguments json.RawMessage) ([]interface{}, error)

// EstimateGasLimit estimates the gas limit for a smart contract method call
EstimateGasLimit(
wallet *Wallet,
contractAddr common.Address,
method string,
arguments json.RawMessage) (uint64, error)
}

// ContractFactory is a function that takes an address and return a Contract instance
Expand Down
49 changes: 43 additions & 6 deletions contracts/feralfile-airdrop-v1/airdrop.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package airdropv1

import (
"context"
"encoding/json"
"fmt"
"math/big"

ethereum "github.com/bitmark-inc/account-vault-ethereum"
airdropv1 "github.com/bitmark-inc/feralfile-exhibition-smart-contract/go-binding/feralfile-airdrop-v1"
goEthereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -66,8 +68,9 @@ func (c *FeralFileAirdropV1Contract) Call(
noSend bool,
customizeGasPriceInWei *int64,
customizedNonce *uint64) (*types.Transaction, error) {
contractAddr := common.HexToAddress(c.contractAddress)
contract, err := airdropv1.NewFeralFileAirdropV1(
common.HexToAddress(c.contractAddress),
contractAddr,
wallet.RPCClient())
if err != nil {
return nil, err
Expand All @@ -87,7 +90,7 @@ func (c *FeralFileAirdropV1Contract) Call(
t.Nonce = big.NewInt(int64(*customizedNonce))
}

params, err := c.Parse(method, arguments)
params, err := c.Parse(wallet, method, arguments)
if nil != err {
return nil, err
}
Expand All @@ -108,7 +111,12 @@ func (c *FeralFileAirdropV1Contract) Call(
return nil, fmt.Errorf("invalid amount")
}

t.GasLimit = 100000
gasLimit, err := c.EstimateGasLimit(wallet, contractAddr, method, arguments)
if nil != err {
return nil, err
}

t.GasLimit = gasLimit
return contract.Mint(t, tokenID, amount)
case "airdrop":
if len(params) != 2 {
Expand All @@ -125,8 +133,12 @@ func (c *FeralFileAirdropV1Contract) Call(
return nil, fmt.Errorf("invalid to address")
}

const gasLimitPerItem = 150000
t.GasLimit = uint64(gasLimitPerItem * len(to))
gasLimit, err := c.EstimateGasLimit(wallet, contractAddr, method, arguments)
if nil != err {
return nil, err
}

t.GasLimit = gasLimit

return contract.Airdrop(t, tokenID, to)
}
Expand All @@ -135,14 +147,15 @@ func (c *FeralFileAirdropV1Contract) Call(
}

func (c *FeralFileAirdropV1Contract) Pack(
wallet *ethereum.Wallet,
method string,
arguments json.RawMessage) ([]byte, error) {
abi, err := airdropv1.FeralFileAirdropV1MetaData.GetAbi()
if nil != err {
return nil, err
}

parsedArgs, err := c.Parse(method, arguments)
parsedArgs, err := c.Parse(wallet, method, arguments)
if nil != err {
return nil, err
}
Expand All @@ -151,6 +164,7 @@ func (c *FeralFileAirdropV1Contract) Pack(
}

func (c *FeralFileAirdropV1Contract) Parse(
wallet *ethereum.Wallet,
method string,
arguments json.RawMessage) ([]interface{}, error) {
switch method {
Expand Down Expand Up @@ -184,6 +198,29 @@ func (c *FeralFileAirdropV1Contract) Parse(
}
}

func (c *FeralFileAirdropV1Contract) EstimateGasLimit(
wallet *ethereum.Wallet,
contractAddr common.Address,
method string,
arguments json.RawMessage) (uint64, error) {
data, err := c.Pack(wallet, method, arguments)
if nil != err {
return 0, err
}

gas, err := wallet.RPCClient().EstimateGas(context.Background(), goEthereum.CallMsg{
From: common.HexToAddress(wallet.Account()),
To: &contractAddr,
Data: data,
})

if nil != err {
return 0, err
}

return gas * 115 / 100, nil // add 15% buffer
}

func init() {
ethereum.RegisterContract("FeralFileAirdropV1", FeralFileAirdropV1ContractFactory)
}
41 changes: 38 additions & 3 deletions contracts/feralfile-english-auction/english_auction.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package english_auction

import (
"context"
"encoding/hex"
"encoding/json"
"errors"
Expand All @@ -9,6 +10,7 @@ import (
"strconv"
"strings"

goEthereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -60,7 +62,8 @@ func (c *FeralfileEnglishAuctionContract) Call(
noSend bool,
customizeGasPriceInWei *int64,
customizedNonce *uint64) (*types.Transaction, error) {
contract, err := english_auction.NewFeralfileEnglishAuction(common.HexToAddress(c.contractAddress), wallet.RPCClient())
contractAddr := common.HexToAddress(c.contractAddress)
contract, err := english_auction.NewFeralfileEnglishAuction(contractAddr, wallet.RPCClient())
if err != nil {
return nil, err
}
Expand All @@ -78,7 +81,7 @@ func (c *FeralfileEnglishAuctionContract) Call(
t.Nonce = big.NewInt(int64(*customizedNonce))
}

params, err := c.Parse(method, arguments)
params, err := c.Parse(wallet, method, arguments)
if nil != err {
return nil, err
}
Expand All @@ -94,6 +97,13 @@ func (c *FeralfileEnglishAuctionContract) Call(
return nil, fmt.Errorf("invalid auctions params")
}

gasLimit, err := c.EstimateGasLimit(wallet, contractAddr, method, arguments)
if nil != err {
return nil, err
}

t.GasLimit = gasLimit

return contract.RegisterAuctions(t, auctions)
case "placeSignedBid":
if len(params) != 7 {
Expand Down Expand Up @@ -203,14 +213,15 @@ func (c *FeralfileEnglishAuctionContract) Call(
}

func (c *FeralfileEnglishAuctionContract) Pack(
wallet *ethereum.Wallet,
method string,
arguments json.RawMessage) ([]byte, error) {
abi, err := english_auction.FeralfileEnglishAuctionMetaData.GetAbi()
if nil != err {
return nil, err
}

parsedArgs, err := c.Parse(method, arguments)
parsedArgs, err := c.Parse(wallet, method, arguments)
if nil != err {
return nil, err
}
Expand All @@ -219,6 +230,7 @@ func (c *FeralfileEnglishAuctionContract) Pack(
}

func (c *FeralfileEnglishAuctionContract) Parse(
wallet *ethereum.Wallet,
method string,
arguments json.RawMessage) ([]interface{}, error) {
switch method {
Expand Down Expand Up @@ -390,6 +402,29 @@ func (c *FeralfileEnglishAuctionContract) Parse(
}
}

func (c *FeralfileEnglishAuctionContract) EstimateGasLimit(
wallet *ethereum.Wallet,
contractAddr common.Address,
method string,
arguments json.RawMessage) (uint64, error) {
data, err := c.Pack(wallet, method, arguments)
if nil != err {
return 0, err
}

gas, err := wallet.RPCClient().EstimateGas(context.Background(), goEthereum.CallMsg{
From: common.HexToAddress(wallet.Account()),
To: &contractAddr,
Data: data,
})

if nil != err {
return 0, err
}

return gas * 115 / 100, nil // add 15% buffer
}

func init() {
ethereum.RegisterContract("FeralfileEnglishAuction", FeralfileEnglishAuctionFactory)
}
Loading

0 comments on commit 3c7382f

Please sign in to comment.