Native transfer between accounts
There is support for native transfer between contract purse and user account, but what about transfer between accounts?
It should be fairly easy to implement... maybe something like below?
/// Transfers native token between accounts.
pub fn transfer_tokens_accounts<B: Into<Balance>>(from: Account, to: Address, amount: B) {
let main_purse = from.main_purse();
match to {
Address::Account(account) => {
transfer_from_purse_to_account(main_purse, account, amount.into().inner(), None)
.unwrap_or_revert();
}
Address::Contract(_) => revert(ExecutionError::can_not_transfer_to_contract())
};
}
In Casper there are two possible context execution: account (session) and contract. What you are suggesting is executing the code in the context of an account. Odra (for now) is purelly focused on writing the code for the contracts and we don't support Casper's session code. Casper will go through a lot of changes in 2.0 and we don't event know if those two contexts of execution will still be there in future. I'm happy to revisit this idea when Casper is 2.0 and we know the shape of it.
For now you should be fine with using casper-contract crate directly.
I didn't mean to use it within contract code, but only in the tests, e.g.:
#[test]
fn test_foo() {
let alice = test_env::get_account(0);
let bob = test_env::get_account(1);
test_env::transfer_tokens_accounts(alice, bob, &Balance::from(25));
// ...
}
I imagine this could be useful for implementing end-to-end tests. I see there is
casper_contract::contract_api::system::transfer_to_account, but not sure how to switch accounts and give them initial supply :sweat:. I though Odra could provide helpers for such things.
@andrzej-casper We are creating 20 accounts with initial balance during genesis here: https://github.com/odradev/odra/blob/release/0.5.0/odra-casper/test-env/src/env.rs#L65
The CSPR helper methods can be useful when working with livenet client #258. Consider adding, env.cspr_balance(account), env.try_cspr_transfer(recipient).