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

Use truly free balance (liquid) in chain extension #501

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading