Skip to content

Commit

Permalink
Multicurrency handling updates to support #12951 (#823)
Browse files Browse the repository at this point in the history
* GenesisBuild -> BuildGenesisConfig

* Index -> Nonce, #14290

* Bump ethabi v18.0.0, ethereum v14.0.0, ethereum-types v0.14.1

* Transfer merge to Mutate, use of fungibles::metadata::Inspect instead of fungibles::InspectMetadata round 1.

* Update Inspect, Mutate, Unbalanced, TransferExt trait implementations for pallet-assets-ext, update the extrinsics to use new trait updates.

* Update fungible::Inspect, Currency trait implementations for AssetCurrency.

* Update mocks and tests

* Update Multicurrency usage

* fmt

* Update Multicurrency use tests
  • Loading branch information
surangap authored Mar 25, 2024
1 parent 3cab933 commit 924c7de
Show file tree
Hide file tree
Showing 33 changed files with 409 additions and 186 deletions.
63 changes: 49 additions & 14 deletions pallet/assets-ext/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ use sp_std::marker::PhantomData;

use frame_support::traits::{
fungible,
fungibles::{self, Inspect, Transfer, Unbalanced},
tokens::{DepositConsequence, WithdrawConsequence},
fungibles::{self, Inspect, Mutate, Unbalanced},
tokens::{
DepositConsequence, Fortitude, Precision, Preservation, Provenance, WithdrawConsequence,
},
};

use seed_primitives::{AssetId, Balance};
Expand All @@ -48,24 +50,41 @@ where
<pallet_assets::Pallet<T>>::total_issuance(U::get())
}

fn active_issuance() -> Self::Balance {
<pallet_assets::Pallet<T>>::active_issuance(U::get())
}

fn minimum_balance() -> Balance {
<pallet_assets::Pallet<T>>::minimum_balance(U::get())
}

fn total_balance(who: &T::AccountId) -> Self::Balance {
<pallet_assets::Pallet<T>>::total_balance(U::get(), who)
}

fn balance(who: &T::AccountId) -> Balance {
<pallet_assets::Pallet<T>>::balance(U::get(), who)
}

fn reducible_balance(who: &T::AccountId, keep_alive: bool) -> Balance {
fn reducible_balance(
who: &T::AccountId,
preservation: Preservation,
force: Fortitude,
) -> Balance {
<pallet_assets::Pallet<T> as fungibles::Inspect<_>>::reducible_balance(
U::get(),
who,
keep_alive,
preservation,
force,
)
}

fn can_deposit(who: &T::AccountId, amount: Balance, mint: bool) -> DepositConsequence {
<pallet_assets::Pallet<T>>::can_deposit(U::get(), who, amount, mint)
fn can_deposit(
who: &T::AccountId,
amount: Balance,
provenance: Provenance,
) -> DepositConsequence {
<pallet_assets::Pallet<T>>::can_deposit(U::get(), who, amount, provenance)
}

fn can_withdraw(who: &T::AccountId, amount: Balance) -> WithdrawConsequence<Balance> {
Expand All @@ -83,7 +102,12 @@ where
type PositiveImbalance = imbalances::PositiveImbalance<T>;

fn free_balance(who: &T::AccountId) -> Self::Balance {
<pallet_assets::Pallet<T>>::reducible_balance(U::get(), who, false)
<pallet_assets::Pallet<T>>::reducible_balance(
U::get(),
who,
Preservation::Expendable,
Fortitude::Polite,
)
}
fn total_issuance() -> Self::Balance {
<pallet_assets::Pallet<T>>::total_issuance(U::get())
Expand All @@ -101,11 +125,11 @@ where
req: ExistenceRequirement,
) -> DispatchResult {
// used by evm
let keep_alive = match req {
ExistenceRequirement::KeepAlive => true,
ExistenceRequirement::AllowDeath => false,
let preservation = match req {
ExistenceRequirement::KeepAlive => Preservation::Preserve,
ExistenceRequirement::AllowDeath => Preservation::Expendable,
};
<Pallet<T> as Transfer<T::AccountId>>::transfer(U::get(), from, to, value, keep_alive)
<Pallet<T> as Mutate<T::AccountId>>::transfer(U::get(), from, to, value, preservation)
.map(|_| ())
}
fn ensure_can_withdraw(
Expand All @@ -128,10 +152,21 @@ where
who: &T::AccountId,
value: Self::Balance,
_reasons: WithdrawReasons,
_req: ExistenceRequirement,
req: ExistenceRequirement,
) -> Result<Self::NegativeImbalance, DispatchError> {
let preservation = match req {
ExistenceRequirement::KeepAlive => Preservation::Preserve,
ExistenceRequirement::AllowDeath => Preservation::Expendable,
};
// used by pallet-transaction payment & pallet-evm
<pallet_assets::Pallet<T>>::decrease_balance(U::get(), who, value)?;
<pallet_assets::Pallet<T>>::decrease_balance(
U::get(),
who,
value,
Precision::Exact,
preservation,
Fortitude::Polite,
)?;

<Pallet<T>>::deposit_event(Event::InternalWithdraw {
asset_id: U::get(),
Expand All @@ -151,7 +186,7 @@ where
if value.is_zero() {
return Ok(PositiveImbalance::new(0, U::get()))
}
<pallet_assets::Pallet<T>>::increase_balance(U::get(), who, value)?;
<pallet_assets::Pallet<T>>::increase_balance(U::get(), who, value, Precision::Exact)?;
<Pallet<T>>::deposit_event(Event::InternalDeposit {
asset_id: U::get(),
who: who.clone(),
Expand Down
Loading

0 comments on commit 924c7de

Please sign in to comment.