-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcross_chain_helper.rs
187 lines (164 loc) · 7.12 KB
/
cross_chain_helper.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use ink::primitives::AccountId;
use ink::prelude::vec::Vec;
use payload::message_define::{
ISentMessage,
ISQoS,
IContext,
IContent,
ISession,
IRequestMessage,
IResponseMessage,
};
const CROSS_CHAIN_CONTRACT_ADDRESS: &str = "0x9b33e9dbcc468833b9cec8e0642e4932487931ea092d789ffe51ee41fea4de7a";
const SEND_MESSAGE_SELECTOR: [u8; 4] = [0x27, 0x26, 0x79, 0x17];
// const REGISTER_SQOS_SELECTOR: [u8; 4] = [0x32, 0x80, 0x5c, 0x58];
const GET_CONTEXT_SELECTOR: [u8; 4] = [0xee, 0xe9, 0xc1, 0xb3];
const GET_SQOS_SELECTOR: [u8; 4] = [0x8d, 0xe9, 0x09, 0xd7];
const SET_SQOS_SELECTOR: [u8; 4] = [0xc1, 0xe9, 0xbc, 0xda];
const REMOVE_SQOS_SELECTOR: [u8; 4] = [0x35, 0x2b, 0x71, 0xb5];
/// Converts hex string of address into [u8; 32]
fn convert_address(s: &str) -> AccountId {
let mut begin = 0;
if &s[0..2] == "0x" {
begin = 2;
}
let mut v: [u8; 32] = [0; 32];
let mut index = 0;
for i in begin/2..s.len()/2 {
v[index] = u8::from_str_radix(&s[i * 2..i * 2 + 2], 16).unwrap();
index = index + 1;
}
AccountId::try_from(v).unwrap()
}
/// If you want to use functionalities of the SDK, this trait must be implemented.
pub trait CrossChainBase {
/// Returns the cross-chain contract address.
fn get_cross_chain_contract_address(& self) -> AccountId {
let default_address = convert_address(CROSS_CHAIN_CONTRACT_ADDRESS);
default_address
}
}
// /// Registers sqos
// pub fn register_sqos<T: CrossChainBase>(contract: &T, sqos: ISQoS) {
// let cross_chain: AccountId = <T as CrossChainBase>::get_cross_chain_contract_address(&contract);
// ink::env::call::build_call::<ink::env::DefaultEnvironment>()
// .call_type(
// ink::env::call::Call::new()
// .callee(cross_chain)
// .gas_limit(0)
// .transferred_value(0))
// .exec_input(
// ink::env::call::ExecutionInput::new(ink::env::call::Selector::new(REGISTER_SQOS_SELECTOR))
// .push_arg(sqos)
// )
// .returns::<()>()
// .fire()
// .unwrap();
// }
/// Sends a cross-chain message.
fn send_message<T: CrossChainBase>(contract: &mut T, message: ISentMessage) -> u128 {
let cross_chain: AccountId = <T as CrossChainBase>::get_cross_chain_contract_address(&contract);
let id: u128 = ink::env::call::build_call::<ink::env::DefaultEnvironment>()
.call_type(
ink::env::call::Call::new()
.callee(cross_chain)
.gas_limit(0)
.transferred_value(0))
.exec_input(
ink::env::call::ExecutionInput::new(ink::env::call::Selector::new(SEND_MESSAGE_SELECTOR))
.push_arg(message)
)
.returns::<u128>()
.fire()
.unwrap();
id
}
/// Sends a cross-chain message, and returns the message id.
pub fn cross_chain_send_message<T: CrossChainBase>(contract: &mut T, request: IRequestMessage) -> u128 {
let session = ISession::new(0, 1, Vec::<u8>::new(), Vec::<u8>::new(), Vec::<u8>::new());
let message = ISentMessage::new(request.to_chain, request.sqos, request.content, session);
send_message(contract, message)
}
/// Sends a cross-chain message, and returns the message id.
/// Latar a callback will be called.
pub fn cross_chain_call<T: CrossChainBase>(contract: &mut T, request: IRequestMessage, callback: [u8; 4]) -> u128 {
let session = ISession::new(0, 2, Vec::from(callback), Vec::<u8>::new(), Vec::<u8>::new());
let message = ISentMessage::new(request.to_chain, request.sqos, request.content, session);
send_message(contract, message)
}
/// Responds a cross-chain message, and returns the message id.
pub fn cross_chain_respond<T: CrossChainBase>(contract: &mut T, response: IResponseMessage) -> u128 {
let context = get_context(contract).unwrap();
let session = ISession::new(context.id, 3, Vec::<u8>::new(), Vec::<u8>::new(), Vec::<u8>::new());
let content = IContent::new(context.sender, context.session.callback, response.data);
let message = ISentMessage::new(context.from_chain, response.sqos, content, session);
send_message(contract, message)
}
/// Returns context of Cross Chain
pub fn get_context<T: CrossChainBase>(contract: &T) -> Option<IContext> {
let cross_chain: AccountId = <T as CrossChainBase>::get_cross_chain_contract_address(&contract);
ink::env::call::build_call::<ink::env::DefaultEnvironment>()
.call_type(
ink::env::call::Call::new()
.callee(cross_chain)
.gas_limit(0)
.transferred_value(0))
.exec_input(
ink::env::call::ExecutionInput::new(ink::env::call::Selector::new(GET_CONTEXT_SELECTOR))
)
.returns::<Option<IContext>>()
.fire()
.unwrap()
}
/// Returns SQoS registered in Cross Chain
pub fn get_sqos<T: CrossChainBase>(contract: &T, account_id: AccountId) -> Option<ISQoS> {
let cross_chain: AccountId = <T as CrossChainBase>::get_cross_chain_contract_address(&contract);
ink::env::call::build_call::<ink::env::DefaultEnvironment>()
.call_type(
ink::env::call::Call::new()
.callee(cross_chain)
.gas_limit(0)
.transferred_value(0))
.exec_input(
ink::env::call::ExecutionInput::new(ink::env::call::Selector::new(GET_SQOS_SELECTOR))
.push_arg(account_id)
)
.returns::<Option<ISQoS>>()
.fire()
.unwrap()
}
/// Sets SQoS registered in Cross Chain
pub fn set_sqos<T: CrossChainBase>(contract: &T, sqos: ISQoS, account_id: AccountId) {
let cross_chain: AccountId = <T as CrossChainBase>::get_cross_chain_contract_address(&contract);
ink::env::call::build_call::<ink::env::DefaultEnvironment>()
.call_type(
ink::env::call::Call::new()
.callee(cross_chain)
.gas_limit(0)
.transferred_value(0))
.exec_input(
ink::env::call::ExecutionInput::new(ink::env::call::Selector::new(SET_SQOS_SELECTOR))
.push_arg(account_id)
.push_arg(sqos)
)
.returns::<()>()
.fire()
.unwrap();
}
/// Remove SQoS registered in Cross Chain
pub fn remove_sqos<T: CrossChainBase>(contract: &T, account_id: AccountId) {
let cross_chain: AccountId = <T as CrossChainBase>::get_cross_chain_contract_address(&contract);
ink::env::call::build_call::<ink::env::DefaultEnvironment>()
.call_type(
ink::env::call::Call::new()
.callee(cross_chain)
.gas_limit(0)
.transferred_value(0))
.exec_input(
ink::env::call::ExecutionInput::new(ink::env::call::Selector::new(REMOVE_SQOS_SELECTOR))
.push_arg(account_id)
)
.returns::<()>()
.fire()
.unwrap();
}