Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multicurrency handling updates to support #12951 #823

Merged
merged 10 commits into from
Mar 25, 2024
Prev Previous commit
Next Next commit
Update Multicurrency usage
  • Loading branch information
surangap committed Mar 25, 2024
commit 254da54d1e9d7ef72a59a10f20326f62329be475
79 changes: 65 additions & 14 deletions pallet/dex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ pub use pallet::*;
use alloc::string::ToString;
use frame_support::{
pallet_prelude::*,
traits::fungibles::{self, Inspect, InspectMetadata, Mutate, Transfer},
traits::{
fungibles::{self, metadata::Inspect as MetadataInspect, Inspect, Mutate},
tokens::{Fortitude, Precision, Preservation},
},
transactional, PalletId,
};
use frame_system::pallet_prelude::*;
Expand Down Expand Up @@ -540,9 +543,9 @@ where

fn create_lp_token(trading_pair: &TradingPair) -> Result<AssetId, DispatchError> {
let symbol_a_truncated: Vec<u8> =
T::MultiCurrency::symbol(&trading_pair.0).into_iter().take(20).collect();
T::MultiCurrency::symbol(trading_pair.0).into_iter().take(20).collect();
let symbol_b_truncated: Vec<u8> =
T::MultiCurrency::symbol(&trading_pair.1).into_iter().take(20).collect();
T::MultiCurrency::symbol(trading_pair.1).into_iter().take(20).collect();

// name: b"LP " + symbol_a_truncated + b" " + symbol_b_truncated
let mut lp_token_name =
Expand Down Expand Up @@ -663,8 +666,20 @@ where
};

let pool_address = trading_pair.pool_address();
T::MultiCurrency::transfer(token_a, who, &pool_address, amount_a.saturated_into(), false)?;
T::MultiCurrency::transfer(token_b, who, &pool_address, amount_b.saturated_into(), false)?;
T::MultiCurrency::transfer(
token_a,
who,
&pool_address,
amount_a.saturated_into(),
Preservation::Expendable,
)?;
T::MultiCurrency::transfer(
token_b,
who,
&pool_address,
amount_b.saturated_into(),
Preservation::Expendable,
)?;

let balance_0 = T::MultiCurrency::balance(token_a, &pool_address);
let balance_1 = T::MultiCurrency::balance(token_b, &pool_address);
Expand Down Expand Up @@ -760,7 +775,13 @@ where

// transfer lp tokens to dex
let pool_address = trading_pair.pool_address();
T::MultiCurrency::transfer(lp_share_asset_id, &who, &pool_address, liquidity, false)?;
T::MultiCurrency::transfer(
lp_share_asset_id,
&who,
&pool_address,
liquidity,
Preservation::Expendable,
)?;

// match trading-pair to inputs - to match reserves in liquidity pool
let (token_a, token_b, amount_a_min, amount_b_min, is_swapped) =
Expand Down Expand Up @@ -792,9 +813,27 @@ where
ensure!(amount_0 >= amount_a_min, Error::<T>::InsufficientWithdrawnAmountA);
ensure!(amount_1 >= amount_b_min, Error::<T>::InsufficientWithdrawnAmountB);

T::MultiCurrency::burn_from(lp_share_asset_id, &pool_address, liquidity)?;
T::MultiCurrency::transfer(token_a, &pool_address, &to, amount_0, false)?;
T::MultiCurrency::transfer(token_b, &pool_address, &to, amount_1, false)?;
T::MultiCurrency::burn_from(
lp_share_asset_id,
&pool_address,
liquidity,
Precision::Exact,
Fortitude::Polite,
)?;
T::MultiCurrency::transfer(
token_a,
&pool_address,
&to,
amount_0,
Preservation::Expendable,
)?;
T::MultiCurrency::transfer(
token_b,
&pool_address,
&to,
amount_1,
Preservation::Expendable,
)?;

balance_0 = T::MultiCurrency::balance(token_a, &pool_address);
balance_1 = T::MultiCurrency::balance(token_b, &pool_address);
Expand Down Expand Up @@ -1046,7 +1085,7 @@ where
&pool_address,
&to,
amount_0_out,
false,
Preservation::Expendable,
)?;
}
if amount_1_out > 0 {
Expand All @@ -1056,7 +1095,7 @@ where
&pool_address,
&to,
amount_1_out,
false,
Preservation::Expendable,
)?;
}

Expand Down Expand Up @@ -1177,7 +1216,13 @@ where
// transfer tokens to module account (uniswapv2 trading pair)
let pool_address = trading_pair.pool_address();

T::MultiCurrency::transfer(path[0], who, &pool_address, amounts[0], false)?;
T::MultiCurrency::transfer(
path[0],
who,
&pool_address,
amounts[0],
Preservation::Expendable,
)?;

let swap_res = Self::_swap(&amounts, &path, &to)?;
Self::deposit_event(Event::Swap(
Expand Down Expand Up @@ -1215,7 +1260,13 @@ where
ensure!(amounts[0] <= amount_in_max, Error::<T>::ExcessiveSupplyAmount);
let trading_pair = TradingPair::new(path[0], path[1]);
let pool_address = trading_pair.pool_address();
T::MultiCurrency::transfer(path[0], who, &pool_address, amounts[0], false)?;
T::MultiCurrency::transfer(
path[0],
who,
&pool_address,
amounts[0],
Preservation::Expendable,
)?;

let swap_res = Self::_swap(&amounts, &path, &to)?;
Self::deposit_event(Event::Swap(who.clone(), path.to_vec(), amounts[0], amount_out, to));
Expand Down Expand Up @@ -1266,4 +1317,4 @@ where
Ok(false)
}
}
}
}
27 changes: 23 additions & 4 deletions pallet/erc20-peg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use frame_support::{
pallet_prelude::*,
traits::{
fungibles,
fungibles::{Mutate},
fungibles::Mutate,
tokens::{Fortitude, Precision, Preservation},
Get, IsType,
},
transactional,
Expand Down Expand Up @@ -345,10 +346,22 @@ impl<T: Config> Module<T> {
if asset_id == T::NativeAssetId::get() {
// transfer all ROOT tokens to the peg address
let pallet_address: T::AccountId = T::PegPalletId::get().into_account_truncating();
T::MultiCurrency::transfer(asset_id, origin, &pallet_address, amount, false)?;
T::MultiCurrency::transfer(
asset_id,
origin,
&pallet_address,
amount,
Preservation::Expendable,
)?;
} else {
// burn all other tokens
T::MultiCurrency::burn_from(asset_id, origin, amount)?;
T::MultiCurrency::burn_from(
asset_id,
origin,
amount,
Precision::Exact,
Fortitude::Polite,
)?;
}
Ok(())
}
Expand Down Expand Up @@ -539,7 +552,13 @@ impl<T: Config> Module<T> {
if asset_id == T::NativeAssetId::get() {
// Transfer all ROOT tokens from the peg address
let pallet_address: T::AccountId = T::PegPalletId::get().into_account_truncating();
T::MultiCurrency::transfer(asset_id, &pallet_address, beneficiary, amount, false)?;
T::MultiCurrency::transfer(
asset_id,
&pallet_address,
beneficiary,
amount,
Preservation::Expendable,
)?;
} else {
// Mint all other tokens
T::MultiCurrency::mint_into(asset_id, beneficiary, amount)?;
Expand Down
14 changes: 12 additions & 2 deletions pallet/nft/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
// You may obtain a copy of the License at the root of this project source code

use crate::{traits::NFTCollectionInfo, *};
use frame_support::{ensure, traits::Get, weights::Weight};
use frame_support::{
ensure,
traits::{tokens::Preservation, Get},
weights::Weight,
};
use frame_system::RawOrigin;
use precompile_utils::constants::ERC721_PRECOMPILE_ADDRESS_PREFIX;
use seed_pallet_common::{
Expand Down Expand Up @@ -258,7 +262,13 @@ impl<T: Config> Pallet<T> {
};
// Charge the fee if there is a fee set
if let Some((asset, total_fee)) = total_fee {
T::MultiCurrency::transfer(asset, who, &collection_owner, total_fee, false)?;
T::MultiCurrency::transfer(
asset,
who,
&collection_owner,
total_fee,
Preservation::Expendable,
)?;
// Deposit event
Self::deposit_event(Event::<T>::MintFeePaid {
who: who.clone(),
Expand Down
10 changes: 8 additions & 2 deletions pallet/sft/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You may obtain a copy of the License at the root of this project source code

use crate::*;
use frame_support::ensure;
use frame_support::{ensure, traits::tokens::Preservation};
use precompile_utils::constants::ERC1155_PRECOMPILE_ADDRESS_PREFIX;
use seed_pallet_common::{utils::PublicMintInformation, SFTExt};
use seed_primitives::{CollectionUuid, MAX_COLLECTION_ENTITLEMENTS};
Expand Down Expand Up @@ -147,7 +147,13 @@ impl<T: Config> Pallet<T> {
};
// Charge the fee if there is a fee set
if let Some((asset, total_fee)) = total_fee {
T::MultiCurrency::transfer(asset, who, &collection_owner, total_fee, false)?;
T::MultiCurrency::transfer(
asset,
who,
&collection_owner,
total_fee,
Preservation::Expendable,
)?;
// Deposit event
Self::deposit_event(Event::<T>::MintFeePaid {
who: who.clone(),
Expand Down
27 changes: 20 additions & 7 deletions pallet/vortex-distribution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use frame_support::{
log,
pallet_prelude::*,
traits::{
tokens::fungibles::{self, Inspect, Mutate, Transfer},
tokens::fungibles::{self, Inspect, Mutate},
Get,
},
PalletId,
Expand Down Expand Up @@ -77,8 +77,10 @@ type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup

#[frame_support::pallet]
pub mod pallet {

use frame_support::transactional;
use frame_support::{
traits::tokens::{Fortitude, Precision, Preservation},
transactional,
};
use sp_runtime::traits::AtLeast32BitUnsigned;

use super::*;
Expand Down Expand Up @@ -619,7 +621,13 @@ pub mod pallet {
}

// Burn the vortex token
T::MultiCurrency::burn_from(T::VtxAssetId::get(), &who, vortex_token_amount)?;
T::MultiCurrency::burn_from(
T::VtxAssetId::get(),
&who,
vortex_token_amount,
Precision::Exact,
Fortitude::Polite,
)?;
Ok(())
}
}
Expand Down Expand Up @@ -772,8 +780,13 @@ pub mod pallet {
amount: BalanceOf<T>,
_keep_live: bool,
) -> DispatchResult {
let transfer_result =
T::MultiCurrency::transfer(asset_id, source, dest, amount, false)?;
let transfer_result = T::MultiCurrency::transfer(
asset_id,
source,
dest,
amount,
Preservation::Expendable,
)?;
ensure!(transfer_result == amount, Error::<T>::InvalidAmount);
Ok(())
}
Expand All @@ -793,4 +806,4 @@ pub mod pallet {
}
}
}
}
}
4 changes: 2 additions & 2 deletions runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,9 +783,9 @@ where
let amount = <pallet_assets_ext::Pallet<T> as fungibles::Inspect<
<T as frame_system::Config>::AccountId,
>>::reducible_balance(asset_id, current_owner, false);
<pallet_assets_ext::Pallet<T> as fungibles::Transfer<
<pallet_assets_ext::Pallet<T> as fungibles::Mutate<
<T as frame_system::Config>::AccountId,
>>::transfer(asset_id, current_owner, new_owner, amount, false)?;
>>::transfer(asset_id, current_owner, new_owner, amount, Preservation::Expendable)?;
Ok(())
}

Expand Down