|
| 1 | +# Cartesi viem extension |
| 2 | + |
| 3 | +Viem provides an [extension mechanism to clients](https://viem.sh/docs/clients/custom) that can provide a convenient and familiar API for application developers based on L2 solutions. |
| 4 | + |
| 5 | +This extension provides extensions for L1 operations and L2 operations related to Cartesi. |
| 6 | + |
| 7 | +## WalletClient L1 |
| 8 | + |
| 9 | +Transactions related to Cartesi applications are sent directly to the base layer (L1). This extension provides convenient methods for [sending inputs](https://docs.cartesi.io/cartesi-rollups/1.3/rollups-apis/json-rpc/input-box/#addinput) and interacting with the [portals](https://docs.cartesi.io/cartesi-rollups/1.3/rollups-apis/json-rpc/portals/ERC20Portal/) provided by Cartesi Rollups. |
| 10 | + |
| 11 | +- addInput |
| 12 | +- depositEther |
| 13 | +- depositERC20Tokens |
| 14 | +- depositERC721Token |
| 15 | +- depositSingleERC1155Token |
| 16 | +- depositBatchERC1155Token |
| 17 | +- executeVoucher |
| 18 | +- relayDAppAddress |
| 19 | + |
| 20 | +## PublicClient L1 |
| 21 | + |
| 22 | +The following methods are provided to interact with the Cartesi Rollups Smart Contracts. |
| 23 | + |
| 24 | +- estimateAddInputGas |
| 25 | +- estimateDepositEtherGas |
| 26 | +- estimateDepositERC20TokensGas |
| 27 | +- estimateDepositERC721TokenGas |
| 28 | +- estimateDepositSingleERC1155TokenGas |
| 29 | +- estimateDepositBatchERC1155TokenGas |
| 30 | +- estimateExecuteVoucherGas |
| 31 | +- estimateRelayDAppAddressGas |
| 32 | + |
| 33 | +## PublicClient L2 |
| 34 | + |
| 35 | +The following methods are provided to interact with the Cartesi Rollups Node. |
| 36 | + |
| 37 | +- inputNumber |
| 38 | +- getInput |
| 39 | +- getNoticeCount |
| 40 | +- getReportCount |
| 41 | +- getVoucherCount |
| 42 | +- getNotice |
| 43 | +- getReport |
| 44 | +- getVoucher |
| 45 | + |
| 46 | +## Utilities |
| 47 | + |
| 48 | +- getInputsAdded: this provides a utility method to get the input(s) added to the InputBox given a `TransactionReceipt`. |
| 49 | + |
| 50 | +```typescript |
| 51 | +const receipt = await publicClient.waitForTransactionReceipt({ hash }); |
| 52 | +const [inputAdded] = getInputsAdded(receipt); |
| 53 | +``` |
| 54 | + |
| 55 | +## Example |
| 56 | + |
| 57 | +Find below a complete example of depositing ERC-20 tokens and waiting for its effect on L2. |
| 58 | + |
| 59 | +```typescript |
| 60 | +import { createPublicClient, createWalletClient, http, parseUnits } from "viem"; |
| 61 | +import { privateKeyToAccount } from "viem/accounts"; |
| 62 | +import { foundry } from "viem/chains"; |
| 63 | +import { getInputsAdded, publicActionsL2, walletActionsL1 } from "../src"; |
| 64 | + |
| 65 | +async function main() { |
| 66 | + const chain = foundry; |
| 67 | + const account = privateKeyToAccount( |
| 68 | + "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", |
| 69 | + ); |
| 70 | + |
| 71 | + // create public client to chain default url |
| 72 | + const publicClient = createPublicClient({ chain, transport: http() }); |
| 73 | + |
| 74 | + // create extended public client to L2 with RPC url |
| 75 | + // https://github.com/tuler/deroll/tree/main/packages/rpc |
| 76 | + const publicClientL2 = createPublicClient({ |
| 77 | + chain, |
| 78 | + transport: http("http://localhost:4000/rpc"), |
| 79 | + }).extend(publicActionsL2()); |
| 80 | + |
| 81 | + // create extended wallet client to chain default url |
| 82 | + const walletClient = createWalletClient({ |
| 83 | + chain, |
| 84 | + transport: http(), |
| 85 | + }).extend(walletActionsL1()); |
| 86 | + |
| 87 | + // application address |
| 88 | + const application = "0xab7528bb862fb57e8a2bcd567a2e929a0be56a5e"; |
| 89 | + const token = "0x92C6bcA388E99d6B304f1Af3c3Cd749Ff0b591e2"; |
| 90 | + |
| 91 | + // send input transaction |
| 92 | + const hash = await walletClient.depositERC20Tokens({ |
| 93 | + account, |
| 94 | + application, |
| 95 | + chain, |
| 96 | + token, |
| 97 | + amount: parseUnits("1", 18), |
| 98 | + execLayerData: "0x", |
| 99 | + }); |
| 100 | + |
| 101 | + // wait for receipt |
| 102 | + const receipt = await publicClient.waitForTransactionReceipt({ hash }); |
| 103 | + |
| 104 | + // get input index from receipt |
| 105 | + const [inputAdded] = getInputsAdded(receipt); |
| 106 | + if (inputAdded) { |
| 107 | + const { inputIndex } = inputAdded; |
| 108 | + |
| 109 | + // wait for input to be processed |
| 110 | + const input = await publicClientL2.waitForInput({ |
| 111 | + inputNumber: Number(inputIndex), |
| 112 | + }); |
| 113 | + console.log(input); |
| 114 | + |
| 115 | + // get notice 0 produced by application |
| 116 | + const notice = await publicClientL2.getNotice({ |
| 117 | + inputNumber: Number(inputIndex), |
| 118 | + noticeNumber: 0, |
| 119 | + }); |
| 120 | + |
| 121 | + console.log(notice); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +main(); |
| 126 | +``` |
0 commit comments