跳转到内容

发送交易

发送交易

使用 wagmi 发送交易

import { useWriteContract, useWaitForTransactionReceipt } from 'wagmi'
function MintButton() {
const { writeContract, data: hash } = useWriteContract()
const { isLoading, isSuccess } = useWaitForTransactionReceipt({ hash })
return (
<button
onClick={() =>
writeContract({
address: '0x...',
abi,
functionName: 'mint',
args: [parseEther('1')],
})
}
disabled={isLoading}
>
{isLoading ? '等待确认...' : 'Mint'}
</button>
)
}

EIP-712 结构化签名

import { useSignTypedData } from 'wagmi'
const { signTypedData } = useSignTypedData()
signTypedData({
domain: {
name: 'MyApp',
version: '1',
chainId: 1,
verifyingContract: '0x...',
},
types: {
Permit: [
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint256' },
],
},
primaryType: 'Permit',
message: {
owner: '0x...',
spender: '0x...',
value: parseEther('100'),
nonce: 0n,
deadline: BigInt(Math.floor(Date.now() / 1000) + 3600),
},
})

使用 viem WalletClient

import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
const walletClient = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum),
})
const hash = await walletClient.writeContract({
address: '0x...',
abi,
functionName: 'transfer',
args: ['0x...', parseEther('1')],
})

延伸阅读