Skip to content

Commit

Permalink
refactor: build functions
Browse files Browse the repository at this point in the history
  • Loading branch information
evilrobot-01 committed Jul 23, 2024
1 parent 5c25ab5 commit ee4d499
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
12 changes: 10 additions & 2 deletions pop-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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.
Expand Down Expand Up @@ -73,3 +72,12 @@ impl From<ink::scale::Error> 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]))
}
10 changes: 0 additions & 10 deletions pop-api/src/utils.rs

This file was deleted.

27 changes: 12 additions & 15 deletions pop-api/src/v0/assets/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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::<Result<()>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -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::<Result<()>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -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::<Result<()>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -173,7 +170,7 @@ const TOKEN_DECIMALS: u8 = 5;

#[inline]
pub fn total_supply(id: AssetId) -> Result<Balance> {
build_extension_method(V0, READ_STATE, ASSETS, TOTAL_SUPPLY)
build_read_state(ASSETS, TOTAL_SUPPLY)
.input::<AssetId>()
.output::<Result<Vec<u8>>, true>()
.handle_error_code::<StatusCode>()
Expand All @@ -183,7 +180,7 @@ pub fn total_supply(id: AssetId) -> Result<Balance> {

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

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

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

#[inline]
pub fn token_decimals(id: AssetId) -> Result<u8> {
build_extension_method(V0, READ_STATE, ASSETS, TOKEN_DECIMALS)
build_read_state(ASSETS, TOKEN_DECIMALS)
.input::<AssetId>()
.output::<Result<Vec<u8>>, true>()
.handle_error_code::<StatusCode>()
Expand Down
18 changes: 18 additions & 0 deletions pop-api/src/v0/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,3 +18,19 @@ impl From<StatusCode> 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)
}

0 comments on commit ee4d499

Please sign in to comment.