From 22b9da4a6438c0324bb6fb326ccbb88f34159038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Pobiar=C5=BCyn?= Date: Mon, 17 Jun 2024 11:31:20 +0200 Subject: [PATCH] Update articles to reflect changes regarding testing events api and native token transfers --- docusaurus/docs/basics/07-testing.md | 2 +- docusaurus/docs/basics/09-events.md | 8 ++++---- docusaurus/docs/basics/12-native-token.md | 12 ++++++++++++ docusaurus/docs/tutorials/erc20.md | 10 +++++----- docusaurus/docs/tutorials/ownable.md | 6 +++--- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/docusaurus/docs/basics/07-testing.md b/docusaurus/docs/basics/07-testing.md index 4db3752a5..f841f511b 100644 --- a/docusaurus/docs/basics/07-testing.md +++ b/docusaurus/docs/basics/07-testing.md @@ -121,7 +121,7 @@ the function we are calling inside the contract. - `fn advance_block_time(&self, time_diff: u64)` - increases the current value of `block_time` - `fn get_account(&self, n: usize) -> Address` - returns an n-th address that was prepared for you by Odra in advance; by default, you start with the 0-th account -- `fn emitted_event(&self, contract_address: &Address, event: &T) -> bool` - verifies if the event was emitted by the contract +- `fn emitted_event(&self, contract_address: &R, event: &T) -> bool` - verifies if the event was emitted by the contract Full list of functions can be found in the [`HostEnv`] documentation. diff --git a/docusaurus/docs/basics/09-events.md b/docusaurus/docs/basics/09-events.md index 18ff494d6..5df759caa 100644 --- a/docusaurus/docs/basics/09-events.md +++ b/docusaurus/docs/basics/09-events.md @@ -61,23 +61,23 @@ Odra's `HostEnv` comes with a few functions which lets you easily test the event ```rust title="examples/src/features/events.rs" use super::{PartyContractHostRef, PartyStarted}; -use odra::host::{Deployer, HostEnv, HostRef, NoArgs}; +use odra::host::{Deployer, HostEnv, NoArgs}; #[test] fn test_party() { let test_env: HostEnv = odra_test::env(); let party_contract = PartyContractHostRef::deploy(&test_env, NoArgs); test_env.emitted_event( - party_contract.address(), + &party_contract, &PartyStarted { caller: test_env.get_account(0), block_time: 0 } ); // If you do not want to check the exact event, you can use `emitted` function - test_env.emitted(party_contract.address(), "PartyStarted"); + test_env.emitted(&party_contract, "PartyStarted"); // You can also check how many events were emitted. - assert_eq!(test_env.events_count(party_contract.address()), 1); + assert_eq!(test_env.events_count(&party_contract), 1); } ``` diff --git a/docusaurus/docs/basics/12-native-token.md b/docusaurus/docs/basics/12-native-token.md index 7a665e180..b0c79422d 100644 --- a/docusaurus/docs/basics/12-native-token.md +++ b/docusaurus/docs/basics/12-native-token.md @@ -63,3 +63,15 @@ mod tests { } } ``` + +## HostEnv +In a broader context of the host environment (test, livenet), you can also transfer `CSPR` tokens between accounts: + +```rust showLineNumbers +let env = odra_casper_livenet_env::env(); +//let env = odra_test::env(); +let (alice, bob) = (env.get_account(0), env.get_account(1)); + +env.set_caller(alice); +let result = env.transfer_tokens(bob, odra::casper_types::U512::from(100)); +``` diff --git a/docusaurus/docs/tutorials/erc20.md b/docusaurus/docs/tutorials/erc20.md index 373d036ea..d82c000f4 100644 --- a/docusaurus/docs/tutorials/erc20.md +++ b/docusaurus/docs/tutorials/erc20.md @@ -238,7 +238,7 @@ pub mod tests { // Then a Transfer event was emitted. assert!(env.emitted_event( - erc20.address(), + &erc20, &Transfer { from: None, to: Some(env.get_account(0)), @@ -269,7 +269,7 @@ pub mod tests { // Then Transfer event was emitted. assert!(env.emitted_event( - erc20.address(), + &erc20, &Transfer { from: Some(sender), to: Some(recipient), @@ -308,7 +308,7 @@ pub mod tests { // Allowance was recorded. assert_eq!(erc20.allowance(&owner, &spender), approved_amount); assert!(env.emitted_event( - erc20.address(), + &erc20, &Approval { owner, spender, @@ -327,7 +327,7 @@ pub mod tests { ); assert_eq!(erc20.balance_of(&recipient), transfer_amount); assert!(env.emitted_event( - erc20.address(), + &erc20, &Approval { owner, spender, @@ -335,7 +335,7 @@ pub mod tests { } )); assert!(env.emitted_event( - erc20.address(), + &erc20, &Transfer { from: Some(owner), to: Some(recipient), diff --git a/docusaurus/docs/tutorials/ownable.md b/docusaurus/docs/tutorials/ownable.md index c4d714790..9396a4fc6 100644 --- a/docusaurus/docs/tutorials/ownable.md +++ b/docusaurus/docs/tutorials/ownable.md @@ -146,7 +146,7 @@ mod tests { assert_eq!(ownable.get_owner(), owner); env.emitted_event( - ownable.address(), + &ownable, &OwnershipChanged { prev_owner: None, new_owner: owner @@ -164,7 +164,7 @@ mod tests { assert_eq!(ownable.get_owner(), new_owner); env.emitted_event( - ownable.address(), + &ownable, &OwnershipChanged { prev_owner: Some(owner), new_owner @@ -192,7 +192,7 @@ mod tests { :::note You may have noticed, we use here the term `module` interchangeably with `contract`. The reason is once we deploy our module onto a virtual blockchain it may be considered a contract. ::: -* **L19-25** - On the contract, only the `init()` function has been called, so we expect one event to have been emitted. To assert that, let's use `HostEnv`. To get the env, we call `env()` on the contract, then call `HostEnv::emitted_event`. As the first argument, pass the contract address you want to read events from, followed by an event as you expect it to have occurred. +* **L19-25** - On the contract, only the `init()` function has been called, so we expect one event to have been emitted. To assert that, let's use `HostEnv`. To get the env, we call `env()` on the contract, then call `HostEnv::emitted_event`. As the first argument, pass the contract you want to read events from, followed by an event as you expect it to have occurred. * **L31** - Because we know the initial owner is the 0th account, we must select a different account. It could be any index from 1 to 19 - the `HostEnv` predefines 20 accounts. * **L33** - As mentioned, the default is the 0th account, if you want to change the executor, call the `HostEnv::set_caller()` function. :::note