diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 918e100c..dc07810e 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -1,6 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -use ink::env::chain_extension::FromStatusCode; +use ink::env::chain_extension::{ChainExtensionMethod, FromStatusCode}; use constants::DECODING_FAILED; @@ -14,7 +14,6 @@ pub use v0::cross_chain; pub use v0::nfts; pub mod primitives; -pub mod utils; pub mod v0; /// A result type used by the API, with the `StatusCode` as the error type. @@ -73,3 +72,12 @@ impl From for StatusCode { StatusCode(DECODING_FAILED) } } + +fn build_extension_method( + version: u8, + function: u8, + module: u8, + dispatchable: u8, +) -> ChainExtensionMethod<(), (), (), false> { + ChainExtensionMethod::build(u32::from_le_bytes([version, function, module, dispatchable])) +} diff --git a/pop-api/src/utils.rs b/pop-api/src/utils.rs deleted file mode 100644 index 59a982f6..00000000 --- a/pop-api/src/utils.rs +++ /dev/null @@ -1,10 +0,0 @@ -use ink::env::chain_extension::ChainExtensionMethod; - -pub(crate) fn build_extension_method( - version: u8, - function: u8, - module: u8, - dispatchable: u8, -) -> ChainExtensionMethod<(), (), (), false> { - ChainExtensionMethod::build(u32::from_le_bytes([version, function, module, dispatchable])) -} diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index 41c1fb6c..9c483392 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -1,10 +1,9 @@ use ink::{prelude::vec::Vec, scale::Decode}; +use super::{build_dispatch, build_read_state}; use crate::{ - constants::{ASSETS, DECODING_FAILED, DISPATCH, READ_STATE}, + constants::{ASSETS, DECODING_FAILED}, primitives::{AccountId, AssetId, Balance}, - utils::build_extension_method, - v0::V0, Result, StatusCode, }; @@ -92,9 +91,7 @@ const TRANSFER_APPROVED: u8 = 25; /// Move some assets from the sender account to another, keeping the sender account alive. #[inline] pub fn transfer_keep_alive(id: AssetId, target: AccountId, amount: Balance) -> Result<()> { - build_extension_method( - V0, - DISPATCH, + build_dispatch( ASSETS, // E.D. is always respected with transferring tokens via the API. TRANSFER_KEEP_ALIVE, @@ -123,7 +120,7 @@ pub fn transfer_keep_alive(id: AssetId, target: AccountId, amount: Balance) -> R /// Approve an amount of asset for transfer by a delegated third-party account. #[inline] pub fn approve_transfer(id: AssetId, delegate: AccountId, amount: Balance) -> Result<()> { - build_extension_method(V0, DISPATCH, ASSETS, APPROVE_TRANSFER) + build_dispatch(ASSETS, APPROVE_TRANSFER) .input::<(AssetId, AccountId, Balance)>() .output::, true>() .handle_error_code::() @@ -133,7 +130,7 @@ pub fn approve_transfer(id: AssetId, delegate: AccountId, amount: Balance) -> Re /// Cancel all of some asset approved for delegated transfer by a third-party account. #[inline] pub fn cancel_approval(id: AssetId, delegate: AccountId) -> Result<()> { - build_extension_method(V0, DISPATCH, ASSETS, CANCEL_APPROVAL) + build_dispatch(ASSETS, CANCEL_APPROVAL) .input::<(AssetId, AccountId)>() .output::, true>() .handle_error_code::() @@ -149,7 +146,7 @@ pub fn transfer_approved( to: AccountId, amount: Balance, ) -> Result<()> { - build_extension_method(V0, DISPATCH, ASSETS, TRANSFER_APPROVED) + build_dispatch(ASSETS, TRANSFER_APPROVED) .input::<(AssetId, AccountId, AccountId, Balance)>() .output::, true>() .handle_error_code::() @@ -173,7 +170,7 @@ const TOKEN_DECIMALS: u8 = 5; #[inline] pub fn total_supply(id: AssetId) -> Result { - build_extension_method(V0, READ_STATE, ASSETS, TOTAL_SUPPLY) + build_read_state(ASSETS, TOTAL_SUPPLY) .input::() .output::>, true>() .handle_error_code::() @@ -183,7 +180,7 @@ pub fn total_supply(id: AssetId) -> Result { #[inline] pub fn balance_of(id: AssetId, owner: AccountId) -> Result { - build_extension_method(V0, READ_STATE, ASSETS, BALANCE_OF) + build_read_state(ASSETS, BALANCE_OF) .input::<(AssetId, AccountId)>() .output::>, true>() .handle_error_code::() @@ -193,7 +190,7 @@ pub fn balance_of(id: AssetId, owner: AccountId) -> Result { #[inline] pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { - build_extension_method(V0, READ_STATE, ASSETS, ALLOWANCE) + build_read_state(ASSETS, ALLOWANCE) .input::<(AssetId, AccountId, AccountId)>() .output::>, true>() .handle_error_code::() @@ -203,7 +200,7 @@ pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result Result> { - build_extension_method(V0, READ_STATE, ASSETS, TOKEN_NAME) + build_read_state(ASSETS, TOKEN_NAME) .input::() .output::>, true>() .handle_error_code::() @@ -213,7 +210,7 @@ pub fn token_name(id: AssetId) -> Result> { // #[inline] pub fn token_symbol(id: AssetId) -> Result> { - build_extension_method(V0, READ_STATE, ASSETS, TOKEN_SYMBOL) + build_read_state(ASSETS, TOKEN_SYMBOL) .input::() .output::>, true>() .handle_error_code::() @@ -223,7 +220,7 @@ pub fn token_symbol(id: AssetId) -> Result> { #[inline] pub fn token_decimals(id: AssetId) -> Result { - build_extension_method(V0, READ_STATE, ASSETS, TOKEN_DECIMALS) + build_read_state(ASSETS, TOKEN_DECIMALS) .input::() .output::>, true>() .handle_error_code::() diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index f7dab6b4..2a971f98 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -1,4 +1,6 @@ +use crate::constants::{DISPATCH, READ_STATE}; use crate::{primitives::error::Error, StatusCode}; +use ink::env::chain_extension::ChainExtensionMethod; #[cfg(feature = "assets")] pub mod assets; @@ -16,3 +18,19 @@ impl From for Error { value.0.into() } } + +fn build_extension_method( + function: u8, + module: u8, + dispatchable: u8, +) -> ChainExtensionMethod<(), (), (), false> { + super::build_extension_method(V0, function, module, dispatchable) +} + +fn build_dispatch(module: u8, dispatchable: u8) -> ChainExtensionMethod<(), (), (), false> { + build_extension_method(DISPATCH, module, dispatchable) +} + +fn build_read_state(module: u8, dispatchable: u8) -> ChainExtensionMethod<(), (), (), false> { + build_extension_method(READ_STATE, module, dispatchable) +}