Skip to content

Commit 1f7d452

Browse files
committed
add more functions to fungibles extension
1 parent cc23d60 commit 1f7d452

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

guest-examples/sum-balance-percent/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ mod sum_balance_percent {
66
type AssetId = u32;
77
type AccountId = [u8; 32];
88
type Balance = u64;
9-
#[program::extension_fn(extension_id = 4071833530116166512u64, fn_index = 1)]
9+
#[program::extension_fn(extension_id = 1248491991627109725u64, fn_index = 6)]
1010
fn balance(asset: AssetId, who: AccountId) -> Balance {}
11-
#[program::extension_fn(extension_id = 4071833530116166512u64, fn_index = 0)]
11+
#[program::extension_fn(extension_id = 1248491991627109725u64, fn_index = 5)]
1212
fn total_supply(asset: AssetId) -> Balance {}
1313

1414
#[program::entrypoint]

guest-examples/sum-balance/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod sum_balance {
2020
}
2121
}
2222

23-
#[program::extension_fn(extension_id = 4071833530116166512u64, fn_index = 1)]
23+
#[program::extension_fn(extension_id = 1248491991627109725u64, fn_index = 6)]
2424
fn balance(asset: AssetId, who: AccountId) -> Balance {}
2525

2626
#[program::entrypoint]

poc/runtime/src/pvq.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#[allow(unused_imports)]
2-
use frame::deps::scale_info::prelude::{format, string::String};
3-
41
use pvq_extension::metadata::Metadata;
52
use pvq_extension::{extensions_impl, ExtensionsExecutor, InvokeSource};
63

74
#[extensions_impl]
85
pub mod extensions {
6+
use frame::deps::scale_info::prelude::vec::Vec;
7+
use frame::token::fungibles;
8+
99
#[extensions_impl::impl_struct]
1010
pub struct ExtensionImpl;
1111

@@ -20,14 +20,29 @@ pub mod extensions {
2020

2121
#[extensions_impl::extension]
2222
impl pvq_extension_fungibles::extension::ExtensionFungibles for ExtensionImpl {
23-
type AccountId = [u8; 32];
23+
type AccountId = crate::interface::AccountId;
2424
type Balance = crate::interface::Balance;
2525
type AssetId = crate::interface::AssetId;
26+
fn name(asset: Self::AssetId) -> Vec<u8> {
27+
<crate::Assets as fungibles::metadata::Inspect<crate::interface::AccountId>>::name(asset)
28+
}
29+
fn symbol(asset: Self::AssetId) -> Vec<u8> {
30+
<crate::Assets as fungibles::metadata::Inspect<crate::interface::AccountId>>::symbol(asset)
31+
}
32+
fn decimals(asset: Self::AssetId) -> u8 {
33+
<crate::Assets as fungibles::metadata::Inspect<crate::interface::AccountId>>::decimals(asset)
34+
}
2635
fn balance(asset: Self::AssetId, who: Self::AccountId) -> Self::Balance {
27-
crate::Assets::balance(asset, crate::interface::AccountId::from(who))
36+
<crate::Assets as fungibles::Inspect<crate::interface::AccountId>>::balance(asset, &who)
2837
}
2938
fn total_supply(asset: Self::AssetId) -> Self::Balance {
30-
crate::Assets::total_supply(asset)
39+
<crate::Assets as fungibles::Inspect<crate::interface::AccountId>>::total_issuance(asset)
40+
}
41+
fn minimum_balance(asset: Self::AssetId) -> Self::Balance {
42+
<crate::Assets as fungibles::Inspect<crate::interface::AccountId>>::minimum_balance(asset)
43+
}
44+
fn asset_exists(asset: Self::AssetId) -> bool {
45+
<crate::Assets as fungibles::Inspect<crate::interface::AccountId>>::asset_exists(asset)
3146
}
3247
}
3348
}

pvq-extension-fungibles/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ use pvq_extension::extension_decl;
33

44
#[extension_decl]
55
pub mod extension {
6+
use scale_info::prelude::vec::Vec;
67
#[extension_decl::extension]
78
pub trait ExtensionFungibles {
89
type AssetId;
910
type Balance;
1011
type AccountId;
12+
fn asset_exists(asset: Self::AssetId) -> bool;
13+
fn name(asset: Self::AssetId) -> Vec<u8>;
14+
fn symbol(asset: Self::AssetId) -> Vec<u8>;
15+
fn decimals(asset: Self::AssetId) -> u8;
16+
fn minimum_balance(asset: Self::AssetId) -> Self::Balance;
1117
fn total_supply(asset: Self::AssetId) -> Self::Balance;
1218
fn balance(asset: Self::AssetId, who: Self::AccountId) -> Self::Balance;
1319
}

pvq-test-runner/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,24 @@ pub mod extensions {
3737
type AssetId = u32;
3838
type AccountId = [u8; 32];
3939
type Balance = u64;
40+
fn name(_asset: Self::AssetId) -> Vec<u8> {
41+
b"Test".to_vec()
42+
}
43+
fn symbol(_asset: Self::AssetId) -> Vec<u8> {
44+
b"TEST".to_vec()
45+
}
46+
fn decimals(_asset: Self::AssetId) -> u8 {
47+
18
48+
}
4049
fn total_supply(_asset: Self::AssetId) -> Self::Balance {
4150
100
4251
}
52+
fn minimum_balance(_asset: Self::AssetId) -> Self::Balance {
53+
100
54+
}
55+
fn asset_exists(_asset: Self::AssetId) -> bool {
56+
true
57+
}
4358
fn balance(_asset: Self::AssetId, _who: Self::AccountId) -> Self::Balance {
4459
100
4560
}
@@ -133,6 +148,9 @@ impl TestRunner {
133148

134149
pub fn expected_result(program_path: &str, chain: &str, entrypoint_idx: u8) -> Vec<u8> {
135150
// TODO: add more entrypoints
151+
if program_path.contains("sum-balance") && chain == "poc" && entrypoint_idx == 0 {
152+
return 1000_000_000u64.encode();
153+
}
136154
if program_path.contains("swap-info") && chain == "ah" && entrypoint_idx == 2 {
137155
return (10_235_709_412_325u128, 12_117_819_770_919u128).encode();
138156
}

0 commit comments

Comments
 (0)