Skip to content

Commit

Permalink
add e2e tests
Browse files Browse the repository at this point in the history
fix tsconfig
update snapshots
  • Loading branch information
just-mitch committed Feb 22, 2024
1 parent 5696325 commit 2ed0652
Show file tree
Hide file tree
Showing 10 changed files with 95,115 additions and 56,407 deletions.
73 changes: 44 additions & 29 deletions yarn-project/end-to-end/src/e2e_dapp_subscription.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import {
AccountWalletWithPrivateKey,
AztecAddress,
AztecNode,
CompleteAddress,
FeePaymentMethod,
Contract,
Fr,
FunctionCall,
PXE,
PrivateFeePaymentMethod,
SentTx,
computeAuthWitMessageHash,
} from '@aztec/aztec.js';
import { FunctionData, FunctionSelector } from '@aztec/circuits.js';
import { DefaultDappEntrypoint } from '@aztec/entrypoints/dapp';
import {
AppSubscriptionContractContract,
Expand All @@ -20,11 +15,11 @@ import {
CounterContract,
GasTokenContract,
} from '@aztec/noir-contracts.js';
import { getCanonicalGasToken } from '@aztec/protocol-contracts/gas-token';

import { jest } from '@jest/globals';

import { setup } from './fixtures/utils.js';
import { EndToEndContext, setup } from './fixtures/utils.js';
import { GasBridgingTestHarness } from './shared/gas_portal_test_harness.js';

jest.setTimeout(100_000);

Expand All @@ -33,10 +28,6 @@ const TOKEN_SYMBOL = 'BAC';
const TOKEN_DECIMALS = 18n;

describe('e2e_fees', () => {
let accounts: CompleteAddress[];
let aztecNode: AztecNode;
let pxe: PXE;
let wallets: AccountWalletWithPrivateKey[];
let aliceWallet: AccountWalletWithPrivateKey;
let bobWallet: AccountWalletWithPrivateKey;
let aliceAddress: AztecAddress; // Dapp subscriber.
Expand All @@ -47,30 +38,36 @@ describe('e2e_fees', () => {
let counterContract: CounterContract;
let subscriptionContract: AppSubscriptionContractContract;
let gasTokenContract: GasTokenContract;

let bananaFPC: BananaFPCContract;
let e2eContext: EndToEndContext;
let gasBridgeTestHarness: GasBridgingTestHarness;

beforeAll(async () => {
process.env.PXE_URL = '';
({ accounts, aztecNode, pxe, wallets } = await setup(3));
e2eContext = await setup(3);

const { wallets, accounts, aztecNode, deployL1ContractsValues, logger, pxe } = e2eContext;

aliceAddress = accounts.at(0)!.address;
bobAddress = accounts.at(1)!.address;
sequencerAddress = accounts.at(2)!.address;

gasBridgeTestHarness = await GasBridgingTestHarness.new(
pxe,
deployL1ContractsValues.publicClient,
deployL1ContractsValues.walletClient,
wallets[0],
logger,
);

gasTokenContract = gasBridgeTestHarness.l2Token;

await aztecNode.setConfig({
feeRecipient: sequencerAddress,
});

[aliceWallet, bobWallet] = wallets;

// deploy the gas token
// this is what the sequencer will be paid in
const canonicalGasToken = getCanonicalGasToken();
gasTokenContract = await GasTokenContract.deploy(aliceWallet)
.send({ contractAddressSalt: canonicalGasToken.instance.salt })
.deployed();

bananaCoin = await BananaCoin.deploy(aliceWallet, aliceAddress, TOKEN_NAME, TOKEN_SYMBOL, TOKEN_DECIMALS)
.send()
.deployed();
Expand All @@ -96,14 +93,30 @@ describe('e2e_fees', () => {
// mint some test tokens for Alice
// she'll pay for the subscription with these
await bananaCoin.methods.privately_mint_private_note(1000n).send().wait();
// credit the app sub contract with 1000n gas tokens
// await gasTokenContract.methods.redeem_bridged_balance(1000n, subscriptionContract.address).send().wait();
// await gasTokenContract.methods.redeem_bridged_balance(1000n, bananaFPC.address).send().wait();
}, 100_000);
await gasBridgeTestHarness.bridgeFromL1ToL2(1000n, 1000n, subscriptionContract.address);
await gasBridgeTestHarness.bridgeFromL1ToL2(1000n, 1000n, bananaFPC.address);

const balances = async (symbol: string, contract: Contract) => {
const [sequencerBalance, subscriptionBalance, fpcBalance] = await Promise.all([
contract.methods.balance_of_public(sequencerAddress).view(),
contract.methods.balance_of_public(subscriptionContract.address).view(),
contract.methods.balance_of_public(bananaFPC.address).view(),
]);

e2eContext.logger(
`${symbol} balances: Alice ${subscriptionBalance}, bananaPay: ${fpcBalance}, sequencer: ${sequencerBalance}`,
);

return { sequencerBalance, subscriptionBalance, fpcBalance };
};

it('should deploy the gas contract at ' + getCanonicalGasToken().address, () => {
expect(gasTokenContract.address).toEqual(getCanonicalGasToken().address);
});
{
const { sequencerBalance, subscriptionBalance, fpcBalance } = await balances('⛽', gasTokenContract);
expect(sequencerBalance).toEqual(0n);
expect(subscriptionBalance).toEqual(1000n);
expect(fpcBalance).toEqual(1000n);
}
}, 100_000);

it('should allow Alice to subscribe', async () => {
// Authorize the subscription contract to transfer the subscription amount from the subscriber.
Expand All @@ -119,6 +132,7 @@ describe('e2e_fees', () => {
}, 100_000);

it('should call dapp subscription entrypoint', async () => {
const { pxe } = e2eContext;
const dappPayload = new DefaultDappEntrypoint(aliceAddress, aliceWallet, subscriptionContract.address);
const action = counterContract.methods.increment(bobAddress).request();
const txExReq = await dappPayload.createTxExecutionRequest([action]);
Expand Down Expand Up @@ -155,7 +169,7 @@ describe('e2e_fees', () => {

return subscriptionContract
.withWallet(aliceWallet)
.methods.subscribe(aliceAddress, nonce, (await pxe.getBlockNumber()) + blockDelta, txCount)
.methods.subscribe(aliceAddress, nonce, (await e2eContext.pxe.getBlockNumber()) + blockDelta, txCount)
.send({
fee: {
maxFee: 1n,
Expand All @@ -167,6 +181,7 @@ describe('e2e_fees', () => {
}

async function dappIncrement() {
const { pxe } = e2eContext;
const dappEntrypoint = new DefaultDappEntrypoint(aliceAddress, aliceWallet, subscriptionContract.address);
const action = counterContract.methods.increment(bobAddress).request();
const txExReq = await dappEntrypoint.createTxExecutionRequest([action]);
Expand Down
32 changes: 2 additions & 30 deletions yarn-project/end-to-end/src/e2e_fees.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import {
TxHash,
computeAuthWitMessageHash,
computeMessageSecretHash,
sleep,
} from '@aztec/aztec.js';
import { EthAddress, FunctionData } from '@aztec/circuits.js';
import { FunctionData } from '@aztec/circuits.js';
import { decodeFunctionSignature } from '@aztec/foundation/abi';
import { TokenContract as BananaCoin, BananaFPCContract, GasTokenContract } from '@aztec/noir-contracts.js';

Expand All @@ -34,7 +33,6 @@ describe('e2e_fees', () => {
let bananaCoin: BananaCoin;
let bananaFPC: BananaFPCContract;

let l1GasHolder: EthAddress;
let gasBridgeTestHarness: GasBridgingTestHarness;
let e2eContext: EndToEndContext;

Expand All @@ -52,7 +50,6 @@ describe('e2e_fees', () => {
logger,
);

l1GasHolder = gasBridgeTestHarness.ethAccount;
gasTokenContract = gasBridgeTestHarness.l2Token;

BananaCoin.artifact.functions.forEach(fn => {
Expand All @@ -72,8 +69,6 @@ describe('e2e_fees', () => {
feeRecipient: accounts.at(-1)!.address,
});

logger(`Gas token contract deployed at ${gasTokenContract.address}`);

aliceAddress = accounts.at(0)!.address;
sequencerAddress = accounts.at(-1)!.address;
}, 30_000);
Expand All @@ -96,7 +91,7 @@ describe('e2e_fees', () => {
.send()
.deployed();
e2eContext.logger(`bananaPay deployed at ${bananaFPC.address}`);
await bridgeFromL1ToL2(InitialFPCGas, bananaFPC.address);
await gasBridgeTestHarness.bridgeFromL1ToL2(InitialFPCGas + 1n, InitialFPCGas, bananaFPC.address);
{
const { sequencerBalance, aliceBalance, fpcBalance } = await balances('⛽', gasTokenContract);
expect(sequencerBalance).toEqual(0n);
Expand Down Expand Up @@ -165,29 +160,6 @@ describe('e2e_fees', () => {
}
}, 100_000);

const bridgeFromL1ToL2 = async (bridgeAmount: bigint, owner: AztecAddress) => {
const l1TokenBalance = 1000000n;

const [secret, secretHash] = gasBridgeTestHarness.generateClaimSecret();

// 1. Mint tokens on L1
await gasBridgeTestHarness.mintTokensOnL1(l1TokenBalance);

// 2. Deposit tokens to the TokenPortal
const messageKey = await gasBridgeTestHarness.sendTokensToPortalPublic(bridgeAmount, owner, secretHash);
expect(await gasBridgeTestHarness.getL1BalanceOf(l1GasHolder)).toBe(l1TokenBalance - bridgeAmount);

// Wait for the archiver to process the message
await sleep(2500);

// Perform an unrelated transaction on L2 to progress the rollup. Here we mint public tokens.
await gasTokenContract.methods.check_balance(0).send().wait();

// 3. Consume L1-> L2 message and mint public tokens on L2
await gasBridgeTestHarness.consumeMessageOnAztecAndMintPublicly(bridgeAmount, owner, messageKey, secret);
await gasBridgeTestHarness.expectPublicBalanceOnL2(owner, bridgeAmount);
};

const addPendingShieldNoteToPXE = async (accountIndex: number, amount: bigint, secretHash: Fr, txHash: TxHash) => {
const storageSlot = new Fr(5); // The storage slot of `pending_shields` is 5.
const noteTypeId = new Fr(84114971101151129711410111011678111116101n); // TransparentNote
Expand Down
22 changes: 22 additions & 0 deletions yarn-project/end-to-end/src/shared/gas_portal_test_harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Wallet,
computeMessageSecretHash,
deployL1Contract,
sleep,
} from '@aztec/aztec.js';
import { GasPortalAbi, GasPortalBytecode, OutboxAbi, PortalERC20Abi, PortalERC20Bytecode } from '@aztec/l1-artifacts';
import { GasTokenContract } from '@aztec/noir-contracts.js';
Expand Down Expand Up @@ -244,5 +245,26 @@ export class GasBridgingTestHarness {
const balance = await this.getL2PublicBalanceOf(owner);
expect(balance).toBe(expectedBalance);
}

async bridgeFromL1ToL2(l1TokenBalance: bigint, bridgeAmount: bigint, owner: AztecAddress) {
const [secret, secretHash] = this.generateClaimSecret();

// 1. Mint tokens on L1
await this.mintTokensOnL1(l1TokenBalance);

// 2. Deposit tokens to the TokenPortal
const messageKey = await this.sendTokensToPortalPublic(bridgeAmount, owner, secretHash);
expect(await this.getL1BalanceOf(this.ethAccount)).toBe(l1TokenBalance - bridgeAmount);

// Wait for the archiver to process the message
await sleep(2500);

// Perform an unrelated transaction on L2 to progress the rollup. Here we mint public tokens.
await this.l2Token.methods.check_balance(0).send().wait();

// 3. Consume L1-> L2 message and mint public tokens on L2
await this.consumeMessageOnAztecAndMintPublicly(bridgeAmount, owner, messageKey, secret);
await this.expectPublicBalanceOnL2(owner, bridgeAmount);
}
}
// docs:end:cross_chain_test_harness
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`FunctionSelector computes a function selector from signature 1`] = `
FunctionSelector {
"value": 2835717307,
}
`;
Loading

0 comments on commit 2ed0652

Please sign in to comment.