viem-goviem-go

getCode

Retrieves the bytecode at an address

Import

Import the public actions package so you can call this action.

import "github.com/ChefBingbong/viem-go/actions/public"

Usage

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))
}

Returns

[]byte

The contract bytecode. Returns nil if no code exists at the address (empty account).

Parameters

Configuration options accepted by this action.

Address

  • Type: common.Address
  • Required

The 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"),
})

BlockNumber

  • Type: *uint64
  • Optional

The 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

  • Type: BlockTag
  • Default: "latest"
  • Optional

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,
})

JSON-RPC Method

Underlying JSON-RPC method used by this action.

eth_getCode