Contracts: seal0::balance should return the free balance (#1254)

This commit is contained in:
Cyrill Leutwiler
2023-09-01 14:33:15 +02:00
committed by GitHub
parent e165eacd58
commit 24c41f10bb
4 changed files with 128 additions and 7 deletions
+50
View File
@@ -5891,3 +5891,53 @@ fn root_cannot_instantiate() {
);
});
}
#[test]
fn balance_api_returns_free_balance() {
let (wasm, _code_hash) = compile_module::<Test>("balance").unwrap();
ExtBuilder::default().existential_deposit(200).build().execute_with(|| {
let _ = <Test as Config>::Currency::set_balance(&ALICE, 1_000_000);
// Instantiate the BOB contract without any extra balance.
let addr = Contracts::bare_instantiate(
ALICE,
0,
GAS_LIMIT,
None,
Code::Upload(wasm.to_vec()),
vec![],
vec![],
DebugInfo::Skip,
CollectEvents::Skip,
)
.result
.unwrap()
.account_id;
let value = 0;
// Call BOB which makes it call the balance runtime API.
// The contract code asserts that the returned balance is 0.
assert_ok!(Contracts::call(
RuntimeOrigin::signed(ALICE),
addr.clone(),
value,
GAS_LIMIT,
None,
vec![]
));
let value = 1;
// Calling with value will trap the contract.
assert_err_ignore_postinfo!(
Contracts::call(
RuntimeOrigin::signed(ALICE),
addr.clone(),
value,
GAS_LIMIT,
None,
vec![]
),
<Error<Test>>::ContractTrapped
);
});
}