|
| 1 | +# @evstack/evnode-viem |
| 2 | + |
| 3 | +Viem client extension for EvNode transactions (type 0x76). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @evstack/evnode-viem viem |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Basic Transaction |
| 14 | + |
| 15 | +```typescript |
| 16 | +import { createClient, http } from 'viem'; |
| 17 | +import { privateKeyToAccount, sign } from 'viem/accounts'; |
| 18 | +import { createEvnodeClient } from '@evstack/evnode-viem'; |
| 19 | + |
| 20 | +const client = createClient({ |
| 21 | + transport: http('http://localhost:8545'), |
| 22 | +}); |
| 23 | + |
| 24 | +const account = privateKeyToAccount('0x...'); |
| 25 | + |
| 26 | +const evnode = createEvnodeClient({ |
| 27 | + client, |
| 28 | + executor: { |
| 29 | + address: account.address, |
| 30 | + signHash: async (hash) => sign({ hash, privateKey: '0x...' }), |
| 31 | + }, |
| 32 | +}); |
| 33 | + |
| 34 | +// Send a transaction |
| 35 | +const txHash = await evnode.send({ |
| 36 | + calls: [ |
| 37 | + { to: '0x...', value: 0n, data: '0x' }, |
| 38 | + ], |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +### Batch Transactions |
| 43 | + |
| 44 | +EvNode transactions support multiple calls in a single transaction: |
| 45 | + |
| 46 | +```typescript |
| 47 | +const txHash = await evnode.send({ |
| 48 | + calls: [ |
| 49 | + { to: recipient1, value: 1000000000000000n, data: '0x' }, |
| 50 | + { to: recipient2, value: 1000000000000000n, data: '0x' }, |
| 51 | + ], |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +### Sponsored Transactions |
| 56 | + |
| 57 | +A sponsor can pay gas fees on behalf of the executor: |
| 58 | + |
| 59 | +```typescript |
| 60 | +const evnode = createEvnodeClient({ |
| 61 | + client, |
| 62 | + executor: { address: executorAddr, signHash: executorSignFn }, |
| 63 | + sponsor: { address: sponsorAddr, signHash: sponsorSignFn }, |
| 64 | +}); |
| 65 | + |
| 66 | +// Create intent (signed by executor) |
| 67 | +const intent = await evnode.createIntent({ |
| 68 | + calls: [{ to: '0x...', value: 0n, data: '0x' }], |
| 69 | +}); |
| 70 | + |
| 71 | +// Sponsor signs and sends |
| 72 | +const txHash = await evnode.sponsorAndSend({ intent }); |
| 73 | +``` |
| 74 | + |
| 75 | +## API |
| 76 | + |
| 77 | +### `createEvnodeClient(options)` |
| 78 | + |
| 79 | +Creates a new EvNode client. |
| 80 | + |
| 81 | +**Options:** |
| 82 | +- `client` - Viem Client instance |
| 83 | +- `executor` - (optional) Default executor signer |
| 84 | +- `sponsor` - (optional) Default sponsor signer |
| 85 | + |
| 86 | +### Client Methods |
| 87 | + |
| 88 | +- `send(args)` - Sign and send an EvNode transaction |
| 89 | +- `createIntent(args)` - Create a sponsorable intent |
| 90 | +- `sponsorIntent(args)` - Add sponsor signature to an intent |
| 91 | +- `sponsorAndSend(args)` - Sponsor and send in one call |
| 92 | +- `serialize(signedTx)` - Serialize a signed transaction |
| 93 | +- `deserialize(hex)` - Deserialize a signed transaction |
| 94 | + |
| 95 | +### Utility Functions |
| 96 | + |
| 97 | +- `computeExecutorSigningHash(tx)` - Get hash for executor to sign |
| 98 | +- `computeSponsorSigningHash(tx, executorAddress)` - Get hash for sponsor to sign |
| 99 | +- `computeTxHash(signedTx)` - Get transaction hash |
| 100 | +- `recoverExecutor(signedTx)` - Recover executor address from signature |
| 101 | +- `recoverSponsor(tx, executorAddress)` - Recover sponsor address from signature |
| 102 | +- `estimateIntrinsicGas(calls)` - Estimate minimum gas for calls |
| 103 | + |
| 104 | +## License |
| 105 | + |
| 106 | +MIT |
0 commit comments