writeContract
Action for calling an ethers Contract write method. Pairs with the prepareWriteContract
action.
import { writeContract } from '@wagmi/core'
Usage
The following examples use the wagmigotchi contract.
import { prepareWriteContract, writeContract } from '@wagmi/core'
const config = await prepareWriteContract({
address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
abi: wagmigotchiABI,
functionName: 'feed',
})
const data = await writeContract(config)
Return Value
{
hash: `0x${string}`,
wait: (confirmations?: number) => Promise<TransactionReceipt>,
}
Configuration
mode
This is automatically populated when using
prepareWriteContract
action.
recklesslyUnprepared
: Allow to pass through an adhoc unprepared config. Note: This has UX pitfalls, it is highly recommended to not use this and instead prepare the config upfront using theprepareWriteContract
action.prepared
: The config has been prepared with parameters required for performing a contract write via theprepareWriteContract
action
import { writeContract } from '@wagmi/core'
const { sendTransaction } = await writeContract({
mode: 'recklesslyUnprepared',
address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
abi: wagmigotchiABI,
functionName: 'feed',
})
abi
This is automatically populated when using
prepareWriteContract
action.
Contract ABI.
By defining inline or adding a const assertion to abi
, TypeScript will infer the correct types for functionName
and args
. See the wagmi TypeScript docs for more information.
import { writeContract } from '@wagmi/core'
const { hash } = await writeContract({
mode: 'recklesslyUnprepared',
abi: wagmigotchiABI,
address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
functionName: 'feed',
})
address
This is automatically populated when using
prepareWriteContract
action.
Contract address.
import { writeContract } from '@wagmi/core'
const { sendTransaction } = await writeContract({
mode: 'recklesslyUnprepared',
abi: wagmigotchiABI,
address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
functionName: 'feed',
})
functionName
This is automatically populated when using
prepareWriteContract
action.
Name of function to call.
import { writeContract } from '@wagmi/core'
const { hash } = await writeContract({
mode: 'recklesslyUnprepared',
address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
abi: wagmigotchiABI,
functionName: 'feed',
})
args (optional)
This is automatically populated when using
prepareWriteContract
action.
Arguments to pass to function call.
import { writeContract } from '@wagmi/core'
const { hash } = await writeContract({
mode: 'recklesslyUnprepared',
address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
abi: wagmigotchiABI,
functionName: 'feed',
args: [],
})
overrides (optional)
This is automatically populated when using
prepareWriteContract
action.
Overrides to pass to function call. If the function is payable, you can pass a value
here.
import { writeContract } from '@wagmi/core'
const { hash } = await writeContract({
mode: 'recklesslyUnprepared',
address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
abi: wagmigotchiABI,
functionName: 'feed',
overrides: {
from: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
value: ethers.utils.parseEther('0.01'),
},
})
chainId (optional)
This is automatically populated when using
prepareWriteContract
action.
Checks the current chain to make sure it is the same as chainId
. If chainId
is not the current chain, the connector attempts to switch to it before sending the transaction.
import { writeContract } from '@wagmi/core'
const { sendTransaction } = await writeContract({
mode: 'recklesslyUnprepared',
address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
abi: wagmigotchiABI,
functionName: 'feed',
chainId: 1,
})
request (optional)
This is automatically populated when using
prepareWriteContract
action.
The request to use when sending the contract transaction.
import { prepareWriteContract, writeContract } from '@wagmi/core'
const { config } = await prepareWriteContract({ ... })
const { hash } = await writeContract({
...config,
request: config.request
})
Reckless Usage
It is possible to use writeContract
without pairing it with prepareWriteContract
action by using "recklessly unprepared" mode.
This usage is not recommended. It comes with UX pitfalls. Only use it as a last resort.
import { writeContract } from '@wagmi/core'
const { hash } = await writeContract({
mode: 'recklesslyUnprepared',
address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
abi: wagmigotchiABI,
functionName: 'claim',
tokenId: 69,
})