getCode
Retrieves the bytecode at an address
Loading...
Retrieves the bytecode at an address
Import the public actions package so you can call this action.
import "github.com/ChefBingbong/viem-go/actions/public"A minimal example showing how to construct a public client and call this action.
import ( "context" "log"
"github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/actions/public" "github.com/ChefBingbong/viem-go/client" "github.com/ChefBingbong/viem-go/client/transport" "github.com/ChefBingbong/viem-go/chain/definitions")
ctx := context.Background()
publicClient, err := client.CreatePublicClient(client.PublicClientConfig{ Chain: definitions.Mainnet, Transport: transport.HTTP("https://eth.llamarpc.com"),})if err != nil { log.Fatal(err)}defer func() { _ = publicClient.Close() }()
code, err := public.GetCode(ctx, publicClient, public.GetCodeParameters{ Address: common.HexToAddress("0xA0Cf798816D4b9b9866b5330EEa46a18382f251e"),})if err != nil { log.Fatal(err)}if code == nil { log.Println("No contract deployed at this address")} else { log.Printf("Contract bytecode length: %d bytes", len(code))}[]byte
The contract bytecode. Returns nil if no code exists at the address (empty account).
Configuration options accepted by this action.
common.AddressThe contract address to retrieve bytecode for.
import ( "github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/actions/public")
code, err := public.GetCode(ctx, publicClient, public.GetCodeParameters{ Address: common.HexToAddress("0xA0Cf798816D4b9b9866b5330EEa46a18382f251e"),})*uint64The block number to get the code at.
import "github.com/ChefBingbong/viem-go/actions/public"
blockNum := uint64(12345)code, err := public.GetCode(ctx, publicClient, public.GetCodeParameters{ Address: common.HexToAddress("0xA0Cf798816D4b9b9866b5330EEa46a18382f251e"), BlockNumber: &blockNum,})BlockTag"latest"The block tag to get the code at (e.g., "latest", "pending").
import "github.com/ChefBingbong/viem-go/actions/public"
code, err := public.GetCode(ctx, publicClient, public.GetCodeParameters{ Address: common.HexToAddress("0xA0Cf798816D4b9b9866b5330EEa46a18382f251e"), BlockTag: public.BlockTagSafe,})Underlying JSON-RPC method used by this action.