Skip to content

Commit

Permalink
refactor: replace private_token in sample_dapp e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Sep 16, 2023
1 parent 1775f31 commit 33225d8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 42 deletions.
2 changes: 1 addition & 1 deletion yarn-project/end-to-end/src/e2e_sandbox_example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('e2e_sandbox_example', () => {
const tokenContractAlice = await TokenContract.at(contract.address, await accounts[0].getWallet());

await tokenContractAlice.methods._initialize({ address: alice }).send().wait();
await tokenContractAlice.methods.set_minter({ address: bob }, 1).send().wait();
await tokenContractAlice.methods.set_minter({ address: bob }, true).send().wait();

logger(`Contract successfully deployed at address ${contract.address.toShortString()}`);

Expand Down
14 changes: 3 additions & 11 deletions yarn-project/end-to-end/src/sample-dapp/contracts.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { Contract } from '@aztec/aztec.js';
import {
PrivateTokenContractAbi as PrivateTokenArtifact,
PublicTokenContractAbi as PublicTokenArtifact,
} from '@aztec/noir-contracts/artifacts';
import { TokenContractAbi } from '@aztec/noir-contracts/artifacts';

import { readFileSync } from 'fs';

// docs:start:get-tokens
export async function getPrivateToken(client) {
export async function getToken(client) {
const addresses = JSON.parse(readFileSync('addresses.json'));
return Contract.at(addresses.privateToken, PrivateTokenArtifact, client);
}

export async function getPublicToken(client) {
const addresses = JSON.parse(readFileSync('addresses.json'));
return Contract.at(addresses.publicToken, PublicTokenArtifact, client);
return Contract.at(addresses.token, TokenContractAbi, client);
}
// docs:end:get-tokens
21 changes: 8 additions & 13 deletions yarn-project/end-to-end/src/sample-dapp/deploy.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { ContractDeployer, createAztecRpcClient } from '@aztec/aztec.js';
import {
PrivateTokenContractAbi as PrivateTokenArtifact,
PublicTokenContractAbi as PublicTokenArtifact,
} from '@aztec/noir-contracts/artifacts';
import { Contract, ContractDeployer, createAztecRpcClient, getSandboxAccountsWallets } from '@aztec/aztec.js';
import { TokenContractAbi } from '@aztec/noir-contracts/artifacts';

import { writeFileSync } from 'fs';
import { fileURLToPath } from 'url';
Expand All @@ -12,17 +9,15 @@ const { SANDBOX_URL = 'http://localhost:8080' } = process.env;

async function main() {
const client = createAztecRpcClient(SANDBOX_URL);
const [owner] = await client.getRegisteredAccounts();
const [owner] = await getSandboxAccountsWallets(client);

const privateTokenDeployer = new ContractDeployer(PrivateTokenArtifact, client);
const { contractAddress: privateTokenAddress } = await privateTokenDeployer.deploy(100n, owner.address).send().wait();
console.log(`Private token deployed at ${privateTokenAddress.toString()}`);
const token = await Contract.deploy(client, TokenContractAbi, []).send().deployed();

const publicTokenDeployer = new ContractDeployer(PublicTokenArtifact, client);
const { contractAddress: publicTokenAddress } = await publicTokenDeployer.deploy().send().wait();
console.log(`Public token deployed at ${publicTokenAddress.toString()}`);
await token.withWallet(owner).methods._initialize({ address: owner.getAddress() }).send().wait();

const addresses = { privateToken: privateTokenAddress.toString(), publicToken: publicTokenAddress.toString() };
console.log(`Token deployed at ${token.address.toString()}`);

const addresses = { token: token.address.toString() };
writeFileSync('addresses.json', JSON.stringify(addresses, null, 2));
}
// docs:end:dapp-deploy
Expand Down
43 changes: 33 additions & 10 deletions yarn-project/end-to-end/src/sample-dapp/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { L2BlockL2Logs, createAztecRpcClient, getSandboxAccountsWallets } from '@aztec/aztec.js';
import {
Fr,
L2BlockL2Logs,
computeMessageSecretHash,
createAztecRpcClient,
getSandboxAccountsWallets,
} from '@aztec/aztec.js';
import { fileURLToPath } from '@aztec/foundation/url';

import { getPrivateToken, getPublicToken } from './contracts.mjs';
import { getToken } from './contracts.mjs';

const { SANDBOX_URL = 'http://localhost:8080' } = process.env;

Expand All @@ -15,22 +21,37 @@ async function showAccounts(client) {
async function showPrivateBalances(client) {
// docs:start:showPrivateBalances
const accounts = await client.getRegisteredAccounts();
const privateToken = await getPrivateToken(client);
const token = await getToken(client);

for (const account of accounts) {
// highlight-next-line:showPrivateBalances
const balance = await privateToken.methods.getBalance(account.address).view();
const balance = await token.methods.balance_of_private({ address: account.address }).view();
console.log(`Balance of ${account.address}: ${balance}`);
}
// docs:end:showPrivateBalances
}

async function mintPrivateFunds(client) {
const [owner] = await getSandboxAccountsWallets(client);
const token = await getToken(owner);

await showPrivateBalances(client);

const mintAmount = 20n;
const secret = Fr.random();
const secretHash = await computeMessageSecretHash(secret);
await token.methods.mint_private(mintAmount, secretHash).send().wait();
await token.methods.redeem_shield({ address: owner.getAddress() }, mintAmount, secret).send().wait();

await showPrivateBalances(client);
}

async function transferPrivateFunds(client) {
// docs:start:transferPrivateFunds
const [owner, recipient] = await getSandboxAccountsWallets(client);
const privateToken = await getPrivateToken(owner);
const token = await getToken(owner);

const tx = privateToken.methods.transfer(1n, recipient.getAddress()).send();
const tx = token.methods.transfer({ address: owner.getAddress() }, { address: recipient.getAddress() }, 1n, 0).send();
console.log(`Sent transfer transaction ${await tx.getTxHash()}`);
await showPrivateBalances(client);

Expand All @@ -44,11 +65,11 @@ async function transferPrivateFunds(client) {
async function showPublicBalances(client) {
// docs:start:showPublicBalances
const accounts = await client.getRegisteredAccounts();
const publicToken = await getPublicToken(client);
const token = await getToken(client);

for (const account of accounts) {
// highlight-next-line:showPublicBalances
const balance = await publicToken.methods.publicBalanceOf(account.address).view();
const balance = await token.methods.balance_of_public({ address: account.address }).view();
console.log(`Balance of ${account.address}: ${balance}`);
}
// docs:end:showPublicBalances
Expand All @@ -57,9 +78,9 @@ async function showPublicBalances(client) {
async function mintPublicFunds(client) {
// docs:start:mintPublicFunds
const [owner] = await getSandboxAccountsWallets(client);
const publicToken = await getPublicToken(owner);
const token = await getToken(owner);

const tx = publicToken.methods.mint(100n, owner.getAddress()).send();
const tx = token.methods.mint_public({ address: owner.getAddress() }, 100n).send();
console.log(`Sent mint transaction ${await tx.getTxHash()}`);
await showPublicBalances(client);

Expand All @@ -84,6 +105,8 @@ async function main() {

await showAccounts(client);

await mintPrivateFunds(client);

await transferPrivateFunds(client);

await mintPublicFunds(client);
Expand Down
25 changes: 18 additions & 7 deletions yarn-project/end-to-end/src/sample-dapp/index.test.mjs
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import { createSandbox } from '@aztec/aztec-sandbox';
import { Contract, createAccount } from '@aztec/aztec.js';
import { PrivateTokenContractAbi as PrivateTokenArtifact } from '@aztec/noir-contracts/artifacts';
import { Contract, Fr, computeMessageSecretHash, createAccount } from '@aztec/aztec.js';
import { TokenContractAbi } from '@aztec/noir-contracts/artifacts';

describe('private token', () => {
describe('token', () => {
// docs:start:setup
let rpc, stop, owner, recipient, token;
beforeAll(async () => {
({ rpcServer: rpc, stop } = await createSandbox());
owner = await createAccount(rpc);
recipient = await createAccount(rpc);
token = await Contract.deploy(owner, PrivateTokenArtifact, [100n, owner.getAddress()]).send().deployed();

token = await Contract.deploy(owner, TokenContractAbi, []).send().deployed();
await token.methods._initialize({ address: owner.getAddress() }).send().wait();

const initialBalance = 20n;
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();
}, 60_000);

afterAll(() => stop());
// docs:end:setup

// docs:start:test
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);
expect(await token.methods.balance_of_private({ address: recipient.getAddress() }).view()).toEqual(0n);
await token.methods
.transfer({ address: owner.getAddress() }, { address: recipient.getAddress() }, 20n, 0)
.send()
.wait();
expect(await token.methods.balance_of_private({ address: recipient.getAddress() }).view()).toEqual(20n);
});
// docs:end:test
});

0 comments on commit 33225d8

Please sign in to comment.