Simulate Contract
Run a contract call without sending a transaction
Loading...
Run a contract call without sending a transaction
SimulateContract executes a contract call via eth_call and returns the decoded return value (and optionally revert info). Use it to test state-changing calls or to validate arguments before sending a transaction. Works with both view and write functions.
import ( "math/big" "github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/actions/public")// SimulateContractParameters requires *abi.ABIparsedABI, _ := abi.Parse(erc20ABI)
result, err := public.SimulateContract(ctx, publicClient, public.SimulateContractParameters{ Account: &senderAddr, Address: common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"), ABI: parsedABI, FunctionName: "transfer", Args: []any{recipient, big.NewInt(1000)}, Value: big.NewInt(0),})if err != nil { // e.g. execution reverted log.Fatal(err)}// result.Result is the decoded return value; result.Request can be used for writeContract*common.Addressnilcommon.Address*abi.ABIstring"transfer", "approve").[]anynil*big.Intnil*uint64 or BlockTag(*SimulateContractReturnType, error). SimulateContractReturnType has Result (any — decoded return value) and Request (SimulateContractRequest — can be used to build a writeContract call). On revert, returns an error (use abi.DecodeErrorResult to decode revert data). PublicClient.SimulateContract returns ([]any, error) (decoded return values only).