Skip to content

Commit

Permalink
feat: implement native fungible methods
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed Aug 5, 2024
1 parent 5ac5a9a commit 7547313
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pop-api/integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use scale::{Decode, Encode};
use sp_runtime::{traits::Hash, AccountId32, BuildStorage, DispatchError};

use pop_runtime_devnet::{
config::assets::TrustBackedAssetsInstance, Assets, Contracts, Runtime, RuntimeOrigin, System,
UNIT,
config::assets::TrustBackedAssetsInstance, Assets, Balances, Contracts, Runtime, RuntimeOrigin,
System, UNIT,
};

mod local_fungibles;
Expand Down
10 changes: 10 additions & 0 deletions pop-api/integration-tests/src/local_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,16 @@ fn total_supply_works() {
});
}

#[test]
fn native_fungible_total_supply_works() {
new_test_ext().execute_with(|| {
let _ = env_logger::try_init();
let addr = instantiate(CONTRACT, INIT_VALUE, vec![]);
// Tokens in circulation.
assert_eq!(Balances::total_issuance(), total_supply(addr.clone(), 0));
});
}

#[test]
fn balance_of_works() {
new_test_ext().execute_with(|| {
Expand Down
60 changes: 60 additions & 0 deletions pop-api/src/v0/assets/balances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::{
constants::BALANCES,
primitives::{AccountId, Balance},
Result, StatusCode,
};
use constants::*;
use ink::env::chain_extension::ChainExtensionMethod;

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

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

mod constants {
/// - total_issuance
pub const TOTAL_ISSUANCE: u8 = 0;
/// - transfer_keep_alive
pub(super) const TRANSFER_KEEP_ALIVE: u8 = 3;
}

/// Returns the total supply for a native token
///
/// # Returns
/// The total supply of the token, or an error if the operation fails.
#[inline]
pub fn total_issuance() -> Result<Balance> {
build_read_state(TOTAL_ISSUANCE)
.output::<Result<Balance>, true>()
.handle_error_code::<StatusCode>()
.call(&())
}

/// Transfers `value` amount of tokens from the caller's account to account `to`, with additional
/// `data` in unspecified format.
///
/// # Arguments
/// * `to` - The recipient account.
/// * `value` - The number of native tokens to transfer.
///
/// # Returns
/// Returns `Ok(())` if successful, or an error if the transfer fails.
#[inline]
pub fn transfer_keep_alive(target: AccountId, amount: Balance) -> Result<()> {
build_dispatch(TRANSFER_KEEP_ALIVE)
.input::<(AccountId, Balance)>()
.output::<Result<()>, true>()
.handle_error_code::<StatusCode>()
.call(&(target, amount))
}
29 changes: 19 additions & 10 deletions pop-api/src/v0/assets/fungibles.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
assets::balances,
constants::{ASSETS, BALANCES, FUNGIBLES},
primitives::{AccountId, AssetId, Balance},
Result, StatusCode,
Expand Down Expand Up @@ -74,11 +75,15 @@ mod constants {
/// The total supply of the token, or an error if the operation fails.
#[inline]
pub fn total_supply(id: AssetId) -> Result<Balance> {
build_read_state(TOTAL_SUPPLY)
.input::<AssetId>()
.output::<Result<Balance>, true>()
.handle_error_code::<StatusCode>()
.call(&(id))
if id == 0 {
balances::total_issuance()
} else {
build_read_state(TOTAL_SUPPLY)
.input::<AssetId>()
.output::<Result<Balance>, true>()
.handle_error_code::<StatusCode>()
.call(&(id))
}
}

/// Returns the account balance for the specified `owner` for a given asset ID. Returns `0` if
Expand Down Expand Up @@ -130,11 +135,15 @@ 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<()> {
build_dispatch(TRANSFER)
.input::<(AssetId, AccountId, Balance)>()
.output::<Result<()>, true>()
.handle_error_code::<StatusCode>()
.call(&(id, target, amount))
if id == 0 {
balances::transfer_keep_alive(target, amount)
} else {
build_dispatch(TRANSFER)
.input::<(AssetId, AccountId, Balance)>()
.output::<Result<()>, true>()
.handle_error_code::<StatusCode>()
.call(&(id, target, amount))
}
}

/// Transfers `value` tokens on behalf of `from` to account `to` with additional `data`
Expand Down
2 changes: 2 additions & 0 deletions pop-api/src/v0/assets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#[cfg(feature = "fungibles")]
mod balances;
#[cfg(feature = "fungibles")]
pub mod fungibles;

0 comments on commit 7547313

Please sign in to comment.