deployContract
Deploys a contract to the network
Deploys a contract to the network
Internally, encodes the deploy data (bytecode + ABI-encoded constructor args) and delegates to sendTransaction with no to address (contract creation).
Import the wallet actions package so you can call this action.
import "github.com/ChefBingbong/viem-go/actions/wallet"See how to construct a wallet client and call this action.
import ( "context" "log"
"github.com/ChefBingbong/viem-go/actions/wallet" "github.com/ChefBingbong/viem-go/client" "github.com/ChefBingbong/viem-go/client/transport" "github.com/ChefBingbong/viem-go/chain/definitions")
ctx := context.Background()
walletClient, err := client.CreateWalletClient(client.WalletClientConfig{ Chain: definitions.Mainnet, Transport: transport.HTTP("https://eth.llamarpc.com"),})if err != nil { log.Fatal(err)}defer func() { _ = walletClient.Close() }()
hash, err := wallet.DeployContract(ctx, walletClient, wallet.DeployContractParameters{ ABI: contractABI, Bytecode: "0x608060405260405161083e38038061083e833981016040819052610...",})if err != nil { log.Fatal(err)}log.Printf("Deployment transaction hash: %s", hash)Deploy a contract that expects constructor arguments.
hash, err := wallet.DeployContract(ctx, walletClient, wallet.DeployContractParameters{ ABI: contractABI, Bytecode: "0x608060405260405161083e38038061083e833981016040819052610...", Args: []any{"MyToken", "MTK", uint8(18)},})if err != nil { log.Fatal(err)}string
The transaction hash as a hex string.
Configuration options accepted by this action.
Control which account is used to deploy the contract.
Type: Account
Optional
The account to deploy from. If nil, uses the client's account.
Provide the ABI used to encode the constructor arguments.
Type: any ([]byte, string, or *abi.ABI)
Required
The contract ABI as JSON bytes, string, or a pre-parsed *abi.ABI. Required for encoding constructor arguments.
hash, err := wallet.DeployContract(ctx, walletClient, wallet.DeployContractParameters{ ABI: contractABI, Bytecode: "0x608060405260405161083e38038061083e833981016040819052610...",})Specify the compiled bytecode for the contract.
Type: string
Required
The contract bytecode as a hex string (with or without 0x prefix).
hash, err := wallet.DeployContract(ctx, walletClient, wallet.DeployContractParameters{ ABI: contractABI, Bytecode: "0x608060405260405161083e38038061083e833981016040819052610...",})Pass the constructor arguments for the contract.
Type: []any
Optional
The constructor arguments.
hash, err := wallet.DeployContract(ctx, walletClient, wallet.DeployContractParameters{ ABI: contractABI, Bytecode: "0x608060405260405161083e38038061083e833981016040819052610...", Args: []any{"MyToken", "MTK", uint8(18)},})Optionally send ETH alongside the deployment transaction.
Type: *big.Int
Optional
The amount of ETH to send with the deployment transaction.
All transaction fields from sendTransaction are supported:
Chain - The target chainAssertChainID - Whether to assert chain ID matchesDataSuffix - Data to append to the end of the calldataGas - Gas limitGasPrice - Gas price (Legacy transactions)MaxFeePerGas - Max fee per gas (EIP-1559)MaxPriorityFeePerGas - Max priority fee per gas (EIP-1559)Nonce - Transaction nonceType - Transaction typeAuthorizationList - Authorization list (EIP-7702)Blobs - Blobs (EIP-4844)BlobVersionedHashes - Blob versioned hashes (EIP-4844)MaxFeePerBlobGas - Max fee per blob gas (EIP-4844)Note: To is not used for contract deployment (it's intentionally empty for contract creation).
eth_sendTransaction (with wallet_sendTransaction fallback)eth_sendRawTransaction