Skip to content

Commit

Permalink
Use truly free balance (liquid) in chain extension (#501)
Browse files Browse the repository at this point in the history
* use reducible_balance to get free balance

* clean zombienet local command

* add missing import
  • Loading branch information
gianfra-t authored Oct 15, 2024
1 parent 63bd13a commit 3f45706
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions chain-extensions/token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sp-runtime = { git = "https://github.com/paritytech/polkadot-sdk", default-featu
sp-std = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0", default-features = false }
sp-tracing = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.1.0" }
sp-weights = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.1.0" }
pallet-balances = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.1.0" }
pallet-contracts = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.1.0" }

# Open Runtime Module Library
Expand Down Expand Up @@ -56,4 +57,5 @@ std = [
"spacewalk-primitives/std",
"chain-extension-common/std",
"sp-std/std",
"pallet-balances/std"
]
21 changes: 13 additions & 8 deletions chain-extensions/token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use frame_support::{
pallet_prelude::{Decode, Get, PhantomData},
DefaultNoBound,
};
use frame_support::traits::tokens::{fungibles,fungible, Preservation, Fortitude};
use orml_currencies::WeightInfo;
use orml_currencies_allowance_extension::{
default_weights::WeightInfo as AllowanceWeightInfo, Config as AllowanceConfig,
Expand All @@ -16,11 +17,12 @@ use pallet_contracts::chain_extension::{
ChainExtension, Environment, Ext, InitState, RetVal, SysConfig,
};
use sp_core::crypto::UncheckedFrom;
use sp_std::vec::Vec;
use sp_runtime::DispatchError;
use sp_tracing::{error, trace};
use sp_weights::Weight;
use spacewalk_primitives::CurrencyId;

use spacewalk_primitives::{CurrencyId};
use pallet_balances;
pub(crate) type BalanceOfForChainExt<T> =
<<T as orml_currencies::Config>::MultiCurrency as orml_traits::MultiCurrency<
<T as frame_system::Config>::AccountId,
Expand Down Expand Up @@ -69,6 +71,7 @@ where
T: SysConfig
+ orml_tokens::Config<CurrencyId = CurrencyId>
+ pallet_contracts::Config
+ pallet_balances::Config
+ orml_currencies::Config<MultiCurrency = Tokens, AccountId = AccountId>
+ orml_currencies_allowance_extension::Config,
<T as SysConfig>::AccountId: UncheckedFrom<<T as SysConfig>::Hash> + AsRef<[u8]>,
Expand Down Expand Up @@ -154,7 +157,8 @@ where
T: SysConfig
+ orml_tokens::Config<CurrencyId = CurrencyId>
+ pallet_contracts::Config
+ orml_currencies::Config<MultiCurrency = Tokens, AccountId = AccountId>
+ orml_currencies::Config<MultiCurrency = Tokens, AccountId = AccountId>
+ pallet_balances::Config
+ orml_currencies_allowance_extension::Config,
E: Ext<T = T>,
Tokens: orml_traits::MultiCurrency<AccountId, CurrencyId = CurrencyId>,
Expand All @@ -177,12 +181,13 @@ where
return Ok(RetVal::Converging(ChainExtensionTokenError::Unsupported.as_u32()))
}

let balance = <orml_currencies::Pallet<T> as MultiCurrency<T::AccountId>>::free_balance(
currency_id,
&account_id,
);
let balance_encoded: Vec<u8> = if currency_id == T::GetNativeCurrencyId::get() {
<pallet_balances::Pallet<T> as fungible::Inspect<T::AccountId>>::reducible_balance(&account_id, Preservation::Preserve, Fortitude::Polite).encode()
} else {
<orml_tokens::Pallet<T> as fungibles::Inspect<T::AccountId>>::reducible_balance(currency_id, &account_id, Preservation::Preserve, Fortitude::Polite).encode()
};

if let Err(_) = env.write(&balance.encode(), false, None) {
if let Err(_) = env.write(&balance_encoded, false, None) {
return Ok(RetVal::Converging(ChainExtensionOutcome::WriteError.as_u32()))
};
return Ok(RetVal::Converging(ChainExtensionOutcome::Success.as_u32()))
Expand Down
19 changes: 9 additions & 10 deletions zombienet/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ default_command = "../polkadot/target/release/polkadot"
default_args = [ "-lparachain=debug" ]
chain = "rococo-local"

[[relaychain.nodes]]
name = "alice"
validator = true
[[relaychain.nodes]]
name = "alice"
validator = true

[[relaychain.nodes]]
name = "bob"
validator = true
[[relaychain.nodes]]
name = "bob"
validator = true

[[parachains]]
id = 1000
cumulus_based = true
chain = "foucoco" # "pendulum"/"amplitude"/"dev"/"local"

[parachains.collator]
name = "alice"
command = "../pendulum/target/release/pendulum-node"

[parachains.collator]
name = "alice"
command = "../pendulum/target/release/pendulum-node"

0 comments on commit 3f45706

Please sign in to comment.