deployContract
Deploys a contract to the network, given bytecode & constructor arguments.
Usage
ts
import { wagmiAbi } from './abi'
import { account, walletClient } from './config'
const hash = await walletClient.deployContract({
abi,
account,
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
import { wagmiAbi } from './abi'
import { account, walletClient } from './config'
const hash = await walletClient.deployContract({
abi,
account,
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
ts
export const wagmiAbi = [
...
{
inputs: [],
stateMutability: "nonpayable",
type: "constructor",
},
...
] as const;
export const wagmiAbi = [
...
{
inputs: [],
stateMutability: "nonpayable",
type: "constructor",
},
...
] as const;
ts
import { createWalletClient, custom } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
export const walletClient = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum)
})
// JSON-RPC Account
export const [account] = await walletClient.getAddresses()
// Local Account
export const account = privateKeyToAccount(...)
import { createWalletClient, custom } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
export const walletClient = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum)
})
// JSON-RPC Account
export const [account] = await walletClient.getAddresses()
// Local Account
export const account = privateKeyToAccount(...)
Deploying with Constructor Args
ts
import { deployContract } from 'viem'
import { wagmiAbi } from './abi'
import { account, walletClient } from './config'
const hash = await walletClient.deployContract({
abi,
account,
args: [69420],
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
import { deployContract } from 'viem'
import { wagmiAbi } from './abi'
import { account, walletClient } from './config'
const hash = await walletClient.deployContract({
abi,
account,
args: [69420],
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
ts
export const wagmiAbi = [
...
{
inputs: [{ name: "x", type: "uint32" }],
stateMutability: "nonpayable",
type: "constructor",
},
...
] as const;
export const wagmiAbi = [
...
{
inputs: [{ name: "x", type: "uint32" }],
stateMutability: "nonpayable",
type: "constructor",
},
...
] as const;
ts
import { createWalletClient, custom } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
export const walletClient = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum)
})
// JSON-RPC Account
export const [account] = await walletClient.getAddresses()
// Local Account
export const account = privateKeyToAccount(...)
import { createWalletClient, custom } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
export const walletClient = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum)
})
// JSON-RPC Account
export const [account] = await walletClient.getAddresses()
// Local Account
export const account = privateKeyToAccount(...)
Returns
The Transaction hash.
Parameters
abi
- Type:
Abi
The contract's ABI.
ts
const hash = await walletClient.deployContract({
abi: wagmiAbi,
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
const hash = await walletClient.deployContract({
abi: wagmiAbi,
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
account
- Type:
Account | Address
The Account to deploy the contract from.
Accepts a JSON-RPC Account or Local Account (Private Key, etc).
ts
const hash = await walletClient.deployContract({
abi: wagmiAbi,
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
const hash = await walletClient.deployContract({
abi: wagmiAbi,
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
bytecode
- Type:
Hex
The contract's bytecode.
ts
const hash = await walletClient.deployContract({
abi: wagmiAbi,
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
const hash = await walletClient.deployContract({
abi: wagmiAbi,
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
args (if required)
- Type: Inferred from ABI.
Constructor arguments to call upon deployment.
ts
const hash = await walletClient.deployContract({
abi: wagmiAbi,
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
args: [69]
})
const hash = await walletClient.deployContract({
abi: wagmiAbi,
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
args: [69]
})
Live Example
Check out the usage of deployContract
in the live Deploying Contracts Example below.