-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
178 lines (160 loc) · 5.87 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
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
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::UnorderedMap;
use near_sdk::json_types::U128;
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{
env, ext_contract, near_bindgen, AccountId, Balance, BorshStorageKey, Gas, PanicOnDefault,
PromiseOrValue, PromiseResult,
};
use protocol_sdk::{Content, Context, OmniChain, Payload, Value};
const GAS_FOR_CALLBACK: Gas = Gas(5_000_000_000_000);
const NO_DEPOSIT: Balance = 0;
#[derive(Clone, PartialEq, BorshDeserialize, BorshSerialize, Serialize, Deserialize, Debug)]
#[serde(crate = "near_sdk::serde")]
pub struct ComputeTask {
pub nums: Vec<u32>,
pub result: Option<u32>,
}
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct Computation {
omni_chain: OmniChain,
compute_task: UnorderedMap<(String, u128), ComputeTask>,
}
#[ext_contract(ext_self)]
pub trait MyContract {
fn callback(&mut self, to_chain: String, nums: Vec<u32>) -> U128;
}
#[derive(BorshSerialize, BorshStorageKey)]
enum StorageKey {
DestinationContract,
PermittedContract,
Result,
}
#[near_bindgen]
impl Computation {
#[init]
pub fn new(owner_id: AccountId, omni_chain_contract_id: AccountId) -> Self {
Self {
omni_chain: OmniChain::new(
owner_id,
StorageKey::DestinationContract,
StorageKey::PermittedContract,
omni_chain_contract_id,
),
compute_task: UnorderedMap::new(StorageKey::Result),
}
}
pub fn send_compute_task(&mut self, to_chain: String, nums: Vec<u32>) -> PromiseOrValue<U128> {
let mut payload = Payload::new();
payload.push_item("nums".to_string(), Value::VecUint32(nums.clone()));
let action_name = "receive_compute_task".to_string();
let dst_contract = self
.omni_chain
.destination_contract
.get(&to_chain.clone())
.expect("to chain not register");
let contract = dst_contract
.get(&action_name)
.expect("contract not register");
let content = Content {
contract: contract.contract_address.clone(),
action: contract.action_name.clone(),
data: payload,
};
let callback = "receive_compute_result".as_bytes().to_vec();
self.omni_chain
.call_cross_with_session(to_chain.clone(), content, callback)
.then(ext_self::callback(
to_chain,
nums,
env::current_account_id(),
NO_DEPOSIT,
GAS_FOR_CALLBACK,
))
.into()
}
pub fn receive_compute_task(&self, payload: Payload, context: Context) {
assert_eq!(
self.omni_chain.omni_chain_contract_id,
env::predecessor_account_id(),
"Processs by cross chain contract"
);
self.omni_chain.assert_register_permitted_contract(
&context.from_chain,
&context.sender,
&context.action,
);
let item = payload.get_item("nums".to_string()).unwrap();
let nums = item.get_value::<Vec<u32>>().unwrap();
let mut sum: u32 = 0;
for num in nums {
sum += num;
}
let mut payload = Payload::new();
payload.push_item("result".to_string(), Value::Uint32(sum));
let content = Content {
contract: context.sender,
action: context.session.callback.unwrap(),
data: payload,
};
self.omni_chain
.send_response_message(context.from_chain, content, context.session.id);
}
pub fn receive_compute_result(&mut self, payload: Payload, context: Context) {
assert_eq!(
self.omni_chain.omni_chain_contract_id,
env::predecessor_account_id(),
"Processs by cross chain contract."
);
let item = payload.get_item("result".to_string()).unwrap();
let result = item.get_value::<u32>().unwrap();
let session = context.session;
let id = session.id.0;
let key = (context.from_chain, id);
self.compute_task.get(&key).as_mut().and_then(|task| {
task.result = Some(result);
self.compute_task.insert(&key, task)
});
}
pub fn get_compute_task(&self, to_chain: String, id: U128) -> Option<ComputeTask> {
self.compute_task.get(&(to_chain, id.0))
}
#[private]
pub fn callback(&mut self, to_chain: String, nums: Vec<u32>) -> U128 {
// let mut session_id: u64 = 0;
match env::promise_result(0) {
PromiseResult::Successful(result) => {
let session_id = near_sdk::serde_json::from_slice::<U128>(&result)
.expect("unwrap session id failed");
self.compute_task.insert(
&(to_chain, session_id.0),
&ComputeTask { nums, result: None },
);
session_id
}
_ => env::panic_str("call omi-chain failed"),
}
}
pub fn get_dst_contract(&self, chain: String, action_name: String) -> (Vec<u8>, Vec<u8>) {
let dst_contract = self.omni_chain.destination_contract.get(&chain).unwrap();
// let action_name = "receive_"
let contract = dst_contract.get(&action_name).unwrap();
(
contract.contract_address.clone(),
contract.action_name.clone(),
)
}
// UnorderedMap<(String, String), Vec<String>>,
pub fn get_permitted_contract(&self) -> Vec<((String, Vec<u8>), Vec<String>)> {
self.omni_chain
.permitted_contract
.iter()
.map(|res| res)
.collect()
}
pub fn clear_compute_task(&mut self) {
self.compute_task.clear();
}
}
protocol_sdk::impl_omni_chain_register!(Computation, omni_chain);