Skip to content

Commit

Permalink
docs using test contract for unencrypted logs
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Oct 17, 2023
1 parent c0a2a08 commit 90b99c9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
4 changes: 2 additions & 2 deletions docs/docs/dev_docs/contracts/syntax/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ They can be emitted by both public and private functions.

To emit unencrypted logs first import the `emit_unencrypted_log` utility function inside your contract:

#include_code unencrypted_import /yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust
#include_code unencrypted_import /yarn-project/noir-contracts/src/contracts/test_contract/src/main.nr rust

Then you can call the function:

#include_code unencrypted_log /yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust
#include_code emit_unencrypted /yarn-project/noir-contracts/src/contracts/test_contract/src/main.nr rust

Once emitted, unencrypted events are stored in AztecNode and can be queried by anyone:
<Tabs groupId="events">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { readFileSync } from "fs";
import TokenContractArtifact from "../contracts/token/target/Token.json" assert { type: "json" };
```

And then add the following code for initialising the `Contract` instances:
And then add the following code for initializing the `Contract` instances:

#include_code get-tokens yarn-project/end-to-end/src/sample-dapp/contracts.mjs javascript

Expand Down Expand Up @@ -122,18 +122,6 @@ Balance of 0x226f8087792beff8d5009eb94e65d2a4a505b70baf4a9f28d33c8d620b0ba972: 0
Balance of 0x0e1f60e8566e2c6d32378bdcadb7c63696e853281be798c107266b8c3a88ea9b: 0
```

Public functions can emit [unencrypted logs](../../contracts/syntax/events.md#unencrypted-events), which can be queried via the PXE interface. In particular, the token contract emits a generic `Public tokens minted` whenever the `mint_public` method is called:

#include_code unencrypted_log yarn-project/noir-contracts/src/contracts/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

:::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.
:::

## Next steps

In the next and final section, we'll [set up automated tests for our application](./testing.md).
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// A contract used for testing a random hodgepodge of small features from simulator and end-to-end tests.
contract Test {
// docs:start:unencrypted_import
use dep::aztec::log::emit_unencrypted_log;
// docs:end:unencrypted_import

use dep::aztec::{
abi,
abi::PrivateContextInputs,
Expand All @@ -9,7 +13,6 @@ contract Test {
context::get_portal_address,
rand::rand
},
log::emit_unencrypted_log,
types::vec::BoundedVec,
constants_gen::EMPTY_NULLIFIED_COMMITMENT,
};
Expand Down Expand Up @@ -130,15 +133,15 @@ contract Test {
}
// docs:end:is-time-equal

// docs:start:emit_unencrypted_log
#[aztec(public)]
fn emit_unencrypted(
value: Field
) -> Field {
// docs:start:emit_unencrypted
emit_unencrypted_log(&mut context, value);
// docs:end:emit_unencrypted
0
}
// docs:end:emit_unencrypted_log

#[aztec(public)]
fn consume_mint_public_message(
Expand Down

0 comments on commit 90b99c9

Please sign in to comment.