writeContract
Uses a smart account to executes a write function on a contract.
A "write" function on a Solidity contract modifies the state of the blockchain.
Usage
import { smartAccountClient } from "./smartAccountClient"
import { simpleAbi } from './abi'
const hash = await smartAccountClient.writeContract({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
abi: simpleAbi,
functionName: 'mint',
})import { createSmartAccountClient } from "permissionless"
import { toSimpleSmartAccount } from "permissionless/accounts"
import { createPimlicoClient } from "permissionless/clients/pimlico"
import { createPublicClient, http } from "viem"
import { entryPoint07Address } from "viem/account-abstraction"
import { privateKeyToAccount } from "viem/accounts"
import { sepolia } from "viem/chains"
const publicClient = createPublicClient({
chain: sepolia,
transport: http("https://sepolia.rpc.thirdweb.com"),
})
// if using a paymaster
const apiKey = "YOUR_PIMLICO_API_KEY"
const pimlicoUrl = `https://api.pimlico.io/v2/sepolia/rpc?apikey=${apiKey}`
const pimlicoClient = createPimlicoClient({
transport: http(pimlicoUrl),
entryPoint: {
address: entryPoint07Address,
version: "0.7",
},
})
export const simpleSmartAccount = await toSimpleSmartAccount({
client: publicClient,
owner: privateKeyToAccount("0xPRIVATE_KEY"),
entryPoint: {
address: entryPoint07Address,
version: "0.7",
}, // global entrypoint
})
export const smartAccountClient = createSmartAccountClient({
account: simpleSmartAccount,
chain: sepolia,
bundlerTransport: http(pimlicoUrl),
paymaster: pimlicoClient,
userOperation: {
estimateFeesPerGas: async () => {
return (await pimlicoClient.getUserOperationGasPrice()).fast
},
},
})export const simpleAbi = [
...
{
inputs: [],
name: "mint",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
...
] as const;Return Value
- Type:
Hash
The transaction hash of the associated write operation
Parameters
address
- Type:
Address
The contract address.
abi
- Type:
Abi
The contract's ABI.
functionName
- Type:
string
A function to extract from the ABI.
args (optional)
- Type: Inferred from ABI.
Arguments to pass to function call.
gas (optional)
- Type:
bigint
The gas limit for the transaction. Note that passing a gas limit also skips the gas estimation step.
nonce (optional)
- Type:
number
Unique number identifying this transaction.
value (optional)
- Type:
number
Value in wei sent with this transaction.
gasPrice (optional)
- Type:
bigint
The price (in wei) to pay per gas. Only applies to Legacy Transactions
maxFeePerGas (optional)
- Type:
bigint
Total fee per gas (in wei), inclusive of maxPriorityFeePerGas. Only applies to EIP-1559 Transactions
maxPriorityFeePerGas (optional)
- Type:
bigint
Max priority fee per gas (in wei). Only applies to EIP-1559 Transactions
dataSuffix (optional)
- Type:
Hex
Data to append to the end of the calldata.
account (optional)
- Type:
SmartAccount
The Account to write to the contract from.