This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
lib.rs
65 lines (57 loc) · 2.09 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
60
61
62
63
64
65
#![cfg_attr(not(feature = "std"), no_std)]
use ink_lang as ink;
#[ink::contract]
mod contract_introspection {
use ink_env::{
call::{
build_call,
Call,
ExecutionInput,
Selector,
},
CallFlags,
DefaultEnvironment,
};
#[ink(storage)]
pub struct ContractInspection {}
impl ContractInspection {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}
/// Returns `true` if the caller is a contract.
#[ink(message, selector = 1)]
pub fn is_caller_contract(&self) -> bool {
self.env().is_contract(&self.env().caller())
}
/// Returns `true` if the caller is the origin of this call.
#[ink(message, selector = 2)]
pub fn is_caller_origin(&self) -> bool {
self.env().caller_is_origin()
}
/// Calls into this contract to the [`is_caller_contract`] function
/// and returns the return value of that function.
#[ink(message)]
pub fn calls_is_caller_contract(&self) -> bool {
build_call::<DefaultEnvironment>()
.call_type(Call::new().callee(self.env().account_id()))
.exec_input(ExecutionInput::new(Selector::new([0x00, 0x00, 0x00, 0x01])))
.returns::<bool>()
.call_flags(CallFlags::default().set_allow_reentry(true))
.fire()
.expect("failed executing call")
}
/// Calls into this contract to the [`is_caller_origin`] function
/// and returns the return value of that function.
#[ink(message)]
pub fn calls_is_caller_origin(&self) -> bool {
build_call::<DefaultEnvironment>()
.call_type(Call::new().callee(self.env().account_id()))
.exec_input(ExecutionInput::new(Selector::new([0x00, 0x00, 0x00, 0x02])))
.returns::<bool>()
.call_flags(CallFlags::default().set_allow_reentry(true))
.fire()
.expect("failed executing call")
}
}
}