Skip to content

Commit

Permalink
added a section to Readme to show how to check account balances (#174)
Browse files Browse the repository at this point in the history
* added a section to Readme to show how to check account balances

See #172

* Update README.md with better example of balance check from @ChaoticTempest

Co-authored-by: Phuong Nguyen <[email protected]>

* Using `max_gas()` in the new Common Usage section of Readme

Co-authored-by: Phuong Nguyen <[email protected]>
  • Loading branch information
2 people authored and Phuong Nguyen committed Aug 16, 2022
1 parent 8125ed8 commit 0404987
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,43 @@ Then later on, we can view our minted NFT's metadata via our `view` call into `n
}
```

## Common Usage

One advantage of using NEAR Workspaces-rs instead of a unit test is that Workspaces allows you to check balances after a transfer (unit tests don't).

Here is an example:

```rs
#[test(tokio::test)]
async fn test_some_function_that_involves_a_transfer() -> anyhow::Result<()> {
let transfer_amount = near_units::near::parse("0.1").unwrap();
let worker = workspaces::sandbox().await?;
let contract = worker
.dev_deploy(&include_bytes!("../target/res/your_project_name.wasm").to_vec())
.await?;
contract.call(&worker, "new").max_gas().transact().await?;
let alice = worker.dev_create_account().await?;
let bob = worker.dev_create_account().await?;
let bob_original_balance = bob.view_account(&worker).await?.balance;
let _result = alice
.call(
&worker,
contract.id(),
"some_function_that_involves_a_transfer",
)
.args_json(json!({"destination_account": &bob.id()}))?
.max_gas()
.deposit(transfer_amount)
.transact()
.await?;
assert_eq!(
bob.view_account(&worker).await?.balance,
bob_original_balance + transfer_amount
);
Ok(())
}
```

## Examples
More standalone examples can be found in `examples/src/*.rs`.

Expand Down

0 comments on commit 0404987

Please sign in to comment.