Skip to content

Commit

Permalink
Merge pull request #24 from bitmark-inc/fix/2081-bug-out-of-gas-becau…
Browse files Browse the repository at this point in the history
…se-gas-limit

Fixed estimate gas limit instead by hard code
  • Loading branch information
jollyjoker992 authored Mar 25, 2024
2 parents 4e5cbb3 + f6353de commit c8f8857
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 122 deletions.
5 changes: 3 additions & 2 deletions contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ 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
Expand Down
21 changes: 10 additions & 11 deletions contracts/feralfile-airdrop-v1/airdrop.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ 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(
common.HexToAddress(c.contractAddress),
contractAddr,
wallet.RPCClient())
if err != nil {
return nil, err
Expand All @@ -79,12 +81,13 @@ 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(method, arguments)
Expand All @@ -108,7 +111,6 @@ func (c *FeralFileAirdropV1Contract) Call(
return nil, fmt.Errorf("invalid amount")
}

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

const gasLimitPerItem = 150000
t.GasLimit = uint64(gasLimitPerItem * len(to))

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

Expand Down
17 changes: 10 additions & 7 deletions contracts/feralfile-english-auction/english_auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ func (c *FeralfileEnglishAuctionContract) Call(
fund string,
arguments json.RawMessage,
noSend bool,
customizeGasPriceInWei *int64,
customizedNonce *uint64) (*types.Transaction, error) {
contract, err := english_auction.NewFeralfileEnglishAuction(common.HexToAddress(c.contractAddress), wallet.RPCClient())
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 {
return nil, err
}
Expand All @@ -70,12 +72,13 @@ 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(method, arguments)
Expand Down
72 changes: 38 additions & 34 deletions contracts/feralfile-exhibition-v2/feralfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ import (
feralfilev2 "github.com/bitmark-inc/feralfile-exhibition-smart-contract/go-binding/feralfile-exhibition-v2"
)

const (
GasLimitSwapArtworkFromBitmark = 400000
GasLimitTransfer = 120000
GasLimitApproveForAll = 60000
)

type FeralfileExhibitionV2Contract struct {
contractAddress string
}
Expand Down Expand Up @@ -70,9 +64,11 @@ func (c *FeralfileExhibitionV2Contract) Call(
fund string,
arguments json.RawMessage,
noSend bool,
customizeGasPriceInWei *int64,
customizedNonce *uint64) (*types.Transaction, error) {
contract, err := feralfilev2.NewFeralfileExhibitionV2(common.HexToAddress(c.contractAddress), wallet.RPCClient())
gasLimit uint64,
gasPrice *int64,
nonce *uint64) (*types.Transaction, error) {
contractAddr := common.HexToAddress(c.contractAddress)
contract, err := feralfilev2.NewFeralfileExhibitionV2(contractAddr, wallet.RPCClient())
if err != nil {
return nil, err
}
Expand All @@ -83,12 +79,13 @@ func (c *FeralfileExhibitionV2Contract) 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(method, arguments)
Expand All @@ -97,7 +94,7 @@ func (c *FeralfileExhibitionV2Contract) Call(
}

switch method {
case "create_artwork":
case "createArtwork":
if len(params) != 4 {
return nil, fmt.Errorf("invalid params")
}
Expand Down Expand Up @@ -128,7 +125,7 @@ func (c *FeralfileExhibitionV2Contract) Call(
title,
artistName,
editionSize)
case "swap_artwork_from_bitmark":
case "swapArtworkFromBitmark":
if len(params) != 5 {
return nil, fmt.Errorf("invalid params")
}
Expand Down Expand Up @@ -158,21 +155,24 @@ func (c *FeralfileExhibitionV2Contract) Call(
return nil, fmt.Errorf("invalid ipfs cid params")
}

t.GasLimit = GasLimitSwapArtworkFromBitmark

return contract.SwapArtworkFromBitmark(
t,
artworkID,
bitmarkID,
editionNumber,
to,
ipfsCID)
case "transfer":
if len(params) != 2 {
case "safeTransferFrom":
if len(params) != 3 {
return nil, fmt.Errorf("invalid params")
}

to, ok := params[0].(common.Address)
from, ok := params[0].(common.Address)
if !ok {
return nil, fmt.Errorf("invalid from params")
}

to, ok := params[1].(common.Address)
if !ok {
return nil, fmt.Errorf("invalid to params")
}
Expand All @@ -182,15 +182,13 @@ func (c *FeralfileExhibitionV2Contract) Call(
return nil, fmt.Errorf("invalid token id params")
}

t.GasLimit = GasLimitTransfer

return contract.SafeTransferFrom(
t,
common.HexToAddress(wallet.Account()),
from,
to,
tokenID)
case "approve_for_all":
if len(params) != 1 {
case "setApprovalForAll":
if len(params) != 2 {
return nil, fmt.Errorf("invalid params")
}

Expand All @@ -199,9 +197,12 @@ func (c *FeralfileExhibitionV2Contract) Call(
return nil, fmt.Errorf("invalid operator params")
}

t.GasLimit = GasLimitApproveForAll
approved, ok := params[1].(bool)
if !ok {
return nil, fmt.Errorf("invalid approved params")
}

return contract.SetApprovalForAll(t, operator, true)
return contract.SetApprovalForAll(t, operator, approved)
default:
return nil, fmt.Errorf("unsupported method")
}
Expand All @@ -227,7 +228,7 @@ func (c *FeralfileExhibitionV2Contract) Parse(
method string,
arguments json.RawMessage) ([]interface{}, error) {
switch method {
case "create_artwork":
case "createArtwork":
var params struct {
Fingerprint string `json:"fingerprint"`
Title string `json:"title"`
Expand All @@ -245,7 +246,7 @@ func (c *FeralfileExhibitionV2Contract) Parse(
params.ArtistName,
big.NewInt(params.EditionSize)},
nil
case "swap_artwork_from_bitmark":
case "swapArtworkFromBitmark":
var params struct {
ArtworkID ethereum.BigInt `json:"artwork_id"`
BitmarkID ethereum.BigInt `json:"bitmark_id"`
Expand All @@ -262,27 +263,30 @@ func (c *FeralfileExhibitionV2Contract) Parse(
&params.ArtworkID.Int,
&params.BitmarkID.Int,
&params.EditionNumber.Int,
params.To, params.IPFSCID}, nil
case "transfer":
params.To,
params.IPFSCID}, nil
case "safeTransferFrom":
var params struct {
From common.Address `json:"from"`
To common.Address `json:"to"`
TokenID ethereum.BigInt `json:"token_id"`
}
if err := json.Unmarshal(arguments, &params); err != nil {
return nil, err
}

return []interface{}{params.To, &params.TokenID.Int}, nil
return []interface{}{params.From, params.To, &params.TokenID.Int}, nil

case "approve_for_all":
case "setApprovalForAll":
var params struct {
Operator common.Address `json:"operator"`
Approved bool `json:"approved"`
}
if err := json.Unmarshal(arguments, &params); err != nil {
return nil, err
}

return []interface{}{params.Operator}, nil
return []interface{}{params.Operator, params.Approved}, nil
default:
return nil, fmt.Errorf("unsupported method")
}
Expand Down
Loading

0 comments on commit c8f8857

Please sign in to comment.