diff --git a/yarn-project/end-to-end/src/e2e_sandbox_example.test.ts b/yarn-project/end-to-end/src/e2e_sandbox_example.test.ts index 39d36a7f0976..0e4058a2015d 100644 --- a/yarn-project/end-to-end/src/e2e_sandbox_example.test.ts +++ b/yarn-project/end-to-end/src/e2e_sandbox_example.test.ts @@ -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()}`); diff --git a/yarn-project/end-to-end/src/sample-dapp/contracts.mjs b/yarn-project/end-to-end/src/sample-dapp/contracts.mjs index d72e4f4bd5fe..b0892378b017 100644 --- a/yarn-project/end-to-end/src/sample-dapp/contracts.mjs +++ b/yarn-project/end-to-end/src/sample-dapp/contracts.mjs @@ -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 diff --git a/yarn-project/end-to-end/src/sample-dapp/deploy.mjs b/yarn-project/end-to-end/src/sample-dapp/deploy.mjs index af8d7604f000..1fa4036fee56 100644 --- a/yarn-project/end-to-end/src/sample-dapp/deploy.mjs +++ b/yarn-project/end-to-end/src/sample-dapp/deploy.mjs @@ -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'; @@ -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 diff --git a/yarn-project/end-to-end/src/sample-dapp/index.mjs b/yarn-project/end-to-end/src/sample-dapp/index.mjs index a911862283f3..e07d8d78ed94 100644 --- a/yarn-project/end-to-end/src/sample-dapp/index.mjs +++ b/yarn-project/end-to-end/src/sample-dapp/index.mjs @@ -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; @@ -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); @@ -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 @@ -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); @@ -84,6 +105,8 @@ async function main() { await showAccounts(client); + await mintPrivateFunds(client); + await transferPrivateFunds(client); await mintPublicFunds(client); diff --git a/yarn-project/end-to-end/src/sample-dapp/index.test.mjs b/yarn-project/end-to-end/src/sample-dapp/index.test.mjs index 001412e17f6b..88f8cf4d3355 100644 --- a/yarn-project/end-to-end/src/sample-dapp/index.test.mjs +++ b/yarn-project/end-to-end/src/sample-dapp/index.test.mjs @@ -1,15 +1,23 @@ 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()); @@ -17,9 +25,12 @@ describe('private token', () => { // 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 });