Skip to content

Commit 7639869

Browse files
committed
Remove swap pallet error shadowing in max_amount staking calculators
1 parent 6ead71a commit 7639869

File tree

4 files changed

+97
-79
lines changed

4 files changed

+97
-79
lines changed

pallets/subtensor/src/staking/add_stake.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,28 +180,30 @@ impl<T: Config> Pallet<T> {
180180
}
181181

182182
// Returns the maximum amount of RAO that can be executed with price limit
183-
pub fn get_max_amount_add(netuid: NetUid, limit_price: TaoCurrency) -> Result<u64, Error<T>> {
183+
pub fn get_max_amount_add(
184+
netuid: NetUid,
185+
limit_price: TaoCurrency,
186+
) -> Result<u64, DispatchError> {
184187
// Corner case: root and stao
185188
// There's no slippage for root or stable subnets, so if limit price is 1e9 rao or
186189
// higher, then max_amount equals u64::MAX, otherwise it is 0.
187190
if netuid.is_root() || SubnetMechanism::<T>::get(netuid) == 0 {
188191
if limit_price >= 1_000_000_000.into() {
189192
return Ok(u64::MAX);
190193
} else {
191-
return Err(Error::ZeroMaxStakeAmount);
194+
return Err(Error::<T>::ZeroMaxStakeAmount.into());
192195
}
193196
}
194197

195198
// Use reverting swap to estimate max limit amount
196199
let order = GetAlphaForTao::<T>::with_amount(u64::MAX);
197200
let result = T::SwapInterface::swap(netuid.into(), order, limit_price, false, true)
198-
.map(|r| r.amount_paid_in.saturating_add(r.fee_paid))
199-
.map_err(|_| Error::ZeroMaxStakeAmount)?;
201+
.map(|r| r.amount_paid_in.saturating_add(r.fee_paid))?;
200202

201203
if !result.is_zero() {
202204
Ok(result.into())
203205
} else {
204-
Err(Error::ZeroMaxStakeAmount)
206+
Err(Error::<T>::ZeroMaxStakeAmount.into())
205207
}
206208
}
207209
}

pallets/subtensor/src/staking/move_stake.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ impl<T: Config> Pallet<T> {
424424
origin_netuid: NetUid,
425425
destination_netuid: NetUid,
426426
limit_price: TaoCurrency,
427-
) -> Result<AlphaCurrency, Error<T>> {
428-
let tao: U64F64 = U64F64::saturating_from_num(1_000_000_000);
427+
) -> Result<AlphaCurrency, DispatchError> {
428+
let tao = U64F64::saturating_from_num(1_000_000_000);
429429

430430
// Corner case: both subnet IDs are root or stao
431431
// There's no slippage for root or stable subnets, so slippage is always 0.
@@ -434,7 +434,7 @@ impl<T: Config> Pallet<T> {
434434
&& (destination_netuid.is_root() || SubnetMechanism::<T>::get(destination_netuid) == 0)
435435
{
436436
if limit_price > tao.saturating_to_num::<u64>().into() {
437-
return Err(Error::ZeroMaxStakeAmount);
437+
return Err(Error::<T>::ZeroMaxStakeAmount.into());
438438
} else {
439439
return Ok(AlphaCurrency::MAX);
440440
}
@@ -476,7 +476,7 @@ impl<T: Config> Pallet<T> {
476476
let subnet_tao_2 = SubnetTAO::<T>::get(destination_netuid)
477477
.saturating_add(SubnetTaoProvided::<T>::get(destination_netuid));
478478
if subnet_tao_1.is_zero() || subnet_tao_2.is_zero() {
479-
return Err(Error::ZeroMaxStakeAmount);
479+
return Err(Error::<T>::ZeroMaxStakeAmount.into());
480480
}
481481
let subnet_tao_1_float: U64F64 = U64F64::saturating_from_num(subnet_tao_1);
482482
let subnet_tao_2_float: U64F64 = U64F64::saturating_from_num(subnet_tao_2);
@@ -487,7 +487,7 @@ impl<T: Config> Pallet<T> {
487487
let alpha_in_2 = SubnetAlphaIn::<T>::get(destination_netuid)
488488
.saturating_add(SubnetAlphaInProvided::<T>::get(destination_netuid));
489489
if alpha_in_1.is_zero() || alpha_in_2.is_zero() {
490-
return Err(Error::ZeroMaxStakeAmount);
490+
return Err(Error::<T>::ZeroMaxStakeAmount.into());
491491
}
492492
let alpha_in_1_float: U64F64 = U64F64::saturating_from_num(alpha_in_1);
493493
let alpha_in_2_float: U64F64 = U64F64::saturating_from_num(alpha_in_2);
@@ -503,7 +503,7 @@ impl<T: Config> Pallet<T> {
503503
T::SwapInterface::current_alpha_price(destination_netuid.into()),
504504
);
505505
if limit_price_float > current_price {
506-
return Err(Error::ZeroMaxStakeAmount);
506+
return Err(Error::<T>::ZeroMaxStakeAmount.into());
507507
}
508508

509509
// Corner case: limit_price is zero
@@ -529,7 +529,7 @@ impl<T: Config> Pallet<T> {
529529
if final_result != 0 {
530530
Ok(final_result.into())
531531
} else {
532-
Err(Error::ZeroMaxStakeAmount)
532+
Err(Error::<T>::ZeroMaxStakeAmount.into())
533533
}
534534
}
535535
}

pallets/subtensor/src/staking/remove_stake.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,28 +391,27 @@ impl<T: Config> Pallet<T> {
391391
pub fn get_max_amount_remove(
392392
netuid: NetUid,
393393
limit_price: TaoCurrency,
394-
) -> Result<AlphaCurrency, Error<T>> {
394+
) -> Result<AlphaCurrency, DispatchError> {
395395
// Corner case: root and stao
396396
// There's no slippage for root or stable subnets, so if limit price is 1e9 rao or
397397
// lower, then max_amount equals u64::MAX, otherwise it is 0.
398398
if netuid.is_root() || SubnetMechanism::<T>::get(netuid) == 0 {
399399
if limit_price <= 1_000_000_000.into() {
400400
return Ok(AlphaCurrency::MAX);
401401
} else {
402-
return Err(Error::ZeroMaxStakeAmount);
402+
return Err(Error::<T>::ZeroMaxStakeAmount.into());
403403
}
404404
}
405405

406406
// Use reverting swap to estimate max limit amount
407407
let order = GetTaoForAlpha::<T>::with_amount(u64::MAX);
408408
let result = T::SwapInterface::swap(netuid.into(), order, limit_price.into(), false, true)
409-
.map(|r| r.amount_paid_in.saturating_add(r.fee_paid))
410-
.map_err(|_| Error::ZeroMaxStakeAmount)?;
409+
.map(|r| r.amount_paid_in.saturating_add(r.fee_paid))?;
411410

412411
if !result.is_zero() {
413412
Ok(result)
414413
} else {
415-
Err(Error::ZeroMaxStakeAmount)
414+
Err(Error::<T>::ZeroMaxStakeAmount.into())
416415
}
417416
}
418417

0 commit comments

Comments
 (0)