-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
59 lines (51 loc) · 1.48 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#![cfg_attr(not(feature = "std"), no_std, no_main)]
use ink::primitives::AccountId;
use sp_runtime::MultiAddress;
#[derive(scale::Encode)]
enum RuntimeCall {
#[codec(index = 4)]
Balances(BalancesCall),
}
#[derive(scale::Encode)]
enum BalancesCall {
#[codec(index = 0)]
Transfer {
dest: MultiAddress<AccountId, ()>,
#[codec(compact)]
value: u128,
},
}
#[ink::contract]
mod call_runtime_template {
use crate::{BalancesCall, RuntimeCall};
#[ink(storage)]
#[derive(Default)]
pub struct CallRuntimeTemplate {}
#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum RuntimeError {
CallRuntimeFailed,
}
impl CallRuntimeTemplate {
#[ink(constructor, payable)]
pub fn new() -> Self {
Default::default()
}
/// Tries to transfer `value` from the contract's balance to `receiver`.
///
/// Returns Error CallRuntimeFailed if the call fails.
#[ink(message)]
pub fn transfer_call_runtime(
&mut self,
receiver: AccountId,
value: Balance,
) -> Result<(), RuntimeError> {
self.env()
.call_runtime(&RuntimeCall::Balances(BalancesCall::Transfer {
dest: receiver.into(),
value,
}))
.map_err(|_| RuntimeError::CallRuntimeFailed)
}
}
}