From 12e854f71a18c04bf4e5e9e6c682d35b6b3f2dcb Mon Sep 17 00:00:00 2001 From: Rahul Kothari Date: Wed, 20 Sep 2023 09:36:25 +0000 Subject: [PATCH 1/4] fix tutorial --- .../dapps/tutorials/contract_deployment.md | 43 ++++++++++++------- .../dapps/tutorials/contract_interaction.md | 20 ++++++--- .../end-to-end/src/sample-dapp/deploy.mjs | 11 +++-- .../src/contracts/token_contract/src/main.nr | 2 + .../src/contracts/token_contract/src/types.nr | 4 +- .../src/contracts/token_contract/src/util.nr | 4 +- 6 files changed, 56 insertions(+), 28 deletions(-) diff --git a/docs/docs/dev_docs/dapps/tutorials/contract_deployment.md b/docs/docs/dev_docs/dapps/tutorials/contract_deployment.md index 6bcd4062724..ba8aae370e9 100644 --- a/docs/docs/dev_docs/dapps/tutorials/contract_deployment.md +++ b/docs/docs/dev_docs/dapps/tutorials/contract_deployment.md @@ -8,24 +8,35 @@ Follow the instructions [here](../../getting_started/noir_contracts.md) to insta ## Initialise nargo project -Create a new `contracts` folder, and from there, initialise a new project called `private_token`: +Create a new `contracts` folder, and from there, initialise a new project called `token`: ```sh mkdir contracts && cd contracts -nargo new --contract private_token +nargo new --contract token ``` -Then, open the `contracts/private_token/Nargo.toml` configuration file, and add the `aztec.nr` and `value_note` libraries as dependencies: +Then, open the `contracts/token/Nargo.toml` configuration file, and add the `aztec.nr` and `value_note` libraries as dependencies: ```toml [dependencies] aztec = { git="https://github.com/AztecProtocol/aztec-nr", tag="master", directory="aztec" } value_note = { git="https://github.com/AztecProtocol/aztec-nr", tag="master", directory="value-note" } +safe_math = { git="https://github.com/AztecProtocol/aztec-nr", tag="master", directory="safe-math" } ``` -Last, copy-paste the code from the `PrivateToken` contract into `contracts/private_token/main.nr`: +Last, copy-paste the code from the `Token` contract into `contracts/token/main.nr`: -#include_code all yarn-project/noir-contracts/src/contracts/private_token_contract/src/main.nr rust +#include_code all yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust + +The `Token` contract also requires two helper files. Copy-them too: + +Create `contracts/token/types.nr` and copy-paste the following: + +#include_code all yarn-project/noir-contracts/src/contracts/token_contract/src/types.nr rust + +Finally, create `contracts/token/util.nr` and copy-paste the following: + +#include_code all yarn-project/noir-contracts/src/contracts/token_contract/src/util.nr rust ## Compile your contract @@ -38,14 +49,14 @@ yarn add -D @aztec/cli Now run the following from your project root: ```sh -yarn aztec-cli compile contracts/private_token +yarn aztec-cli compile contracts/token ``` :::info If you are using Typescript, consider including the `--typescript` option to [generate type-safe wrappers](../../contracts/compiling.md#typescript-interfaces) for your contracts. ::: -This should have created an artifact `contracts/private_token/target/private_token-Main.json` with the interface and bytecode for your contract. +This should have created an artifact `contracts/token/target/Token.json` with the interface and bytecode for your contract. ## Adding a second contract @@ -65,14 +76,14 @@ With both contracts ready, we'll now proceed to deployment. Let's now write a script for deploying your contracts to the Sandbox. We'll create an RPC client, and then use the `ContractDeployer` class to deploy our contracts, and store the deployment address to a local JSON file. -Create a new file `src/deploy.mjs`, importing the contract artifacts we have generated plus the dependencies we'll need, and with a call to a `main` function that we'll populate in a second: +Create a new file `src/deploy.mjs`, with a call to a `main` function that we'll populate in a second: ```js // src/deploy.mjs -import { writeFileSync } from "fs"; -import { createAztecRpcClient, ContractDeployer } from "@aztec/aztec.js"; -import PrivateTokenArtifact from "../contracts/private_token/target/PrivateToken.json" assert { type: "json" }; -import PublicTokenArtifact from "../contracts/public_token/target/PublicToken.json" assert { type: "json" }; +import { writeFileSync } from 'fs'; +import { Contract, ContractDeployer, createAztecRpcClient, getSandboxAccountsWallets } from '@aztec/aztec.js'; +import TokenContractAbi from "../contracts/token/target/Token.json" assert { type: "json" }; +import PublicTokenContractAbi from "../contracts/public_token/target/PublicToken.json" assert { type: "json" }; async function main() {} @@ -82,17 +93,17 @@ main().catch((err) => { }); ``` -Now we can deploy the contracts by adding the following code to the `src/deploy.mjs` file. Here, we are using the `ContractDeployer` class with the compiled artifact to send a new deployment transaction. The `wait` method will block execution until the transaction is successfully mined, and return a receipt with the deployed contract address. +Now we will import the contract artifacts we have generated plus the dependencies we'll need, and then we can deploy the contracts by adding the following code to the `src/deploy.mjs` file. Here, we are using the `ContractDeployer` class with the compiled artifact to send a new deployment transaction. The `wait` method will block execution until the transaction is successfully mined, and return a receipt with the deployed contract address. #include_code dapp-deploy yarn-project/end-to-end/src/sample-dapp/deploy.mjs javascript -Note that the private token constructor expects an `owner` address to mint an initial set of tokens to. We are using the first account from the Sandbox for this. +Note that the token's `_initialize()` method expects an `owner` address to mint an initial set of tokens to. We are using the first account from the Sandbox for this. :::info If you are using the generated typescript classes, you can drop the generic `ContractDeployer` in favor of using the `deploy` method of the generated class, which will automatically load the artifact for you and type-check the constructor arguments: ```typescript -await PrivateToken.deploy(client, 100n, owner.address).send().wait(); +await Token.deploy(client).send().wait(); ``` ::: @@ -100,7 +111,7 @@ await PrivateToken.deploy(client, 100n, owner.address).send().wait(); Run the snippet above as `node src/deploy.mjs`, and you should see the following output, along with a new `addresses.json` file in your project root: ```text -Private token deployed to 0x2950b0f290422ff86b8ee8b91af4417e1464ddfd9dda26de8af52dac9ea4f869 +Token deployed to 0x2950b0f290422ff86b8ee8b91af4417e1464ddfd9dda26de8af52dac9ea4f869 Public token deployed to 0x2b54f68fd1e18f7dcfa71e3be3c91bb06ecbe727a28d609e964c225a4b5549c8 ``` diff --git a/docs/docs/dev_docs/dapps/tutorials/contract_interaction.md b/docs/docs/dev_docs/dapps/tutorials/contract_interaction.md index 3f4508a2f2f..334c2e2c892 100644 --- a/docs/docs/dev_docs/dapps/tutorials/contract_interaction.md +++ b/docs/docs/dev_docs/dapps/tutorials/contract_interaction.md @@ -4,12 +4,12 @@ In this section, we'll write the logic in our app that will interact with the co ## Showing user balance -Let's start by showing our user balance for the private token across their accounts. To do this, we can leverage the `balance_of_private` unconstrained view function of the private token contract: +Let's start by showing our user's private balance for the token across their accounts. To do this, we can leverage the `balance_of_private` unconstrained view function of the token contract: #include_code balance_of_private yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust :::info -Note that this function will only return a valid response for accounts registered in the RPC Server, since it requires access to the [user's private state](../../wallets/main.md#private-state). In other words, you cannot query the balance of another user for a private token contract. +Note that this function will only return a valid response for accounts registered in the RPC Server, since it requires access to the [user's private state](../../wallets/main.md#private-state). In other words, you cannot query the pprivate balance of another user for the token contract. ::: To do this, let's first initialise a new `Contract` instance using `aztec.js` that represents our deployed token contracts. Create a new `src/contracts.mjs` file with the imports for our artifacts and other dependencies: @@ -18,8 +18,8 @@ To do this, let's first initialise a new `Contract` instance using `aztec.js` th // src/contracts.mjs import { Contract } from "@aztec/aztec.js"; import { readFileSync } from "fs"; -import PrivateTokenArtifact from "../contracts/private_token/target/PrivateToken.json" assert { type: "json" }; -import PublicTokenArtifact from "../contracts/public_token/target/PublicToken.json" assert { type: "json" }; +import TokenContractAbi from "../contracts/token/target/Token.json" assert { type: "json" }; +import PublicTokenContractAbi from "../contracts/public_token/target/PublicToken.json" assert { type: "json" }; ``` And then add the following code for initialising the `Contract` instances: @@ -30,7 +30,7 @@ And then add the following code for initialising the `Contract` instances: You can use the typescript autogenerated interface instead of the generic `Contract` class to get type-safe methods. ::: -We can now get the private token instance in our main code in `src/index.mjs`, and query the private balance for each of the user accounts. To query a function, without sending a transaction, use the `view` function of the method: +We can now get the token instance in our main code in `src/index.mjs`, and query the private balance for each of the user accounts. To query a function, without sending a transaction, use the `view` function of the method: #include_code showPrivateBalances yarn-project/end-to-end/src/sample-dapp/index.mjs javascript @@ -46,7 +46,7 @@ Balance of 0x0e1f60e8566e2c6d32378bdcadb7c63696e853281be798c107266b8c3a88ea9b: 0 Now that we can see the balance for each user, let's transfer tokens from one account to another. To do this, we will first need access to a `Wallet` object. This wraps access to an RPC Server and also provides an interface to craft and sign transactions on behalf of one of the user accounts. -We can initialise a wallet using one of the `getAccount` methods from `aztec.js``, along with the corresponding signing and encryption keys: +We can initialise a wallet using one of the `getAccount` methods from `aztec.js`, along with the corresponding signing and encryption keys: ```js import { getSchnorrAccount } from "@aztec/aztec.js"; @@ -57,7 +57,13 @@ const wallet = await getSchnorrAccount( ).getWallet(); ``` -For ease of use, `aztec.js` also ships with a helper `getSandboxAccountsWallets` method that returns a wallet for each of the pre-initialised accounts in the Sandbox, so you can send transactions as any of them. We'll use one of these wallets to initialise the `Contract` instance that represents our private token contract, so every transaction sent through it will be sent through that wallet. +For ease of use, `aztec.js` also ships with a helper `getSandboxAccountsWallets` method that returns a wallet for each of the pre-initialised accounts in the Sandbox, so you can send transactions as any of them. + +```js +import { getSandboxAccountsWallets } from '@aztec/aztec.js'; +``` + +We'll use one of these wallets to initialise the `Contract` instance that represents our private token contract, so every transaction sent through it will be sent through that wallet. #include_code transferPrivateFunds yarn-project/end-to-end/src/sample-dapp/index.mjs javascript 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 1fa4036fee5..498447fe8e1 100644 --- a/yarn-project/end-to-end/src/sample-dapp/deploy.mjs +++ b/yarn-project/end-to-end/src/sample-dapp/deploy.mjs @@ -1,5 +1,5 @@ import { Contract, ContractDeployer, createAztecRpcClient, getSandboxAccountsWallets } from '@aztec/aztec.js'; -import { TokenContractAbi } from '@aztec/noir-contracts/artifacts'; +import { PublicTokenContractAbi, TokenContractAbi } from '@aztec/noir-contracts/artifacts'; import { writeFileSync } from 'fs'; import { fileURLToPath } from 'url'; @@ -12,12 +12,17 @@ async function main() { const [owner] = await getSandboxAccountsWallets(client); const token = await Contract.deploy(client, TokenContractAbi, []).send().deployed(); - await token.withWallet(owner).methods._initialize({ address: owner.getAddress() }).send().wait(); + const publicToken = await Contract.deploy(client, PublicTokenContractAbi, []).send().deployed(); + console.log(`Token deployed at ${token.address.toString()}`); + console.log(`Public Token deployed at ${publicToken.address.toString()}`); - const addresses = { token: token.address.toString() }; + const addresses = { + token: token.address.toString(), + publicToken: publicToken.address.toString(), + }; writeFileSync('addresses.json', JSON.stringify(addresses, null, 2)); } // docs:end:dapp-deploy diff --git a/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr index 30c1d55b3ae..e3c007b6206 100644 --- a/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr @@ -1,3 +1,4 @@ +// docs:start:all mod types; mod util; @@ -421,3 +422,4 @@ contract Token { } } +// docs:end:all diff --git a/yarn-project/noir-contracts/src/contracts/token_contract/src/types.nr b/yarn-project/noir-contracts/src/contracts/token_contract/src/types.nr index d2deceb268f..7c8800048d9 100644 --- a/yarn-project/noir-contracts/src/contracts/token_contract/src/types.nr +++ b/yarn-project/noir-contracts/src/contracts/token_contract/src/types.nr @@ -1,3 +1,4 @@ +// docs:start:all use dep::std::hash::pedersen; use dep::std::hash::pedersen_with_separator; use dep::aztec::note::{ @@ -125,4 +126,5 @@ global TransparentNoteMethods = NoteInterface { compute_nullifier, get_header, set_header, -}; \ No newline at end of file +}; +// docs:start:all \ No newline at end of file diff --git a/yarn-project/noir-contracts/src/contracts/token_contract/src/util.nr b/yarn-project/noir-contracts/src/contracts/token_contract/src/util.nr index 09d030e3318..4a605e87e10 100644 --- a/yarn-project/noir-contracts/src/contracts/token_contract/src/util.nr +++ b/yarn-project/noir-contracts/src/contracts/token_contract/src/util.nr @@ -1,3 +1,4 @@ +// docs:start:all use dep::std::hash::{pedersen_with_separator}; use dep::aztec::constants_gen::GENERATOR_INDEX__SIGNATURE_PAYLOAD; @@ -5,4 +6,5 @@ fn compute_message_hash(args: [Field; N]) -> Field { // @todo @lherskind We should probably use a separate generator for this, // to avoid any potential collisions with payloads. pedersen_with_separator(args, GENERATOR_INDEX__SIGNATURE_PAYLOAD)[0] -} \ No newline at end of file +} +// docs:start:all \ No newline at end of file From 189d112f1a006295f5ba6822bc3b57bbb235f7a7 Mon Sep 17 00:00:00 2001 From: Rahul Kothari Date: Wed, 20 Sep 2023 09:55:59 +0000 Subject: [PATCH 2/4] remove ppublic token --- .../dapps/tutorials/contract_deployment.md | 22 +++---------------- .../dapps/tutorials/contract_interaction.md | 8 +------ .../end-to-end/src/sample-dapp/deploy.mjs | 6 +---- .../src/contracts/token_contract/src/main.nr | 4 ++-- .../src/contracts/token_contract/src/types.nr | 4 ++-- .../src/contracts/token_contract/src/util.nr | 4 ++-- 6 files changed, 11 insertions(+), 37 deletions(-) diff --git a/docs/docs/dev_docs/dapps/tutorials/contract_deployment.md b/docs/docs/dev_docs/dapps/tutorials/contract_deployment.md index ba8aae370e9..90b607a846c 100644 --- a/docs/docs/dev_docs/dapps/tutorials/contract_deployment.md +++ b/docs/docs/dev_docs/dapps/tutorials/contract_deployment.md @@ -26,17 +26,17 @@ safe_math = { git="https://github.com/AztecProtocol/aztec-nr", tag="master", di Last, copy-paste the code from the `Token` contract into `contracts/token/main.nr`: -#include_code all yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust +#include_code token_all yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust The `Token` contract also requires two helper files. Copy-them too: Create `contracts/token/types.nr` and copy-paste the following: -#include_code all yarn-project/noir-contracts/src/contracts/token_contract/src/types.nr rust +#include_code token_types_all yarn-project/noir-contracts/src/contracts/token_contract/src/types.nr rust Finally, create `contracts/token/util.nr` and copy-paste the following: -#include_code all yarn-project/noir-contracts/src/contracts/token_contract/src/util.nr rust +#include_code token_util_all yarn-project/noir-contracts/src/contracts/token_contract/src/util.nr rust ## Compile your contract @@ -58,20 +58,6 @@ If you are using Typescript, consider including the `--typescript` option to [ge This should have created an artifact `contracts/token/target/Token.json` with the interface and bytecode for your contract. -## Adding a second contract - -For the purposes of this tutorial, we'll set up a second contract: a public token contract. Follow the same steps as above for initialising a new Nargo project, include the dependencies, and copy-paste the following code into `contracts/public_token/main.nr`: - -#include_code all yarn-project/noir-contracts/src/contracts/public_token_contract/src/main.nr rust - -Compile the contract with the CLI: - -```sh -yarn aztec-cli compile contracts/public_token -``` - -With both contracts ready, we'll now proceed to deployment. - ## Deploy your contracts Let's now write a script for deploying your contracts to the Sandbox. We'll create an RPC client, and then use the `ContractDeployer` class to deploy our contracts, and store the deployment address to a local JSON file. @@ -83,7 +69,6 @@ Create a new file `src/deploy.mjs`, with a call to a `main` function that we'll import { writeFileSync } from 'fs'; import { Contract, ContractDeployer, createAztecRpcClient, getSandboxAccountsWallets } from '@aztec/aztec.js'; import TokenContractAbi from "../contracts/token/target/Token.json" assert { type: "json" }; -import PublicTokenContractAbi from "../contracts/public_token/target/PublicToken.json" assert { type: "json" }; async function main() {} @@ -112,7 +97,6 @@ Run the snippet above as `node src/deploy.mjs`, and you should see the following ```text Token deployed to 0x2950b0f290422ff86b8ee8b91af4417e1464ddfd9dda26de8af52dac9ea4f869 -Public token deployed to 0x2b54f68fd1e18f7dcfa71e3be3c91bb06ecbe727a28d609e964c225a4b5549c8 ``` ## Next steps diff --git a/docs/docs/dev_docs/dapps/tutorials/contract_interaction.md b/docs/docs/dev_docs/dapps/tutorials/contract_interaction.md index 334c2e2c892..df163c90798 100644 --- a/docs/docs/dev_docs/dapps/tutorials/contract_interaction.md +++ b/docs/docs/dev_docs/dapps/tutorials/contract_interaction.md @@ -122,7 +122,7 @@ Balance of 0x226f8087792beff8d5009eb94e65d2a4a505b70baf4a9f28d33c8d620b0ba972: 0 Balance of 0x0e1f60e8566e2c6d32378bdcadb7c63696e853281be798c107266b8c3a88ea9b: 0 ``` -Public functions can emit [unencrypted public logs](../../contracts/events.md#unencrypted-events), which we can query via the RPC Server interface. In particular, the public token contract emits a generic `Coins minted` whenever the `mint` method is called: +Public functions can emit [unencrypted public logs](../../contracts/events.md#unencrypted-events), which we can query via the RPC Server interface. For example, here we have a `mint` method that emits a generic `Coins minted` whenever it is called: #include_code unencrypted_log yarn-project/noir-contracts/src/contracts/public_token_contract/src/main.nr rust @@ -130,12 +130,6 @@ We can extend our code by querying the logs emitted on the last block when the m #include_code showLogs yarn-project/end-to-end/src/sample-dapp/index.mjs javascript -Running the code again would now show an extra line with: - -```text -Log: Coins minted -``` - :::info At the time of this writing, there is no event-based mechanism in the `aztec.js` library to subscribe to events. The only option to consume them is to poll on every new block detected. This will change in a future version. ::: 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 498447fe8e1..ea1408e042c 100644 --- a/yarn-project/end-to-end/src/sample-dapp/deploy.mjs +++ b/yarn-project/end-to-end/src/sample-dapp/deploy.mjs @@ -1,5 +1,5 @@ import { Contract, ContractDeployer, createAztecRpcClient, getSandboxAccountsWallets } from '@aztec/aztec.js'; -import { PublicTokenContractAbi, TokenContractAbi } from '@aztec/noir-contracts/artifacts'; +import { TokenContractAbi } from '@aztec/noir-contracts/artifacts'; import { writeFileSync } from 'fs'; import { fileURLToPath } from 'url'; @@ -14,14 +14,10 @@ async function main() { const token = await Contract.deploy(client, TokenContractAbi, []).send().deployed(); await token.withWallet(owner).methods._initialize({ address: owner.getAddress() }).send().wait(); - const publicToken = await Contract.deploy(client, PublicTokenContractAbi, []).send().deployed(); - console.log(`Token deployed at ${token.address.toString()}`); - console.log(`Public Token deployed at ${publicToken.address.toString()}`); const addresses = { token: token.address.toString(), - publicToken: publicToken.address.toString(), }; writeFileSync('addresses.json', JSON.stringify(addresses, null, 2)); } diff --git a/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr index e3c007b6206..76c6a88ecb4 100644 --- a/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr @@ -1,4 +1,4 @@ -// docs:start:all +// docs:start:token_all mod types; mod util; @@ -422,4 +422,4 @@ contract Token { } } -// docs:end:all +// docs:end:token_all diff --git a/yarn-project/noir-contracts/src/contracts/token_contract/src/types.nr b/yarn-project/noir-contracts/src/contracts/token_contract/src/types.nr index 7c8800048d9..438f7ab53d4 100644 --- a/yarn-project/noir-contracts/src/contracts/token_contract/src/types.nr +++ b/yarn-project/noir-contracts/src/contracts/token_contract/src/types.nr @@ -1,4 +1,4 @@ -// docs:start:all +// docs:start:token_types_all use dep::std::hash::pedersen; use dep::std::hash::pedersen_with_separator; use dep::aztec::note::{ @@ -127,4 +127,4 @@ global TransparentNoteMethods = NoteInterface { get_header, set_header, }; -// docs:start:all \ No newline at end of file +// docs:end:token_types_all \ No newline at end of file diff --git a/yarn-project/noir-contracts/src/contracts/token_contract/src/util.nr b/yarn-project/noir-contracts/src/contracts/token_contract/src/util.nr index 4a605e87e10..701d0dd0527 100644 --- a/yarn-project/noir-contracts/src/contracts/token_contract/src/util.nr +++ b/yarn-project/noir-contracts/src/contracts/token_contract/src/util.nr @@ -1,4 +1,4 @@ -// docs:start:all +// docs:start:token_util_all use dep::std::hash::{pedersen_with_separator}; use dep::aztec::constants_gen::GENERATOR_INDEX__SIGNATURE_PAYLOAD; @@ -7,4 +7,4 @@ fn compute_message_hash(args: [Field; N]) -> Field { // to avoid any potential collisions with payloads. pedersen_with_separator(args, GENERATOR_INDEX__SIGNATURE_PAYLOAD)[0] } -// docs:start:all \ No newline at end of file +// docs:end:token_util_all \ No newline at end of file From 21f8eafeec3f5cfd2d74fd212f55ca876ac56fba Mon Sep 17 00:00:00 2001 From: Rahul Kothari Date: Wed, 20 Sep 2023 10:05:22 +0000 Subject: [PATCH 3/4] remove public properly --- docs/docs/dev_docs/dapps/tutorials/contract_interaction.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/docs/dev_docs/dapps/tutorials/contract_interaction.md b/docs/docs/dev_docs/dapps/tutorials/contract_interaction.md index df163c90798..bf029bdea39 100644 --- a/docs/docs/dev_docs/dapps/tutorials/contract_interaction.md +++ b/docs/docs/dev_docs/dapps/tutorials/contract_interaction.md @@ -9,7 +9,7 @@ Let's start by showing our user's private balance for the token across their acc #include_code balance_of_private yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust :::info -Note that this function will only return a valid response for accounts registered in the RPC Server, since it requires access to the [user's private state](../../wallets/main.md#private-state). In other words, you cannot query the pprivate balance of another user for the token contract. +Note that this function will only return a valid response for accounts registered in the RPC Server, since it requires access to the [user's private state](../../wallets/main.md#private-state). In other words, you cannot query the private balance of another user for the token contract. ::: To do this, let's first initialise a new `Contract` instance using `aztec.js` that represents our deployed token contracts. Create a new `src/contracts.mjs` file with the imports for our artifacts and other dependencies: @@ -19,7 +19,6 @@ To do this, let's first initialise a new `Contract` instance using `aztec.js` th import { Contract } from "@aztec/aztec.js"; import { readFileSync } from "fs"; import TokenContractAbi from "../contracts/token/target/Token.json" assert { type: "json" }; -import PublicTokenContractAbi from "../contracts/public_token/target/PublicToken.json" assert { type: "json" }; ``` And then add the following code for initialising the `Contract` instances: From 5b79abe07f31358d1995fcbea1913f8bb2a3194e Mon Sep 17 00:00:00 2001 From: Rahul Kothari Date: Wed, 20 Sep 2023 10:22:14 +0000 Subject: [PATCH 4/4] fix testing --- docs/docs/dev_docs/dapps/tutorials/testing.md | 6 +++--- yarn-project/end-to-end/src/sample-dapp/deploy.mjs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/docs/dev_docs/dapps/tutorials/testing.md b/docs/docs/dev_docs/dapps/tutorials/testing.md index d75b984686a..24da56dbf84 100644 --- a/docs/docs/dev_docs/dapps/tutorials/testing.md +++ b/docs/docs/dev_docs/dapps/tutorials/testing.md @@ -27,9 +27,9 @@ Create a new file `src/index.test.mjs` with the imports we'll be using and an em ```js import { createSandbox } from "@aztec/aztec-sandbox"; import { Contract, createAccount } from "@aztec/aztec.js"; -import PrivateTokenArtifact from "../contracts/private_token/target/PrivateToken.json" assert { type: "json" }; +import TokenContractAbi from "../contracts/token/target/Token.json" assert { type: "json" }; -describe("private token", () => {}); +describe("token", () => {}); ``` Let's set up our test suite. We'll start [a new Sandbox instance within the test](../../testing/testing.md#running-sandbox-in-the-nodejs-process), create two fresh accounts to test with, and deploy an instance of our contract. The `aztec-sandbox` and `aztec.js` provide the helper functions we need to do this: @@ -40,7 +40,7 @@ Note that, since we are starting a new Sandbox instance, we need to `stop` it in ## Writing our test -Now that we have a working test environment, we can write our first test for exercising the `transfer` function on the private token contract. We will use the same `aztec.js` methods we used when building our dapp: +Now that we have a working test environment, we can write our first test for exercising the `transfer` function on the token contract. We will use the same `aztec.js` methods we used when building our dapp: #include_code test yarn-project/end-to-end/src/sample-dapp/index.test.mjs javascript 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 ea1408e042c..9010da5a29b 100644 --- a/yarn-project/end-to-end/src/sample-dapp/deploy.mjs +++ b/yarn-project/end-to-end/src/sample-dapp/deploy.mjs @@ -16,7 +16,7 @@ async function main() { console.log(`Token deployed at ${token.address.toString()}`); - const addresses = { + const addresses = { token: token.address.toString(), }; writeFileSync('addresses.json', JSON.stringify(addresses, null, 2));