import { parseAbi, decodeEventLog } from 'viem';
const ERC20_ABI = parseAbi([
'function transfer(address to, uint256 amount) returns (bool)',
'event Transfer(address indexed from, address indexed to, uint256 value)',
]);
const hash = await walletClient.writeContract({
address: TOKEN,
abi: ERC20_ABI,
functionName: 'transfer',
args: ['0xRecipient', 1_000_000n],
});
const receipt = await client.waitForTransactionReceipt({ hash });
// viem returns typed logs automatically when you pass the ABI to getTransactionReceipt
const typedReceipt = await client.getTransactionReceipt({ hash });
// Or decode manually from raw logs
for (const log of receipt.logs) {
try {
const event = decodeEventLog({ abi: ERC20_ABI, ...log });
console.log('Event:', event.eventName, event.args);
} catch {
// Log from a different contract or unknown ABI
}
}