|
| 1 | +import type { Wallet } from '@aztec/aztec.js/wallet'; |
| 2 | +import { AztecAddress } from '@aztec/aztec.js/addresses'; |
| 3 | +import type { ContractFunctionInteractionCallIntent } from '@aztec/aztec.js/authorization'; |
| 4 | + |
| 5 | +// Import the new Benchmark base class and context |
| 6 | +import { Benchmark, BenchmarkContext } from '@defi-wonderland/aztec-benchmark'; |
| 7 | + |
| 8 | +import { MultiTokenContract } from '../src/artifacts/MultiToken.js'; |
| 9 | +import { |
| 10 | + deployMultiTokenWithMinter, |
| 11 | + initializeMultiTokenTransferCommitment, |
| 12 | + setupTestSuite, |
| 13 | + ID_A, |
| 14 | +} from '../src/ts/test/utils.js'; |
| 15 | + |
| 16 | +// Extend the BenchmarkContext from the new package |
| 17 | +interface MultiTokenBenchmarkContext extends BenchmarkContext { |
| 18 | + cleanup: () => Promise<void>; |
| 19 | + wallet: Wallet; |
| 20 | + deployer: AztecAddress; |
| 21 | + accounts: AztecAddress[]; |
| 22 | + multiTokenContract: MultiTokenContract; |
| 23 | + commitments: bigint[]; |
| 24 | +} |
| 25 | + |
| 26 | +// --- Helper Functions --- |
| 27 | + |
| 28 | +function amt(x: bigint | number) { |
| 29 | + // MultiToken carries NO decimals (its constructor takes only name/symbol/minter/auth_contract, unlike the |
| 30 | + // Token contract's `decimals` arg), so amounts are raw u128 values. We keep the sibling token benchmark's |
| 31 | + // magnitudes (mint 100, move 10) without the 10**18 scaling that `parseUnits` would apply. |
| 32 | + return BigInt(x); |
| 33 | +} |
| 34 | + |
| 35 | +// Use export default class extending Benchmark |
| 36 | +export default class MultiTokenContractBenchmark extends Benchmark { |
| 37 | + /** |
| 38 | + * Sets up the benchmark environment for the MultiTokenContract. |
| 39 | + * Creates wallet, gets accounts, and deploys the contract. |
| 40 | + */ |
| 41 | + async setup(): Promise<MultiTokenBenchmarkContext> { |
| 42 | + const { cleanup, wallet, accounts } = await setupTestSuite(true); |
| 43 | + const [deployer] = accounts; |
| 44 | + // minter = deployer, auth_contract = ZERO (ARC-403 hook disabled) — mirrors the default JS suite deploy. |
| 45 | + const multiTokenContract = await deployMultiTokenWithMinter(wallet, deployer, deployer, AztecAddress.ZERO); |
| 46 | + |
| 47 | + // Pre-initialize the partial notes consumed by transfer_private_to_commitment / transfer_public_to_commitment. |
| 48 | + // The commitment is id-agnostic (the completer binds the id at completion); the payer (alice) must be the |
| 49 | + // completer, and the note must come from a PRIOR settled tx (the helper settles each one). |
| 50 | + const [alice, bob] = accounts; |
| 51 | + const commitment_1 = await initializeMultiTokenTransferCommitment(multiTokenContract, alice, bob, alice); |
| 52 | + const commitment_2 = await initializeMultiTokenTransferCommitment(multiTokenContract, alice, bob, alice); |
| 53 | + |
| 54 | + const commitments = [commitment_1, commitment_2]; |
| 55 | + |
| 56 | + return { cleanup, wallet, deployer, accounts, multiTokenContract, commitments }; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns the list of MultiTokenContract methods to be benchmarked. |
| 61 | + * Ordering matters: the mints seed the balances that the following transfers/burns/commitments spend. |
| 62 | + * Every op is an `alice` self-spend of token id `ID_A` (nonce = 0, so no authwit is needed). |
| 63 | + */ |
| 64 | + getMethods(context: MultiTokenBenchmarkContext): ContractFunctionInteractionCallIntent[] { |
| 65 | + const { multiTokenContract, accounts, wallet, commitments } = context; |
| 66 | + const [alice, bob] = accounts; |
| 67 | + const owner = alice; |
| 68 | + const id = ID_A; |
| 69 | + |
| 70 | + const methods: ContractFunctionInteractionCallIntent[] = [ |
| 71 | + // Mint methods |
| 72 | + { |
| 73 | + caller: alice, |
| 74 | + action: multiTokenContract.withWallet(wallet).methods.mint_to_private(owner, id, amt(100)), |
| 75 | + }, |
| 76 | + { |
| 77 | + caller: alice, |
| 78 | + action: multiTokenContract.withWallet(wallet).methods.mint_to_public(owner, id, amt(100)), |
| 79 | + }, |
| 80 | + |
| 81 | + // Transfer methods |
| 82 | + { |
| 83 | + caller: alice, |
| 84 | + action: multiTokenContract.withWallet(wallet).methods.transfer_private_to_public(owner, bob, id, amt(10), 0), |
| 85 | + }, |
| 86 | + { |
| 87 | + caller: alice, |
| 88 | + action: multiTokenContract.withWallet(wallet).methods.transfer_private_to_private(owner, bob, id, amt(10), 0), |
| 89 | + }, |
| 90 | + { |
| 91 | + caller: alice, |
| 92 | + action: multiTokenContract.withWallet(wallet).methods.transfer_public_to_private(owner, bob, id, amt(10), 0), |
| 93 | + }, |
| 94 | + { |
| 95 | + caller: alice, |
| 96 | + action: multiTokenContract.withWallet(wallet).methods.transfer_public_to_public(owner, bob, id, amt(10), 0), |
| 97 | + }, |
| 98 | + |
| 99 | + // Burn methods |
| 100 | + { |
| 101 | + caller: alice, |
| 102 | + action: multiTokenContract.withWallet(wallet).methods.burn_private(owner, id, amt(10), 0), |
| 103 | + }, |
| 104 | + { |
| 105 | + caller: alice, |
| 106 | + action: multiTokenContract.withWallet(wallet).methods.burn_public(owner, id, amt(10), 0), |
| 107 | + }, |
| 108 | + |
| 109 | + // Partial notes methods |
| 110 | + { |
| 111 | + caller: alice, |
| 112 | + action: multiTokenContract.withWallet(wallet).methods.initialize_transfer_commitment(bob, owner), |
| 113 | + }, |
| 114 | + { |
| 115 | + caller: alice, |
| 116 | + action: multiTokenContract |
| 117 | + .withWallet(wallet) |
| 118 | + .methods.transfer_private_to_commitment(owner, id, commitments[0], amt(10), 0), |
| 119 | + }, |
| 120 | + { |
| 121 | + caller: alice, |
| 122 | + action: multiTokenContract |
| 123 | + .withWallet(wallet) |
| 124 | + .methods.transfer_public_to_commitment(owner, id, commitments[1], amt(10), 0), |
| 125 | + }, |
| 126 | + ]; |
| 127 | + |
| 128 | + return methods.filter(Boolean); |
| 129 | + } |
| 130 | + |
| 131 | + async teardown(context: MultiTokenBenchmarkContext): Promise<void> { |
| 132 | + await context.cleanup(); |
| 133 | + } |
| 134 | +} |
0 commit comments