call
Executes a new message call immediately without submitting a transaction to the network
Executes a new message call immediately without submitting a transaction to the network
Import the public actions package so you can call this action.
import "github.com/ChefBingbong/viem-go/actions/public"Examples showing how to execute a read-only call against a contract.
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() }()
contractAddr := common.HexToAddress("0x70997970c51812dc3a010c7d01b50e0d17dc79c8")calldata := common.FromHex("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2")
result, err := public.Call(ctx, publicClient, public.CallParameters{ To: &contractAddr, Data: calldata,})if err != nil { log.Fatal(err)}log.Printf("Call result: %x", result.Data)It is possible to call a function on a contract that has not been deployed yet. For instance, we may want to call a function on an ERC-4337 Smart Account contract which has not been deployed.
Viem-go offers two ways of performing a Deployless Call, via:
The example below demonstrates how we can utilize a Deployless Call via Bytecode:
import ( "github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/actions/public")
contractBytecode := common.FromHex("0x...") // Contract bytecodecalldata := common.FromHex("0x...") // Function call data
result, err := public.Call(ctx, publicClient, public.CallParameters{ Code: contractBytecode, Data: calldata,})The example below demonstrates how we can utilize a Deployless Call via a Deploy Factory:
import ( "github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/actions/public")
factoryAddr := common.HexToAddress("0xE8Df82fA4E10e6A12a9Dab552bceA2acd26De9bb")contractAddr := common.HexToAddress("0x70997970c51812dc3a010c7d01b50e0d17dc79c8")factoryCalldata := common.FromHex("0x...") // Factory deployment calldatacontractCalldata := common.FromHex("0x...") // Contract function call
result, err := public.Call(ctx, publicClient, public.CallParameters{ Factory: &factoryAddr, FactoryData: factoryCalldata, To: &contractAddr, Data: contractCalldata,})*CallReturnType
An object containing:
Data []byte - The return data from the call, or nil if the call returned emptyConfiguration options accepted by this action.
*common.AddressThe account attached to the call (msg.sender).
import ( "github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/actions/public")
account := common.HexToAddress("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266")result, err := public.Call(ctx, publicClient, public.CallParameters{ Account: &account, To: &contractAddr, Data: calldata,})*common.AddressThe contract address to call.
contractAddr := common.HexToAddress("0x70997970c51812dc3a010c7d01b50e0d17dc79c8")result, err := public.Call(ctx, publicClient, public.CallParameters{ To: &contractAddr, Data: calldata,})[]byteThe calldata to send.
calldata := common.FromHex("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2")result, err := public.Call(ctx, publicClient, public.CallParameters{ To: &contractAddr, Data: calldata,})*big.IntThe amount of wei to send with the call.
import "math/big"
result, err := public.Call(ctx, publicClient, public.CallParameters{ To: &contractAddr, Data: calldata, Value: big.NewInt(1000000000000000000), // 1 ETH})*uint64The block number to execute the call at.
blockNum := uint64(12345)result, err := public.Call(ctx, publicClient, public.CallParameters{ To: &contractAddr, Data: calldata, BlockNumber: &blockNum,})BlockTagThe block tag to execute the call at (e.g., "latest", "pending").
result, err := public.Call(ctx, publicClient, public.CallParameters{ To: &contractAddr, Data: calldata, BlockTag: public.BlockTagSafe,})[]byteBytecode for deployless calls (call code without deploying). Mutually exclusive with Factory/FactoryData.
contractBytecode := common.FromHex("0x...")result, err := public.Call(ctx, publicClient, public.CallParameters{ Code: contractBytecode, Data: calldata,})*common.AddressThe contract deployment factory address (e.g., Create2 factory). Used with FactoryData for deployless calls via factory.
factoryAddr := common.HexToAddress("0xE8Df82fA4E10e6A12a9Dab552bceA2acd26De9bb")result, err := public.Call(ctx, publicClient, public.CallParameters{ Factory: &factoryAddr, FactoryData: factoryCalldata, To: &contractAddr, Data: calldata,})[]byteThe calldata to execute on the factory to deploy the contract. Used with Factory for deployless calls via factory.
result, err := public.Call(ctx, publicClient, public.CallParameters{ Factory: &factoryAddr, FactoryData: factoryCalldata, To: &contractAddr, Data: calldata,})*boolEnables multicall batching for this call. If nil, uses client's batch setting.
batch := trueresult, err := public.Call(ctx, publicClient, public.CallParameters{ To: &contractAddr, Data: calldata, Batch: &batch,})types.StateOverrideState overrides for the call.
*types.BlockOverridesBlock-level overrides (baseFeePerGas, gasLimit, etc.).
Underlying JSON-RPC method used by this action.