odra icon indicating copy to clipboard operation
odra copied to clipboard

Native transfer between accounts

Open andrzej-casper opened this issue 2 years ago • 4 comments

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())
    };
}

andrzej-casper avatar Jun 02 '23 22:06 andrzej-casper

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.

zie1ony avatar Jun 06 '23 07:06 zie1ony

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.

ghost avatar Jul 03 '23 11:07 ghost

@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

kubaplas avatar Jul 03 '23 11:07 kubaplas

The CSPR helper methods can be useful when working with livenet client #258. Consider adding, env.cspr_balance(account), env.try_cspr_transfer(recipient).

zie1ony avatar Nov 20 '23 09:11 zie1ony