Skip to content

Commit

Permalink
feat: pop api fungibles pallet
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanvdplas committed Jul 19, 2024
1 parent e683e16 commit 360bb2a
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 16 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 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
50 changes: 50 additions & 0 deletions pallets/fungibles/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[package]
name = "pallet-fungibles"
authors = ["Anonymous"]
description = "Frame Pallet"
version = "0.1.0"
license = "Unlicense"
edition = "2021"

[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

[dev-dependencies]
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",
"sp-runtime/runtime-benchmarks",
]
std = [
"codec/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"pallet-assets/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
]
22 changes: 22 additions & 0 deletions pallets/fungibles/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! Benchmarking setup for pallet-parachain-template
// https://docs.substrate.io/test/benchmark/

use super::*;

#[allow(unused)]
use crate::Pallet as Template;
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_system::RawOrigin;

benchmarks! {
do_something {
let s in 0 .. 100;
let caller: T::AccountId = whitelisted_caller();
}: _(RawOrigin::Signed(caller), s)
verify {
assert_eq!(Something::<T>::get(), Some(s));

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

View workflow job for this annotation

GitHub Actions / clippy

failed to resolve: use of undeclared type `Something`

error[E0433]: failed to resolve: use of undeclared type `Something` --> pallets/fungibles/src/benchmarking.rs:18:14 | 18 | assert_eq!(Something::<T>::get(), Some(s)); | ^^^^^^^^^ use of undeclared type `Something`
}
}

impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test,);
64 changes: 64 additions & 0 deletions pallets/fungibles/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

#[frame_support::pallet]
pub mod pallet {
use frame_support::{pallet_prelude::*, traits::fungibles::Inspect};
use frame_system::pallet_prelude::*;
use sp_runtime::traits::StaticLookup;

type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
type AssetIdOf<T> = <pallet_assets::Pallet<T, TrustBackedAssetsInstance> as Inspect<
<T as frame_system::Config>::AccountId,
>>::AssetId;
type AssetIdParameterOf<T> =
<T as pallet_assets::Config<TrustBackedAssetsInstance>>::AssetIdParameter;
type BalanceOf<T> = <pallet_assets::Pallet<T, TrustBackedAssetsInstance> as Inspect<
<T as frame_system::Config>::AccountId,
>>::Balance;
// Should be defined in primitives.
type TrustBackedAssetsInstance = pallet_assets::Instance1;

#[pallet::config]
pub trait Config:
frame_system::Config + pallet_assets::Config<TrustBackedAssetsInstance>
{
}

#[pallet::pallet]
pub struct Pallet<T>(_);

use pallet_assets::WeightInfo;

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(<T as pallet_assets::Config<TrustBackedAssetsInstance>>::WeightInfo::transfer_keep_alive())]
pub fn transfer(
origin: OriginFor<T>,
id: AssetIdParameterOf<T>,
target: AccountIdLookupOf<T>,
amount: BalanceOf<T>,
) -> DispatchResult {
pallet_assets::Pallet::<T, TrustBackedAssetsInstance>::transfer_keep_alive(
origin, id, target, amount,
)
}
}

impl<T: Config> Pallet<T> {
pub fn total_supply(id: AssetIdOf<T>) -> BalanceOf<T> {
pallet_assets::Pallet::<T, TrustBackedAssetsInstance>::total_supply(id)
}
}
}
60 changes: 60 additions & 0 deletions pallets/fungibles/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use frame_support::{parameter_types, traits::Everything};
use frame_system as system;
use sp_core::H256;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
};

type Block = frame_system::mocking::MockBlock<Test>;

// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
pub enum Test
{
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
fungibles: crate::{Pallet, Call, Storage, Event<T>},
}
);

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const SS58Prefix: u8 = 42;
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl system::Config for Test {
type BaseCallFilter = Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Nonce = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = SS58Prefix;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

impl crate::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
system::GenesisConfig::<Test>::default().build_storage().unwrap().into()
}
22 changes: 22 additions & 0 deletions pallets/fungibles/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::{mock::*, Error};
use frame_support::{assert_noop, assert_ok};

// https://docs.substrate.io/test/

#[test]
fn it_works_for_default_value() {
new_test_ext().execute_with(|| {
// Dispatch a signed extrinsic.
assert_ok!(fungibles::do_something(RuntimeOrigin::signed(1), 42));
// Read pallet storage and assert an expected result.
assert_eq!(fungibles::something(), Some(42));
});
}

#[test]
fn correct_error_for_none_value() {
new_test_ext().execute_with(|| {
// Ensure the expected error is thrown when no value is present.
assert_noop!(fungibles::cause_error(RuntimeOrigin::signed(1)), Error::<Test>::NoneValue);
});
}
2 changes: 2 additions & 0 deletions runtime/devnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ smallvec.workspace = true
# Local
pop-primitives = { workspace = true, features = ["assets", "cross-chain", "nfts"] }
pop-runtime-common = { workspace = true, default-features = false }
pallet-fungibles = { path = "../../pallets/fungibles/", default-features = false }

# Substrate
frame-benchmarking.workspace = true
Expand Down Expand Up @@ -119,6 +120,7 @@ std = [
"pallet-balances/std",
"pallet-collator-selection/std",
"pallet-contracts/std",
"pallet-fungibles/std",
"pallet-message-queue/std",
"pallet-multisig/std",
"pallet-nft-fractionalization/std",
Expand Down
2 changes: 2 additions & 0 deletions runtime/devnet/src/config/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,5 @@ impl pallet_assets::Config<TrustBackedAssetsInstance> for Runtime {
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

impl pallet_fungibles::Config for Runtime {}
5 changes: 3 additions & 2 deletions runtime/devnet/src/extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fn construct_call(
match pallet_index {
index if index == super::Assets::index() as u8 => {
let call = versioned_construct_assets_call(version, call_index, params)?;
Ok(RuntimeCall::Assets(call))
Ok(RuntimeCall::Fungibles(call))
},
_ => Err(DispatchError::Other("UnknownFunctionId")),
}
Expand All @@ -199,7 +199,8 @@ fn versioned_construct_assets_call(
version: u8,
call_index: u8,
params: Vec<u8>,
) -> Result<pallet_assets::Call<Runtime, TrustBackedAssetsInstance>, DispatchError> {
// ) -> Result<pallet_assets::Call<Runtime, TrustBackedAssetsInstance>, DispatchError> {
) -> Result<pallet_fungibles::Call<Runtime>, DispatchError> {
match version {
V0 => v0::assets::construct_assets_call(call_index, params),
_ => Err(DispatchError::Other("UnknownFunctionId")),
Expand Down
30 changes: 18 additions & 12 deletions runtime/devnet/src/extensions/v0/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,33 @@ pub(crate) fn construct_assets_key(
pub(crate) fn construct_assets_call(
call_index: u8,
params: Vec<u8>,
) -> Result<pallet_assets::Call<Runtime, TrustBackedAssetsInstance>, DispatchError> {
// ) -> Result<pallet_assets::Call<Runtime, TrustBackedAssetsInstance>, DispatchError> {
) -> Result<pallet_fungibles::Call<Runtime>, DispatchError> {
match call_index {
9 => {
let (id, target, amount) = <(AssetId, AccountId32, Balance)>::decode(&mut &params[..])
.map_err(|_| DispatchError::Other("DecodingFailed"))?;
Ok(pallet_assets::Call::<Runtime, TrustBackedAssetsInstance>::transfer_keep_alive {
// Ok(pallet_assets::Call::<Runtime, TrustBackedAssetsInstance>::transfer_keep_alive {
// id: Compact(id),
// target: MultiAddress::Id(target),
// amount,
// })
Ok(pallet_fungibles::Call::<Runtime>::transfer {
id: Compact(id),
target: MultiAddress::Id(target),
amount,
})
},
22 => {
let (id, delegate, amount) =
<(AssetId, AccountId32, Balance)>::decode(&mut &params[..])
.map_err(|_| DispatchError::Other("DecodingFailed"))?;
Ok(pallet_assets::Call::<Runtime, TrustBackedAssetsInstance>::approve_transfer {
id: Compact(id),
delegate: MultiAddress::Id(delegate),
amount,
})
},
// 22 => {
// let (id, delegate, amount) =
// <(AssetId, AccountId32, Balance)>::decode(&mut &params[..])
// .map_err(|_| DispatchError::Other("DecodingFailed"))?;
// Ok(pallet_assets::Call::<Runtime, TrustBackedAssetsInstance>::approve_transfer {
// id: Compact(id),
// delegate: MultiAddress::Id(delegate),
// amount,
// })
// },
// other calls
_ => Err(DispatchError::Other("UnknownFunctionId")),
}
Expand Down
5 changes: 3 additions & 2 deletions runtime/devnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl Contains<RuntimeCall> for FilteredCalls {

/// A type to identify allowed calls to the Runtime from contracts. Used by Pop API
pub struct AllowedApiCalls;
impl Contains<RuntimeCall> for crate::AllowedApiCalls {
impl Contains<RuntimeCall> for AllowedApiCalls {
fn contains(c: &RuntimeCall) -> bool {
use config::assets::AssetsCall;
use pallet_nfts::Call as NftsCall;
Expand Down Expand Up @@ -321,7 +321,7 @@ impl Contains<RuntimeCall> for crate::AllowedApiCalls {
| NftsCall::create_swap { .. }
| NftsCall::cancel_swap { .. }
| NftsCall::claim_swap { .. }
)
) | RuntimeCall::Fungibles(pallet_fungibles::Call::transfer { .. })
)
}
}
Expand Down Expand Up @@ -663,6 +663,7 @@ construct_runtime!(
Nfts: pallet_nfts = 50,
NftFractionalization: pallet_nft_fractionalization = 51,
Assets: pallet_assets::<Instance1> = 52,
Fungibles: pallet_fungibles = 53,
}
);

Expand Down

0 comments on commit 360bb2a

Please sign in to comment.