forked from SO4-Markets/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
211 lines (188 loc) · 5.84 KB
/
Copy pathlib.rs
File metadata and controls
211 lines (188 loc) · 5.84 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#![no_std]
use gmx_types::{OrderProps, OrderType};
use soroban_sdk::{
contract, contracterror, contractevent, contractimpl, contracttype, panic_with_error, Address,
Bytes, BytesN, Env,
};
const DEFAULT_ORDER_EXPIRY: u64 = 2_880;
#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum Error {
OrderNotFound = 1,
NotYetExpired = 2,
}
#[allow(dead_code)]
#[soroban_sdk::contractclient(name = "DataStoreClient")]
trait IDataStore {
fn get_u128(env: Env, key: BytesN<32>) -> u128;
fn set_u128(env: Env, caller: Address, key: BytesN<32>, value: u128) -> u128;
}
#[allow(dead_code)]
#[soroban_sdk::contractclient(name = "OrderHandlerClient")]
trait IOrderHandler {
fn get_order(env: Env, key: BytesN<32>) -> Option<OrderProps>;
fn cleanup_expired_order(env: Env, caller: Address, key: BytesN<32>);
}
#[contractevent(topics = ["ord_exp"])]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExpiredOrderCancelled {
pub key: BytesN<32>,
pub account: Address,
pub caller: Address,
pub age: u64,
pub expiry: u64,
pub cleanup_fee: i128,
}
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExpiredOrderPreview {
pub exists: bool,
pub is_expired: bool,
pub age: u64,
pub expiry: u64,
pub cleanup_fee: i128,
}
#[contract]
pub struct OrderCleanup;
#[contractimpl]
impl OrderCleanup {
pub fn set_order_expiry(
env: Env,
data_store: Address,
caller: Address,
order_type: OrderType,
expiry: u64,
) {
caller.require_auth();
DataStoreClient::new(&env, &data_store).set_u128(
&caller,
&order_expiry_key(&env, &order_type),
&(expiry as u128),
);
}
pub fn cancel_expired_order(
env: Env,
data_store: Address,
order_handler: Address,
caller: Address,
key: BytesN<32>,
) {
caller.require_auth();
let helper = env.current_contract_address();
let order_client = OrderHandlerClient::new(&env, &order_handler);
let order = order_client
.get_order(&key)
.unwrap_or_else(|| panic_with_error!(&env, Error::OrderNotFound));
let expiry = expiry_for_order(&env, &data_store, &order.order_type);
let now = env.ledger().timestamp();
let age = now.saturating_sub(order.updated_at_time);
if age < expiry {
panic_with_error!(&env, Error::NotYetExpired);
}
let cleanup_fee = cleanup_fee_from_execution_fee(order.execution_fee);
order_client.cleanup_expired_order(&helper, &key);
env.events().publish_event(&ExpiredOrderCancelled {
key,
account: order.account,
caller,
age,
expiry,
cleanup_fee,
});
}
pub fn record_manual_refund(
env: Env,
admin: Address,
token: Address,
receiver: Address,
amount: i128,
reason: BytesN<32>,
) {
admin.require_auth();
env.events().publish(
(soroban_sdk::symbol_short!("man_ref"),),
(admin, token, receiver, amount, reason),
);
}
pub fn preview_expired_order(
env: Env,
data_store: Address,
order_handler: Address,
key: BytesN<32>,
) -> ExpiredOrderPreview {
let order = OrderHandlerClient::new(&env, &order_handler).get_order(&key);
if let Some(order) = order {
let expiry = expiry_for_order(&env, &data_store, &order.order_type);
let age = env.ledger().timestamp().saturating_sub(order.updated_at_time);
ExpiredOrderPreview {
exists: true,
is_expired: age >= expiry,
age,
expiry,
cleanup_fee: cleanup_fee_from_execution_fee(order.execution_fee),
}
} else {
ExpiredOrderPreview {
exists: false,
is_expired: false,
age: 0,
expiry: DEFAULT_ORDER_EXPIRY,
cleanup_fee: 0,
}
}
}
}
fn expiry_for_order(env: &Env, data_store: &Address, order_type: &OrderType) -> u64 {
let stored = DataStoreClient::new(env, data_store).get_u128(&order_expiry_key(env, order_type));
if stored == 0 {
DEFAULT_ORDER_EXPIRY
} else {
stored as u64
}
}
fn cleanup_fee_from_execution_fee(execution_fee: i128) -> i128 {
if execution_fee <= 0 {
0
} else {
execution_fee / 10
}
}
fn order_expiry_key(env: &Env, order_type: &OrderType) -> BytesN<32> {
let mut bytes = Bytes::new(env);
bytes.append(&Bytes::from_slice(env, b"ORDER_EXPIRY_LEDGERS"));
bytes.append(&Bytes::from_slice(env, &[order_type_code(order_type)]));
env.crypto().sha256(&bytes).into()
}
fn order_type_code(order_type: &OrderType) -> u8 {
match order_type {
OrderType::MarketSwap => 0,
OrderType::LimitSwap => 1,
OrderType::MarketIncrease => 2,
OrderType::LimitIncrease => 3,
OrderType::MarketDecrease => 4,
OrderType::LimitDecrease => 5,
OrderType::StopLossDecrease => 6,
OrderType::Liquidation => 7,
OrderType::StopIncrease => 8,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_expiry_matches_four_hour_ledger_target() {
assert_eq!(DEFAULT_ORDER_EXPIRY, 2_880);
}
#[test]
fn cleanup_fee_is_small_portion_of_execution_fee() {
assert_eq!(cleanup_fee_from_execution_fee(1_000), 100);
assert_eq!(cleanup_fee_from_execution_fee(0), 0);
assert_eq!(cleanup_fee_from_execution_fee(-1), 0);
}
#[test]
fn order_type_codes_are_stable() {
assert_eq!(order_type_code(&OrderType::MarketSwap), 0);
assert_eq!(order_type_code(&OrderType::StopIncrease), 8);
}
}