Get Code
Retrieve contract bytecode at an address with getCode
GetCode returns the bytecode at a given address (e.g. contract deployment code). Uses eth_getCode. No ABI required.
Import
import ("github.com/ethereum/go-ethereum/common""github.com/ChefBingbong/viem-go/actions/public")
Usage
code, err := public.GetCode(ctx, publicClient, public.GetCodeParameters{Address: common.HexToAddress("0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2"),})if err != nil {log.Fatal(err)}if code == nil {// No contract at this address (EOA or empty)}fmt.Printf("Bytecode length: %d bytes", len(code))
Parameters
Address (required)
- Type:
common.Address - The address to get bytecode for.
BlockNumber (optional)
- Type:
*uint64 - Default: use BlockTag
- Block number to query. Mutually exclusive with BlockTag.
BlockTag (optional)
- Type:
BlockTag(e.g."latest","pending","safe") - Default:
"latest" - Block tag to query. Mutually exclusive with BlockNumber.
Return type
- GetCode returns
([]byte, error).nilslice means no code (empty account or EOA).
See also
- Get Storage At — read storage slots
- Reading Contracts — call contract view functions