From 1f4246d8a31fa4601df98fe65e36c1e280b905ed Mon Sep 17 00:00:00 2001 From: LHerskind Date: Wed, 13 Sep 2023 16:18:22 +0000 Subject: [PATCH] refactor: remove the native and private token from dapp_testing --- .../src/guides/dapp_testing.test.ts | 141 +++++++++++++----- 1 file changed, 100 insertions(+), 41 deletions(-) diff --git a/yarn-project/end-to-end/src/guides/dapp_testing.test.ts b/yarn-project/end-to-end/src/guides/dapp_testing.test.ts index f8adaec436ba..fe2a860ae0bf 100644 --- a/yarn-project/end-to-end/src/guides/dapp_testing.test.ts +++ b/yarn-project/end-to-end/src/guides/dapp_testing.test.ts @@ -5,13 +5,17 @@ import { CheatCodes, Fr, L2BlockL2Logs, + TxStatus, + computeMessageSecretHash, createAccount, createAztecRpcClient, getSandboxAccountsWallets, waitForSandbox, } from '@aztec/aztec.js'; import { toBigIntBE } from '@aztec/foundation/bigint-buffer'; -import { NativeTokenContract, PrivateTokenContract, TestContract } from '@aztec/noir-contracts/types'; +import { TestContract, TokenContract } from '@aztec/noir-contracts/types'; + +const { SANDBOX_URL = 'http://localhost:8080', ETHEREUM_HOST = 'http://localhost:8545' } = process.env; describe('guides/dapp/testing', () => { describe('on in-proc sandbox', () => { @@ -20,7 +24,7 @@ describe('guides/dapp/testing', () => { let stop: () => Promise; let owner: AccountWallet; let recipient: AccountWallet; - let token: PrivateTokenContract; + let token: TokenContract; beforeAll(async () => { // docs:start:in-proc-sandbox @@ -28,78 +32,95 @@ describe('guides/dapp/testing', () => { // docs:end:in-proc-sandbox owner = await createAccount(rpc); recipient = await createAccount(rpc); - token = await PrivateTokenContract.deploy(owner, 100n, owner.getAddress()).send().deployed(); + token = await TokenContract.deploy(owner).send().deployed(); + expect((await token.methods._initialize({ address: owner.getAddress() }).send().wait()).status).toEqual( + TxStatus.MINED, + ); }, 60_000); // docs:start:stop-in-proc-sandbox afterAll(() => stop()); // docs:end:stop-in-proc-sandbox - it('increases recipient funds on transfer', async () => { - expect(await token.methods.getBalance(recipient.getAddress()).view()).toEqual(0n); - await token.methods.transfer(20n, recipient.getAddress()).send().wait(); - expect(await token.methods.getBalance(recipient.getAddress()).view()).toEqual(20n); + it('increases recipient funds on mint', async () => { + expect(await token.methods.balance_of_private({ address: recipient.getAddress() }).view()).toEqual(0n); + const secret = Fr.random(); + const secretHash = await computeMessageSecretHash(secret); + expect((await token.methods.mint_private(20n, secretHash).send().wait()).status).toEqual(TxStatus.MINED); + expect( + await token.methods.redeem_shield({ address: recipient.getAddress() }, 20n, secret).send().wait(), + ).toEqual(TxStatus.MINED); + expect(await token.methods.balance_of_private({ address: recipient.getAddress() }).view()).toEqual(20n); }); }); }); describe('on local sandbox', () => { beforeAll(async () => { - const { SANDBOX_URL = 'http://localhost:8080' } = process.env; const rpc = createAztecRpcClient(SANDBOX_URL); await waitForSandbox(rpc); }); // docs:start:sandbox-example describe('private token contract', () => { - const { SANDBOX_URL = 'http://localhost:8080' } = process.env; - let rpc: AztecRPC; let owner: AccountWallet; let recipient: AccountWallet; - let token: PrivateTokenContract; + let token: TokenContract; beforeEach(async () => { rpc = createAztecRpcClient(SANDBOX_URL); owner = await createAccount(rpc); recipient = await createAccount(rpc); - token = await PrivateTokenContract.deploy(owner, 100n, owner.getAddress()).send().deployed(); + token = await TokenContract.deploy(owner).send().deployed(); + expect((await token.methods._initialize({ address: owner.getAddress() }).send().wait()).status).toEqual( + TxStatus.MINED, + ); }, 30_000); - it('increases recipient funds on transfer', async () => { - expect(await token.methods.getBalance(recipient.getAddress()).view()).toEqual(0n); - await token.methods.transfer(20n, recipient.getAddress()).send().wait(); - expect(await token.methods.getBalance(recipient.getAddress()).view()).toEqual(20n); + it('increases recipient funds on mint', async () => { + expect(await token.methods.balance_of_private({ address: recipient.getAddress() }).view()).toEqual(0n); + const secret = Fr.random(); + const secretHash = await computeMessageSecretHash(secret); + expect((await token.methods.mint_private(20n, secretHash).send().wait()).status).toEqual(TxStatus.MINED); + expect( + await token.methods.redeem_shield({ address: recipient.getAddress() }, 20n, secret).send().wait(), + ).toEqual(TxStatus.MINED); + expect(await token.methods.balance_of_private({ address: recipient.getAddress() }).view()).toEqual(20n); }); }); // docs:end:sandbox-example describe('private token contract with initial accounts', () => { - const { SANDBOX_URL = 'http://localhost:8080' } = process.env; - let rpc: AztecRPC; let owner: AccountWallet; let recipient: AccountWallet; - let token: PrivateTokenContract; + let token: TokenContract; beforeEach(async () => { // docs:start:use-existing-wallets rpc = createAztecRpcClient(SANDBOX_URL); [owner, recipient] = await getSandboxAccountsWallets(rpc); - token = await PrivateTokenContract.deploy(owner, 100n, owner.getAddress()).send().deployed(); + token = await TokenContract.deploy(owner).send().deployed(); + expect((await token.methods._initialize({ address: owner.getAddress() }).send().wait()).status).toEqual( + TxStatus.MINED, + ); // docs:end:use-existing-wallets }, 30_000); - it('increases recipient funds on transfer', async () => { - expect(await token.methods.getBalance(recipient.getAddress()).view()).toEqual(0n); - await token.methods.transfer(20n, recipient.getAddress()).send().wait(); - expect(await token.methods.getBalance(recipient.getAddress()).view()).toEqual(20n); + it('increases recipient funds on mint', async () => { + expect(await token.methods.balance_of_private({ address: recipient.getAddress() }).view()).toEqual(0n); + const secret = Fr.random(); + const secretHash = await computeMessageSecretHash(secret); + expect((await token.methods.mint_private(20n, secretHash).send().wait()).status).toEqual(TxStatus.MINED); + expect( + await token.methods.redeem_shield({ address: recipient.getAddress() }, 20n, secret).send().wait(), + ).toEqual(TxStatus.MINED); + expect(await token.methods.balance_of_private({ address: recipient.getAddress() }).view()).toEqual(20n); }); }); describe('cheats', () => { - const { SANDBOX_URL = 'http://localhost:8080', ETHEREUM_HOST = 'http://localhost:8545' } = process.env; - let rpc: AztecRPC; let owner: AccountWallet; let testContract: TestContract; @@ -122,13 +143,10 @@ describe('guides/dapp/testing', () => { }); describe('assertions', () => { - const { SANDBOX_URL = 'http://localhost:8080', ETHEREUM_HOST = 'http://localhost:8545' } = process.env; - let rpc: AztecRPC; let owner: AccountWallet; let recipient: AccountWallet; - let token: PrivateTokenContract; - let nativeToken: NativeTokenContract; + let token: TokenContract; let cheats: CheatCodes; let ownerSlot: Fr; @@ -136,8 +154,19 @@ describe('guides/dapp/testing', () => { rpc = createAztecRpcClient(SANDBOX_URL); owner = await createAccount(rpc); recipient = await createAccount(rpc); - token = await PrivateTokenContract.deploy(owner, 100n, owner.getAddress()).send().deployed(); - nativeToken = await NativeTokenContract.deploy(owner, 100n, owner.getAddress()).send().deployed(); + token = await TokenContract.deploy(owner).send().deployed(); + expect((await token.methods._initialize({ address: owner.getAddress() }).send().wait()).status).toEqual( + TxStatus.MINED, + ); + const secret = Fr.random(); + const secretHash = await computeMessageSecretHash(secret); + expect((await token.methods._initialize({ address: owner.getAddress() }).send().wait()).status).toEqual( + TxStatus.MINED, + ); + expect((await token.methods.mint_private(secretHash, 100n).send().wait()).status).toEqual(TxStatus.MINED); + expect( + (await token.methods.redeem_shield({ address: owner.getAddress() }, 100n, secretHash).send().wait()).status, + ).toEqual(TxStatus.MINED); // docs:start:calc-slot cheats = await CheatCodes.create(ETHEREUM_HOST, rpc); @@ -157,16 +186,16 @@ describe('guides/dapp/testing', () => { it('checks public storage', async () => { // docs:start:public-storage - await nativeToken.methods.owner_mint_pub(owner.getAddress(), 100n).send().wait(); + await token.methods.mint_public({ address: owner.getAddress() }, 100n).send().wait(); const ownerPublicBalanceSlot = cheats.aztec.computeSlotInMap(4n, owner.getAddress()); - const balance = await rpc.getPublicStorageAt(nativeToken.address, ownerPublicBalanceSlot); + const balance = await rpc.getPublicStorageAt(token.address, ownerPublicBalanceSlot); expect(toBigIntBE(balance!)).toEqual(100n); // docs:end:public-storage }); it('checks unencrypted logs', async () => { // docs:start:unencrypted-logs - const tx = await nativeToken.methods.owner_mint_pub(owner.getAddress(), 100n).send().wait(); + const tx = await token.methods.mint_public({ address: owner.getAddress() }, 100n).send().wait(); const logs = await rpc.getUnencryptedLogs(tx.blockNumber!, 1); const textLogs = L2BlockL2Logs.unrollLogs(logs).map(log => log.toString('ascii')); expect(textLogs).toEqual(['Coins minted']); @@ -175,22 +204,42 @@ describe('guides/dapp/testing', () => { it('asserts a local transaction simulation fails by calling simulate', async () => { // docs:start:local-tx-fails - const call = token.methods.transfer(200n, recipient.getAddress()); + const call = token.methods.transfer( + { address: owner.getAddress() }, + { address: recipient.getAddress() }, + 200n, + 0, + ); await expect(call.simulate()).rejects.toThrowError(/Balance too low/); // docs:end:local-tx-fails }); it('asserts a local transaction simulation fails by calling send', async () => { // docs:start:local-tx-fails-send - const call = token.methods.transfer(200n, recipient.getAddress()); + const call = token.methods.transfer( + { address: owner.getAddress() }, + { address: recipient.getAddress() }, + 200n, + 0, + ); await expect(call.send().wait()).rejects.toThrowError(/Balance too low/); // docs:end:local-tx-fails-send }); it('asserts a transaction is dropped', async () => { // docs:start:tx-dropped - const call1 = token.methods.transfer(80n, recipient.getAddress()); - const call2 = token.methods.transfer(50n, recipient.getAddress()); + const call1 = token.methods.transfer( + { address: owner.getAddress() }, + { address: recipient.getAddress() }, + 80n, + 0, + ); + const call2 = token.methods.transfer( + { address: owner.getAddress() }, + { address: recipient.getAddress() }, + 50n, + 0, + ); await call1.simulate(); await call2.simulate(); @@ -202,14 +251,24 @@ describe('guides/dapp/testing', () => { it('asserts a simulation for a public function call fails', async () => { // docs:start:local-pub-fails - const call = nativeToken.methods.transfer_pub(recipient.getAddress(), 1000n); + const call = token.methods.transfer_public( + { address: owner.getAddress() }, + { address: recipient.getAddress() }, + 1000n, + 0, + ); await expect(call.simulate()).rejects.toThrowError(/Balance too low/); // docs:end:local-pub-fails }); it('asserts a transaction with a failing public call is dropped (until we get public reverts)', async () => { // docs:start:pub-dropped - const call = nativeToken.methods.transfer_pub(recipient.getAddress(), 1000n); + const call = token.methods.transfer_public( + { address: owner.getAddress() }, + { address: recipient.getAddress() }, + 1000n, + 0, + ); await expect(call.send({ skipPublicSimulation: true }).wait()).rejects.toThrowError(/dropped/); // docs:end:pub-dropped });