You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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!!
The text was updated successfully, but these errors were encountered:
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:
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)
Add a helper fn ink_env::test::transfer_in(value: Balance).
which transfers the value from caller to the contract.
Add a helper macro which expands like this (roughly): pay_with_call!(contract.message(), 1000) -->> (equivalent to): { transfer_in(1000); contract.message() }
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.
Actually, this may be a bug or I may not understand something. So, I'm here for help. : )
Thanks in advance!!
The text was updated successfully, but these errors were encountered: