Skip to content

Commit

Permalink
test: Update more e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Sep 16, 2023
1 parent fae4698 commit a1d3f91
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 41 deletions.
8 changes: 4 additions & 4 deletions yarn-project/end-to-end/src/e2e_2_rpc_servers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ describe('e2e_2_rpc_servers', () => {
expect(balance).toBe(expectedBalance);
};

const deployPrivateTokenContract = async (initialBalance: bigint, owner: AztecAddress) => {
logger(`Deploying PrivateToken contract...`);
const deployTokenContract = async (initialBalance: bigint, owner: AztecAddress) => {
logger(`Deploying Token contract...`);
const contract = await TokenContract.deploy(walletA).send().deployed();
expect((await contract.methods._initialize({ address: owner }).send().wait()).status).toBe(TxStatus.MINED);

Expand All @@ -102,15 +102,15 @@ describe('e2e_2_rpc_servers', () => {
const transferAmount1 = 654n;
const transferAmount2 = 323n;

const completeTokenAddress = await deployPrivateTokenContract(initialBalance, userA.address);
const completeTokenAddress = await deployTokenContract(initialBalance, userA.address);
const tokenAddress = completeTokenAddress.address;

// Add account B to wallet A
await aztecRpcServerA.registerRecipient(userB);
// Add account A to wallet B
await aztecRpcServerB.registerRecipient(userA);

// Add privateToken to RPC server B
// Add token to RPC server B
await aztecRpcServerB.addContracts([
{
abi: TokenContract.abi,
Expand Down
71 changes: 44 additions & 27 deletions yarn-project/end-to-end/src/e2e_aztec_js_browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as AztecJs from '@aztec/aztec.js';
import { AztecAddress, GrumpkinScalar } from '@aztec/circuits.js';
import { DebugLogger, createDebugLogger } from '@aztec/foundation/log';
import { fileURLToPath } from '@aztec/foundation/url';
import { PrivateTokenContractAbi } from '@aztec/noir-contracts/artifacts';
import { TokenContractAbi } from '@aztec/noir-contracts/artifacts';

import { Server } from 'http';
import Koa from 'koa';
Expand Down Expand Up @@ -92,7 +92,7 @@ conditionalDescribe()('e2e_aztec.js_browser', () => {
pageLogger.error(err.toString());
});
await page.goto(`http://localhost:${PORT}/index.html`);
}, 120_000);
}, 200_000);

afterAll(async () => {
await browser.close();
Expand Down Expand Up @@ -128,55 +128,67 @@ conditionalDescribe()('e2e_aztec.js_browser', () => {
expect(stringAccounts.includes(result)).toBeTruthy();
}, 15_000);

it('Deploys Private Token contract', async () => {
await deployPrivateTokenContract();
}, 30_000);
it('Deploys Token contract', async () => {
await deployTokenContract();
}, 60_000);

it("Gets the owner's balance", async () => {
const result = await page.evaluate(
async (rpcUrl, contractAddress, PrivateTokenContractAbi) => {
async (rpcUrl, contractAddress, TokenContractAbi) => {
const { Contract, AztecAddress, createAztecRpcClient } = window.AztecJs;
const client = createAztecRpcClient(rpcUrl!);
const owner = (await client.getRegisteredAccounts())[0].address;
const [wallet] = await AztecJs.getSandboxAccountsWallets(client);
const contract = await Contract.at(AztecAddress.fromString(contractAddress), PrivateTokenContractAbi, wallet);
const balance = await contract.methods.getBalance(owner).view({ from: owner });
const contract = await Contract.at(AztecAddress.fromString(contractAddress), TokenContractAbi, wallet);
const balance = await contract.methods.balance_of_private({ address: owner }).view({ from: owner });
return balance;
},
SANDBOX_URL,
(await getPrivateTokenAddress()).toString(),
PrivateTokenContractAbi,
(await getTokenAddress()).toString(),
TokenContractAbi,
);
logger('Owner balance:', result);
expect(result).toEqual(initialBalance);
});

it('Sends a transfer TX', async () => {
const result = await page.evaluate(
async (rpcUrl, contractAddress, transferAmount, PrivateTokenContractAbi) => {
async (rpcUrl, contractAddress, transferAmount, TokenContractAbi) => {
console.log(`Starting transfer tx`);
const { AztecAddress, Contract, createAztecRpcClient } = window.AztecJs;
const client = createAztecRpcClient(rpcUrl!);
const accounts = await client.getRegisteredAccounts();
const receiver = accounts[1].address;
const [wallet] = await AztecJs.getSandboxAccountsWallets(client);
const contract = await Contract.at(AztecAddress.fromString(contractAddress), PrivateTokenContractAbi, wallet);
await contract.methods.transfer(transferAmount, receiver).send().wait();
const contract = await Contract.at(AztecAddress.fromString(contractAddress), TokenContractAbi, wallet);
await contract.methods
.transfer({ address: accounts[0].address }, { address: receiver }, transferAmount, 0)
.send()
.wait();
console.log(`Transferred ${transferAmount} tokens to new Account`);
return await contract.methods.getBalance(receiver).view({ from: receiver });
return await contract.methods.balance_of_private({ address: receiver }).view({ from: receiver });
},
SANDBOX_URL,
(await getPrivateTokenAddress()).toString(),
(await getTokenAddress()).toString(),
transferAmount,
PrivateTokenContractAbi,
TokenContractAbi,
);
expect(result).toEqual(transferAmount);
}, 60_000);

const deployPrivateTokenContract = async () => {
const deployTokenContract = async () => {
const txHash = await page.evaluate(
async (rpcUrl, privateKeyString, initialBalance, PrivateTokenContractAbi) => {
const { GrumpkinScalar, DeployMethod, createAztecRpcClient, getUnsafeSchnorrAccount } = window.AztecJs;
async (rpcUrl, privateKeyString, initialBalance, TokenContractAbi) => {
const {
GrumpkinScalar,
DeployMethod,
createAztecRpcClient,
getUnsafeSchnorrAccount,
Contract,
Fr,
computeMessageSecretHash,
getSandboxAccountsWallets,
} = window.AztecJs;
const client = createAztecRpcClient(rpcUrl!);
let accounts = await client.getRegisteredAccounts();
if (accounts.length === 0) {
Expand All @@ -185,30 +197,35 @@ conditionalDescribe()('e2e_aztec.js_browser', () => {
await getUnsafeSchnorrAccount(client, privateKey).waitDeploy();
accounts = await client.getRegisteredAccounts();
}
const owner = accounts[0];
const tx = new DeployMethod(owner.publicKey, client, PrivateTokenContractAbi, [
initialBalance,
owner.address,
]).send();
const [owner] = await getSandboxAccountsWallets(client);
const tx = new DeployMethod(accounts[0].publicKey, client, TokenContractAbi).send();
await tx.wait();
const receipt = await tx.getReceipt();
console.log(`Contract Deployed: ${receipt.contractAddress}`);

const token = await Contract.at(receipt.contractAddress!, TokenContractAbi, owner);
await token.methods._initialize({ address: owner.getAddress() }).send().wait();
const secret = Fr.random();
const secretHash = await computeMessageSecretHash(secret);
await token.methods.mint_private(initialBalance, secretHash).send().wait();
await token.methods.redeem_shield({ address: owner.getAddress() }, initialBalance, secret).send().wait();

return receipt.txHash.toString();
},
SANDBOX_URL,
privKey.toString(),
initialBalance,
PrivateTokenContractAbi,
TokenContractAbi,
);

const txResult = await testClient.getTxReceipt(AztecJs.TxHash.fromString(txHash));
expect(txResult.status).toEqual(AztecJs.TxStatus.MINED);
contractAddress = txResult.contractAddress!;
};

const getPrivateTokenAddress = async () => {
const getTokenAddress = async () => {
if (!contractAddress) {
await deployPrivateTokenContract();
await deployTokenContract();
}
return contractAddress;
};
Expand Down
36 changes: 26 additions & 10 deletions yarn-project/end-to-end/src/e2e_cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AztecNodeService } from '@aztec/aztec-node';
import { AztecAddress, AztecRPCServer } from '@aztec/aztec-rpc';
import { startHttpRpcServer } from '@aztec/aztec-sandbox';
import { createDebugLogger } from '@aztec/aztec.js';
import { Fr, computeMessageSecretHash, createDebugLogger } from '@aztec/aztec.js';
import { getProgram } from '@aztec/cli';
import { AztecRPC, CompleteAddress } from '@aztec/types';

Expand Down Expand Up @@ -75,7 +75,9 @@ describe('CLI e2e test', () => {
if (addRpcUrl) {
args.push('--rpc-url', RPC_URL);
}
return cli.parseAsync(args);
const res = cli.parseAsync(args);
resetCli();
return res;
};

// Returns first match across all logs collected so far
Expand Down Expand Up @@ -138,8 +140,8 @@ describe('CLI e2e test', () => {
expect(foundAddress).toBeDefined();
const ownerAddress = AztecAddress.fromString(foundAddress!);

debug('Deploy Private Token Contract using created account.');
await run(`deploy PrivateTokenContractAbi --args ${INITIAL_BALANCE} ${ownerAddress} --salt 0`);
debug('Deploy Token Contract using created account.');
await run(`deploy TokenContractAbi --salt 0`);
const loggedAddress = findInLogs(/Contract\sdeployed\sat\s+(?<address>0x[a-fA-F0-9]+)/)?.groups?.address;
expect(loggedAddress).toBeDefined();
contractAddress = AztecAddress.fromString(loggedAddress!);
Expand All @@ -152,6 +154,22 @@ describe('CLI e2e test', () => {
const checkResult = findInLogs(/Contract\sfound\sat\s+(?<address>0x[a-fA-F0-9]+)/)?.groups?.address;
expect(checkResult).toEqual(deployedContract?.contractAddress.toString());

debug('Initialize token contract.');
await run(
`send _initialize --args ${ownerAddress} --contract-abi TokenContractAbi --contract-address ${contractAddress.toString()} --private-key ${privKey}`,
);

const secret = Fr.random();
const secretHash = await computeMessageSecretHash(secret);

debug('Mint initial tokens.');
await run(
`send mint_private --args ${INITIAL_BALANCE} ${secretHash} --contract-abi TokenContractAbi --contract-address ${contractAddress.toString()} --private-key ${privKey}`,
);
await run(
`send redeem_shield --args ${ownerAddress} ${INITIAL_BALANCE} ${secret} --contract-abi TokenContractAbi --contract-address ${contractAddress.toString()} --private-key ${privKey}`,
);

// clear logs
clearLogs();
await run(`get-contract-data ${loggedAddress}`);
Expand All @@ -160,7 +178,7 @@ describe('CLI e2e test', () => {

debug("Check owner's balance");
await run(
`call getBalance --args ${ownerAddress} --contract-abi PrivateTokenContractAbi --contract-address ${contractAddress.toString()}`,
`call balance_of_private --args ${ownerAddress} --contract-abi TokenContractAbi --contract-address ${contractAddress.toString()}`,
);
const balance = findInLogs(/View\sresult:\s+(?<data>\S+)/)?.groups?.data;
expect(balance!).toEqual(`${BigInt(INITIAL_BALANCE).toString()}n`);
Expand All @@ -171,7 +189,7 @@ describe('CLI e2e test', () => {
const receiver = existingAccounts.find(acc => acc.address.toString() !== ownerAddress.toString());

await run(
`send transfer --args ${TRANSFER_BALANCE} ${receiver?.address.toString()} --contract-address ${contractAddress.toString()} --contract-abi PrivateTokenContractAbi --private-key ${privKey}`,
`send transfer --args ${ownerAddress.toString()} ${receiver?.address.toString()} ${TRANSFER_BALANCE} 0 --contract-address ${contractAddress.toString()} --contract-abi TokenContractAbi --private-key ${privKey}`,
);
const txHash = findInLogs(/Transaction\shash:\s+(?<txHash>\S+)/)?.groups?.txHash;

Expand All @@ -182,13 +200,11 @@ describe('CLI e2e test', () => {
expect(parsedResult.txHash).toEqual(txHash);
expect(parsedResult.status).toEqual('mined');
debug("Check Receiver's balance");
// Reset CLI as we're calling getBalance again
resetCli();
clearLogs();
await run(
`call getBalance --args ${receiver?.address.toString()} --contract-abi PrivateTokenContractAbi --contract-address ${contractAddress.toString()}`,
`call balance_of_private --args ${receiver?.address.toString()} --contract-abi TokenContractAbi --contract-address ${contractAddress.toString()}`,
);
const receiverBalance = findInLogs(/View\sresult:\s+(?<data>\S+)/)?.groups?.data;
expect(receiverBalance).toEqual(`${BigInt(TRANSFER_BALANCE).toString()}n`);
}, 30_000);
}, 100_000);
});

0 comments on commit a1d3f91

Please sign in to comment.