From 19fb3655fd2b036b86aef002c9648e063e29e8b1 Mon Sep 17 00:00:00 2001 From: yooml Date: Tue, 5 Dec 2023 10:55:40 +0800 Subject: [PATCH] Change as_u128 to try_from (#1072) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 🐛 rm as_u128 * fix: 🐛 rpc of stable-pool * fix: 🐛 test-all * fix: 🐛 clippy * fix: 🐛 clippy * fix: 🐛 token_to_vtoken * fix: 🐛 clippy --- pallets/farming/src/gauge.rs | 27 +++++----- pallets/farming/src/rewards.rs | 56 ++++++++++++--------- pallets/farming/src/tests.rs | 1 - pallets/salp/src/lib.rs | 2 +- pallets/salp/src/tests.rs | 2 +- pallets/slp/src/agents/phala_agent/agent.rs | 4 +- pallets/stable-pool/src/lib.rs | 6 +-- pallets/stable-pool/src/tests.rs | 29 ++++++++--- pallets/system-maker/src/lib.rs | 3 +- pallets/system-staking/src/benchmarking.rs | 3 ++ pallets/system-staking/src/lib.rs | 6 +-- pallets/ve-minting/src/incentive.rs | 22 ++++---- pallets/ve-minting/src/lib.rs | 12 +++-- pallets/vtoken-minting/src/lib.rs | 44 +++++++++------- primitives/src/traits.rs | 12 ++--- 15 files changed, 133 insertions(+), 96 deletions(-) diff --git a/pallets/farming/src/gauge.rs b/pallets/farming/src/gauge.rs index 2b148cd99a..1ceba4eaba 100644 --- a/pallets/farming/src/gauge.rs +++ b/pallets/farming/src/gauge.rs @@ -322,15 +322,16 @@ where let gauge_reward = gauge_rate * reward; // reward_to_claim = farming rate * gauge rate * gauge rewards * // existing rewards in the gauge pool - let reward_to_claim: BalanceOf = + let reward_to_claim: BalanceOf = u128::try_from( U256::from(share_info.share.to_owned().saturated_into::()) .saturating_mul(U256::from( gauge_reward.to_owned().saturated_into::(), )) .checked_div(total_shares) - .unwrap_or_default() - .as_u128() - .unique_saturated_into(); + .unwrap_or_default(), + ) + .map_err(|_| ArithmeticError::Overflow)? + .unique_saturated_into(); *total_gauged_reward = total_gauged_reward .checked_add(&gauge_reward) .ok_or(ArithmeticError::Overflow)?; @@ -416,15 +417,16 @@ where let withdrawn_reward = share_info.withdrawn_rewards.get(reward_currency).copied().unwrap_or_default(); - let total_reward_proportion: BalanceOf = + let total_reward_proportion: BalanceOf = u128::try_from( U256::from(share_info.share.to_owned().saturated_into::()) .saturating_mul(U256::from( total_reward.to_owned().saturated_into::(), )) .checked_div(total_shares) - .unwrap_or_default() - .as_u128() - .unique_saturated_into(); + .unwrap_or_default(), + ) + .map_err(|_| ArithmeticError::Overflow)? + .unique_saturated_into(); let reward_to_withdraw = total_reward_proportion .saturating_sub(withdrawn_reward) @@ -498,15 +500,16 @@ where let gauge_reward = gauge_rate * reward; // reward_to_claim = farming rate * gauge rate * gauge rewards * // existing rewards in the gauge pool - let reward_to_claim: BalanceOf = + let reward_to_claim: BalanceOf = u128::try_from( U256::from(share_info.share.to_owned().saturated_into::()) .saturating_mul(U256::from( gauge_reward.to_owned().saturated_into::(), )) .checked_div(total_shares) - .unwrap_or_default() - .as_u128() - .unique_saturated_into(); + .unwrap_or_default(), + ) + .map_err(|_| ArithmeticError::Overflow)? + .unique_saturated_into(); result_vec.push((*reward_currency, reward_to_claim)); Ok(()) }, diff --git a/pallets/farming/src/rewards.rs b/pallets/farming/src/rewards.rs index a5a1371620..5454041705 100644 --- a/pallets/farming/src/rewards.rs +++ b/pallets/farming/src/rewards.rs @@ -168,14 +168,19 @@ impl Pallet { let reward_inflation = if initial_total_shares.is_zero() { Zero::zero() } else { - U256::from(add_amount.to_owned().saturated_into::()) - .saturating_mul(total_reward.to_owned().saturated_into::().into()) - .checked_div( - initial_total_shares.to_owned().saturated_into::().into(), - ) - .unwrap_or_default() - .as_u128() - .saturated_into() + // If the total shares is zero or reward amount overflow, the reward inflation + // is zero. + u128::try_from( + U256::from(add_amount.to_owned().saturated_into::()) + .saturating_mul(total_reward.to_owned().saturated_into::().into()) + .checked_div( + initial_total_shares.to_owned().saturated_into::().into(), + ) + .unwrap_or_default(), + ) + .ok() + .unwrap_or_default() + .saturated_into() }; *total_reward = total_reward.saturating_add(reward_inflation); *total_withdrawn_reward = total_withdrawn_reward.saturating_add(reward_inflation); @@ -246,15 +251,16 @@ impl Pallet { // update withdrawn rewards for each reward currency share_info.withdrawn_rewards.iter_mut().try_for_each( |(reward_currency, withdrawn_reward)| -> DispatchResult { - let withdrawn_reward_to_remove: BalanceOf = removing_share - .saturating_mul( - withdrawn_reward.to_owned().saturated_into::().into(), - ) - .checked_div(share_info.share.saturated_into::().into()) - .unwrap_or_default() - .as_u128() - .saturated_into(); - + let withdrawn_reward_to_remove: BalanceOf = u128::try_from( + removing_share + .saturating_mul( + withdrawn_reward.to_owned().saturated_into::().into(), + ) + .checked_div(share_info.share.saturated_into::().into()) + .unwrap_or_default(), + ) + .map_err(|_| ArithmeticError::Overflow)? + .unique_saturated_into(); if let Some((total_reward, total_withdrawn_reward)) = pool_info.rewards.get_mut(reward_currency) { @@ -310,15 +316,15 @@ impl Pallet { .copied() .unwrap_or_default(); - let total_reward_proportion: BalanceOf = U256::from( - share_info.share.to_owned().saturated_into::(), + let total_reward_proportion: BalanceOf = u128::try_from( + U256::from(share_info.share.to_owned().saturated_into::()) + .saturating_mul(U256::from( + total_reward.to_owned().saturated_into::(), + )) + .checked_div(total_shares) + .unwrap_or_default(), ) - .saturating_mul(U256::from( - total_reward.to_owned().saturated_into::(), - )) - .checked_div(total_shares) - .unwrap_or_default() - .as_u128() + .map_err(|_| ArithmeticError::Overflow)? .unique_saturated_into(); let reward_to_withdraw = total_reward_proportion diff --git a/pallets/farming/src/tests.rs b/pallets/farming/src/tests.rs index 10b2b080e6..adc142cdf4 100644 --- a/pallets/farming/src/tests.rs +++ b/pallets/farming/src/tests.rs @@ -47,7 +47,6 @@ fn asset_registry() { #[test] fn boost() { ExtBuilder::default().one_hundred_for_alice_n_bob().build().execute_with(|| { - env_logger::try_init().unwrap_or(()); asset_registry(); System::set_block_number(System::block_number() + 20); assert_ok!(VeMinting::set_config( diff --git a/pallets/salp/src/lib.rs b/pallets/salp/src/lib.rs index 107f3fd165..524ba9a3fb 100644 --- a/pallets/salp/src/lib.rs +++ b/pallets/salp/src/lib.rs @@ -1270,7 +1270,7 @@ pub mod pallet { relay_currency_id, relay_vtoken_id, value, - ); + )?; T::StablePool::swap( &T::BuybackPalletId::get().into_account_truncating(), pool_id, diff --git a/pallets/salp/src/tests.rs b/pallets/salp/src/tests.rs index 7593bcca4a..757ec6e346 100644 --- a/pallets/salp/src/tests.rs +++ b/pallets/salp/src/tests.rs @@ -1448,7 +1448,7 @@ fn refund_meanwhile_issue_should_work() { orml_tokens::Error::::BalanceTooLow ); let token_value = VtokenMinting::token_to_vtoken(KSM, VKSM, 100); - assert_eq!(token_value, 100); + assert_eq!(token_value, Ok(100)); assert_eq!(Tokens::free_balance(KSM, &ALICE), 95000); assert_ok!(Tokens::set_balance(RuntimeOrigin::root(), buyback_account, KSM, 100, 0)); diff --git a/pallets/slp/src/agents/phala_agent/agent.rs b/pallets/slp/src/agents/phala_agent/agent.rs index 43e2954b9a..4b059174f5 100644 --- a/pallets/slp/src/agents/phala_agent/agent.rs +++ b/pallets/slp/src/agents/phala_agent/agent.rs @@ -886,9 +886,9 @@ impl PhalaAgent { let shares: u128 = U256::from((*total_shares).saturated_into::()) .saturating_mul(amount.saturated_into::().into()) .checked_div((*total_value).saturated_into::().into()) + .map(|x| u128::try_from(x)) .ok_or(Error::::OverFlow)? - .as_u128() - .saturated_into(); + .map_err(|_| Error::::OverFlow)?; Ok(BalanceOf::::unique_saturated_from(shares)) } diff --git a/pallets/stable-pool/src/lib.rs b/pallets/stable-pool/src/lib.rs index 6691be6099..8af5fe6be4 100644 --- a/pallets/stable-pool/src/lib.rs +++ b/pallets/stable-pool/src/lib.rs @@ -785,9 +785,8 @@ impl Pallet { currency_id_out: PoolTokenIndex, amount: T::Balance, ) -> Result { - let mut pool_info = + let pool_info = T::StableAsset::pool(pool_id).ok_or(bifrost_stable_asset::Error::::PoolNotFound)?; - T::StableAsset::collect_yield(pool_id, &mut pool_info)?; let dx = Self::upscale( amount, pool_id, @@ -854,7 +853,7 @@ impl Pallet { pool_id: StableAssetPoolId, mut amounts: Vec, ) -> Result { - let mut pool_info = + let pool_info = T::StableAsset::pool(pool_id).ok_or(bifrost_stable_asset::Error::::PoolNotFound)?; for (i, amount) in amounts.iter_mut().enumerate() { *amount = Self::upscale( @@ -866,7 +865,6 @@ impl Pallet { .ok_or(bifrost_stable_asset::Error::::ArgumentsMismatch)?, )?; } - T::StableAsset::collect_yield(pool_id, &mut pool_info)?; let MintResult { mint_amount, .. } = bifrost_stable_asset::Pallet::::get_mint_amount(&pool_info, &amounts)?; diff --git a/pallets/stable-pool/src/tests.rs b/pallets/stable-pool/src/tests.rs index 8be55608b5..cc83a58bf0 100644 --- a/pallets/stable-pool/src/tests.rs +++ b/pallets/stable-pool/src/tests.rs @@ -252,6 +252,8 @@ fn swap_successful() { #[test] fn get_swap_output_amount() { ExtBuilder::default().new_test_ext().build().execute_with(|| { + env_logger::try_init().unwrap_or(()); + System::set_block_number(2); assert_ok!(VtokenMinting::set_minimum_mint(RuntimeOrigin::signed(1), DOT, 0)); assert_ok!(VtokenMinting::mint(Some(3).into(), DOT, 100_000_000, BoundedVec::default())); @@ -263,7 +265,24 @@ fn get_swap_output_amount() { )); let amounts = vec![10000000u128, 20000000u128]; assert_ok!(StablePool::mint_inner(&3, 0, amounts, 0)); - assert_ok!(StablePool::on_swap(&3u128, 0, 0, 1, 5000000u128, 0)); + assert_eq!(StablePool::get_swap_output(0, 0, 1, 5000000u128).ok(), Some(4999301)); + assert_noop!( + StablePool::on_swap(&3u128, 0, 0, 1, 5000000u128, 4999302), + Error::::SwapUnderMin + ); + assert_ok!(StablePool::on_swap(&3u128, 0, 0, 1, 5000000u128, 4999301)); + assert_ok!(StablePool::edit_token_rate( + RuntimeOrigin::root(), + 0, + vec![(DOT, (1, 1)), (VDOT, (90_000_000, 100_000_000))] + )); + assert_eq!(StablePool::get_swap_output(0, 0, 1, 5000000u128).ok(), Some(4485945)); + assert_ok!(StablePool::edit_token_rate( + RuntimeOrigin::root(), + 0, + vec![(coin0, (1, 1)), (coin1, (1, 1))] + )); + assert_eq!(StablePool::get_swap_output(0, 0, 1, 5000000u128).ok(), Some(4980724)); assert_noop!( StablePool::on_swap(&3u128, 0, 1, 1, 5000000u128, 0), bifrost_stable_asset::Error::::ArgumentsError @@ -284,10 +303,6 @@ fn get_swap_output_amount() { StablePool::on_swap(&3u128, 0, 0, 1, 0u128, 0), bifrost_stable_asset::Error::::ArgumentsError ); - assert_noop!( - StablePool::on_swap(&3u128, 0, 0, 1, 5000000u128, 50000000000000000u128), - Error::::SwapUnderMin - ); assert_noop!( StablePool::on_swap(&3u128, 0, 0, 1, 500000000u128, 0u128), orml_tokens::Error::::BalanceTooLow @@ -304,9 +319,9 @@ fn get_swap_output_amount() { redeem_fee: 50000000u128, total_supply: 300006989999594867u128, a: 10000u128, - a_block: 0, + a_block: 2, future_a: 10000u128, - future_a_block: 0, + future_a_block: 2, balances: vec![150000000000000000u128, 150006990000000000u128], fee_recipient: 2, account_id: swap_id, diff --git a/pallets/system-maker/src/lib.rs b/pallets/system-maker/src/lib.rs index 553ec62477..d569657b65 100644 --- a/pallets/system-maker/src/lib.rs +++ b/pallets/system-maker/src/lib.rs @@ -249,8 +249,9 @@ pub mod pallet { let amount_out_min: u128 = U256::from(info.granularity.saturated_into::()) .saturating_mul(U256::from(1_000_000u32)) .checked_div(denominator) + .map(|x| u128::try_from(x)) .ok_or(ArithmeticError::Overflow)? - .as_u128(); + .map_err(|_| ArithmeticError::Overflow)?; T::DexOperator::inner_swap_exact_assets_for_assets( system_maker, diff --git a/pallets/system-staking/src/benchmarking.rs b/pallets/system-staking/src/benchmarking.rs index fc962f2fb5..4115d9be19 100644 --- a/pallets/system-staking/src/benchmarking.rs +++ b/pallets/system-staking/src/benchmarking.rs @@ -94,6 +94,9 @@ benchmarks! { Some(vec![1 as PoolId]), Some(vec![Perbill::from_percent(100)]), )); + let caller: T::AccountId = whitelisted_caller(); + assert_ok!(T::MultiCurrency::deposit(KSM, &caller, BalanceOf::::unique_saturated_from(1000u128))); + assert_ok!(T::VtokenMintingInterface::mint(caller, KSM, BalanceOf::::unique_saturated_from(1000u128), BoundedVec::default(),)); }: _(RawOrigin::Root,KSM) on_redeem_success { diff --git a/pallets/system-staking/src/lib.rs b/pallets/system-staking/src/lib.rs index d72e6cc1c8..5112d4495a 100644 --- a/pallets/system-staking/src/lib.rs +++ b/pallets/system-staking/src/lib.rs @@ -455,12 +455,12 @@ pub mod pallet { // Calculate the revenue generated by vtoken let vfree_amount = T::MultiCurrency::free_balance(vtoken_id, &pallet_account); let free_amount = - T::VtokenMintingInterface::vtoken_to_token(token, vtoken_id, vfree_amount); + T::VtokenMintingInterface::vtoken_to_token(token, vtoken_id, vfree_amount)?; let token_amount = free_amount.saturating_sub(token_info.system_shadow_amount); // Calculate the number of benefits converted to vtoken let vtoken_amount = - T::VtokenMintingInterface::token_to_vtoken(token, vtoken_id, token_amount); + T::VtokenMintingInterface::token_to_vtoken(token, vtoken_id, token_amount)?; // Transfer vtoken(benefits) to TreasuryAccount T::MultiCurrency::transfer( @@ -581,7 +581,7 @@ impl Pallet { // Calculate how many ksm can be received by vksm through VtokenMintingInterface // ===> vredeem_amount(vksm amount) let vredeem_amount = - T::VtokenMintingInterface::token_to_vtoken(token_id, vtoken_id, redeem_amount); + T::VtokenMintingInterface::token_to_vtoken(token_id, vtoken_id, redeem_amount)?; if vredeem_amount != BalanceOf::::zero() { // redeem vksm ===> vTokenMinting redeem_inner on_redeemed , update // pending_redeem_amount += token_amount diff --git a/pallets/ve-minting/src/incentive.rs b/pallets/ve-minting/src/incentive.rs index 6650faf61a..8cc34e7581 100644 --- a/pallets/ve-minting/src/incentive.rs +++ b/pallets/ve-minting/src/incentive.rs @@ -45,23 +45,24 @@ impl Pallet { pub fn reward_per_token() -> Result, BalanceOf>, DispatchError> { let mut conf = Self::incentive_configs(); let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); - let _total_supply = Self::total_supply(current_block_number)?; - if _total_supply == BalanceOf::::zero() { + let total_supply = Self::total_supply(current_block_number)?; + if total_supply == BalanceOf::::zero() { return Ok(conf.reward_per_token_stored); } conf.reward_rate.iter().try_for_each(|(currency, &reward)| -> DispatchResult { - let increment: BalanceOf = U256::from( + let increment: BalanceOf = U512::from( Self::last_time_reward_applicable() .saturating_sub(conf.last_update_time) .saturated_into::(), ) - .checked_mul(U256::from(reward.saturated_into::())) + .checked_mul(U512::from(reward.saturated_into::())) .ok_or(ArithmeticError::Overflow)? - .checked_mul(U256::from(T::Multiplier::get().saturated_into::())) + .checked_mul(U512::from(T::Multiplier::get().saturated_into::())) .ok_or(ArithmeticError::Overflow)? - .checked_div(U256::from(_total_supply.saturated_into::())) - .unwrap_or_default() - .as_u128() + .checked_div(U512::from(total_supply.saturated_into::())) + .map(|x| u128::try_from(x)) + .ok_or(ArithmeticError::Overflow)? + .map_err(|_| ArithmeticError::Overflow)? .unique_saturated_into(); conf.reward_per_token_stored .entry(*currency) @@ -97,8 +98,9 @@ impl Pallet { )) .ok_or(ArithmeticError::Overflow)? .checked_div(U256::from(T::Multiplier::get().saturated_into::())) - .unwrap_or_default() - .as_u128() + .map(|x| u128::try_from(x)) + .ok_or(ArithmeticError::Overflow)? + .map_err(|_| ArithmeticError::Overflow)? .unique_saturated_into(); rewards .entry(*currency) diff --git a/pallets/ve-minting/src/lib.rs b/pallets/ve-minting/src/lib.rs index a8818ca4e7..91640bd691 100644 --- a/pallets/ve-minting/src/lib.rs +++ b/pallets/ve-minting/src/lib.rs @@ -49,7 +49,7 @@ use frame_system::pallet_prelude::*; pub use incentive::*; use orml_traits::{MultiCurrency, MultiLockableCurrency}; pub use pallet::*; -use sp_core::U256; +use sp_core::{U256, U512}; use sp_std::{borrow::ToOwned, collections::btree_map::BTreeMap, vec, vec::Vec}; pub use traits::VeMintingInterface; pub use weights::WeightInfo; @@ -354,8 +354,9 @@ pub mod pallet { if old_locked.end > current_block_number && old_locked.amount > BalanceOf::::zero() { u_old.slope = U256::from(old_locked.amount.saturated_into::()) .checked_div(U256::from(T::MaxBlock::get().saturated_into::())) - .unwrap_or_default() - .as_u128() + .map(|x| u128::try_from(x)) + .ok_or(ArithmeticError::Overflow)? + .map_err(|_| ArithmeticError::Overflow)? .unique_saturated_into(); u_old.bias = u_old .slope @@ -368,8 +369,9 @@ pub mod pallet { if new_locked.end > current_block_number && new_locked.amount > BalanceOf::::zero() { u_new.slope = U256::from(new_locked.amount.saturated_into::()) .checked_div(U256::from(T::MaxBlock::get().saturated_into::())) - .unwrap_or_default() - .as_u128() + .map(|x| u128::try_from(x)) + .ok_or(ArithmeticError::Overflow)? + .map_err(|_| ArithmeticError::Overflow)? .unique_saturated_into(); u_new.bias = u_new .slope diff --git a/pallets/vtoken-minting/src/lib.rs b/pallets/vtoken-minting/src/lib.rs index f94c2d1448..508256e118 100644 --- a/pallets/vtoken-minting/src/lib.rs +++ b/pallets/vtoken-minting/src/lib.rs @@ -40,7 +40,9 @@ use bifrost_primitives::{ use frame_support::{ pallet_prelude::*, sp_runtime::{ - traits::{AccountIdConversion, CheckedAdd, CheckedSub, Saturating, Zero}, + traits::{ + AccountIdConversion, CheckedAdd, CheckedSub, Saturating, UniqueSaturatedInto, Zero, + }, ArithmeticError, DispatchError, Permill, SaturatedConversion, }, transactional, BoundedVec, PalletId, @@ -858,9 +860,10 @@ pub mod pallet { vtoken_amount = U256::from(token_amount_excluding_fee.saturated_into::()) .saturating_mul(vtoken_total_issuance.saturated_into::().into()) .checked_div(token_pool_amount.saturated_into::().into()) + .map(|x| u128::try_from(x)) .ok_or(Error::::CalculationOverflow)? - .as_u128() - .saturated_into(); + .map_err(|_| Error::::CalculationOverflow)? + .unique_saturated_into(); } // Charging fees @@ -1281,12 +1284,13 @@ pub mod pallet { let token_pool_amount = Self::token_pool(token_id); let vtoken_total_issuance = T::MultiCurrency::total_issuance(vtoken_id); - let token_amount = U256::from(vtoken_amount.saturated_into::()) + let token_amount: BalanceOf = U256::from(vtoken_amount.saturated_into::()) .saturating_mul(token_pool_amount.saturated_into::().into()) .checked_div(vtoken_total_issuance.saturated_into::().into()) + .map(|x| u128::try_from(x)) .ok_or(Error::::CalculationOverflow)? - .as_u128() - .saturated_into(); + .map_err(|_| Error::::CalculationOverflow)? + .unique_saturated_into(); match OngoingTimeUnit::::get(token_id) { Some(time_unit) => { @@ -1409,32 +1413,36 @@ pub mod pallet { token_id: CurrencyIdOf, vtoken_id: CurrencyIdOf, token_amount: BalanceOf, - ) -> BalanceOf { + ) -> Result, DispatchError> { let token_pool_amount = Self::token_pool(token_id); let vtoken_total_issuance = T::MultiCurrency::total_issuance(vtoken_id); - U256::from(token_amount.saturated_into::()) + let value = U256::from(token_amount.saturated_into::()) .saturating_mul(vtoken_total_issuance.saturated_into::().into()) .checked_div(token_pool_amount.saturated_into::().into()) - .unwrap_or(U256::zero()) - .as_u128() - .saturated_into() + .ok_or(Error::::CalculationOverflow)?; + + Ok(u128::try_from(value) + .map(|x| x.unique_saturated_into()) + .map_err(|_| Error::::CalculationOverflow)?) } pub fn vtoken_to_token_inner( token_id: CurrencyIdOf, vtoken_id: CurrencyIdOf, vtoken_amount: BalanceOf, - ) -> BalanceOf { + ) -> Result, DispatchError> { let token_pool_amount = Self::token_pool(token_id); let vtoken_total_issuance = T::MultiCurrency::total_issuance(vtoken_id); - U256::from(vtoken_amount.saturated_into::()) + let value = U256::from(vtoken_amount.saturated_into::()) .saturating_mul(token_pool_amount.saturated_into::().into()) .checked_div(vtoken_total_issuance.saturated_into::().into()) - .unwrap_or(U256::zero()) - .as_u128() - .saturated_into() + .ok_or(Error::::CalculationOverflow)?; + + Ok(u128::try_from(value) + .map(|x| x.unique_saturated_into()) + .map_err(|_| Error::::CalculationOverflow)?) } pub fn vtoken_id_inner(token_id: CurrencyIdOf) -> Option> { @@ -1644,7 +1652,7 @@ impl VtokenMintingInterface, CurrencyIdOf, BalanceO token_id: CurrencyIdOf, vtoken_id: CurrencyIdOf, token_amount: BalanceOf, - ) -> BalanceOf { + ) -> Result, DispatchError> { Self::token_to_vtoken_inner(token_id, vtoken_id, token_amount) } @@ -1652,7 +1660,7 @@ impl VtokenMintingInterface, CurrencyIdOf, BalanceO token_id: CurrencyIdOf, vtoken_id: CurrencyIdOf, vtoken_amount: BalanceOf, - ) -> BalanceOf { + ) -> Result, DispatchError> { Self::vtoken_to_token_inner(token_id, vtoken_id, vtoken_amount) } diff --git a/primitives/src/traits.rs b/primitives/src/traits.rs index bf4a73953f..637f9dce29 100644 --- a/primitives/src/traits.rs +++ b/primitives/src/traits.rs @@ -334,12 +334,12 @@ pub trait VtokenMintingInterface { token_id: CurrencyId, vtoken_id: CurrencyId, token_amount: Balance, - ) -> Balance; + ) -> Result; fn vtoken_to_token( token_id: CurrencyId, vtoken_id: CurrencyId, vtoken_amount: Balance, - ) -> Balance; + ) -> Result; fn vtoken_id(token_id: CurrencyId) -> Option; fn token_id(vtoken_id: CurrencyId) -> Option; fn get_minimums_redeem(vtoken_id: CurrencyId) -> Balance; @@ -382,16 +382,16 @@ impl VtokenMintingInterface Balance { - Zero::zero() + ) -> Result { + Ok(Zero::zero()) } fn vtoken_to_token( _token_id: CurrencyId, _vtoken_id: CurrencyId, _vtoken_amount: Balance, - ) -> Balance { - Zero::zero() + ) -> Result { + Ok(Zero::zero()) } fn vtoken_id(_token_id: CurrencyId) -> Option {