|
| 1 | +#![cfg_attr(not(feature = "std"), no_std, no_main)] |
| 2 | + |
| 3 | +use ink::env::call::{ExecutionInput, Selector, build_call}; |
| 4 | +#[derive(Debug, Clone)] |
| 5 | +pub struct CustomEnvironment; |
| 6 | + |
| 7 | +pub enum FunctionId { |
| 8 | + Dummy = 100, |
| 9 | +} |
| 10 | + |
| 11 | +#[ink::chain_extension(extension = 0x1000)] |
| 12 | +pub trait RuntimeReadWrite { |
| 13 | + type ErrorCode = ReadWriteErrorCode; |
| 14 | + |
| 15 | + #[ink(function = 100)] |
| 16 | + fn dummy(); |
| 17 | +} |
| 18 | + |
| 19 | +#[ink::scale_derive(Encode, Decode, TypeInfo)] |
| 20 | +pub enum ReadWriteErrorCode { |
| 21 | + ReadFailed, |
| 22 | + WriteFailed, |
| 23 | +} |
| 24 | + |
| 25 | +impl ink::env::chain_extension::FromStatusCode for ReadWriteErrorCode { |
| 26 | + fn from_status_code(status_code: u32) -> Result<(), Self> { |
| 27 | + match status_code { |
| 28 | + 0 => Ok(()), |
| 29 | + 1 => Err(ReadWriteErrorCode::ReadFailed), |
| 30 | + 2 => Err(ReadWriteErrorCode::WriteFailed), |
| 31 | + _ => Err(ReadWriteErrorCode::ReadFailed), |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl ink::env::Environment for CustomEnvironment { |
| 37 | + const MAX_EVENT_TOPICS: usize = 4; |
| 38 | + |
| 39 | + type AccountId = ink::primitives::AccountId; |
| 40 | + type Balance = u64; |
| 41 | + type Hash = ink::primitives::Hash; |
| 42 | + type BlockNumber = u32; |
| 43 | + type Timestamp = u64; |
| 44 | + |
| 45 | + type ChainExtension = RuntimeReadWrite; |
| 46 | +} |
| 47 | + |
| 48 | +#[ink::contract(env = crate::CustomEnvironment)] |
| 49 | +mod delegate_call { |
| 50 | + use super::*; |
| 51 | + |
| 52 | + #[ink(storage)] |
| 53 | + pub struct Bittensor {} |
| 54 | + |
| 55 | + impl Bittensor { |
| 56 | + #[ink(constructor)] |
| 57 | + pub fn new() -> Self { |
| 58 | + Self {} |
| 59 | + } |
| 60 | + |
| 61 | + /// Constructor |
| 62 | + #[ink(constructor)] |
| 63 | + pub fn default() -> Self { |
| 64 | + Self::new() |
| 65 | + } |
| 66 | + |
| 67 | + #[ink(message)] |
| 68 | + pub fn dummy(&self, code_hash: [u8; 32]) -> Result<(), ReadWriteErrorCode> { |
| 69 | + // let code_hash = self |
| 70 | + // .delegate_code_hash |
| 71 | + // .unwrap_or(ink::primitives::Hash::from(hash)); |
| 72 | + let my_return_value = build_call::<CustomEnvironment>() |
| 73 | + .delegate(ink::primitives::Hash::from(code_hash)) |
| 74 | + .exec_input(ExecutionInput::new(Selector::new(ink::selector_bytes!( |
| 75 | + "dummy" |
| 76 | + )))) |
| 77 | + .returns::<Result<(), ReadWriteErrorCode>>() |
| 78 | + .invoke(); |
| 79 | + |
| 80 | + my_return_value.map_err(|_e| ReadWriteErrorCode::WriteFailed) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments