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

refactor: build chain extension method #121

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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: 6 additions & 1 deletion pop-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ mod constants {
}

/// Helper method to build `ChainExtensionMethod``
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
pub fn build_extension_method(
///
/// - `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(
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
version: u8,
function: u8,
module: u8,
Expand Down
46 changes: 28 additions & 18 deletions pop-api/src/v0/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use ink::{prelude::vec::Vec, scale::Decode};
use crate::{
constants::{ASSETS, DECODING_FAILED},
primitives::{AccountId, AssetId, Balance},
v0::{build_dispatch, build_read_state},
Result, StatusCode,
};

Expand Down Expand Up @@ -33,6 +32,20 @@ const CANCEL_APPROVAL: u8 = 23;
/// - transfer_approved
const TRANSFER_APPROVED: u8 = 25;

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

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

/// Issue a new class of fungible assets from a public origin.
// pub(crate) fn create(
// id: AssetId,
Expand Down Expand Up @@ -91,15 +104,12 @@ 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_dispatch(
ASSETS,
// E.D. is always respected with transferring tokens via the API.
TRANSFER_KEEP_ALIVE,
)
.input::<(AssetId, AccountId, Balance)>()
.output::<Result<()>, true>()
.handle_error_code::<StatusCode>()
.call(&(id, target, amount))
// E.D. is always respected with transferring tokens via the API.
build_dispatch(TRANSFER_KEEP_ALIVE)
.input::<(AssetId, AccountId, Balance)>()
.output::<Result<()>, true>()
.handle_error_code::<StatusCode>()
.call(&(id, target, amount))
}

// /// Set the metadata for an asset.
Expand All @@ -120,7 +130,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_dispatch(ASSETS, APPROVE_TRANSFER)
build_dispatch(APPROVE_TRANSFER)
.input::<(AssetId, AccountId, Balance)>()
.output::<Result<()>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -130,7 +140,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_dispatch(ASSETS, CANCEL_APPROVAL)
build_dispatch(CANCEL_APPROVAL)
.input::<(AssetId, AccountId)>()
.output::<Result<()>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -146,7 +156,7 @@ pub fn transfer_approved(
to: AccountId,
amount: Balance,
) -> Result<()> {
build_dispatch(ASSETS, TRANSFER_APPROVED)
build_dispatch(TRANSFER_APPROVED)
.input::<(AssetId, AccountId, AccountId, Balance)>()
.output::<Result<()>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -170,7 +180,7 @@ const TOKEN_DECIMALS: u8 = 5;

#[inline]
pub fn total_supply(id: AssetId) -> Result<Balance> {
build_read_state(ASSETS, TOTAL_SUPPLY)
build_read_state(TOTAL_SUPPLY)
.input::<AssetId>()
.output::<Result<Vec<u8>>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -190,7 +200,7 @@ pub fn balance_of(id: AssetId, owner: AccountId) -> Result<Balance> {

#[inline]
pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result<Balance> {
build_read_state(ASSETS, ALLOWANCE)
build_read_state(ALLOWANCE)
.input::<(AssetId, AccountId, AccountId)>()
.output::<Result<Vec<u8>>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -200,7 +210,7 @@ pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result<Ba

#[inline]
pub fn token_name(id: AssetId) -> Result<Vec<u8>> {
build_read_state(ASSETS, TOKEN_NAME)
build_read_state(TOKEN_NAME)
.input::<AssetId>()
.output::<Result<Vec<u8>>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -210,7 +220,7 @@ pub fn token_name(id: AssetId) -> Result<Vec<u8>> {
//
#[inline]
pub fn token_symbol(id: AssetId) -> Result<Vec<u8>> {
build_read_state(ASSETS, TOKEN_SYMBOL)
build_read_state(TOKEN_SYMBOL)
.input::<AssetId>()
.output::<Result<Vec<u8>>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -220,7 +230,7 @@ pub fn token_symbol(id: AssetId) -> Result<Vec<u8>> {

#[inline]
pub fn token_decimals(id: AssetId) -> Result<u8> {
build_read_state(ASSETS, TOKEN_DECIMALS)
build_read_state(TOKEN_DECIMALS)
.input::<AssetId>()
.output::<Result<Vec<u8>>, true>()
.handle_error_code::<StatusCode>()
Expand Down
18 changes: 14 additions & 4 deletions pop-api/src/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ impl From<StatusCode> for Error {
}
}

/// Helper method to build `ChainExtensionMethod``
pub fn build_extension_method_v0(
/// Helper method to build `ChainExtensionMethod` for version `v0`
///
/// - `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_v0(
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
function: u8,
module: u8,
dispatchable: u8,
Expand All @@ -33,11 +37,17 @@ pub fn build_extension_method_v0(
}

/// Helper method to build a dispatch call `ChainExtensionMethod`
pub fn build_dispatch(module: u8, dispatchable: u8) -> ChainExtensionMethod<(), (), (), false> {
///
/// - `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`
pub fn build_read_state(module: u8, dispatchable: u8) -> ChainExtensionMethod<(), (), (), false> {
///
/// - `module`: The index of the runtime module
/// - `dispatchable`: The index of the module dispatchable functions
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
fn build_read_state(module: u8, dispatchable: u8) -> ChainExtensionMethod<(), (), (), false> {
build_extension_method_v0(READ_STATE, module, dispatchable)
}
Loading