viem-goviem-go

deployContract

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

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

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

Usage

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)

With Constructor Arguments

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

Returns

string

The transaction hash as a hex string.

Parameters

Configuration options accepted by this action.

Account

Control which account is used to deploy the contract.

  • Type: Account

  • Optional

The account to deploy from. If nil, uses the client's account.

ABI

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

Bytecode

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

Args

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

Value

Optionally send ETH alongside the deployment transaction.

  • Type: *big.Int

  • Optional

The amount of ETH to send with the deployment transaction.

Transaction Fields

All transaction fields from sendTransaction are supported:

  • Chain - The target chain
  • AssertChainID - Whether to assert chain ID matches
  • DataSuffix - Data to append to the end of the calldata
  • Gas - Gas limit
  • GasPrice - Gas price (Legacy transactions)
  • MaxFeePerGas - Max fee per gas (EIP-1559)
  • MaxPriorityFeePerGas - Max priority fee per gas (EIP-1559)
  • Nonce - Transaction nonce
  • Type - Transaction type
  • AuthorizationList - 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).

JSON-RPC Methods