My smart contract is deployed on https://sepolia.etherscan.io/tx/0x1b1742a3ff2ac160d5f245ab91828fcc56a803cb3826b6cbdb9f3cb2f293ba66
Once deployed I get contract creation code storage out of gas error
I got the gas estimation formula
gasLimit = 21000 + 68 * dataByteLength
from How do you calculate gas limit for transaction with data in Ethereum?
Then here is my Go code where I use the formula:
package main
import (
"context"
"crypto/ecdsa"
"deploy-contract-tool/api"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/jimlawless/whereami"
"math/big"
)
func deploy() (string, string, string, string, error) {
privateKey, err := crypto.HexToECDSA(wallet.privateKey)
if err != nil {
return "", "", whereami.WhereAmI(), "", err
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return "", "", whereami.WhereAmI(), "", err
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := connection.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
return "", "", whereami.WhereAmI(), "", err
}
pendingBalance, err := connection.PendingBalanceAt(context.Background(), fromAddress)
if err != nil {
return "", "", whereami.WhereAmI(), "", err
}
// Set the gas price and gas limit
suggestedGasPrice, err := connection.SuggestGasPrice(context.Background())
if err != nil {
return "", "", whereami.WhereAmI(), "", err
}
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, currentConfig.ChainID)
if err != nil {
return "", "", whereami.WhereAmI(), "", err
}
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0)
auth.GasPrice = suggestedGasPrice
contractByteLength := 15429
//gasLimit calculation below
gasLimit := 21000 + (68 * contractByteLength)
auth.GasLimit = uint64(gasLimit)
fmt.Println("auth.GasLimit: ", auth.GasLimit)
gasLimitBigInt := new(big.Int).SetUint64(auth.GasLimit)
calculatedCost := new(big.Int).Mul(gasLimitBigInt, auth.GasPrice)
authValue := new(big.Int).SetUint64(auth.Value.Uint64())
calculatedCost = new(big.Int).Add(calculatedCost, authValue)
fmt.Println("calculatedCost: ", calculatedCost)
fmt.Println("pendingBalance: ", pendingBalance)
fmt.Println("suggestedGasPrice: ", suggestedGasPrice)
fmt.Println("gasLimit: ", gasLimit)
fmt.Println("auth.GasLimit: ", auth.GasLimit)
fmt.Println("auth.GasPrice: ", auth.GasPrice)
fmt.Println("nonce: ", auth.Nonce)
address, tx, instance, err := api.DeployPigfox(auth, connection)
if err != nil {
return "", "", whereami.WhereAmI(), "", err
}
fmt.Println("----After deploy----")
fmt.Println("Token deployed at:", address)
fmt.Println("Token tx:", tx.Hash())
fmt.Println("Token cost:", tx.Cost())
fmt.Println("Token gas limit:", tx.Gas())
fmt.Println("Token gas price:", tx.GasPrice())
fmt.Println("Waiting to be mined...")
receipt, err := bind.WaitMined(context.Background(), connection, tx)
if err != nil {
return "", "", whereami.WhereAmI(), "", err
}
fmt.Println("Mined...")
if receipt.Status == types.ReceiptStatusSuccessful {
fmt.Println("Token deployed successfully")
} else {
reason, err := getRevertReason(receipt) //nolint:govet
return "", "", whereami.WhereAmI(), reason, err
}
fmt.Println(instance)
return address.Hex(), tx.Hash().Hex(), "", "", err
}
I got the contract byte length from
$ ls -l Contract.sol
-rw-rw-r-- 1 user user 15429 Sep 19 05:11 Contract.sol
It was suggested to me that the gas estimation formula might be old and deprecated but that's all I have found.
This is terminal output when executing the code.
auth.GasLimit: 1070172
calculatedCost: 107351519592456
pendingBalance: 2388409999174700070
suggestedGasPrice: 100312398
gasLimit: 1070172
auth.GasLimit: 1070172
auth.GasPrice: 100312398
nonce: 30
----After deploy----
Token deployed at: 0xf985E6d5F0De41A8F21Dc0D050Dc9C0B3A23B043
Token tx: 0x1b1742a3ff2ac160d5f245ab91828fcc56a803cb3826b6cbdb9f3cb2f293ba66
Token cost: 107351519592456
Token gas limit: 1070172
Token gas price: 100312398
Waiting to be mined...
Mined...
Any suggestions on how I can fix this gas calculation error?