1

In a Rust test, how can I check the state (account balance) of an account?

E.g. I have this helper function:

fn set_context(account_index: usize, is_view: bool, deposit: Amount) {
    let context = VMContextBuilder::new()
        .signer_account_id(accounts(account_index))
        .is_view(is_view)
        .attached_deposit(deposit)
        .build();
    testing_env!(context);
}

And then my test contains:

...

let mut contract = Contract::new();
set_context(1, false, near_string_to_yocto("0.3".to_string()));
let recipient = accounts(0);
let _matcher1_offer_result = contract.offer_matching_funds(&recipient);
set_context(2, false, near_string_to_yocto("0.1".to_string()));
let _matcher2_offer_result = contract.offer_matching_funds(&recipient);
// TODO: Assert that this (escrow) contract now contains the correct amount of funds. Assert that the matchers' account balances have decreased appropriately.

I haven't been able to find an example in any docs or repo.

E.g. https://docs.rs/near-sdk/latest/src/near_sdk/test_utils/context.rs.html#10-14

Ryan
  • 22,332
  • 31
  • 176
  • 357

2 Answers2

4

Can't directly comment on Vlad's post due to low reputation, but the method you'd need to get the account details such as account balance is the account.view_account() method. You can find all related account methods here as well: https://docs.rs/workspaces/0.4.0/workspaces/struct.Account.html

The Workspaces docs aren't good yet

Any feedback as to how we can improve the docs?

  • Yep, I'm compiling a list of confusions, and once I feel like I've resolved them and understand enough, I can submit PRs. – Ryan Aug 08 '22 at 22:18
0

Given that the balance is changed by the NEAR Protocol and the contract method cannot just change the balance directly, the only thing contract developers can check in unit tests is whether there was some promise returned with the tokens transferred. The current balance in the unit-test environment is available through near_sdk::env::account_balance()

If there is a need to do end-to-end testing, I recommend using https://github.com/near/workspaces-rs or https://github.com/near/workspaces-js

Vlad Frolov
  • 7,445
  • 5
  • 33
  • 52
  • Thanks. The Workspaces docs aren't good yet, so I won't try that approach yet. Can you please clarify what you mean in these parts? "the only thing contract developers can check in unit tests is whether there was some promise returned with the tokens transferred" So can I somehow check the balance of a recipient and a sender after a transfer? "The current balance in the unit-test environment is available through `near_sdk::env::account_balance()`". But whenever I call `env::account_balance()` it shows the balance just for alice.near. How can I specify *whose* balance I want to check? Thanks! – Ryan Aug 03 '22 at 16:38
  • 1
    > So can I somehow check the balance of a recipient and a sender after a transfer? Unit-test is the environment that emulates a contract context. When you start caring about interactions, you should use integration testing and workspaces-rs is exactly the tool built for that purpose. – Vlad Frolov Aug 05 '22 at 19:50