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

Use reducible_balance instead free_balance #420

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ use codec::{Decode, Encode};
use evm::Config as EvmConfig;
use frame_support::dispatch::DispatchResultWithPostInfo;
use frame_support::traits::{
Currency, ExistenceRequirement, FindAuthor, Get, Imbalance, OnUnbalanced, WithdrawReasons,
tokens::fungible::Inspect, Currency, ExistenceRequirement, FindAuthor, Get, Imbalance,
OnUnbalanced, WithdrawReasons,
};
use frame_support::weights::{Pays, PostDispatchInfo, Weight};
use frame_system::RawOrigin;
Expand Down Expand Up @@ -118,7 +119,7 @@ pub mod pallet {
/// Mapping from address to account id.
type AddressMapping: AddressMapping<Self::AccountId>;
/// Currency type for withdraw and balance storage.
type Currency: Currency<Self::AccountId>;
type Currency: Currency<Self::AccountId> + Inspect<Self::AccountId>;

/// The overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
Expand Down Expand Up @@ -621,7 +622,8 @@ impl<T: Config> Pallet<T> {
let account_id = T::AddressMapping::into_account_id(*address);

let nonce = frame_system::Pallet::<T>::account_nonce(&account_id);
let balance = T::Currency::free_balance(&account_id);
// keepalive `true` takes into account ExistentialDeposit as part of what's considered liquid balance.
let balance = T::Currency::reducible_balance(&account_id, true);

Account {
nonce: U256::from(UniqueSaturatedInto::<u128>::unique_saturated_into(nonce)),
Expand Down
23 changes: 22 additions & 1 deletion frame/evm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use super::*;
use crate::mock::*;

use frame_support::assert_ok;
use frame_support::traits::GenesisBuild;
use frame_support::traits::{GenesisBuild, LockIdentifier, LockableCurrency, WithdrawReasons};
use std::{collections::BTreeMap, str::FromStr};

type Balances = pallet_balances::Pallet<Test>;
Expand Down Expand Up @@ -121,3 +121,24 @@ fn find_author() {
);
});
}

#[test]
fn reducible_balance() {
new_test_ext().execute_with(|| {
let evm_addr = H160::from_str("1000000000000000000000000000000000000001").unwrap();
let account_id = <Test as Config>::AddressMapping::into_account_id(evm_addr);
let existential = ExistentialDeposit::get();

// Genesis Balance.
let genesis_balance = EVM::account_basic(&evm_addr).balance;

// Lock identifier.
let lock_id: LockIdentifier = *b"te/stlok";
// Reserve some funds.
let to_lock = 1000;
Balances::set_lock(lock_id, &account_id, to_lock, WithdrawReasons::RESERVE);
// Reducible is, as currently configured in `account_basic`, (balance - lock + existential).
let reducible_balance = EVM::account_basic(&evm_addr).balance;
assert_eq!(reducible_balance, (genesis_balance - to_lock + existential));
});
}
6 changes: 3 additions & 3 deletions ts-tests/tests/test-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createAndFinalizeBlock, describeWithFrontier, customRequest } from "./u

describeWithFrontier("Frontier RPC (Balance)", (context) => {
const GENESIS_ACCOUNT = "0x6be02d1d3665660d22ff9624b7be0551ee1ac91b";
const GENESIS_ACCOUNT_BALANCE = "340282366920938463463374607431768211455";
const GENESIS_ACCOUNT_BALANCE = "340282366920938463463374607431768210955";
const GENESIS_ACCOUNT_PRIVATE_KEY = "0x99B3C12287537E38C90A9219D4CB074A89A16E9CDB20BF85728EBD97C343E342";
const TEST_ACCOUNT = "0x1111111111111111111111111111111111111111";

Expand All @@ -25,7 +25,7 @@ describeWithFrontier("Frontier RPC (Balance)", (context) => {
}, GENESIS_ACCOUNT_PRIVATE_KEY);
await customRequest(context.web3, "eth_sendRawTransaction", [tx.rawTransaction]);
await createAndFinalizeBlock(context.web3);
expect(await context.web3.eth.getBalance(GENESIS_ACCOUNT)).to.equal("340282366920938463463374607431768189943");
expect(await context.web3.eth.getBalance(TEST_ACCOUNT)).to.equal("512");
expect(await context.web3.eth.getBalance(GENESIS_ACCOUNT)).to.equal("340282366920938463463374607431768189443");
expect(await context.web3.eth.getBalance(TEST_ACCOUNT)).to.equal("12");
});
});