Skip to content

Instantly share code, notes, and snippets.

@rafaelescrich
Forked from miguelmota/estimate_gas.go
Created November 10, 2021 20:57
Show Gist options
  • Select an option

  • Save rafaelescrich/fa3a28a9f531cbfc3feb4ed0b132340e to your computer and use it in GitHub Desktop.

Select an option

Save rafaelescrich/fa3a28a9f531cbfc3feb4ed0b132340e to your computer and use it in GitHub Desktop.
Go estimate ethereum transaction gas limit and add 30% percent
package main
import (
"context"
"fmt"
"log"
ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
client, err := ethclient.Dial("https://rinkeby.infura.io")
if err != nil {
log.Fatal(err)
}
tokenAddress := common.HexToAddress("0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1")
estimatedGas, err := client.EstimateGas(context.Background(), ethereum.CallMsg{
To: &tokenAddress,
Data: []byte{0},
})
if err != nil {
log.Fatal(err)
}
gasLimit := int(float64(estimatedGas) * 1.30)
fmt.Println(gasLimit) // 27305
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment