|
| 1 | +import { createClient, http, walletActions, publicActions } from 'viem'; |
| 2 | +import { privateKeyToAccount } from 'viem/accounts'; |
| 3 | +import { tempo } from 'tempo.ts/chains'; |
| 4 | +import { withFeePayer } from 'tempo.ts/viem'; |
| 5 | +import { config } from './config.js'; |
| 6 | + |
| 7 | +async function main() { |
| 8 | + console.log('Tempo Fee Payer Client Example'); |
| 9 | + |
| 10 | + const account = privateKeyToAccount(config.clientPrivateKey); |
| 11 | + console.log('Client address:', account.address); |
| 12 | + |
| 13 | + const client = createClient({ |
| 14 | + account, |
| 15 | + chain: tempo({ feeToken: config.alphaUsdAddress }), |
| 16 | + transport: withFeePayer( |
| 17 | + http(config.getCleanRpcUrl(), { |
| 18 | + fetchOptions: { |
| 19 | + headers: { |
| 20 | + Authorization: config.getAuthHeader(), |
| 21 | + }, |
| 22 | + }, |
| 23 | + }), |
| 24 | + http(config.feePayerServerUrl) |
| 25 | + ), |
| 26 | + }) |
| 27 | + .extend(publicActions) |
| 28 | + .extend(walletActions); |
| 29 | + |
| 30 | + console.log('Sending transaction via fee payer relay...'); |
| 31 | + |
| 32 | + const hash = await client.sendTransaction({ |
| 33 | + to: '0x0000000000000000000000000000000000000000', |
| 34 | + data: '0xdeadbeef', |
| 35 | + value: 0n, |
| 36 | + feeToken: config.alphaUsdAddress, |
| 37 | + feePayer: true, |
| 38 | + } as any); |
| 39 | + |
| 40 | + console.log('Transaction hash:', hash); |
| 41 | + console.log('Waiting for confirmation...'); |
| 42 | + |
| 43 | + await client.waitForTransactionReceipt({ hash }); |
| 44 | + |
| 45 | + const transaction = await client.getTransaction({ hash }); |
| 46 | + console.log('Transaction confirmed!'); |
| 47 | + if (transaction.feePayer) { |
| 48 | + console.log('Fee payer address:', transaction.feePayer); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +main().catch(console.error); |
0 commit comments