Skip to content

Commit

Permalink
refactor: rebase main, refactor extension with enums
Browse files Browse the repository at this point in the history
  • Loading branch information
evilrobot-01 authored and Daanvdplas committed Jul 21, 2024
1 parent e097f80 commit c8d8b2a
Show file tree
Hide file tree
Showing 19 changed files with 331 additions and 1,564 deletions.
32 changes: 16 additions & 16 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ substrate-wasm-builder = "18.0.1"
substrate-build-script-utils = "11.0.0"

# Local
pallet-pop-api = { path = "pallets/api", default-features = false }
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 Down
4 changes: 2 additions & 2 deletions pallets/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "pallet-pop-api"
name = "pallet-api"
authors.workspace = true
description = "Pop api pallet, enabling smart contracts to use the power of Polkadot"
description = "Api pallet, enabling smart(er) contracts with the power of Polkadot"
edition.workspace = true
license.workspace = true
version = "0.1.0"
Expand Down
84 changes: 0 additions & 84 deletions pallets/api/src/fungibles.rs

This file was deleted.

22 changes: 0 additions & 22 deletions pallets/api/src/fungibles/benchmarking.rs

This file was deleted.

101 changes: 101 additions & 0 deletions pallets/api/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

#[cfg(test)]
mod tests;

#[frame_support::pallet]
pub mod pallet {
use frame_support::{
dispatch::{DispatchResult, DispatchResultWithPostInfo, WithPostDispatchInfo},
pallet_prelude::*,
traits::fungibles::Inspect,
};
use frame_system::pallet_prelude::*;
use pallet_assets::WeightInfo;
use sp_runtime::traits::StaticLookup;

type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
type AssetIdOf<T> = <pallet_assets::Pallet<T, AssetsInstanceOf<T>> as Inspect<
<T as frame_system::Config>::AccountId,
>>::AssetId;
type AssetIdParameterOf<T> =
<T as pallet_assets::Config<AssetsInstanceOf<T>>>::AssetIdParameter;
type Assets<T> = pallet_assets::Pallet<T, AssetsInstanceOf<T>>;
type AssetsInstanceOf<T> = <T as Config>::AssetsInstance;
type AssetsWeightInfo<T> = <T as pallet_assets::Config<AssetsInstanceOf<T>>>::WeightInfo;
type BalanceOf<T> = <pallet_assets::Pallet<T, AssetsInstanceOf<T>> as Inspect<
<T as frame_system::Config>::AccountId,
>>::Balance;

/// The required input for state queries in pallet assets.
#[derive(Encode, Decode, Debug, MaxEncodedLen)]
pub enum AssetsKeys<T: Config> {
#[codec(index = 0)]
TotalSupply(AssetIdOf<T>),
#[codec(index = 1)]
BalanceOf(AssetIdOf<T>, AccountIdOf<T>),
#[codec(index = 2)]
Allowance(AssetIdOf<T>, AccountIdOf<T>, AccountIdOf<T>),
#[codec(index = 3)]
TokenName(AssetIdOf<T>),
#[codec(index = 4)]
TokenSymbol(AssetIdOf<T>),
#[codec(index = 5)]
TokenDecimals(AssetIdOf<T>),
}

#[pallet::config]
pub trait Config: frame_system::Config + pallet_assets::Config<Self::AssetsInstance> {
type AssetsInstance;
}

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

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(9)]
#[pallet::weight(AssetsWeightInfo::<T>::transfer_keep_alive())]
pub fn transfer(
origin: OriginFor<T>,
id: AssetIdOf<T>,
target: AccountIdOf<T>,
amount: BalanceOf<T>,
) -> DispatchResult {
let target = T::Lookup::unlookup(target);
Assets::<T>::transfer_keep_alive(origin, id.into(), target, amount)
}

#[pallet::call_index(10)]
#[pallet::weight(AssetsWeightInfo::<T>::cancel_approval() + AssetsWeightInfo::<T>::approve_transfer())]
pub fn approve(
origin: OriginFor<T>,
id: AssetIdOf<T>,
spender: AccountIdOf<T>,
value: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let spender = T::Lookup::unlookup(spender);
let id: AssetIdParameterOf<T> = id.into();
Assets::<T>::cancel_approval(origin.clone(), id.clone(), spender.clone())
.map_err(|e| e.with_weight(AssetsWeightInfo::<T>::cancel_approval()))?;
Assets::<T>::approve_transfer(origin, id, spender, value).map_err(|e| {
e.with_weight(
AssetsWeightInfo::<T>::cancel_approval()
+ AssetsWeightInfo::<T>::approve_transfer(),
)
})?;
Ok(().into())
}
}

impl<T: Config> Pallet<T> {
pub fn total_supply(id: AssetIdOf<T>) -> BalanceOf<T> {
Assets::<T>::total_supply(id)
}
pub fn balance_of(id: AssetIdOf<T>, owner: &AccountIdOf<T>) -> BalanceOf<T> {
Assets::<T>::balance(id, owner)
}
}
}
7 changes: 0 additions & 7 deletions pop-api/integration-tests/src/local_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ fn token_decimals_asset(asset_id: AssetId) -> u8 {
/// - decrease_allowance

#[test]
#[ignore]
fn total_supply_works() {
new_test_ext().execute_with(|| {
let _ = env_logger::try_init();
Expand All @@ -285,7 +284,6 @@ fn total_supply_works() {
}

#[test]
#[ignore]
fn balance_of_works() {
new_test_ext().execute_with(|| {
let _ = env_logger::try_init();
Expand All @@ -304,7 +302,6 @@ fn balance_of_works() {
}

#[test]
#[ignore]
fn allowance_works() {
new_test_ext().execute_with(|| {
let _ = env_logger::try_init();
Expand All @@ -329,7 +326,6 @@ fn allowance_works() {
}

#[test]
#[ignore]
fn transfer_works() {
new_test_ext().execute_with(|| {
let _ = env_logger::try_init();
Expand Down Expand Up @@ -382,7 +378,6 @@ fn transfer_works() {
}

#[test]
#[ignore]
fn transfer_from_works() {
new_test_ext().execute_with(|| {
let _ = env_logger::try_init();
Expand Down Expand Up @@ -435,7 +430,6 @@ fn transfer_from_works() {
}

#[test]
#[ignore]
fn increase_allowance_works() {
new_test_ext().execute_with(|| {
let _ = env_logger::try_init();
Expand Down Expand Up @@ -493,7 +487,6 @@ fn increase_allowance_works() {
/// - token_decimals

#[test]
#[ignore]
fn token_metadata_works() {
new_test_ext().execute_with(|| {
let _ = env_logger::try_init();
Expand Down
1 change: 1 addition & 0 deletions pop-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod constants {

// Modules:
pub(crate) const ASSETS: u8 = 52;
pub(crate) const FUNGIBLES: u8 = 150;
pub(crate) const BALANCES: u8 = 10;
}

Expand Down
Loading

0 comments on commit c8d8b2a

Please sign in to comment.