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+
24use concordium_base:: base:: { AccountIndex , Energy } ;
35use concordium_base:: contracts_common:: AccountAddress ;
4- use concordium_base:: protocol_level_tokens:: RawCbor ;
6+ use concordium_base:: protocol_level_tokens:: TokenModuleEventType ;
57use concordium_base:: transactions:: Memo ;
68
79pub type StateKey = Vec < u8 > ;
810pub 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" ) ]
2630pub 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