Skip to content

Commit

Permalink
refactor: build chain extension method (#121)
Browse files Browse the repository at this point in the history
Co-authored-by: Daan van der Plas <[email protected]>
Co-authored-by: Daanvdplas <[email protected]>
  • Loading branch information
3 people committed Sep 6, 2024
1 parent 25d6351 commit e4d05bf
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
18 changes: 18 additions & 0 deletions pop-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

use ink::env::chain_extension::{ChainExtensionMethod, FromStatusCode};

use constants::DECODING_FAILED;
use ink::env::chain_extension::FromStatusCode;
#[cfg(feature = "assets")]
Expand Down Expand Up @@ -27,6 +29,22 @@ mod constants {
pub(crate) const FUNGIBLES: u8 = 150;
}

/// Helper method to build `ChainExtensionMethod`.
///
/// Parameters:
/// - 'version': The version of the chain extension
/// - 'function': The ID of the function
/// - 'module': The index of the runtime module
/// - 'dispatchable': The index of the module dispatchable functions
fn build_extension_method(
version: u8,
function: u8,
module: u8,
dispatchable: u8,
) -> ChainExtensionMethod<(), (), (), false> {
ChainExtensionMethod::build(u32::from_le_bytes([version, function, module, dispatchable]))
}

/// Represents a status code returned by the runtime.
///
/// `StatusCode` encapsulates a `u32` value that indicates the status of an operation performed
Expand Down
38 changes: 27 additions & 11 deletions pop-api/src/v0/assets/fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ use crate::{
use constants::*;
pub use metadata::*;

/// Helper method to build a dispatch call `ChainExtensionMethod` for fungibles `v0`
///
/// Parameters:
/// - 'dispatchable': The index of the module dispatchable functions
fn build_dispatch(dispatchable: u8) -> ChainExtensionMethod<(), (), (), false> {
crate::v0::build_dispatch(FUNGIBLES, dispatchable)
}

/// Helper method to build a dispatch call `ChainExtensionMethod` for fungibles `v0``
///
/// Parameters:
/// - 'state_query': The index of the runtime state query
fn build_read_state(state_query: u8) -> ChainExtensionMethod<(), (), (), false> {
crate::v0::build_read_state(FUNGIBLES, state_query)
}

/// Local Fungibles:
/// 1. PSP-22 Interface
/// 2. PSP-22 Metadata Interface
Expand Down Expand Up @@ -60,7 +76,7 @@ mod constants {
/// The total supply of the token, or an error if the operation fails.
#[inline]
pub fn total_supply(id: AssetId) -> Result<Balance> {
ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, TOTAL_SUPPLY]))
build_read_state(TOTAL_SUPPLY)
.input::<AssetId>()
.output::<Result<Vec<u8>>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -79,7 +95,7 @@ pub fn total_supply(id: AssetId) -> Result<Balance> {
/// The balance of the specified account, or an error if the operation fails.
#[inline]
pub fn balance_of(id: AssetId, owner: AccountId) -> Result<Balance> {
ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, BALANCE_OF]))
build_read_state(BALANCE_OF)
.input::<(AssetId, AccountId)>()
.output::<Result<Vec<u8>>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -99,7 +115,7 @@ pub fn balance_of(id: AssetId, owner: AccountId) -> Result<Balance> {
/// The remaining allowance, or an error if the operation fails.
#[inline]
pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result<Balance> {
ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, ALLOWANCE]))
build_read_state(ALLOWANCE)
.input::<(AssetId, AccountId, AccountId)>()
.output::<Result<Vec<u8>>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -119,7 +135,7 @@ pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result<Ba
/// Returns `Ok(())` if successful, or an error if the transfer fails.
#[inline]
pub fn transfer(id: AssetId, target: AccountId, amount: Balance) -> Result<()> {
ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, FUNGIBLES, TRANSFER]))
build_dispatch(TRANSFER)
.input::<(AssetId, AccountId, Balance)>()
.output::<Result<()>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -140,7 +156,7 @@ pub fn transfer(id: AssetId, target: AccountId, amount: Balance) -> Result<()> {
/// Returns `Ok(())` if successful, or an error if the transfer fails.
#[inline]
pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, amount: Balance) -> Result<()> {
ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, FUNGIBLES, TRANSFER_FROM]))
build_dispatch(TRANSFER_FROM)
.input::<(AssetId, AccountId, AccountId, Balance)>()
.output::<Result<()>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -158,7 +174,7 @@ pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, amount: Balanc
/// Returns `Ok(())` if successful, or an error if the approval fails.
#[inline]
pub fn approve(id: AssetId, spender: AccountId, amount: Balance) -> Result<()> {
ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, FUNGIBLES, APPROVE]))
build_dispatch(APPROVE)
.input::<(AssetId, AccountId, Balance)>()
.output::<Result<()>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -176,7 +192,7 @@ pub fn approve(id: AssetId, spender: AccountId, amount: Balance) -> Result<()> {
/// Returns `Ok(())` if successful, or an error if the operation fails.
#[inline]
pub fn increase_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> {
ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, FUNGIBLES, INCREASE_ALLOWANCE]))
build_dispatch(INCREASE_ALLOWANCE)
.input::<(AssetId, AccountId, Balance)>()
.output::<Result<()>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -194,7 +210,7 @@ pub fn increase_allowance(id: AssetId, spender: AccountId, value: Balance) -> Re
/// Returns `Ok(())` if successful, or an error if the operation fails.
#[inline]
pub fn decrease_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> {
ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, FUNGIBLES, DECREASE_ALLOWANCE]))
build_dispatch(DECREASE_ALLOWANCE)
.input::<(AssetId, AccountId, Balance)>()
.output::<Result<()>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -212,7 +228,7 @@ pub mod metadata {
/// The name of the token as a byte vector, or an error if the operation fails.
#[inline]
pub fn token_name(id: AssetId) -> Result<Vec<u8>> {
ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, TOKEN_NAME]))
build_read_state(TOKEN_NAME)
.input::<AssetId>()
.output::<Result<Vec<u8>>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -229,7 +245,7 @@ pub mod metadata {
/// The symbol of the token as a byte vector, or an error if the operation fails.
#[inline]
pub fn token_symbol(id: AssetId) -> Result<Vec<u8>> {
ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, TOKEN_SYMBOL]))
build_read_state(TOKEN_SYMBOL)
.input::<AssetId>()
.output::<Result<Vec<u8>>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -246,7 +262,7 @@ pub mod metadata {
/// The number of decimals of the token as a byte vector, or an error if the operation fails.
#[inline]
pub fn token_decimals(id: AssetId) -> Result<u8> {
ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, TOKEN_DECIMALS]))
build_read_state(TOKEN_DECIMALS)
.input::<AssetId>()
.output::<Result<Vec<u8>>, true>()
.handle_error_code::<StatusCode>()
Expand Down
1 change: 1 addition & 0 deletions pop-api/src/v0/assets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#[cfg(feature = "fungibles")]
pub mod fungibles;

26 changes: 25 additions & 1 deletion pop-api/src/v0/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use crate::{primitives::error::Error, StatusCode};
use crate::{
build_extension_method,
constants::{DISPATCH, READ_STATE},
primitives::error::Error,
StatusCode,
};
use ink::env::chain_extension::ChainExtensionMethod;

#[cfg(feature = "assets")]
pub mod assets;
Expand All @@ -10,3 +16,21 @@ impl From<StatusCode> for Error {
value.0.into()
}
}

/// Helper method to build a dispatch call `ChainExtensionMethod`
///
/// Parameters:
/// - 'module': The index of the runtime module
/// - 'dispatchable': The index of the module dispatchable functions
fn build_dispatch(module: u8, dispatchable: u8) -> ChainExtensionMethod<(), (), (), false> {
build_extension_method(V0, DISPATCH, module, dispatchable)
}

/// Helper method to build a dispatch call `ChainExtensionMethod`
///
/// Parameters:
/// - 'module': The index of the runtime module
/// - 'state_query': The index of the runtime state query
fn build_read_state(module: u8, state_query: u8) -> ChainExtensionMethod<(), (), (), false> {
build_extension_method(V0, READ_STATE, module, state_query)
}

0 comments on commit e4d05bf

Please sign in to comment.