|
| 1 | +#!/usr/bin/env -S deno run --env-file --allow-all |
| 2 | +import { env } from '../tools/lib/index.ts' |
| 3 | +import { abis } from '../codegen/abis.ts' |
| 4 | +import { |
| 5 | + MyTokenEvm, |
| 6 | + MyTokenInk, |
| 7 | + MyTokenPvm, |
| 8 | + MyTokenRustNoAlloc, |
| 9 | + MyTokenRustWithAlloc, |
| 10 | +} from '../codegen/addresses.ts' |
| 11 | +import { Hex, parseEther } from 'viem' |
| 12 | +import Table from 'cli-table3' |
| 13 | + |
| 14 | +const recipient: Hex = '0x3d26c9637dFaB74141bA3C466224C0DBFDfF4A63' |
| 15 | + |
| 16 | +// fund recipient if needed |
| 17 | +{ |
| 18 | + // endow the account wallet with some funds if needed |
| 19 | + const endowment = parseEther('1') |
| 20 | + const balance = await env.wallet.getBalance({ address: recipient }) |
| 21 | + if (balance == 0n) { |
| 22 | + console.log(`funding ${recipient}`) |
| 23 | + const hash = await env.wallet.sendTransaction({ |
| 24 | + to: recipient, |
| 25 | + value: endowment, |
| 26 | + }) |
| 27 | + await env.wallet.waitForTransactionReceipt({ hash }) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +const addresses = [ |
| 32 | + { address: MyTokenEvm, name: 'EVM - solidity' }, |
| 33 | + { address: MyTokenPvm, name: 'PVM - solidity' }, |
| 34 | + { address: MyTokenInk, name: 'PVM - Ink!' }, |
| 35 | + { address: MyTokenRustWithAlloc, name: 'PVM - Rust with alloc' }, |
| 36 | + { address: MyTokenRustNoAlloc, name: 'PVM - Rust no alloc' }, |
| 37 | +] as const |
| 38 | + |
| 39 | +type CodeSizeEntry = { |
| 40 | + name: string |
| 41 | + size: number |
| 42 | +} |
| 43 | + |
| 44 | +type StatEntry = { |
| 45 | + operation: string |
| 46 | + gas: bigint |
| 47 | + weight: { ref_time: bigint; proof_size: bigint } |
| 48 | +} |
| 49 | + |
| 50 | +const codeSizes: CodeSizeEntry[] = [] |
| 51 | +const stats: StatEntry[] = [] |
| 52 | + |
| 53 | +for (const { name, address } of addresses) { |
| 54 | + const code = await env.wallet.getCode({ address }) |
| 55 | + if (!code) { |
| 56 | + console.error(`Deploy contract first with "deno task deploy"`) |
| 57 | + Deno.exit(1) |
| 58 | + } |
| 59 | + codeSizes.push({ |
| 60 | + name, |
| 61 | + size: code.length / 2 - 1, |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +// mint token |
| 66 | +for (const { name, address } of addresses) { |
| 67 | + const { request } = await env.wallet.simulateContract({ |
| 68 | + address, |
| 69 | + abi: abis.MyToken, |
| 70 | + functionName: 'mint', |
| 71 | + args: [ |
| 72 | + '0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac', |
| 73 | + 10_000n, |
| 74 | + ], |
| 75 | + }) |
| 76 | + |
| 77 | + const hash = await env.wallet.writeContract(request) |
| 78 | + const receipt = await env.wallet.waitForTransactionReceipt({ hash }) |
| 79 | + const weight = await env.debugClient.postDispatchWeight(hash) |
| 80 | + |
| 81 | + stats.push({ |
| 82 | + operation: `mint (${name})`, |
| 83 | + gas: receipt.gasUsed, |
| 84 | + weight, |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +// transfer token |
| 89 | +for (const { name, address } of addresses) { |
| 90 | + const { request } = await env.wallet.simulateContract({ |
| 91 | + address, |
| 92 | + abi: abis.MyToken, |
| 93 | + functionName: 'transfer', |
| 94 | + args: [recipient, 1n], |
| 95 | + }) |
| 96 | + |
| 97 | + const hash = await env.wallet.writeContract(request) |
| 98 | + const receipt = await env.wallet.waitForTransactionReceipt({ hash }) |
| 99 | + const weight = await env.debugClient.postDispatchWeight(hash) |
| 100 | + |
| 101 | + stats.push({ |
| 102 | + operation: `transfer (${name})`, |
| 103 | + gas: receipt.gasUsed, |
| 104 | + weight, |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +// Separate mint and transfer operations |
| 109 | +const mintStats = stats.filter((s) => s.operation.includes('mint')) |
| 110 | +const transferStats = stats.filter((s) => s.operation.includes('transfer')) |
| 111 | + |
| 112 | +function createOperationTable(operationStats: StatEntry[], title: string) { |
| 113 | + const minGas = operationStats.reduce( |
| 114 | + (min, s) => s.gas < min ? s.gas : min, |
| 115 | + operationStats[0].gas, |
| 116 | + ) |
| 117 | + const minRefTime = operationStats.reduce( |
| 118 | + (min, s) => s.weight.ref_time < min ? s.weight.ref_time : min, |
| 119 | + operationStats[0].weight.ref_time, |
| 120 | + ) |
| 121 | + const minProofSize = operationStats.reduce( |
| 122 | + (min, s) => s.weight.proof_size < min ? s.weight.proof_size : min, |
| 123 | + operationStats[0].weight.proof_size, |
| 124 | + ) |
| 125 | + |
| 126 | + const table = new Table({ |
| 127 | + head: [ |
| 128 | + 'Implementation', |
| 129 | + 'Gas Used', |
| 130 | + 'Gas %', |
| 131 | + 'Ref Time', |
| 132 | + 'Ref Time %', |
| 133 | + 'Proof Size', |
| 134 | + 'Proof Size %', |
| 135 | + ], |
| 136 | + style: { |
| 137 | + head: ['cyan'], |
| 138 | + }, |
| 139 | + }) |
| 140 | + |
| 141 | + for (const stat of operationStats) { |
| 142 | + const gasPercent = ((Number(stat.gas) / Number(minGas)) * 100).toFixed( |
| 143 | + 1, |
| 144 | + ) |
| 145 | + const refTimePercent = |
| 146 | + ((Number(stat.weight.ref_time) / Number(minRefTime)) * 100).toFixed( |
| 147 | + 1, |
| 148 | + ) |
| 149 | + const proofSizePercent = |
| 150 | + ((Number(stat.weight.proof_size) / Number(minProofSize)) * 100) |
| 151 | + .toFixed(1) |
| 152 | + |
| 153 | + // Extract implementation name (EVM or PVM) from operation |
| 154 | + const implName = stat.operation.match(/\((.*?)\)/)?.[1] || |
| 155 | + stat.operation |
| 156 | + |
| 157 | + table.push([ |
| 158 | + implName, |
| 159 | + stat.gas.toString(), |
| 160 | + `${gasPercent}%`, |
| 161 | + stat.weight.ref_time.toString(), |
| 162 | + `${refTimePercent}%`, |
| 163 | + stat.weight.proof_size.toString(), |
| 164 | + `${proofSizePercent}%`, |
| 165 | + ]) |
| 166 | + } |
| 167 | + |
| 168 | + console.log(`\n## ${title}\n`) |
| 169 | + console.log(table.toString()) |
| 170 | +} |
| 171 | + |
| 172 | +createOperationTable(mintStats, 'Mint Operation') |
| 173 | +createOperationTable(transferStats, 'Transfer Operation') |
| 174 | + |
| 175 | +// Output code size table |
| 176 | +const minCodeSize = codeSizes.reduce( |
| 177 | + (min, c) => c.size < min ? c.size : min, |
| 178 | + codeSizes[0].size, |
| 179 | +) |
| 180 | + |
| 181 | +const codeSizeTable = new Table({ |
| 182 | + head: ['Implementation', 'Code Size (bytes)', 'Size %'], |
| 183 | + style: { |
| 184 | + head: ['cyan'], |
| 185 | + }, |
| 186 | +}) |
| 187 | + |
| 188 | +for (const entry of codeSizes) { |
| 189 | + const sizePercent = ((entry.size / minCodeSize) * 100).toFixed(1) |
| 190 | + codeSizeTable.push([ |
| 191 | + entry.name, |
| 192 | + entry.size.toString(), |
| 193 | + `${sizePercent}%`, |
| 194 | + ]) |
| 195 | +} |
| 196 | + |
| 197 | +console.log('\n' + codeSizeTable.toString()) |
0 commit comments