Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smart contract's balance doesn't change in tests #1346

Closed
vni opened this issue Aug 2, 2022 · 2 comments
Closed

smart contract's balance doesn't change in tests #1346

vni opened this issue Aug 2, 2022 · 2 comments
Assignees
Labels
A-ink_env [ink_env] work item C-bug Something isn't working

Comments

@vni
Copy link

vni commented Aug 2, 2022

Hi!

I'm playing with ink! smart contracts and in tests it seems like there is a bug (or my misunderstanding) when the user transfers tokens to contract, the balance of the contract doesn't change.

Here is the link to simple deposit contract code (lib.rs, without Cargo.toml):
https://gist.github.com/vni/fb897f32244e8ff6931b286bb926a191

All the ink_* dependencies are of version = "3"
(ink_primitives, ink_metadata, ink_env, ink_storage, ink_lang)

Contract was created with cargo +nightly contract new simple_deposit

Here is my question on substrate.stackexchange.com:
https://substrate.stackexchange.com/questions/3951/smart-contracts-balance-doesnt-change-in-tests

Contract was built with +nightly.
cargo-contract 1.4.0-unknown-x86_64-unknown-linux-gnu

Describe the bug
In tests I check the contract balance before and after the user deposits funds to the contract. I expect that the balance_before and balance_after are different, but they are the same. Contract balance doesn't change.

Expected behavior
I expect that in tests when I do some kind of fund transfer the balance of the contract will be increased and I can check it in tests.

        #[ink::test]
        fn deposit() {
            let mut simple_deposit = SimpleDeposit::new(15);
            let balance_before = simple_deposit.env().balance();

            set_caller::<Environment>(bob_account());
            set_value_transferred::<ink_env::DefaultEnvironment>(15);
            let res = simple_deposit.deposit();
            assert!(res.is_ok());

            let balance_after = simple_deposit.env().balance();

            assert_ne!(balance_before, balance_after);
        }

Actually, this may be a bug or I may not understand something. So, I'm here for help. : )
Thanks in advance!!

@HCastano HCastano added C-bug Something isn't working A-ink_env [ink_env] work item labels Aug 2, 2022
@agryaznov
Copy link
Contributor

agryaznov commented Aug 26, 2022

Currently, the off-chain Engine does not provide means for automatically moving funds along while making contract (payable) message invocation. So for now such a transfers can only be done in tests by explicitly accompanying such calls with ink_env::test::set_account_balance().

This is not the best possible developer experience, and we could think about providing more handy way to do this in ink!.

My suggestion is to:

  1. Add a new fn Engine::transfer_in(account_id, value) which transfers amount from caller to callee.
    This is just a companion to Engine::transfer() (which transfers funds out of the contract).
    Having that fn, we can call it before payable msg invocation in our contract tests.
    From that we can go further and
    (this would require impl it for on-chain env as well, where it doesn't make sense)

  2. Add a helper fn ink_env::test::transfer_in(value: Balance).
    which transfers the value from caller to the contract.

  3. Add a helper macro which expands like this (roughly):
    pay_with_call!(contract.message(), 1000) -->> (equivalent to): { transfer_in(1000); contract.message() }

@athei @HCastano @cmichi @ascjones what do you think?

@agryaznov
Copy link
Contributor

Solved by #1379

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ink_env [ink_env] work item C-bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants