Skip to content

Commit

Permalink
refactor: remove the native and private token from dapp_testing
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Sep 13, 2023
1 parent d8ca053 commit 1f4246d
Showing 1 changed file with 100 additions and 41 deletions.
141 changes: 100 additions & 41 deletions yarn-project/end-to-end/src/guides/dapp_testing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -20,86 +24,103 @@ describe('guides/dapp/testing', () => {
let stop: () => Promise<void>;
let owner: AccountWallet;
let recipient: AccountWallet;
let token: PrivateTokenContract;
let token: TokenContract;

beforeAll(async () => {
// docs:start:in-proc-sandbox
({ rpcServer: rpc, stop } = await createSandbox());
// 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;
Expand All @@ -122,22 +143,30 @@ 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;

beforeAll(async () => {
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);
Expand All @@ -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']);
Expand All @@ -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();
Expand All @@ -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
});
Expand Down

0 comments on commit 1f4246d

Please sign in to comment.