Skip to content

Commit

Permalink
Update articles to reflect changes regarding testing events api and n…
Browse files Browse the repository at this point in the history
…ative token transfers
  • Loading branch information
kpob committed Jun 17, 2024
1 parent 0dfd511 commit 22b9da4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docusaurus/docs/basics/07-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: ToBytes + EventInstance>(&self, contract_address: &Address, event: &T) -> bool` - verifies if the event was emitted by the contract
- `fn emitted_event<T: ToBytes + EventInstance, R: Addressable>(&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.

Expand Down
8 changes: 4 additions & 4 deletions docusaurus/docs/basics/09-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
```

Expand Down
12 changes: 12 additions & 0 deletions docusaurus/docs/basics/12-native-token.md
Original file line number Diff line number Diff line change
Expand Up @@ -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));
```
10 changes: 5 additions & 5 deletions docusaurus/docs/tutorials/erc20.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand All @@ -327,15 +327,15 @@ pub mod tests {
);
assert_eq!(erc20.balance_of(&recipient), transfer_amount);
assert!(env.emitted_event(
erc20.address(),
&erc20,
&Approval {
owner,
spender,
value: approved_amount - transfer_amount
}
));
assert!(env.emitted_event(
erc20.address(),
&erc20,
&Transfer {
from: Some(owner),
to: Some(recipient),
Expand Down
6 changes: 3 additions & 3 deletions docusaurus/docs/tutorials/ownable.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 22b9da4

Please sign in to comment.