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

feat: add native token support for fungibles api #147

Open
wants to merge 50 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
7c2eac2
feat: pallet assets pop api integration
Daanvdplas Apr 5, 2024
418aa63
style: renaming assets example
Daanvdplas Apr 12, 2024
3d385df
refactor: chain extension tests
Daanvdplas Apr 22, 2024
fd46a27
fix: add error handling other than ModuleError
Daanvdplas Jun 4, 2024
1022926
test: add transfer
Daanvdplas Jun 6, 2024
ecc82e2
chore: remove polkadot launch
Daanvdplas Jun 6, 2024
e81f78a
# This is a combination of 8 commits.
Daanvdplas Jun 6, 2024
f824801
refactor: error handling
Daanvdplas Jun 20, 2024
4670503
refactor: clarify todos
Daanvdplas Jun 20, 2024
2283161
refactor: error handling comments part 1
Daanvdplas Jun 24, 2024
f88dd89
refactor: apply comments part 2
Daanvdplas Jun 25, 2024
61b3a17
refactor: tests local fungibles
Daanvdplas Jun 26, 2024
c1616a3
refactor: naming pallet error
Daanvdplas Jun 26, 2024
13e947e
refactor: optimising pop api and additional logic + test
Daanvdplas Jun 26, 2024
7757319
refactor: runtime
Daanvdplas Jul 3, 2024
924df85
refactor: draft final design
Daanvdplas Jul 2, 2024
972e4c9
feat: add more interfaces
Daanvdplas Jul 5, 2024
47e2ea6
refactor: final bits
Daanvdplas Jul 5, 2024
9dcfccb
fix: compiling
Daanvdplas Jul 6, 2024
b067eda
fix: cannot find macro vec error
Daanvdplas Jul 16, 2024
8730f90
refactor: decoding failed and commenting out unimplemented tests
Daanvdplas Jul 17, 2024
b591222
style: renaming
Daanvdplas Jul 17, 2024
c2acbb5
refactor: bits and pieces
Daanvdplas Jul 18, 2024
3639320
docs: add docs to code
Daanvdplas Jul 18, 2024
81e9844
fix: replace test code to api fungibles
Daanvdplas Jul 18, 2024
023d3dc
fix: own review + fixing CI fail
Daanvdplas Jul 18, 2024
e683e16
refactor: add Error variants index + clippy allowance
Daanvdplas Jul 19, 2024
28d971b
feat: api fungibles pallet (#113)
Daanvdplas Jul 26, 2024
4e4512e
refactor: streamline error from the decoded method by returning `Resu…
chungquantin Jul 26, 2024
02dffc6
merge main
Daanvdplas Jul 26, 2024
027cb85
refactor: build chain extension method (#121)
chungquantin Jul 26, 2024
30ff91a
fix: clean up imports (#135)
chungquantin Jul 27, 2024
9820e7f
refactor: remove read state encoding (#122)
Daanvdplas Jul 28, 2024
6f01940
feat: add native token support in transfer()
chungquantin Jul 30, 2024
8c0d4c3
fix: transfer_works() contract integration test
chungquantin Jul 30, 2024
d5034bc
refactor: migrate parameter type to support native
chungquantin Jul 30, 2024
773dd4e
chore: return error from method if not supported
chungquantin Jul 30, 2024
5ac5a9a
feat: transfer_from and decrease_allowance (#134)
chungquantin Jul 31, 2024
2564a88
Merge branch 'daan/api' into chungquantin/feat-native_token
chungquantin Jul 31, 2024
2e9b73e
fix: transfer_from and decrease_allowance test
chungquantin Jul 31, 2024
52f37fb
test: aadd native_fungible_transfer_works()
chungquantin Jul 31, 2024
58178e8
test: add unsupported methods test cases
chungquantin Jul 31, 2024
05dbd15
test: contract test for native unsupported method
chungquantin Jul 31, 2024
955547b
test: add native total supply test
chungquantin Aug 1, 2024
e042435
feat: add benchmarking
chungquantin Aug 2, 2024
7834611
feat: add benchmarking
chungquantin Aug 2, 2024
151ae33
refactor: fork union_of and update for pop case
chungquantin Aug 2, 2024
4316605
fix: benchmarking and generate weight
chungquantin Aug 5, 2024
da7485b
fix: comments
chungquantin Aug 5, 2024
293ea2e
test: add test case of native fungible transfer
chungquantin Aug 5, 2024
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,737 changes: 892 additions & 845 deletions Cargo.lock

Large diffs are not rendered by default.

36 changes: 19 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = [
"runtime/devnet",
"runtime/testnet",
"integration-tests",
"pallets/*",
"primitives",
"scripts/fund-dev-accounts",
]
Expand Down Expand Up @@ -47,10 +48,11 @@ subxt-signer = "0.34.0"
tokio = { version = "1.36", features = ["macros", "time", "rt-multi-thread"] }

# Build
substrate-wasm-builder = "18.0.0"
substrate-wasm-builder = "18.0.1"
substrate-build-script-utils = "11.0.0"

# Local
pallet-api = { path = "pallets/api", default-features = false }
pop-runtime-devnet = { path = "runtime/devnet", default-features = true } # default-features=true required for `-p pop-node` builds
pop-runtime-testnet = { path = "runtime/testnet", default-features = true } # default-features=true required for `-p pop-node` builds
pop-runtime-common = { path = "runtime/common", default-features = false }
Expand All @@ -76,15 +78,15 @@ sc-transaction-pool-api = "29.0.0"
frame-benchmarking = { version = "29.0.0", default-features = false }
frame-benchmarking-cli = "33.0.0"
frame-executive = { version = "29.0.0", default-features = false }
frame-support = { version = "29.0.0", default-features = false }
frame-support = { version = "29.0.2", default-features = false }
frame-system = { version = "29.0.0", default-features = false }
frame-system-benchmarking = { version = "29.0.0", default-features = false }
frame-system-rpc-runtime-api = { version = "27.0.0", default-features = false }
frame-try-runtime = { version = "0.35.0", default-features = false }
pallet-aura = { version = "28.0.0", default-features = false }
pallet-authorship = { version = "29.0.0", default-features = false }
pallet-assets = { version = "30.0.0", default-features = false }
pallet-balances = { version = "29.0.0", default-features = false }
pallet-balances = { version = "29.0.2", default-features = false }
pallet-contracts = { version = "28.0.0", default-features = false }
pallet-message-queue = { version = "32.0.0", default-features = false }
pallet-multisig = { version = "29.0.0", default-features = false }
Expand All @@ -97,11 +99,11 @@ pallet-scheduler = { version = "30.0.0", default-features = false }
pallet-session = { version = "29.0.0", default-features = false }
pallet-sudo = { version = "29.0.0", default-features = false }
pallet-timestamp = { version = "28.0.0", default-features = false }
pallet-transaction-payment = { version = "29.0.0", default-features = false }
pallet-transaction-payment = { version = "29.0.2", default-features = false }
pallet-transaction-payment-rpc = "31.0.0"
pallet-transaction-payment-rpc-runtime-api = { version = "29.0.0", default-features = false }
pallet-utility = { version = "29.0.0", default-features = false }
sp-api = { version = "27.0.0", default-features = false }
sp-api = { version = "27.0.1", default-features = false }
sp-authority-discovery = { version = "27.0.0", default-features = false }
sp-block-builder = { version = "27.0.0", default-features = false }
sp-blockchain = "29.0.0"
Expand All @@ -125,20 +127,20 @@ sp-transaction-pool = { version = "27.0.0", default-features = false }
sp-version = { version = "30.0.0", default-features = false }

# Polkadot
pallet-xcm = { version = "=8.0.1", default-features = false }
pallet-xcm = { version = "8.0.5", default-features = false }
polkadot-cli = "8.0.0"
polkadot-parachain-primitives = { version = "7.0.0", default-features = false }
polkadot-runtime-parachains = { version = "8.0.1", default-features = false }
polkadot-runtime-parachains = { version = "8.0.3", default-features = false }
polkadot-primitives = { version = "8.0.1", default-features = false }
polkadot-runtime-common = { version = "8.0.1", default-features = false }
polkadot-runtime-common = { version = "8.0.3", default-features = false }
rococo-runtime-constants = { version = "8.0.0", default-features = false }
rococo-runtime = { version = "8.0.0", default-features = false }
xcm = { package = "staging-xcm", version = "8.0.1", default-features = false }
xcm-builder = { package = "staging-xcm-builder", version = "8.0.1", default-features = false }
xcm-executor = { package = "staging-xcm-executor", version = "8.0.1", default-features = false }
rococo-runtime = { version = "8.0.1", default-features = false }
xcm = { version = "8.0.1", package = "staging-xcm", default-features = false }
xcm-builder = { version = "8.0.3", package = "staging-xcm-builder", default-features = false }
xcm-executor = { version = "8.0.2", package = "staging-xcm-executor", default-features = false }

# Cumulus
asset-hub-rococo-runtime = { version = "0.12.0", default-features = false }
asset-hub-rococo-runtime = { version = "0.12.3", default-features = false }
asset-test-utils = { version = "8.0.1", default-features = false }
cumulus-pallet-aura-ext = { version = "0.8.0", default-features = false }
cumulus-pallet-parachain-system = { version = "0.8.1", default-features = false, features = ["parameterized-consensus-hook"] }
Expand All @@ -149,9 +151,9 @@ cumulus-primitives-aura = { version = "0.8.0", default-features = false }
cumulus-primitives-core = { version = "0.8.0", default-features = false }
cumulus-primitives-utility = { version = "0.8.1", default-features = false }
emulated-integration-tests-common = { version = "4.0.0", default-features = false }
pallet-collator-selection = { version = "10.0.0", default-features = false }
parachains-common = { version = "8.0.0", default-features = false }
parachain-info = { package = "staging-parachain-info", version = "0.8.0", default-features = false }
pallet-collator-selection = { version = "10.0.3", default-features = false }
parachains-common = { version = "8.0.1", default-features = false }
parachain-info = { version = "0.8.0", package = "staging-parachain-info", default-features = false }
cumulus-primitives-parachain-inherent = "0.8.0"
cumulus-relay-chain-interface = "0.8.0"
color-print = "0.3.4"
Expand All @@ -160,4 +162,4 @@ cumulus-client-collator = "0.8.0"
cumulus-client-consensus-aura = "0.8.0"
cumulus-client-consensus-common = "0.8.0"
cumulus-client-consensus-proposer = "0.8.0"
cumulus-client-service = "0.8.0"
cumulus-client-service = "0.8.0"
13 changes: 2 additions & 11 deletions node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,9 @@ impl RuntimeResolver for PathBuf {

fn load_spec(id: &str) -> std::result::Result<Box<dyn ChainSpec>, String> {
Ok(match id {
#[cfg(not(feature = "paseo"))]
"dev-rococo" => Box::new(chain_spec::development_config(Relay::RococoLocal)),
#[cfg(feature = "paseo")]
"dev-paseo" => Box::new(chain_spec::development_config(Relay::PaseoLocal)),
#[cfg(not(feature = "paseo"))]
"pop-rococo" => Box::new(chain_spec::testnet_config(Relay::Rococo)),
#[cfg(feature = "paseo")]
"pop-paseo" => Box::new(chain_spec::testnet_config(Relay::Paseo)),
#[cfg(feature = "paseo")]
"dev" | "dev-paseo" => Box::new(chain_spec::development_config(Relay::PaseoLocal)),
"test" | "pop-paseo" => Box::new(chain_spec::testnet_config(Relay::Paseo)),
"" | "local" => Box::new(chain_spec::development_config(Relay::PaseoLocal)),
#[cfg(not(feature = "paseo"))]
"" | "local" => Box::new(chain_spec::development_config(Relay::RococoLocal)),
path => {
let path: PathBuf = path.into();
match path.runtime() {
Expand Down
55 changes: 55 additions & 0 deletions pallets/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
[package]
name = "pallet-api"
authors.workspace = true
description = "Api pallet, enabling smart(er) contracts with the power of Polkadot"
edition.workspace = true
license.workspace = true
version = "0.1.0"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec.workspace = true
scale-info.workspace = true

# Substrate
frame-benchmarking.workspace = true
frame-support.workspace = true
frame-system.workspace = true
pallet-assets.workspace = true
sp-runtime.workspace = true
sp-std.workspace = true

[dev-dependencies]
pallet-balances.workspace = true
sp-core.workspace = true
sp-io.workspace = true

[features]
default = ["std"]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
std = [
"codec/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"pallet-assets/std",
"pallet-balances/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
]
106 changes: 106 additions & 0 deletions pallets/api/src/fungibles/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//! Benchmarking setup for pallet-api::fungibles

use super::{AccountIdOf, AssetIdOf, AssetsInstanceOf, AssetsOf, BalanceOf, Call, Config, Pallet};
use frame_benchmarking::{account, v2::*};
use frame_support::{
assert_ok,
traits::{
fungibles::{
approvals::{Inspect as ApprovalInspect, Mutate},
Create, Inspect,
},
Currency,
},
};
use frame_system::RawOrigin;
use sp_runtime::traits::Zero;

const SEED: u32 = 1;

// See if `generic_event` has been emitted.
fn assert_has_event<T: Config>(
generic_event: <T as pallet_assets::Config<AssetsInstanceOf<T>>>::RuntimeEvent,
) {
frame_system::Pallet::<T>::assert_has_event(generic_event.into());
}

#[benchmarks(
where
<pallet_assets::Pallet<T, AssetsInstanceOf<T>> as Inspect<<T as frame_system::Config>::AccountId>>::AssetId: Zero,
)]
mod benchmarks {
use super::*;

// Parameter:
// - 'a': whether `approve_transfer` is required.
// - 'c': whether `cancel_approval` is required.
#[benchmark]
fn approve(a: Linear<0, 1>, c: Linear<0, 1>) -> Result<(), BenchmarkError> {
let asset_id = AssetIdOf::<T>::zero();
let min_balance = <BalanceOf<T>>::from(1u32);
let owner: AccountIdOf<T> = account("Alice", 0, SEED);
let spender: AccountIdOf<T> = account("Bob", 0, SEED);
let current_allowance = <BalanceOf<T>>::from(u32::MAX / 2);
T::Currency::make_free_balance_be(&owner, u32::MAX.into());
// Set the `current_allowance`.
assert_ok!(<AssetsOf<T> as Create<AccountIdOf<T>>>::create(
asset_id.clone(),
owner.clone(),
true,
min_balance
));
assert_ok!(<AssetsOf<T> as Mutate<AccountIdOf<T>>>::approve(
asset_id.clone(),
&owner,
&spender,
current_allowance,
));
let approval_value = match (a, c) {
// Equal to the current allowance.
(0, 0) => current_allowance,
// Greater than the current allowance.
(1, 0) => <BalanceOf<T>>::from(u32::MAX),
// Zero.
(0, 1) => <BalanceOf<T>>::from(0u32),
// Smaller than the current allowance.
(1, 1) => <BalanceOf<T>>::from(u32::MAX / 4),
_ => unreachable!("values can only be 0 or 1"),
};

#[extrinsic_call]
_(RawOrigin::Signed(owner.clone()), asset_id.clone(), spender.clone(), approval_value);

Check failure on line 71 in pallets/api/src/fungibles/benchmarking.rs

View workflow job for this annotation

GitHub Actions / clippy

mismatched types

error[E0308]: mismatched types --> pallets/api/src/fungibles/benchmarking.rs:71:39 | 27 | / #[benchmarks( 28 | | where 29 | | <pallet_assets::Pallet<T, AssetsInstanceOf<T>> as Inspect<<T as frame_system::Config>::AccountId>>::AssetId: Zero, 30 | | )] | |__- arguments to this function are incorrect ... 71 | _(RawOrigin::Signed(owner.clone()), asset_id.clone(), spender.clone(), approval_value); | ^^^^^^^^^^^^^^^^ expected `fungibles::pallet::Config::AssetKind`, found `pallet_assets::Config::AssetId` | = note: expected associated type `<T as fungibles::pallet::Config>::AssetKind` found associated type `<T as pallet_assets::Config<<T as fungibles::pallet::Config>::AssetsInstance>>::AssetId` = note: an associated type was expected, but a different one was found note: associated function defined here --> pallets/api/src/fungibles/mod.rs:177:10 | 177 | pub fn approve( | ^^^^^^^ 178 | origin: OriginFor<T>, 179 | asset: T::AssetKind, | -------------------

assert_eq!(AssetsOf::<T>::allowance(asset_id.clone(), &owner, &spender), approval_value);
if c == 1 {
assert_has_event::<T>(
pallet_assets::Event::ApprovalCancelled {
asset_id: asset_id.clone(),
owner: owner.clone(),
delegate: spender.clone(),
}
.into(),
);
}
if a == 1 {
let amount = match c {
// When the allowance was cancelled and then approved with the new value.
1 => approval_value,
// When the allowance was increased.
0 => approval_value - current_allowance,
_ => unreachable!("`c` can only be 0 or 1"),
};
assert_has_event::<T>(
pallet_assets::Event::ApprovedTransfer {
asset_id,
source: owner,
delegate: spender,
amount,
}
.into(),
);
}
Ok(())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
}
Loading
Loading