-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(docs): fix tutorial in dapp development #2421
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the |
||
::: | ||
|
||
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" }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove as unneeded. |
||
``` | ||
|
||
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 | ||
|
||
|
@@ -116,20 +122,14 @@ 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 | ||
|
||
We can extend our code by querying the logs emitted on the last block when the minting transaction is mined: | ||
|
||
#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. | ||
::: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
yarn-project/noir-contracts/src/contracts/token_contract/src/util.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
// docs:start:token_util_all | ||
use dep::std::hash::{pedersen_with_separator}; | ||
use dep::aztec::constants_gen::GENERATOR_INDEX__SIGNATURE_PAYLOAD; | ||
|
||
fn compute_message_hash<N>(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] | ||
} | ||
} | ||
// docs:end:token_util_all |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.