|
| 1 | +// Placeholder types to be defined or replaced with types from concordium-base. |
| 2 | + |
| 3 | +type MutableTokenState = (); |
| 4 | +type PLTConfiguration = (); |
| 5 | +type TokenRawAmount = (); |
| 6 | +type TokenAmountDelta = (); |
| 7 | +type TokenIndex = (); |
| 8 | +type AccountIndex = (); |
| 9 | +type TokenId = (); |
| 10 | + |
| 11 | +/// Operations on the state of a block in the chain. |
| 12 | +trait BlockStateOperations { |
| 13 | + // Protocol-level token state query interface. |
| 14 | + |
| 15 | + /// Get the [`TokenId`]s of all protocol-level tokens registered on the chain. |
| 16 | + /// |
| 17 | + /// If the protocol version does not support protocol-level tokens, this will return the empty |
| 18 | + /// list. |
| 19 | + fn get_plt_list(&self) -> impl std::iter::Iterator<Item = TokenId>; |
| 20 | + |
| 21 | + /// Get the [`TokenIndex`] associated with a [`TokenId`] (if it exists). |
| 22 | + /// |
| 23 | + /// # Arguments |
| 24 | + /// |
| 25 | + /// - `token_index` The token index to update. |
| 26 | + fn get_token_index(&self, token_id: TokenId) -> Option<TokenIndex>; |
| 27 | + |
| 28 | + /// Convert a persistent state to a mutable one that can be updated by the scheduler. |
| 29 | + /// |
| 30 | + /// Updates to this state will only persist in the block state using [`BlockStateOperaionts::set_token_state`]. |
| 31 | + /// |
| 32 | + /// # Arguments |
| 33 | + /// |
| 34 | + /// - `token_index` The index of the token to get the state from. |
| 35 | + /// |
| 36 | + /// # Panics |
| 37 | + /// |
| 38 | + /// Panics if the token identified by `token_index` does not exist. |
| 39 | + fn get_mutable_token_state(&self, token_index: TokenIndex) -> MutableTokenState; |
| 40 | + |
| 41 | + /// Get the configuration of a protocol-level token. |
| 42 | + /// |
| 43 | + /// # Arguments |
| 44 | + /// |
| 45 | + /// - `token_index` The index of the token to get the config for. |
| 46 | + /// |
| 47 | + /// # Panics |
| 48 | + /// |
| 49 | + /// Panics if the token identified by `token_index` does not exist. |
| 50 | + fn get_token_configuration(&self, token_index: TokenIndex) -> PLTConfiguration; |
| 51 | + |
| 52 | + /// Get the circulating supply of a protocol-level token. |
| 53 | + /// |
| 54 | + /// # Arguments |
| 55 | + /// |
| 56 | + /// - `token_index` The index of the token to get the circulating supply. |
| 57 | + /// |
| 58 | + /// # Panics |
| 59 | + /// |
| 60 | + /// Panics if the token identified by `token_index` does not exist. |
| 61 | + fn get_token_circulating_supply(&self, token_index: TokenIndex) -> TokenRawAmount; |
| 62 | + |
| 63 | + /// Set the recorded total circulating supply for a protocol-level token. |
| 64 | + /// |
| 65 | + /// This should always be kept up-to-date with the total balance held in accounts. |
| 66 | + /// |
| 67 | + /// # Arguments |
| 68 | + /// |
| 69 | + /// - `token_index` The token index to update. |
| 70 | + /// - `circulation_supply` The new total circulating supply for the token. |
| 71 | + /// |
| 72 | + /// # Panics |
| 73 | + /// |
| 74 | + /// Panics if the token identified by `token_index` does not exist. |
| 75 | + fn set_token_circulating_supply( |
| 76 | + &mut self, |
| 77 | + token_index: TokenIndex, |
| 78 | + circulating_supply: TokenRawAmount, |
| 79 | + ); |
| 80 | + |
| 81 | + /// Create a new token with the given configuration. The initial state will be empty |
| 82 | + /// and the initial supply will be 0. Returns the token index and the updated state. |
| 83 | + /// |
| 84 | + /// # Arguments |
| 85 | + /// |
| 86 | + /// - `configuration` The configuration for the token. |
| 87 | + /// |
| 88 | + /// # Preconditions |
| 89 | + /// |
| 90 | + /// The caller must ensure the following conditions are true, and failing to do so results in |
| 91 | + /// undefined behavior. |
| 92 | + /// |
| 93 | + /// - The `token_id` of the given configuration MUST NOT already be in use by a protocol-level |
| 94 | + /// token, i.e. `assert_eq!(s.get_token_index(configuration.token_id), None)`. |
| 95 | + /// - The [`PLTConfiguration`] MUST be valid and in particular the 'governance_account_index' |
| 96 | + /// MUST reference a valid account. |
| 97 | + fn create_token(&mut self, configuration: PLTConfiguration) -> TokenIndex; |
| 98 | + |
| 99 | + /// Update the token balance of an account. |
| 100 | + /// |
| 101 | + /// # Arguments |
| 102 | + /// |
| 103 | + /// - `token_index` The token index to update. |
| 104 | + /// - `account_index` The account to update. |
| 105 | + /// - `amount_delta` The token balance delta. |
| 106 | + /// |
| 107 | + /// # Errors |
| 108 | + /// |
| 109 | + /// - [`OverflowError`] The update would overflow or underflow the token balance on the account. |
| 110 | + /// |
| 111 | + /// # Panics |
| 112 | + /// |
| 113 | + /// Panics if the token identified by `token_index` does not exist. |
| 114 | + fn update_token_account_balance( |
| 115 | + &mut self, |
| 116 | + token_index: TokenIndex, |
| 117 | + account_index: AccountIndex, |
| 118 | + amount_delta: TokenAmountDelta, |
| 119 | + ) -> Result<(), OverflowError>; |
| 120 | + |
| 121 | + /// Touch the token account. This initializes a token account state with a |
| 122 | + /// balance of zero. This only affects an account if its state for the token |
| 123 | + /// is empty. |
| 124 | + /// |
| 125 | + /// Returns `false`, if the account already contained a token account state. |
| 126 | + /// |
| 127 | + /// # Arguments |
| 128 | + /// |
| 129 | + /// - `token_index` The token index to update. |
| 130 | + /// - `account_index` The account to update. |
| 131 | + /// |
| 132 | + /// # Panics |
| 133 | + /// |
| 134 | + /// Panics if: |
| 135 | + /// |
| 136 | + /// - the token identified by `token_index` does not exist. |
| 137 | + /// - the account identified by `account_index` does not exist. |
| 138 | + #[must_use] |
| 139 | + fn touch_token_account(&mut self, token_index: TokenIndex, account_index: AccountIndex) |
| 140 | + -> bool; |
| 141 | + |
| 142 | + /// Increment the update sequence number for Protocol Level Tokens (PLT). |
| 143 | + /// |
| 144 | + /// Unlike the other chain updates this is a separate function, since there is no queue associated with PLTs. |
| 145 | + fn increment_plt_update_sequence_number(&mut self); |
| 146 | + |
| 147 | + /// Convert a mutable state to a persistent one and store it in the block state. |
| 148 | + /// |
| 149 | + /// To ensure this is future-proof, the mutable state should not be used after this call. |
| 150 | + /// |
| 151 | + /// # Arguments |
| 152 | + /// |
| 153 | + /// - `token_index` The token index to update. |
| 154 | + /// - `mutable_token_state` The mutated state to set as the current token state. |
| 155 | + /// |
| 156 | + /// # Panics |
| 157 | + /// |
| 158 | + /// Panics if the token identified by `token_index` does not exist. |
| 159 | + fn set_token_state(&mut self, token_index: TokenIndex, mutable_token_state: MutableTokenState); |
| 160 | +} |
| 161 | + |
| 162 | +/// The computation resulted in overflow. |
| 163 | +#[derive(Debug)] |
| 164 | +pub struct OverflowError; |
0 commit comments