Skip to content

Commit bdd85cd

Browse files
committed
Use evm balance converter in staking precompile
1 parent 6ead778 commit bdd85cd

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

runtime/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,8 +1107,7 @@ where
11071107
fn into_substrate_balance(value: U256) -> Option<pallet_evm::BalanceOf<T>> {
11081108
value
11091109
.checked_div(U256::from(EVM_DECIMALS_FACTOR))
1110-
.map(|result| result.try_into().ok())
1111-
.flatten()
1110+
.and_then(|result| result.try_into().ok())
11121111
}
11131112
}
11141113

runtime/src/precompiles/staking.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//
2727

2828
use frame_system::RawOrigin;
29-
use pallet_evm::{AddressMapping, HashedAddressMapping};
29+
use pallet_evm::{AddressMapping, BalanceConverter, HashedAddressMapping};
3030
use pallet_evm::{
3131
ExitError, ExitSucceed, PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult,
3232
};
@@ -68,10 +68,14 @@ impl StakingPrecompile {
6868
fn add_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
6969
let hotkey = Self::parse_hotkey(data)?.into();
7070
let amount: U256 = handle.context().apparent_value;
71+
let amount_sub =
72+
<Runtime as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
73+
.ok_or(ExitError::OutOfFund)?;
74+
7175
// Create the add_stake call
7276
let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::<Runtime>::add_stake {
7377
hotkey,
74-
amount_staked: amount.as_u64(),
78+
amount_staked: amount_sub,
7579
});
7680
// Dispatch the add_stake call
7781
Self::dispatch(handle, call)
@@ -85,10 +89,14 @@ impl StakingPrecompile {
8589
let amount = data
8690
.get(56..64)
8791
.map(U256::from_big_endian)
88-
.map_or(0, |v| v.as_u64());
92+
.ok_or(ExitError::OutOfFund)?;
93+
let amount_sub =
94+
<Runtime as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
95+
.ok_or(ExitError::OutOfFund)?;
96+
8997
let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::<Runtime>::remove_stake {
9098
hotkey,
91-
amount_unstaked: amount,
99+
amount_unstaked: amount_sub,
92100
});
93101
Self::dispatch(handle, call)
94102
}
@@ -148,12 +156,15 @@ impl StakingPrecompile {
148156
});
149157
}
150158
};
159+
let amount_sub =
160+
<Runtime as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
161+
.ok_or(ExitError::OutOfFund)?;
151162

152163
// Create a transfer call from the smart contract to the caller
153164
let transfer_call =
154165
RuntimeCall::Balances(pallet_balances::Call::<Runtime>::transfer_allow_death {
155166
dest: account_id.clone().into(),
156-
value: amount.as_u64(),
167+
value: amount_sub,
157168
});
158169

159170
// Execute the transfer

0 commit comments

Comments
 (0)