|
| 1 | +#![cfg_attr(not(feature = "std"), no_std)] |
| 2 | + |
| 3 | +use ink_env::Environment; |
| 4 | +use ink_lang as ink; |
| 5 | +use ink_prelude::vec::Vec; |
| 6 | + |
| 7 | +type DefaultAccountId = <ink_env::DefaultEnvironment as Environment>::AccountId; |
| 8 | +type DefaultBalance = <ink_env::DefaultEnvironment as Environment>::Balance; |
| 9 | + |
| 10 | +#[ink::chain_extension] |
| 11 | +pub trait Psp22Extension { |
| 12 | + type ErrorCode = Psp22Error; |
| 13 | + |
| 14 | + // PSP22 Metadata interfaces |
| 15 | + |
| 16 | + #[ink(extension = 0x3d26)] |
| 17 | + fn token_name(asset_id: u32) -> Result<Vec<u8>>; |
| 18 | + |
| 19 | + #[ink(extension = 0x3420)] |
| 20 | + fn token_symbol(asset_id: u32) -> Result<Vec<u8>>; |
| 21 | + |
| 22 | + #[ink(extension = 0x7271)] |
| 23 | + fn token_decimals(asset_id: u32) -> Result<u8>; |
| 24 | + |
| 25 | + // PSP22 interface queries |
| 26 | + |
| 27 | + #[ink(extension = 0x162d)] |
| 28 | + fn total_supply(asset_id: u32) -> Result<DefaultBalance>; |
| 29 | + |
| 30 | + #[ink(extension = 0x6568)] |
| 31 | + fn balance_of(asset_id: u32, owner: DefaultAccountId) -> Result<DefaultBalance>; |
| 32 | + |
| 33 | + #[ink(extension = 0x4d47)] |
| 34 | + fn allowance( |
| 35 | + asset_id: u32, |
| 36 | + owner: DefaultAccountId, |
| 37 | + spender: DefaultAccountId, |
| 38 | + ) -> Result<DefaultBalance>; |
| 39 | + |
| 40 | + // PSP22 transfer |
| 41 | + #[ink(extension = 0xdb20)] |
| 42 | + fn transfer(asset_id: u32, to: DefaultAccountId, value: DefaultBalance) |
| 43 | + -> Result<()>; |
| 44 | + |
| 45 | + // PSP22 transfer_from |
| 46 | + #[ink(extension = 0x54b3)] |
| 47 | + fn transfer_from( |
| 48 | + asset_id: u32, |
| 49 | + from: DefaultAccountId, |
| 50 | + to: DefaultAccountId, |
| 51 | + value: DefaultBalance, |
| 52 | + ) -> Result<()>; |
| 53 | + |
| 54 | + // PSP22 approve |
| 55 | + #[ink(extension = 0xb20f)] |
| 56 | + fn approve( |
| 57 | + asset_id: u32, |
| 58 | + spender: DefaultAccountId, |
| 59 | + value: DefaultBalance, |
| 60 | + ) -> Result<()>; |
| 61 | + |
| 62 | + // PSP22 increase_allowance |
| 63 | + #[ink(extension = 0x96d6)] |
| 64 | + fn increase_allowance( |
| 65 | + asset_id: u32, |
| 66 | + spender: DefaultAccountId, |
| 67 | + value: DefaultBalance, |
| 68 | + ) -> Result<()>; |
| 69 | + |
| 70 | + // PSP22 decrease_allowance |
| 71 | + #[ink(extension = 0xfecb)] |
| 72 | + fn decrease_allowance( |
| 73 | + asset_id: u32, |
| 74 | + spender: DefaultAccountId, |
| 75 | + value: DefaultBalance, |
| 76 | + ) -> Result<()>; |
| 77 | +} |
| 78 | + |
| 79 | +#[derive(scale::Encode, scale::Decode)] |
| 80 | +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] |
| 81 | +pub enum Psp22Error { |
| 82 | + TotalSupplyFailed, |
| 83 | +} |
| 84 | + |
| 85 | +pub type Result<T> = core::result::Result<T, Psp22Error>; |
| 86 | + |
| 87 | +impl From<scale::Error> for Psp22Error { |
| 88 | + fn from(_: scale::Error) -> Self { |
| 89 | + panic!("encountered unexpected invalid SCALE encoding") |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl ink_env::chain_extension::FromStatusCode for Psp22Error { |
| 94 | + fn from_status_code(status_code: u32) -> core::result::Result<(), Self> { |
| 95 | + match status_code { |
| 96 | + 0 => Ok(()), |
| 97 | + 1 => Err(Self::TotalSupplyFailed), |
| 98 | + _ => panic!("encountered unknown status code"), |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/// An environment using default ink environment types, with PSP-22 extension included |
| 104 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 105 | +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] |
| 106 | +pub enum CustomEnvironment {} |
| 107 | + |
| 108 | +impl Environment for CustomEnvironment { |
| 109 | + const MAX_EVENT_TOPICS: usize = |
| 110 | + <ink_env::DefaultEnvironment as Environment>::MAX_EVENT_TOPICS; |
| 111 | + |
| 112 | + type AccountId = DefaultAccountId; |
| 113 | + type Balance = DefaultBalance; |
| 114 | + type Hash = <ink_env::DefaultEnvironment as Environment>::Hash; |
| 115 | + type Timestamp = <ink_env::DefaultEnvironment as Environment>::Timestamp; |
| 116 | + type BlockNumber = <ink_env::DefaultEnvironment as Environment>::BlockNumber; |
| 117 | + |
| 118 | + type ChainExtension = crate::Psp22Extension; |
| 119 | +} |
| 120 | + |
| 121 | +#[ink::contract(env = crate::CustomEnvironment)] |
| 122 | +mod psp22_ext { |
| 123 | + use super::{ |
| 124 | + Result, |
| 125 | + Vec, |
| 126 | + }; |
| 127 | + |
| 128 | + /// A chain extension which implements the PSP-22 fungible token standard. |
| 129 | + /// For more details see <https://github.com/w3f/PSPs/blob/master/PSPs/psp-22.md> |
| 130 | + #[ink(storage)] |
| 131 | + #[derive(Default)] |
| 132 | + pub struct Psp22Extension {} |
| 133 | + |
| 134 | + impl Psp22Extension { |
| 135 | + /// Creates a new instance of this contract. |
| 136 | + #[ink(constructor)] |
| 137 | + pub fn new() -> Self { |
| 138 | + Default::default() |
| 139 | + } |
| 140 | + |
| 141 | + // PSP22 Metadata interfaces |
| 142 | + |
| 143 | + /// Returns the token name of the specified asset. |
| 144 | + #[ink(message, selector = 0x3d261bd4)] |
| 145 | + pub fn token_name(&self, asset_id: u32) -> Result<Vec<u8>> { |
| 146 | + self.env().extension().token_name(asset_id) |
| 147 | + } |
| 148 | + |
| 149 | + /// Returns the token symbol of the specified asset. |
| 150 | + #[ink(message, selector = 0x34205be5)] |
| 151 | + pub fn token_symbol(&self, asset_id: u32) -> Result<Vec<u8>> { |
| 152 | + self.env().extension().token_symbol(asset_id) |
| 153 | + } |
| 154 | + |
| 155 | + /// Returns the token decimals of the specified asset. |
| 156 | + #[ink(message, selector = 0x7271b782)] |
| 157 | + pub fn token_decimals(&self, asset_id: u32) -> Result<u8> { |
| 158 | + self.env().extension().token_decimals(asset_id) |
| 159 | + } |
| 160 | + |
| 161 | + // PSP22 interface queries |
| 162 | + |
| 163 | + /// Returns the total token supply of the specified asset. |
| 164 | + #[ink(message, selector = 0x162df8c2)] |
| 165 | + pub fn total_supply(&self, asset_id: u32) -> Result<Balance> { |
| 166 | + self.env().extension().total_supply(asset_id) |
| 167 | + } |
| 168 | + |
| 169 | + /// Returns the account balance for the specified asset & owner. |
| 170 | + #[ink(message, selector = 0x6568382f)] |
| 171 | + pub fn balance_of(&self, asset_id: u32, owner: AccountId) -> Result<Balance> { |
| 172 | + self.env().extension().balance_of(asset_id, owner) |
| 173 | + } |
| 174 | + |
| 175 | + /// Returns the amount which `spender` is still allowed to withdraw from `owner` |
| 176 | + /// for the specified asset. |
| 177 | + #[ink(message, selector = 0x4d47d921)] |
| 178 | + pub fn allowance( |
| 179 | + &self, |
| 180 | + asset_id: u32, |
| 181 | + owner: AccountId, |
| 182 | + spender: AccountId, |
| 183 | + ) -> Result<Balance> { |
| 184 | + self.env().extension().allowance(asset_id, owner, spender) |
| 185 | + } |
| 186 | + |
| 187 | + // PSP22 transfer |
| 188 | + |
| 189 | + /// Transfers `value` amount of specified asset from the caller's account to the |
| 190 | + /// account `to`. |
| 191 | + #[ink(message, selector = 0xdb20f9f5)] |
| 192 | + pub fn transfer( |
| 193 | + &mut self, |
| 194 | + asset_id: u32, |
| 195 | + to: AccountId, |
| 196 | + value: Balance, |
| 197 | + ) -> Result<()> { |
| 198 | + self.env().extension().transfer(asset_id, to, value) |
| 199 | + } |
| 200 | + |
| 201 | + // PSP22 transfer_from |
| 202 | + |
| 203 | + /// Transfers `value` amount of specified asset on the behalf of `from` to the |
| 204 | + /// account `to`. |
| 205 | + #[ink(message, selector = 0x54b3c76e)] |
| 206 | + pub fn transfer_from( |
| 207 | + &mut self, |
| 208 | + asset_id: u32, |
| 209 | + from: AccountId, |
| 210 | + to: AccountId, |
| 211 | + value: Balance, |
| 212 | + ) -> Result<()> { |
| 213 | + self.env() |
| 214 | + .extension() |
| 215 | + .transfer_from(asset_id, from, to, value) |
| 216 | + } |
| 217 | + |
| 218 | + // PSP22 approve |
| 219 | + |
| 220 | + /// Allows `spender` to withdraw from the caller's account multiple times, up to |
| 221 | + /// the `value` amount of the specified asset. |
| 222 | + #[ink(message, selector = 0xb20f1bbd)] |
| 223 | + pub fn approve( |
| 224 | + &mut self, |
| 225 | + asset_id: u32, |
| 226 | + spender: AccountId, |
| 227 | + value: Balance, |
| 228 | + ) -> Result<()> { |
| 229 | + self.env().extension().approve(asset_id, spender, value) |
| 230 | + } |
| 231 | + |
| 232 | + // PSP22 increase_allowance |
| 233 | + |
| 234 | + /// Atomically increases the allowance for the specified asset granted to `spender` |
| 235 | + /// by the caller. |
| 236 | + #[ink(message, selector = 0x96d6b57a)] |
| 237 | + pub fn increase_allowance( |
| 238 | + &mut self, |
| 239 | + asset_id: u32, |
| 240 | + spender: AccountId, |
| 241 | + value: Balance, |
| 242 | + ) -> Result<()> { |
| 243 | + self.env() |
| 244 | + .extension() |
| 245 | + .increase_allowance(asset_id, spender, value) |
| 246 | + } |
| 247 | + |
| 248 | + // PSP22 decrease_allowance |
| 249 | + |
| 250 | + /// Atomically decreases the allowance for the specified asset granted to `spender` |
| 251 | + /// by the caller. |
| 252 | + #[ink(message, selector = 0xfecb57d5)] |
| 253 | + pub fn decrease_allowance( |
| 254 | + &mut self, |
| 255 | + asset_id: u32, |
| 256 | + spender: AccountId, |
| 257 | + value: Balance, |
| 258 | + ) -> Result<()> { |
| 259 | + self.env() |
| 260 | + .extension() |
| 261 | + .decrease_allowance(asset_id, spender, value) |
| 262 | + } |
| 263 | + } |
| 264 | +} |
0 commit comments