Skip to content

Commit

Permalink
Use reducible_balance instead free_balance (#420)
Browse files Browse the repository at this point in the history
* Use `tokens::fungible::Inspect::reducible_balance`

* Integration test

* cargo fmt

* integration tests

* recipient's balance is also subject to ed

Co-authored-by: Joshy Orndorff <admin@joshyorndorff.com>
Co-authored-by: Joshy Orndorff <JoshOrndorff@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 23, 2021
1 parent 72abbf5 commit 8139beb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
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");
});
});

0 comments on commit 8139beb

Please sign in to comment.