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: pallet assets pop api integration #71

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 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
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
7 changes: 4 additions & 3 deletions pop-api/integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(test)]

use frame_support::{
assert_ok,
traits::fungibles::{
approvals::Inspect as ApprovalInspect, metadata::Inspect as MetadataInspect, Inspect,
},
Expand All @@ -17,17 +18,17 @@ use pop_runtime_devnet::{

mod local_fungibles;

type Balance = u128;
type AssetId = u32;
const DEBUG_OUTPUT: pallet_contracts::DebugInfo = pallet_contracts::DebugInfo::UnsafeDebug;
type Balance = u128;

const ALICE: AccountId32 = AccountId32::new([1_u8; 32]);
const BOB: AccountId32 = AccountId32::new([2_u8; 32]);
const DEBUG_OUTPUT: pallet_contracts::DebugInfo = pallet_contracts::DebugInfo::UnsafeDebug;
// FERDIE has no initial balance.
const FERDIE: AccountId32 = AccountId32::new([3_u8; 32]);
const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024);
const INIT_AMOUNT: Balance = 100_000_000 * UNIT;
const INIT_VALUE: Balance = 100 * UNIT;
const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024);

fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Runtime>::default()
Expand Down
78 changes: 34 additions & 44 deletions pop-api/integration-tests/src/local_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,22 @@ fn increase_allowance(
// }

fn create_asset(owner: AccountId32, asset_id: AssetId, min_balance: Balance) -> AssetId {
assert_eq!(
Assets::create(
RuntimeOrigin::signed(owner.clone()),
asset_id.into(),
owner.into(),
min_balance
),
Ok(())
);
assert_ok!(Assets::create(
RuntimeOrigin::signed(owner.clone()),
asset_id.into(),
owner.into(),
min_balance
));
asset_id
}

fn mint_asset(owner: AccountId32, asset_id: AssetId, to: AccountId32, value: Balance) -> AssetId {
assert_eq!(
Assets::mint(RuntimeOrigin::signed(owner.clone()), asset_id.into(), to.into(), value),
Ok(())
);
assert_ok!(Assets::mint(
RuntimeOrigin::signed(owner.clone()),
asset_id.into(),
to.into(),
value
));
asset_id
}

Expand All @@ -178,30 +177,27 @@ fn create_asset_mint_and_approve(
approve: Balance,
) {
create_asset_and_mint_to(owner.clone(), asset_id, to.clone(), mint);
assert_eq!(
Assets::approve_transfer(
RuntimeOrigin::signed(to.into()),
asset_id.into(),
spender.into(),
approve,
),
Ok(())
);
assert_ok!(Assets::approve_transfer(
RuntimeOrigin::signed(to.into()),
asset_id.into(),
spender.into(),
approve,
));
}

// Freeze an asset.
fn freeze_asset(owner: AccountId32, asset_id: AssetId) {
assert_eq!(Assets::freeze_asset(RuntimeOrigin::signed(owner.into()), asset_id.into()), Ok(()));
assert_ok!(Assets::freeze_asset(RuntimeOrigin::signed(owner.into()), asset_id.into()));
}

// Thaw an asset.
fn thaw_asset(owner: AccountId32, asset_id: AssetId) {
assert_eq!(Assets::thaw_asset(RuntimeOrigin::signed(owner.into()), asset_id.into()), Ok(()));
assert_ok!(Assets::thaw_asset(RuntimeOrigin::signed(owner.into()), asset_id.into()));
}

// Start destroying an asset.
fn start_destroy_asset(owner: AccountId32, asset_id: AssetId) {
assert_eq!(Assets::start_destroy(RuntimeOrigin::signed(owner.into()), asset_id.into()), Ok(()));
assert_ok!(Assets::start_destroy(RuntimeOrigin::signed(owner.into()), asset_id.into()));
}

// Create an asset and set metadata.
Expand All @@ -212,15 +208,12 @@ fn create_asset_and_set_metadata(
symbol: Vec<u8>,
decimals: u8,
) {
assert_eq!(
Assets::create(
RuntimeOrigin::signed(owner.clone()),
asset_id.into(),
owner.clone().into(),
100
),
Ok(())
);
assert_ok!(Assets::create(
RuntimeOrigin::signed(owner.clone()),
asset_id.into(),
owner.clone().into(),
100
));
set_metadata_asset(owner, asset_id, name, symbol, decimals);
}

Expand All @@ -232,16 +225,13 @@ fn set_metadata_asset(
symbol: Vec<u8>,
decimals: u8,
) {
assert_eq!(
Assets::set_metadata(
RuntimeOrigin::signed(owner.into()),
asset_id.into(),
name,
symbol,
decimals
),
Ok(())
);
assert_ok!(Assets::set_metadata(
RuntimeOrigin::signed(owner.into()),
asset_id.into(),
name,
symbol,
decimals
));
}

fn token_name_asset(asset_id: AssetId) -> Vec<u8> {
Expand Down
36 changes: 16 additions & 20 deletions pop-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

use ink::env::{chain_extension::FromStatusCode, DefaultEnvironment, Environment};
use primitives::error::Error;
use ink::env::chain_extension::FromStatusCode;

use constants::DECODING_FAILED;

#[cfg(feature = "assets")]
pub use v0::assets;
Expand All @@ -15,18 +16,25 @@ pub use v0::nfts;
pub mod primitives;
pub mod v0;

type AccountId = <DefaultEnvironment as Environment>::AccountId;
type Balance = <DefaultEnvironment as Environment>::Balance;
#[cfg(any(feature = "nfts", feature = "cross-chain"))]
type BlockNumber = <DefaultEnvironment as Environment>::BlockNumber;

pub type Result<T> = core::result::Result<T, StatusCode>;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
pub struct StatusCode(pub u32);
Daanvdplas marked this conversation as resolved.
Show resolved Hide resolved

pub(crate) const DECODING_FAILED: u32 = 255;
mod constants {
// Errors:
pub(crate) const DECODING_FAILED: u32 = 255;
pub(crate) const MODULE_ERROR: u8 = 3;

// Function IDs:
pub(crate) const DISPATCH: u8 = 0;
pub(crate) const READ_STATE: u8 = 1;

// Modules:
pub(crate) const ASSETS_MODULE: u8 = 52;
pub(crate) const BALANCES_MODULE: u8 = 10;
}

impl From<u32> for StatusCode {
Daanvdplas marked this conversation as resolved.
Show resolved Hide resolved
fn from(value: u32) -> Self {
Expand All @@ -47,15 +55,3 @@ impl From<ink::scale::Error> for StatusCode {
StatusCode(DECODING_FAILED)
}
}

impl From<StatusCode> for Error {
fn from(value: StatusCode) -> Self {
value.0.into()
}
}

impl From<Error> for StatusCode {
fn from(value: Error) -> Self {
StatusCode::from(u32::from(value))
}
}
6 changes: 6 additions & 0 deletions pop-api/src/primitives.rs
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
use ink::env::{DefaultEnvironment, Environment};
pub use pop_primitives::*;

pub(crate) type AccountId = <DefaultEnvironment as Environment>::AccountId;
pub(crate) type Balance = <DefaultEnvironment as Environment>::Balance;
#[cfg(any(feature = "nfts", feature = "cross-chain"))]
type BlockNumber = <DefaultEnvironment as Environment>::BlockNumber;
58 changes: 38 additions & 20 deletions pop-api/src/v0/assets/fungibles.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use ink::prelude::vec::Vec;

use crate::{assets, primitives::AssetId, AccountId, Balance, Result, StatusCode};
use crate::{
assets,
constants::{ASSETS_MODULE, BALANCES_MODULE, MODULE_ERROR},
primitives::{AccountId, AssetId, Balance},
Result, StatusCode,
};

/// Local Fungibles:
Daanvdplas marked this conversation as resolved.
Show resolved Hide resolved
/// 1. PSP-22 Interface
Expand Down Expand Up @@ -101,7 +106,7 @@ pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, value: Balance
/// Returns `Ok(())` if successful, or an error if the approval fails.
#[inline]
pub fn approve(id: AssetId, spender: AccountId, value: Balance) -> Result<()> {
Daanvdplas marked this conversation as resolved.
Show resolved Hide resolved
assets::cancel_approval(id, spender)?;
assets::cancel_approval(id, spender.clone())?;
assets::approve_transfer(id, spender, value)
}

Expand Down Expand Up @@ -312,22 +317,23 @@ impl From<StatusCode> for FungiblesError {
let encoded = value.0.to_le_bytes();
match encoded {
// Balances.
[3, 10, 2, _] => FungiblesError::NoBalance,
[MODULE_ERROR, BALANCES_MODULE, 2, _] => FungiblesError::NoBalance,
// Assets.
[3, 52, 0, _] => FungiblesError::NoAccount,
[3, 52, 1, _] => FungiblesError::NoPermission,
[3, 52, 2, _] => FungiblesError::Unknown,
[3, 52, 3, _] => FungiblesError::InUse,
[3, 52, 5, _] => FungiblesError::MinBalanceZero,
[3, 52, 7, _] => FungiblesError::InsufficientAllowance,
[3, 52, 10, _] => FungiblesError::AssetNotLive,
[MODULE_ERROR, ASSETS_MODULE, 0, _] => FungiblesError::NoAccount,
[MODULE_ERROR, ASSETS_MODULE, 1, _] => FungiblesError::NoPermission,
[MODULE_ERROR, ASSETS_MODULE, 2, _] => FungiblesError::Unknown,
[MODULE_ERROR, ASSETS_MODULE, 3, _] => FungiblesError::InUse,
[MODULE_ERROR, ASSETS_MODULE, 5, _] => FungiblesError::MinBalanceZero,
[MODULE_ERROR, ASSETS_MODULE, 7, _] => FungiblesError::InsufficientAllowance,
[MODULE_ERROR, ASSETS_MODULE, 10, _] => FungiblesError::AssetNotLive,
_ => FungiblesError::Other(value),
}
}
}

#[cfg(test)]
mod tests {
use crate::constants::{ASSETS_MODULE, BALANCES_MODULE};
use ink::scale::Decode;

use super::FungiblesError;
Expand Down Expand Up @@ -359,7 +365,7 @@ mod tests {

#[test]
fn conversion_status_code_into_fungibles_error_works() {
let errors = vec![
let other_errors = vec![
Other { dispatch_error_index: 5, error_index: 5, error: 1 },
CannotLookup,
BadOrigin,
Expand All @@ -378,30 +384,42 @@ mod tests {
UnknownFunctionId,
DecodingFailed,
];
for error in errors {
for error in other_errors {
let status_code: StatusCode = error.into();
let fungibles_error: FungiblesError = status_code.into();
assert_eq!(fungibles_error, FungiblesError::Other(status_code))
}

assert_eq!(into_fungibles_error(Module { index: 10, error: 2 }), FungiblesError::NoBalance);
assert_eq!(into_fungibles_error(Module { index: 52, error: 0 }), FungiblesError::NoAccount);
assert_eq!(
into_fungibles_error(Module { index: 52, error: 1 }),
into_fungibles_error(Module { index: BALANCES_MODULE, error: 2 }),
FungiblesError::NoBalance
);
assert_eq!(
into_fungibles_error(Module { index: ASSETS_MODULE, error: 0 }),
FungiblesError::NoAccount
);
assert_eq!(
into_fungibles_error(Module { index: ASSETS_MODULE, error: 1 }),
FungiblesError::NoPermission
);
assert_eq!(into_fungibles_error(Module { index: 52, error: 2 }), FungiblesError::Unknown);
assert_eq!(into_fungibles_error(Module { index: 52, error: 3 }), FungiblesError::InUse);
assert_eq!(
into_fungibles_error(Module { index: 52, error: 5 }),
into_fungibles_error(Module { index: ASSETS_MODULE, error: 2 }),
FungiblesError::Unknown
);
assert_eq!(
into_fungibles_error(Module { index: ASSETS_MODULE, error: 3 }),
FungiblesError::InUse
);
assert_eq!(
into_fungibles_error(Module { index: ASSETS_MODULE, error: 5 }),
FungiblesError::MinBalanceZero
);
assert_eq!(
into_fungibles_error(Module { index: 52, error: 7 }),
into_fungibles_error(Module { index: ASSETS_MODULE, error: 7 }),
FungiblesError::InsufficientAllowance
);
assert_eq!(
into_fungibles_error(Module { index: 52, error: 10 }),
into_fungibles_error(Module { index: ASSETS_MODULE, error: 10 }),
FungiblesError::AssetNotLive
);
}
Expand Down
12 changes: 6 additions & 6 deletions pop-api/src/v0/assets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use ink::{env::chain_extension::ChainExtensionMethod, prelude::vec::Vec, scale::Decode};

use crate::{primitives::AssetId, AccountId, Balance, Result, StatusCode, DECODING_FAILED};
use crate::{
constants::{ASSETS_MODULE, DECODING_FAILED, DISPATCH, READ_STATE},
primitives::{AccountId, AssetId, Balance},
v0::VERSION,
Result, StatusCode,
};

pub mod fungibles;

const ASSETS_MODULE: u8 = 52;
const VERSION: u8 = 0;

/// [Pallet Assets](https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/assets/src/lib.rs):
/// 1. Dispatchables
/// 2. Read state functions
///
/// 1. Dispatchables within pallet assets (TrustBackedAssets instance):
const DISPATCH: u8 = 0;
/// - create
/// - start_destroy
/// - destroy_accounts
Expand Down Expand Up @@ -170,7 +171,6 @@ pub fn transfer_approved(
}

/// 2. Read state functions:
const READ_STATE: u8 = 1;
/// - total_supply
const TOTAL_SUPPLY: u8 = 0;
Daanvdplas marked this conversation as resolved.
Show resolved Hide resolved
/// - balance_of
Expand Down
17 changes: 17 additions & 0 deletions pop-api/src/v0/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use crate::StatusCode;
use pop_primitives::error::Error;

#[cfg(feature = "assets")]
pub mod assets;
#[cfg(feature = "balances")]
Expand All @@ -6,3 +9,17 @@ pub mod balances;
pub mod cross_chain;
#[cfg(feature = "nfts")]
pub mod nfts;

pub(crate) const VERSION: u8 = 0;

impl From<StatusCode> for Error {
fn from(value: StatusCode) -> Self {
value.0.into()
}
}

impl From<Error> for StatusCode {
fn from(value: Error) -> Self {
StatusCode::from(u32::from(value))
}
}
Loading