Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
9 changes: 9 additions & 0 deletions prdoc/pr_9617.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
title: '[pallet-revive] Add setting evm balance'
doc:
- audience: Runtime User
description: |-
Adds a balance setter in EVM for pallet-revive.

crates:
- name: pallet-revive
bump: minor
21 changes: 21 additions & 0 deletions substrate/frame/revive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1539,11 +1539,32 @@ where
}

/// Get the balance with EVM decimals of the given `address`.
///
/// Returns the spendable balance excluding the existential deposit.
pub fn evm_balance(address: &H160) -> U256 {
let balance = AccountInfo::<T>::balance((*address).into());
Self::convert_native_to_evm(balance)
}

/// Set the EVM balance of an account.
///
/// The account's total balance becomes the EVM value plus the existential deposit,
/// consistent with `evm_balance` which returns the spendable balance excluding the existential
/// deposit.
pub fn set_evm_balance(address: &H160, evm_value: U256) -> Result<(), Error<T>> {
let ed = T::Currency::minimum_balance();
let balance_with_dust = BalanceWithDust::<BalanceOf<T>>::from_value::<T>(evm_value)
.map_err(|_| <Error<T>>::BalanceConversionFailed)?;
let (value, dust) = balance_with_dust.deconstruct();
let account_id = T::AddressMapper::to_account_id(&address);
T::Currency::set_balance(&account_id, ed.saturating_add(value));
AccountInfoOf::<T>::mutate(address, |account| {
account.as_mut().map(|a| a.dust = dust);
});

Ok(())
}

/// Get the nonce for the given `address`.
pub fn evm_nonce(address: &H160) -> u32
where
Expand Down
16 changes: 16 additions & 0 deletions substrate/frame/revive/src/tests/pvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ fn eth_call_transfer_with_dust_works() {
});
}

#[test]
fn set_evm_balance_works() {
let (binary, _) = compile_module("dummy").unwrap();
ExtBuilder::default().existential_deposit(200).build().execute_with(|| {
let _ = <Test as Config>::Currency::set_balance(&ALICE, 1_000_000);
let Contract { addr, .. } =
builder::bare_instantiate(Code::Upload(binary)).build_and_unwrap_contract();
let native_with_dust = BalanceWithDust::new_unchecked::<Test>(100, 10);
let evm_value = Pallet::<Test>::convert_native_to_evm(native_with_dust);

assert_ok!(Pallet::<Test>::set_evm_balance(&addr, evm_value));

assert_eq!(Pallet::<Test>::evm_balance(&addr), evm_value);
});
}

#[test]
fn contract_call_transfer_with_dust_works() {
let (binary_caller, _code_hash_caller) = compile_module("call_with_value").unwrap();
Expand Down
Loading