Skip to content

Commit e04a66a

Browse files
authored
Merge pull request #1483 from Concordium/ar/psr-22-implement-token-module-queries
Ar/psr 22 implement token module queries
2 parents 660fe2b + a418c8f commit e04a66a

File tree

8 files changed

+515
-482
lines changed

8 files changed

+515
-482
lines changed

plt/plt-scheduler/src/block_state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ impl BlockStateOperations for BlockState {
2929
fn get_token_circulating_supply(
3030
&self,
3131
_token_index: crate::TokenIndex,
32-
) -> plt_token_module::host_interface::TokenRawAmount {
32+
) -> plt_token_module::token_kernel_interface::RawTokenAmount {
3333
todo!()
3434
}
3535

3636
fn set_token_circulating_supply(
3737
&mut self,
3838
_token_index: crate::TokenIndex,
39-
_circulating_supply: plt_token_module::host_interface::TokenRawAmount,
39+
_circulating_supply: plt_token_module::token_kernel_interface::RawTokenAmount,
4040
) {
4141
todo!()
4242
}

plt/plt-scheduler/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use concordium_base::base::{AccountIndex, Energy};
22
use concordium_base::id::types::AccountAddress;
33
use concordium_base::protocol_level_tokens::TokenId;
4-
use plt_token_module::host_interface::TokenRawAmount;
4+
use plt_token_module::token_kernel_interface::RawTokenAmount;
55

66
mod block_state;
77
#[cfg(feature = "ffi")]
@@ -73,7 +73,7 @@ pub trait BlockStateOperations {
7373
/// # Panics
7474
///
7575
/// Panics if the token identified by `token_index` does not exist.
76-
fn get_token_circulating_supply(&self, token_index: TokenIndex) -> TokenRawAmount;
76+
fn get_token_circulating_supply(&self, token_index: TokenIndex) -> RawTokenAmount;
7777

7878
/// Set the recorded total circulating supply for a protocol-level token.
7979
///
@@ -90,7 +90,7 @@ pub trait BlockStateOperations {
9090
fn set_token_circulating_supply(
9191
&mut self,
9292
token_index: TokenIndex,
93-
circulating_supply: TokenRawAmount,
93+
circulating_supply: RawTokenAmount,
9494
);
9595

9696
/// Create a new token with the given configuration. The initial state will be empty

plt/plt-token-module/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
pub mod host_interface;
1+
pub mod token_kernel_interface;
22
pub mod token_module;

plt/plt-token-module/src/host_interface.rs renamed to plt/plt-token-module/src/token_kernel_interface.rs

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
//! Host interface for protocol-level tokens.
1+
//! Token kernel interface for protocol-level tokens. The kernel handles all operations affecting token
2+
//! balance and supply and manages the state and events related to balances and supply.
3+
24
use concordium_base::base::{AccountIndex, Energy};
35
use concordium_base::contracts_common::AccountAddress;
4-
use concordium_base::protocol_level_tokens::RawCbor;
6+
use concordium_base::protocol_level_tokens::TokenModuleEventType;
57
use concordium_base::transactions::Memo;
68

79
pub type StateKey = Vec<u8>;
810
pub type StateValue = Vec<u8>;
9-
pub type TokenEventType = String;
10-
pub type TokenEventDetails = RawCbor;
11-
pub type Parameter = RawCbor;
12-
pub type TokenRawAmount = u64;
11+
12+
/// Token amount without decimals specified. The token amount represented by
13+
/// this type must always be represented with the number of decimals
14+
/// the token natively has.
15+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Default)]
16+
pub struct RawTokenAmount(pub u64);
1317

1418
/// The account has insufficient balance.
1519
#[derive(Debug)]
@@ -25,10 +29,13 @@ pub struct LockedStateKeyError;
2529
#[error("Amount not representable")]
2630
pub struct AmountNotRepresentableError;
2731

28-
/// Operations provided by the deployment unit host.
29-
///
30-
/// This is abstracted in a trait to allow for a testing stub.
31-
pub trait HostOperations {
32+
/// Energy limit for execution reached.
33+
#[derive(Debug, thiserror::Error)]
34+
#[error("Out of energy")]
35+
pub struct OutOfEnergyError;
36+
37+
/// Queries provided by the token kernel.
38+
pub trait TokenKernelQueries {
3239
/// The type for the account object.
3340
///
3441
/// The account is guaranteed to exist on chain, when holding an instance of this type.
@@ -48,8 +55,20 @@ pub trait HostOperations {
4855
fn account_canonical_address(&self, account: &Self::Account) -> AccountAddress;
4956

5057
/// Get the token balance of the account.
51-
fn account_balance(&self, account: &Self::Account) -> TokenRawAmount;
58+
fn account_balance(&self, account: &Self::Account) -> RawTokenAmount;
59+
60+
/// The current token circulation supply.
61+
fn circulating_supply(&self) -> RawTokenAmount;
5262

63+
/// The number of decimals used in the presentation of the token amount.
64+
fn decimals(&self) -> u8;
65+
66+
/// Lookup a key in the token state.
67+
fn get_token_state(&self, key: StateKey) -> Option<StateValue>;
68+
}
69+
70+
/// Operations provided by the token kernel.
71+
pub trait TokenKernelOperations: TokenKernelQueries {
5372
/// Update the balance of the given account to zero if it didn't have a balance before.
5473
///
5574
/// Returns `true` if the balance wasn't present on the given account and `false` otherwise.
@@ -67,7 +86,7 @@ pub trait HostOperations {
6786
fn mint(
6887
&mut self,
6988
account: &Self::Account,
70-
amount: TokenRawAmount,
89+
amount: RawTokenAmount,
7190
) -> Result<(), AmountNotRepresentableError>;
7291

7392
/// Burn a specified amount from the account.
@@ -82,7 +101,7 @@ pub trait HostOperations {
82101
fn burn(
83102
&mut self,
84103
account: &Self::Account,
85-
amount: TokenRawAmount,
104+
amount: RawTokenAmount,
86105
) -> Result<(), InsufficientBalanceError>;
87106

88107
/// Transfer a token amount from one account to another, with an optional memo.
@@ -98,19 +117,10 @@ pub trait HostOperations {
98117
&mut self,
99118
from: &Self::Account,
100119
to: &Self::Account,
101-
amount: TokenRawAmount,
120+
amount: RawTokenAmount,
102121
memo: Option<Memo>,
103122
) -> Result<(), InsufficientBalanceError>;
104123

105-
/// The current token circulation supply.
106-
fn circulating_supply(&self) -> TokenRawAmount;
107-
108-
/// The number of decimals used in the presentation of the token amount.
109-
fn decimals(&self) -> u8;
110-
111-
/// Lookup a key in the token state.
112-
fn get_token_state(&self, key: StateKey) -> Option<StateValue>;
113-
114124
/// Set or clear a value in the token state at the corresponding key.
115125
///
116126
/// Returns whether there was an existing entry.
@@ -126,15 +136,16 @@ pub trait HostOperations {
126136

127137
/// Reduce the available energy for the PLT module execution.
128138
///
129-
/// If the available energy is smaller than the given amount, the containing transaction will
130-
/// abort and the effects of the transaction will be rolled back.
139+
/// If the available energy is smaller than the given amount, an
140+
/// "out of energy" error will be returned, in which case the caller
141+
/// should stop execution and propagate the error upwards.
131142
/// The energy is charged in any case (also in case of failure).
132-
fn tick_energy(&mut self, energy: Energy);
143+
fn tick_energy(&mut self, energy: Energy) -> Result<(), OutOfEnergyError>;
133144

134145
/// Log a token module event with the specified type and details.
135146
///
136147
/// # Events
137148
///
138149
/// This will produce a `TokenModuleEvent` in the logs.
139-
fn log_token_event(&mut self, event_type: TokenEventType, event_details: TokenEventDetails);
150+
fn log_token_event(&mut self, event: TokenModuleEventType);
140151
}

0 commit comments

Comments
 (0)