polymesh_api_ink/
extension.rs

1use ink::env::Environment;
2
3use codec::{Decode, Encode};
4
5#[cfg(not(feature = "std"))]
6use alloc::string::String;
7use alloc::vec::Vec;
8
9use crate::{AccountId, AssetId, Encoded, Error, IdentityId};
10
11#[ink::chain_extension]
12#[derive(Clone, Copy)]
13pub trait PolymeshRuntime {
14  type ErrorCode = Error;
15
16  #[ink(extension = 0x00_00_00_01)]
17  fn call_runtime(call: Encoded);
18
19  #[ink(extension = 0x00_00_00_02)]
20  fn read_storage(key: Encoded) -> Option<Vec<u8>>;
21
22  #[ink(extension = 0x00_00_00_03)]
23  fn get_spec_version() -> u32;
24
25  #[ink(extension = 0x00_00_00_04)]
26  fn get_transaction_version() -> u32;
27
28  #[ink(extension = 0x00_00_00_05)]
29  fn get_key_did(key: AccountId) -> Option<IdentityId>;
30
31  #[ink(extension = 0x00_00_00_10)]
32  fn twox_64(data: Encoded) -> [u8; 8];
33
34  #[ink(extension = 0x00_00_00_11)]
35  fn twox_128(data: Encoded) -> [u8; 16];
36
37  #[ink(extension = 0x00_00_00_12)]
38  fn twox_256(data: Encoded) -> [u8; 32];
39
40  #[ink(extension = 0x00_00_00_13)]
41  fn get_latest_api_upgrade(api: Encoded) -> [u8; 32];
42
43  #[ink(extension = 0x00_00_00_14)]
44  fn call_runtime_with_error(call: Encoded) -> Result<Result<(), CallRuntimeError>, Error>;
45
46  #[ink(extension = 0x00_00_00_15)]
47  fn get_next_asset_id(account_id: AccountId) -> AssetId;
48}
49
50pub type PolymeshRuntimeInstance = <PolymeshRuntime as ink::ChainExtensionInstance>::Instance;
51
52pub fn new_instance() -> PolymeshRuntimeInstance {
53  <PolymeshRuntime as ink::ChainExtensionInstance>::instantiate()
54}
55
56#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
57pub struct CallRuntimeError(pub String);
58
59impl From<CallRuntimeError> for Error {
60  fn from(err: CallRuntimeError) -> Self {
61    Self::ExtrinsicCallFailed { error_msg: err.0 }
62  }
63}
64
65#[derive(Debug, Clone, PartialEq, Eq)]
66pub enum PolymeshEnvironment {}
67
68impl Environment for PolymeshEnvironment {
69  const MAX_EVENT_TOPICS: usize = <ink::env::DefaultEnvironment as Environment>::MAX_EVENT_TOPICS;
70
71  type AccountId = <ink::env::DefaultEnvironment as Environment>::AccountId;
72  type Balance = <ink::env::DefaultEnvironment as Environment>::Balance;
73  type Hash = <ink::env::DefaultEnvironment as Environment>::Hash;
74  type BlockNumber = <ink::env::DefaultEnvironment as Environment>::BlockNumber;
75  type Timestamp = <ink::env::DefaultEnvironment as Environment>::Timestamp;
76
77  type ChainExtension = PolymeshRuntime;
78}