ink icon indicating copy to clipboard operation
ink copied to clipboard

smart contract's balance doesn't change in tests

Open vni opened this issue 3 years ago • 0 comments

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!!

vni avatar Aug 02 '22 06:08 vni