Skip to content

Commit 7005c24

Browse files
committed
update docs
1 parent 7341b75 commit 7005c24

File tree

25 files changed

+467
-67
lines changed

25 files changed

+467
-67
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
1+
//! A guest program that sums the balances of a set of accounts for a given asset and returns the percentage of the total supply.
12
#![no_std]
23
#![no_main]
34

5+
/// The primary module for the sum-balance-percent guest program.
6+
///
7+
/// This module defines the necessary types, extension functions, and the main entrypoint
8+
/// for calculating the percentage of total supply represented by the sum of balances
9+
/// for a given set of accounts.
410
#[pvq_program::program]
511
mod sum_balance_percent {
12+
/// A type alias for the asset identifier.
613
type AssetId = u32;
14+
/// A type alias for the account identifier.
715
type AccountId = [u8; 32];
16+
/// A type alias for the balance of an account.
817
type Balance = u64;
18+
19+
/// Retrieves the balance of a specific account for a given asset.
20+
///
21+
/// This is an extension function that calls into the runtime.
922
#[program::extension_fn(extension_id = 1248491991627109725u64, fn_index = 6)]
1023
fn balance(asset: AssetId, who: AccountId) -> Balance {}
24+
25+
/// Retrieves the total supply of a given asset.
26+
///
27+
/// This is an extension function that calls into the runtime.
1128
#[program::extension_fn(extension_id = 1248491991627109725u64, fn_index = 5)]
1229
fn total_supply(asset: AssetId) -> Balance {}
1330

31+
/// The entrypoint of the program.
32+
///
33+
/// It takes an asset ID and a vector of account IDs, calculates the sum of their balances,
34+
/// and returns the percentage of this sum with respect to the total supply of the asset.
1435
#[program::entrypoint]
1536
fn sum_balance(asset: AssetId, accounts: alloc::vec::Vec<AccountId>) -> Balance {
1637
let mut sum_balance = 0;

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,59 @@
1+
//! A guest program that sums the balance of a given asset for a list of accounts.
12
#![no_std]
23
#![no_main]
34

5+
/// A guest program that sums the balance of a given asset for a list of accounts.
46
#[pvq_program::program]
57
mod sum_balance {
68

79
cfg_if::cfg_if! {
810
if #[cfg(feature = "option_version_1")] {
11+
/// Represents a unique identifier for an account.
912
type AccountId = [u8; 64];
13+
/// Represents a unique identifier for an asset.
1014
type AssetId = u64;
15+
/// Represents the balance of an asset.
1116
type Balance = u128;
1217
} else if #[cfg(feature = "option_version_2")] {
18+
/// Represents a unique identifier for an account.
1319
type AccountId = [u8; 32];
20+
/// Represents a unique identifier for an asset.
1421
type AssetId = u32;
22+
/// Represents the balance of an asset.
1523
type Balance = u64;
1624
} else {
25+
/// Represents a unique identifier for an account.
1726
type AccountId = [u8; 32];
27+
/// Represents a unique identifier for an asset.
1828
type AssetId = u32;
29+
/// Represents the balance of an asset.
1930
type Balance = u64;
2031
}
2132
}
2233

34+
/// Get the balance of a given asset for a specific account.
35+
///
36+
/// # Arguments
37+
///
38+
/// * `asset`: The identifier of the asset.
39+
/// * `who`: The account identifier.
40+
///
41+
/// # Returns
42+
///
43+
/// The balance of the asset for the specified account.
2344
#[program::extension_fn(extension_id = 1248491991627109725u64, fn_index = 6)]
2445
fn balance(asset: AssetId, who: AccountId) -> Balance {}
2546

47+
/// Sums the balance of a given asset for a list of accounts.
48+
///
49+
/// # Arguments
50+
///
51+
/// * `asset`: The identifier of the asset.
52+
/// * `accounts`: A list of account identifiers.
53+
///
54+
/// # Returns
55+
///
56+
/// The total balance of the asset for all the specified accounts.
2657
#[program::entrypoint]
2758
fn sum_balance(asset: AssetId, accounts: alloc::vec::Vec<AccountId>) -> Balance {
2859
let mut sum = 0;

guest-examples/swap-info/src/main.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
1+
//! A guest program that provides information about asset swaps.
12
#![no_std]
23
#![no_main]
34

5+
/// A guest program that provides information about asset swaps.
46
#[pvq_program::program]
57
mod swap_info {
68

9+
/// Represents a unique identifier for an asset.
710
type AssetId = alloc::vec::Vec<u8>;
11+
/// Represents the balance of an asset.
812
type Balance = u128;
13+
/// Represents information about an asset.
914
#[derive(
1015
Debug, Clone, PartialEq, Eq, parity_scale_codec::Encode, parity_scale_codec::Decode, scale_info::TypeInfo,
1116
)]
1217
pub struct AssetInfo {
18+
/// The unique identifier of the asset.
1319
pub asset_id: AssetId,
20+
/// The name of the asset.
1421
pub name: alloc::vec::Vec<u8>,
22+
/// The symbol of the asset.
1523
pub symbol: alloc::vec::Vec<u8>,
24+
/// The number of decimals the asset has.
1625
pub decimals: u8,
1726
}
1827

28+
/// Get the quote price of `asset1` in terms of `asset2` for a given amount of `asset2`.
29+
///
30+
/// # Arguments
31+
///
32+
/// * `asset1`: The identifier of the asset to be quoted.
33+
/// * `asset2`: The identifier of the asset to quote against.
34+
/// * `amount`: The amount of `asset2`.
35+
/// * `include_fee`: Whether to include the fee in the quote.
36+
///
37+
/// # Returns
38+
///
39+
/// The quote price of `asset1` in terms of `asset2`, or `None` if the quote is not available.
1940
#[program::extension_fn(extension_id = 15900548380266538526u64, fn_index = 0)]
2041
fn quote_price_tokens_for_exact_tokens(
2142
asset1: AssetId,
@@ -25,6 +46,18 @@ mod swap_info {
2546
) -> Option<Balance> {
2647
}
2748

49+
/// Get the quote price of `asset2` in terms of `asset1` for a given amount of `asset1`.
50+
///
51+
/// # Arguments
52+
///
53+
/// * `asset1`: The identifier of the asset to quote against.
54+
/// * `asset2`: The identifier of the asset to be quoted.
55+
/// * `amount`: The amount of `asset1`.
56+
/// * `include_fee`: Whether to include the fee in the quote.
57+
///
58+
/// # Returns
59+
///
60+
/// The quote price of `asset2` in terms of `asset1`, or `None` if the quote is not available.
2861
#[program::extension_fn(extension_id = 15900548380266538526u64, fn_index = 1)]
2962
fn quote_price_exact_tokens_for_tokens(
3063
asset1: AssetId,
@@ -34,18 +67,58 @@ mod swap_info {
3467
) -> Option<Balance> {
3568
}
3669

70+
/// Get the liquidity pool of two assets.
71+
///
72+
/// # Arguments
73+
///
74+
/// * `asset1`: The identifier of the first asset.
75+
/// * `asset2`: The identifier of the second asset.
76+
///
77+
/// # Returns
78+
///
79+
/// A tuple containing the balance of each asset in the liquidity pool, or `None` if the pool does not exist.
3780
#[program::extension_fn(extension_id = 15900548380266538526u64, fn_index = 2)]
3881
fn get_liquidity_pool(asset1: AssetId, asset2: AssetId) -> Option<(Balance, Balance)> {}
3982

83+
/// List all available liquidity pools.
84+
///
85+
/// # Returns
86+
///
87+
/// A list of tuples, where each tuple represents a liquidity pool and contains the identifiers of the two assets in the pool.
4088
#[program::extension_fn(extension_id = 15900548380266538526u64, fn_index = 3)]
4189
fn list_pools() -> alloc::vec::Vec<(AssetId, AssetId)> {}
4290

91+
/// Get information about a specific asset.
92+
///
93+
/// # Arguments
94+
///
95+
/// * `asset`: The identifier of the asset.
96+
///
97+
/// # Returns
98+
///
99+
/// Information about the asset, or `None` if the asset does not exist.
43100
#[program::extension_fn(extension_id = 15900548380266538526u64, fn_index = 4)]
44101
fn asset_info(asset: AssetId) -> Option<AssetInfo> {}
45102

103+
/// Get information about all assets.
104+
///
105+
/// # Returns
106+
///
107+
/// A map of asset identifiers to asset information.
46108
#[program::extension_fn(extension_id = 15900548380266538526u64, fn_index = 5)]
47109
fn assets_info() -> alloc::collections::BTreeMap<AssetId, AssetInfo> {}
48110

111+
/// Get the quote price of `asset2` in terms of `asset1` for a given amount of `asset1`.
112+
///
113+
/// # Arguments
114+
///
115+
/// * `asset1`: The identifier of the asset to quote against.
116+
/// * `asset2`: The identifier of the asset to be quoted.
117+
/// * `amount`: The amount of `asset1`.
118+
///
119+
/// # Returns
120+
///
121+
/// The quote price of `asset2` in terms of `asset1`, or `None` if the quote is not available.
49122
#[program::entrypoint]
50123
fn entrypoint_quote_price_exact_tokens_for_tokens(
51124
asset1: AssetId,
@@ -55,6 +128,17 @@ mod swap_info {
55128
quote_price_exact_tokens_for_tokens(asset1, asset2, amount, true)
56129
}
57130

131+
/// Get the quote price of `asset1` in terms of `asset2` for a given amount of `asset2`.
132+
///
133+
/// # Arguments
134+
///
135+
/// * `asset1`: The identifier of the asset to be quoted.
136+
/// * `asset2`: The identifier of the asset to quote against.
137+
/// * `amount`: The amount of `asset2`.
138+
///
139+
/// # Returns
140+
///
141+
/// The quote price of `asset1` in terms of `asset2`, or `None` if the quote is not available.
58142
#[program::entrypoint]
59143
fn entrypoint_quote_price_tokens_for_exact_tokens(
60144
asset1: AssetId,
@@ -64,11 +148,26 @@ mod swap_info {
64148
quote_price_tokens_for_exact_tokens(asset1, asset2, amount, true)
65149
}
66150

151+
/// Get the liquidity pool of two assets.
152+
///
153+
/// # Arguments
154+
///
155+
/// * `asset1`: The identifier of the first asset.
156+
/// * `asset2`: The identifier of the second asset.
157+
///
158+
/// # Returns
159+
///
160+
/// A tuple containing the balance of each asset in the liquidity pool, or `None` if the pool does not exist.
67161
#[program::entrypoint]
68162
fn entrypoint_get_liquidity_pool(asset1: AssetId, asset2: AssetId) -> Option<(Balance, Balance)> {
69163
get_liquidity_pool(asset1, asset2)
70164
}
71165

166+
/// List all available liquidity pools with their asset information.
167+
///
168+
/// # Returns
169+
///
170+
/// A list of tuples, where each tuple represents a liquidity pool and contains the information of the two assets in the pool.
72171
#[program::entrypoint]
73172
fn entrypoint_list_pools() -> alloc::vec::Vec<(AssetInfo, AssetInfo)> {
74173
let pools = list_pools();

guest-examples/total-supply/src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
1+
//! A guest program that queries the total supply of a given asset.
12
#![no_std]
23
#![no_main]
34

5+
/// A guest program that queries the total supply of a given asset.
46
#[pvq_program::program]
57
mod query_total_supply {
8+
/// Get the total supply of a given asset.
9+
///
10+
/// # Arguments
11+
///
12+
/// * `asset`: The identifier of the asset.
13+
///
14+
/// # Returns
15+
///
16+
/// The total supply of the asset.
617
#[program::extension_fn(extension_id = 4071833530116166512u64, fn_index = 0)]
718
fn total_supply(asset: u32) -> u64 {}
819

20+
/// Get the total supply of a given asset.
21+
///
22+
/// # Arguments
23+
///
24+
/// * `asset`: The identifier of the asset.
25+
///
26+
/// # Returns
27+
///
28+
/// The total supply of the asset.
929
#[program::entrypoint]
1030
fn get_total_supply(asset: u32) -> u64 {
1131
total_supply(asset)

pvq-executor/src/context.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
use polkavm::Linker;
22

3+
/// The context for the PVQ executor.
34
pub trait PvqExecutorContext {
5+
/// The user data.
46
type UserData;
7+
/// The user error.
58
type UserError;
9+
/// Registers the host functions.
610
fn register_host_functions(&mut self, linker: &mut Linker<Self::UserData, Self::UserError>);
11+
/// Returns a mutable reference to the user data.
712
fn data(&mut self) -> &mut Self::UserData;
813
}

pvq-executor/src/error.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
use pvq_primitives::PvqError;
2+
/// The error type for the PVQ executor.
23
#[derive(Debug, thiserror::Error)]
34
pub enum PvqExecutorError<UserError> {
5+
/// The program format is invalid.
46
#[error("Invalid PVQ program format")]
57
InvalidProgramFormat,
8+
/// A memory access error occurred.
69
#[error("Memory access error: {0}")]
710
MemoryAccessError(polkavm::MemoryAccessError),
8-
// Extract from the PVM CallError
11+
/// A trap occurred during execution.
912
#[error("Trap")]
1013
Trap,
11-
// Extract from the PVM CallError
14+
/// Not enough gas to execute the program.
1215
#[error("Not enough gas")]
1316
NotEnoughGas,
14-
// Usually a custom error type from the extension system definition
17+
/// A user-defined error occurred.
1518
#[error("User error: {0}")]
1619
User(UserError),
17-
// Other errors directly from the PVM
20+
/// An other error from the PolkaVM occurred.
1821
#[error("Other PVM error: {0}")]
1922
OtherPvmError(polkavm::Error),
2023
}

pvq-executor/src/executor.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@ use polkavm::{Config, Engine, Linker, Module, ModuleConfig, ProgramBlob};
44
use crate::context::PvqExecutorContext;
55
use crate::error::PvqExecutorError;
66

7+
/// The result of a PVQ execution.
78
type PvqExecutorResult<UserError> = Result<Vec<u8>, PvqExecutorError<UserError>>;
9+
/// The gas limit for a PVQ execution.
810
type GasLimit = Option<i64>;
911

12+
/// The PVQ executor.
1013
pub struct PvqExecutor<Ctx: PvqExecutorContext> {
1114
engine: Engine,
1215
linker: Linker<Ctx::UserData, Ctx::UserError>,
1316
context: Ctx,
1417
}
1518

1619
impl<Ctx: PvqExecutorContext> PvqExecutor<Ctx> {
20+
/// Creates a new PVQ executor.
21+
///
22+
/// # Arguments
23+
///
24+
/// * `config`: The PolkaVM configuration.
25+
/// * `context`: The PVQ executor context.
1726
pub fn new(config: Config, mut context: Ctx) -> Self {
1827
let engine = Engine::new(&config).unwrap();
1928
let mut linker = Linker::<Ctx::UserData, Ctx::UserError>::new();
@@ -26,6 +35,17 @@ impl<Ctx: PvqExecutorContext> PvqExecutor<Ctx> {
2635
}
2736
}
2837

38+
/// Executes a PVQ program.
39+
///
40+
/// # Arguments
41+
///
42+
/// * `program`: The PVQ program to execute.
43+
/// * `args`: The arguments to pass to the program.
44+
/// * `gas_limit`: The gas limit for the execution.
45+
///
46+
/// # Returns
47+
///
48+
/// A tuple containing the result of the execution and the remaining gas.
2949
pub fn execute(
3050
&mut self,
3151
program: &[u8],

pvq-executor/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! The PVQ executor.
12
#![cfg_attr(not(feature = "std"), no_std)]
23

34
extern crate alloc;

0 commit comments

Comments
 (0)