Skip to content

Commit 534821d

Browse files
committed
Make staking precompile methods be eth-precised
1 parent 5ca8fa5 commit 534821d

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

precompiles/src/staking.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ where
117117
let account_id = handle.caller_account_id::<R>();
118118
let (hotkey, _) = parse_pubkey(address.as_bytes())?;
119119
let netuid = try_u16_from_u256(netuid)?;
120-
let amount_unstaked = amount.unique_saturated_into();
120+
let amount_unstaked =
121+
<R as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
122+
.ok_or(ExitError::OutOfFund)?;
123+
let amount_unstaked = amount_unstaked.unique_saturated_into();
121124
let call = pallet_subtensor::Call::<R>::remove_stake {
122125
hotkey,
123126
netuid,
@@ -161,6 +164,27 @@ where
161164
Ok(stake_eth)
162165
}
163166

167+
#[precompile::public("getStake(bytes32,bytes32,uint256)")]
168+
#[precompile::view]
169+
fn get_stake(
170+
_: &mut impl PrecompileHandle,
171+
hotkey: H256,
172+
coldkey: H256,
173+
netuid: U256,
174+
) -> EvmResult<U256> {
175+
let (hotkey, _) = parse_pubkey(hotkey.as_bytes())?;
176+
let (coldkey, _) = parse_pubkey(coldkey.as_bytes())?;
177+
let netuid = try_u16_from_u256(netuid)?;
178+
let stake = pallet_subtensor::Pallet::<R>::get_stake_for_hotkey_and_coldkey_on_subnet(
179+
&hotkey, &coldkey, netuid,
180+
);
181+
let stake = U256::from(stake);
182+
let stake = <R as pallet_evm::Config>::BalanceConverter::into_evm_balance(stake)
183+
.ok_or(ExitError::InvalidRange)?;
184+
185+
Ok(stake)
186+
}
187+
164188
#[precompile::public("addProxy(bytes32)")]
165189
fn add_proxy(handle: &mut impl PrecompileHandle, delegate: H256) -> EvmResult<()> {
166190
let account_id = handle.caller_account_id::<R>();
@@ -189,24 +213,6 @@ where
189213
handle.try_dispatch_runtime_call::<R, _>(call, RawOrigin::Signed(account_id))
190214
}
191215

192-
#[precompile::public("getStake(bytes32,bytes32,uint256)")]
193-
#[precompile::view]
194-
fn get_stake(
195-
_: &mut impl PrecompileHandle,
196-
hotkey: H256,
197-
coldkey: H256,
198-
netuid: U256,
199-
) -> EvmResult<U256> {
200-
let (hotkey, _) = parse_pubkey(hotkey.as_bytes())?;
201-
let (coldkey, _) = parse_pubkey(coldkey.as_bytes())?;
202-
let netuid = try_u16_from_u256(netuid)?;
203-
let stake = pallet_subtensor::Pallet::<R>::get_stake_for_hotkey_and_coldkey_on_subnet(
204-
&hotkey, &coldkey, netuid,
205-
);
206-
207-
Ok(stake.into())
208-
}
209-
210216
fn transfer_back_to_caller(
211217
account_id: &<R as frame_system::Config>::AccountId,
212218
amount: U256,

0 commit comments

Comments
 (0)