From ee3eedad802562fb1404f42260f97465bfe8702c Mon Sep 17 00:00:00 2001 From: Squirrel Date: Thu, 8 Jun 2023 21:49:25 +0100 Subject: [PATCH] No need for AssetBalanceOf (#14325) --- frame/asset-conversion/src/lib.rs | 110 ++++++++++++++-------------- frame/asset-conversion/src/types.rs | 3 - 2 files changed, 55 insertions(+), 58 deletions(-) diff --git a/frame/asset-conversion/src/lib.rs b/frame/asset-conversion/src/lib.rs index c1aeb5898da71..8802f4de9b78f 100644 --- a/frame/asset-conversion/src/lib.rs +++ b/frame/asset-conversion/src/lib.rs @@ -241,13 +241,13 @@ pub mod pallet { /// The pool id of the pool that the liquidity was added to. pool_id: PoolIdOf, /// The amount of the first asset that was added to the pool. - amount1_provided: AssetBalanceOf, + amount1_provided: T::AssetBalance, /// The amount of the second asset that was added to the pool. - amount2_provided: AssetBalanceOf, + amount2_provided: T::AssetBalance, /// The id of the lp token that was minted. lp_token: T::PoolAssetId, /// The amount of lp tokens that were minted of that id. - lp_token_minted: AssetBalanceOf, + lp_token_minted: T::AssetBalance, }, /// A successful call of the `RemoveLiquidity` extrinsic will create this event. @@ -259,13 +259,13 @@ pub mod pallet { /// The pool id that the liquidity was removed from. pool_id: PoolIdOf, /// The amount of the first asset that was removed from the pool. - amount1: AssetBalanceOf, + amount1: T::AssetBalance, /// The amount of the second asset that was removed from the pool. - amount2: AssetBalanceOf, + amount2: T::AssetBalance, /// The id of the lp token that was burned. lp_token: T::PoolAssetId, /// The amount of lp tokens that were burned of that id. - lp_token_burned: AssetBalanceOf, + lp_token_burned: T::AssetBalance, /// Liquidity withdrawal fee (%). withdrawal_fee: Permill, }, @@ -280,9 +280,9 @@ pub mod pallet { /// E.g. A -> Dot -> B path: BoundedVec, /// The amount of the first asset that was swapped. - amount_in: AssetBalanceOf, + amount_in: T::AssetBalance, /// The amount of the second asset that was received. - amount_out: AssetBalanceOf, + amount_out: T::AssetBalance, }, /// An amount has been transferred from one account to another. Transfer { @@ -293,7 +293,7 @@ pub mod pallet { /// The asset that was transferred. asset: T::MultiAssetId, /// The amount of the asset that was transferred. - amount: AssetBalanceOf, + amount: T::AssetBalance, }, } @@ -427,10 +427,10 @@ pub mod pallet { origin: OriginFor, asset1: T::MultiAssetId, asset2: T::MultiAssetId, - amount1_desired: AssetBalanceOf, - amount2_desired: AssetBalanceOf, - amount1_min: AssetBalanceOf, - amount2_min: AssetBalanceOf, + amount1_desired: T::AssetBalance, + amount2_desired: T::AssetBalance, + amount1_min: T::AssetBalance, + amount2_min: T::AssetBalance, mint_to: T::AccountId, ) -> DispatchResult { let sender = ensure_signed(origin)?; @@ -453,8 +453,8 @@ pub mod pallet { let maybe_pool = Pools::::get(pool_id.clone()); let pool = maybe_pool.as_ref().ok_or(Error::::PoolNotFound)?; - let amount1: AssetBalanceOf; - let amount2: AssetBalanceOf; + let amount1: T::AssetBalance; + let amount2: T::AssetBalance; let pool_account = Self::get_pool_account(&pool_id); let reserve1 = Self::get_balance(&pool_account, &asset1)?; let reserve2 = Self::get_balance(&pool_account, &asset2)?; @@ -497,7 +497,7 @@ pub mod pallet { let total_supply = T::PoolAssets::total_issuance(pool.lp_token.clone()); - let lp_token_amount: AssetBalanceOf; + let lp_token_amount: T::AssetBalance; if total_supply.is_zero() { lp_token_amount = Self::calc_lp_amount_for_zero_supply(&amount1, &amount2)?; T::PoolAssets::mint_into( @@ -540,9 +540,9 @@ pub mod pallet { origin: OriginFor, asset1: T::MultiAssetId, asset2: T::MultiAssetId, - lp_token_burn: AssetBalanceOf, - amount1_min_receive: AssetBalanceOf, - amount2_min_receive: AssetBalanceOf, + lp_token_burn: T::AssetBalance, + amount1_min_receive: T::AssetBalance, + amount2_min_receive: T::AssetBalance, withdraw_to: T::AccountId, ) -> DispatchResult { let sender = ensure_signed(origin)?; @@ -618,8 +618,8 @@ pub mod pallet { pub fn swap_exact_tokens_for_tokens( origin: OriginFor, path: BoundedVec, - amount_in: AssetBalanceOf, - amount_out_min: AssetBalanceOf, + amount_in: T::AssetBalance, + amount_out_min: T::AssetBalance, send_to: T::AccountId, keep_alive: bool, ) -> DispatchResult { @@ -659,8 +659,8 @@ pub mod pallet { pub fn swap_tokens_for_exact_tokens( origin: OriginFor, path: BoundedVec, - amount_out: AssetBalanceOf, - amount_in_max: AssetBalanceOf, + amount_out: T::AssetBalance, + amount_in_max: T::AssetBalance, send_to: T::AccountId, keep_alive: bool, ) -> DispatchResult { @@ -695,7 +695,7 @@ pub mod pallet { asset_id: &T::MultiAssetId, from: &T::AccountId, to: &T::AccountId, - amount: AssetBalanceOf, + amount: T::AssetBalance, keep_alive: bool, ) -> Result { Self::deposit_event(Event::Transfer { @@ -737,7 +737,7 @@ pub mod pallet { pub(crate) fn do_swap( sender: &T::AccountId, - amounts: &Vec>, + amounts: &Vec, path: &BoundedVec, send_to: &T::AccountId, keep_alive: bool, @@ -827,7 +827,7 @@ pub mod pallet { pub fn get_reserves( asset1: &T::MultiAssetId, asset2: &T::MultiAssetId, - ) -> Result<(AssetBalanceOf, AssetBalanceOf), Error> { + ) -> Result<(T::AssetBalance, T::AssetBalance), Error> { let pool_id = Self::get_pool_id(asset1.clone(), asset2.clone()); let pool_account = Self::get_pool_account(&pool_id); @@ -842,10 +842,10 @@ pub mod pallet { } pub(crate) fn get_amounts_in( - amount_out: &AssetBalanceOf, + amount_out: &T::AssetBalance, path: &BoundedVec, - ) -> Result>, DispatchError> { - let mut amounts: Vec> = vec![*amount_out]; + ) -> Result, DispatchError> { + let mut amounts: Vec = vec![*amount_out]; for assets_pair in path.windows(2).rev() { if let [asset1, asset2] = assets_pair { @@ -861,10 +861,10 @@ pub mod pallet { } pub(crate) fn get_amounts_out( - amount_in: &AssetBalanceOf, + amount_in: &T::AssetBalance, path: &BoundedVec, - ) -> Result>, DispatchError> { - let mut amounts: Vec> = vec![*amount_in]; + ) -> Result, DispatchError> { + let mut amounts: Vec = vec![*amount_in]; for assets_pair in path.windows(2) { if let [asset1, asset2] = assets_pair { @@ -882,9 +882,9 @@ pub mod pallet { pub fn quote_price_exact_tokens_for_tokens( asset1: T::MultiAssetId, asset2: T::MultiAssetId, - amount: AssetBalanceOf, + amount: T::AssetBalance, include_fee: bool, - ) -> Option> { + ) -> Option { let pool_id = Self::get_pool_id(asset1.clone(), asset2.clone()); let pool_account = Self::get_pool_account(&pool_id); @@ -905,9 +905,9 @@ pub mod pallet { pub fn quote_price_tokens_for_exact_tokens( asset1: T::MultiAssetId, asset2: T::MultiAssetId, - amount: AssetBalanceOf, + amount: T::AssetBalance, include_fee: bool, - ) -> Option> { + ) -> Option { let pool_id = Self::get_pool_id(asset1.clone(), asset2.clone()); let pool_account = Self::get_pool_account(&pool_id); @@ -926,18 +926,18 @@ pub mod pallet { /// Calculates the optimal amount from the reserves. pub fn quote( - amount: &AssetBalanceOf, - reserve1: &AssetBalanceOf, - reserve2: &AssetBalanceOf, - ) -> Result, Error> { + amount: &T::AssetBalance, + reserve1: &T::AssetBalance, + reserve2: &T::AssetBalance, + ) -> Result> { // amount * reserve2 / reserve1 Self::mul_div(amount, reserve2, reserve1) } pub(super) fn calc_lp_amount_for_zero_supply( - amount1: &AssetBalanceOf, - amount2: &AssetBalanceOf, - ) -> Result, Error> { + amount1: &T::AssetBalance, + amount2: &T::AssetBalance, + ) -> Result> { let amount1 = T::HigherPrecisionBalance::from(*amount1); let amount2 = T::HigherPrecisionBalance::from(*amount2); @@ -952,10 +952,10 @@ pub mod pallet { } fn mul_div( - a: &AssetBalanceOf, - b: &AssetBalanceOf, - c: &AssetBalanceOf, - ) -> Result, Error> { + a: &T::AssetBalance, + b: &T::AssetBalance, + c: &T::AssetBalance, + ) -> Result> { let a = T::HigherPrecisionBalance::from(*a); let b = T::HigherPrecisionBalance::from(*b); let c = T::HigherPrecisionBalance::from(*c); @@ -974,10 +974,10 @@ pub mod pallet { /// Given an input amount of an asset and pair reserves, returns the maximum output amount /// of the other asset pub fn get_amount_out( - amount_in: &AssetBalanceOf, - reserve_in: &AssetBalanceOf, - reserve_out: &AssetBalanceOf, - ) -> Result, Error> { + amount_in: &T::AssetBalance, + reserve_in: &T::AssetBalance, + reserve_out: &T::AssetBalance, + ) -> Result> { let amount_in = T::HigherPrecisionBalance::from(*amount_in); let reserve_in = T::HigherPrecisionBalance::from(*reserve_in); let reserve_out = T::HigherPrecisionBalance::from(*reserve_out); @@ -1009,10 +1009,10 @@ pub mod pallet { /// Given an output amount of an asset and pair reserves, returns a required input amount /// of the other asset pub fn get_amount_in( - amount_out: &AssetBalanceOf, - reserve_in: &AssetBalanceOf, - reserve_out: &AssetBalanceOf, - ) -> Result, Error> { + amount_out: &T::AssetBalance, + reserve_in: &T::AssetBalance, + reserve_out: &T::AssetBalance, + ) -> Result> { let amount_out = T::HigherPrecisionBalance::from(*amount_out); let reserve_in = T::HigherPrecisionBalance::from(*reserve_in); let reserve_out = T::HigherPrecisionBalance::from(*reserve_out); diff --git a/frame/asset-conversion/src/types.rs b/frame/asset-conversion/src/types.rs index 837b14be283ef..cbe201f01c10b 100644 --- a/frame/asset-conversion/src/types.rs +++ b/frame/asset-conversion/src/types.rs @@ -20,11 +20,8 @@ use core::marker::PhantomData; use sp_std::cmp::Ordering; use codec::{Decode, Encode, MaxEncodedLen}; -use frame_support::traits::fungibles::Inspect; use scale_info::TypeInfo; -pub(super) type AssetBalanceOf = - <::Assets as Inspect<::AccountId>>::Balance; pub(super) type PoolIdOf = (::MultiAssetId, ::MultiAssetId); /// Stores the lp_token asset id a particular pool has been assigned.