Skip to content

Commit

Permalink
add gas limit as mandatory param
Browse files Browse the repository at this point in the history
  • Loading branch information
jollyjoker992 committed Mar 21, 2024
1 parent 3c7382f commit 1d0405e
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 366 deletions.
15 changes: 3 additions & 12 deletions contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"

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

Expand All @@ -22,29 +21,21 @@ type Contract interface {
fund string,
arguments json.RawMessage,
noSend bool,
customizeGasPriceInWei *int64,
customizedNonce *uint64) (tx *types.Transaction, err error)
gasLimit uint64,
gasPrice *int64,
nonce *uint64) (tx *types.Transaction, err error)

// 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
58 changes: 10 additions & 48 deletions contracts/feralfile-airdrop-v1/airdrop.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
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 +64,9 @@ func (c *FeralFileAirdropV1Contract) Call(
fund string,
arguments json.RawMessage,
noSend bool,
customizeGasPriceInWei *int64,
customizedNonce *uint64) (*types.Transaction, error) {
gasLimit uint64,
gasPrice *int64,
nonce *uint64) (*types.Transaction, error) {
contractAddr := common.HexToAddress(c.contractAddress)
contract, err := airdropv1.NewFeralFileAirdropV1(
contractAddr,
Expand All @@ -82,15 +81,16 @@ func (c *FeralFileAirdropV1Contract) Call(
}

t.NoSend = noSend
if customizeGasPriceInWei != nil && *customizeGasPriceInWei != 0 {
t.GasPrice = big.NewInt(*customizeGasPriceInWei * params.Wei)
t.GasLimit = gasLimit
if gasPrice != nil && *gasPrice != 0 {
t.GasPrice = big.NewInt(*gasPrice * params.Wei)
}

if customizedNonce != nil {
t.Nonce = big.NewInt(int64(*customizedNonce))
if nonce != nil {
t.Nonce = big.NewInt(int64(*nonce))
}

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

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 @@ -133,29 +127,21 @@ func (c *FeralFileAirdropV1Contract) Call(
return nil, fmt.Errorf("invalid to address")
}

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

t.GasLimit = gasLimit

return contract.Airdrop(t, tokenID, to)
}

return nil, fmt.Errorf("unsupported method")
}

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(wallet, method, arguments)
parsedArgs, err := c.Parse(method, arguments)
if nil != err {
return nil, err
}
Expand All @@ -164,7 +150,6 @@ func (c *FeralFileAirdropV1Contract) Pack(
}

func (c *FeralFileAirdropV1Contract) Parse(
wallet *ethereum.Wallet,
method string,
arguments json.RawMessage) ([]interface{}, error) {
switch method {
Expand Down Expand Up @@ -198,29 +183,6 @@ 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)
}
52 changes: 10 additions & 42 deletions contracts/feralfile-english-auction/english_auction.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package english_auction

import (
"context"
"encoding/hex"
"encoding/json"
"errors"
Expand All @@ -10,7 +9,6 @@ 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,8 +58,9 @@ func (c *FeralfileEnglishAuctionContract) Call(
fund string,
arguments json.RawMessage,
noSend bool,
customizeGasPriceInWei *int64,
customizedNonce *uint64) (*types.Transaction, error) {
gasLimit uint64,
gasPrice *int64,
nonce *uint64) (*types.Transaction, error) {
contractAddr := common.HexToAddress(c.contractAddress)
contract, err := english_auction.NewFeralfileEnglishAuction(contractAddr, wallet.RPCClient())
if err != nil {
Expand All @@ -73,15 +72,16 @@ func (c *FeralfileEnglishAuctionContract) Call(
}

t.NoSend = noSend
if customizeGasPriceInWei != nil && *customizeGasPriceInWei != 0 {
t.GasPrice = big.NewInt(*customizeGasPriceInWei * params.Wei)
t.GasLimit = gasLimit
if gasPrice != nil && *gasPrice != 0 {
t.GasPrice = big.NewInt(*gasPrice * params.Wei)
}

if customizedNonce != nil {
t.Nonce = big.NewInt(int64(*customizedNonce))
if nonce != nil {
t.Nonce = big.NewInt(int64(*nonce))
}

params, err := c.Parse(wallet, method, arguments)
params, err := c.Parse(method, arguments)
if nil != err {
return nil, err
}
Expand All @@ -97,13 +97,6 @@ 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 @@ -213,15 +206,14 @@ 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(wallet, method, arguments)
parsedArgs, err := c.Parse(method, arguments)
if nil != err {
return nil, err
}
Expand All @@ -230,7 +222,6 @@ func (c *FeralfileEnglishAuctionContract) Pack(
}

func (c *FeralfileEnglishAuctionContract) Parse(
wallet *ethereum.Wallet,
method string,
arguments json.RawMessage) ([]interface{}, error) {
switch method {
Expand Down Expand Up @@ -402,29 +393,6 @@ 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 1d0405e

Please sign in to comment.