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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
use std::collections::HashSet;
use crate::{
error::ContractError,
types::{
i32_to_direction, i32_to_order_type, MarginRatios, Order, OrderType, PositionDirection,
PositionEffect,
},
utils::SignedDecimal,
};
use cosmwasm_std::{Addr, Coin, Decimal};
use cw20::Cw20ReceiveMsg;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct MigrateMsg {}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct InstantiateMsg {
pub whitelist: Vec<String>,
pub denoms: Vec<String>,
pub supported_collateral_denoms: Vec<String>,
pub supported_multicollateral_denoms: Vec<String>,
pub full_denom_mapping: Vec<(String, String, Decimal)>,
pub oracle_denom_mapping: Vec<(String, String, Decimal)>,
pub use_whitelist: bool,
pub multicollateral_whitelist: Vec<Addr>,
pub multicollateral_whitelist_enable: bool,
pub admin: String,
pub limit_order_fee: SignedDecimal,
pub market_order_fee: SignedDecimal,
pub liquidation_order_fee: SignedDecimal,
pub max_leverage: SignedDecimal,
pub funding_payment_lookback: u64,
pub native_token: String,
pub default_base: String,
pub spot_market_contract: Addr,
pub funding_payment_pairs: Vec<(String, String)>,
pub default_margin_ratios: MarginRatios,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
Receive(Cw20ReceiveMsg),
Deposit {},
Withdraw {
coins: Vec<Coin>,
},
WithdrawInsuranceFund {
coin: Coin,
},
SwapMulticollateralToBase {
orders: Vec<OrderPlacement>,
},
UseWhitelist(bool),
AddToCW20DenomMapping {
address: String,
denom: String,
},
AddToFullDenomMapping {
full_denom: String,
internal_denom: String,
conversion_rate: Decimal,
},
AddToOracleDenomMapping {
oracle_denom: String,
internal_denom: String,
conversion_rate: Decimal,
},
AddToWhitelist {
converter: String,
},
AddToSupportedMultiCollateralDenoms {
denom: String,
},
AddToFundingPaymentPairs {
price_denom: String,
asset_denom: String,
},
RemoveFromWhitelist {
converter: String,
},
AddDenom {
denom: String,
},
RemoveDenom {
denom: String,
},
UpdateMarginRatio {
margin_ratio: MarginRatios,
},
UpdateMaxLeverage {
max_leverage: SignedDecimal,
},
UpdateMarketOrderFee {
market_order_fee: SignedDecimal,
},
UpdateLimitOrderFee {
limit_order_fee: SignedDecimal,
},
UpdateLiquidationOrderFee {
liquidation_order_fee: SignedDecimal,
},
UpdateAdmin {
admin: String,
},
UpdateFundingPaymentLookback {
funding_payment_lookback: u64,
},
UpdateNativeToken {
native_token: String,
},
UpdateBase {
default_base: String,
},
UpdateSpotMarketContract {
contract_addr: String,
},
UpdateMultiCollateralWhitelist {
whitelist: Vec<Addr>,
whitelist_enable: bool,
},
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DecimalCoin {
pub denom: String,
pub amount: Decimal,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum SudoMsg {
Settlement {
epoch: i64,
entries: Vec<SettlementEntry>,
},
NewBlock {
epoch: i64,
},
BulkOrderPlacements {
orders: Vec<OrderPlacement>,
deposits: Vec<DepositInfo>,
},
BulkOrderCancellations {
ids: Vec<u64>,
},
Liquidation {
requests: Vec<LiquidationRequest>,
},
FinalizeBlock {
contract_order_results: Vec<ContractOrderResult>,
},
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
GetBalance {
account: String,
symbol: String,
},
GetBalances {
account: String,
},
GetFundingPaymentRates {
price_denom: String,
asset_denom: String,
start_epoch: i64,
end_epoch: i64,
},
GetPosition {
account: String,
price_denom: String,
asset_denom: String,
},
GetOrder {
account: String,
price_denom: String,
asset_denom: String,
},
GetPortfolioSpecs {
account: String,
},
GetInsuranceFundBalance {},
GetOrderEstimate {
order: Order,
},
GetConfig {},
}
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct GetBalanceResponse {
pub amount: SignedDecimal,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct GetBalancesResponse {
pub symbols: Vec<String>,
pub amounts: Vec<SignedDecimal>,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct GetPositionResponse {
pub long_position: SignedDecimal,
pub long_position_margin_debt: SignedDecimal,
pub long_position_last_funding_payment_epoch: i64,
pub short_position: SignedDecimal,
pub short_position_margin_debt: SignedDecimal,
pub short_position_last_funding_payment_epoch: i64,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct GetPortfolioSpecsResponse {
pub equity: SignedDecimal,
pub total_position_value: SignedDecimal,
pub buying_power: SignedDecimal,
pub unrealized_pnl: SignedDecimal,
pub leverage: SignedDecimal,
pub balance: SignedDecimal,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct GetInsuranceFundBalanceResponse {
pub balance: SignedDecimal,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct GetOrderResponse {
pub orders: Vec<Order>,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct GetFundingPaymentRatesResponse {
pub price_diffs: Vec<Decimal>,
pub negatives: Vec<bool>,
pub epochs: Vec<i64>,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct GetOrderEstimateResponse {
pub order_fee_estimate: SignedDecimal,
pub deposits_required: Coin,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct GetConfigResponse {
pub admin: String,
pub whitelist: HashSet<Addr>,
pub use_whitelist: bool,
pub limit_order_fee: SignedDecimal,
pub market_order_fee: SignedDecimal,
pub liquidation_order_fee: SignedDecimal,
pub default_margin_ratios: MarginRatios,
pub max_leverage: SignedDecimal,
pub spot_market_contract: String,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct BulkOrderPlacementsResponse {
pub unsuccessful_orders: Vec<UnsuccessfulOrder>,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct UnsuccessfulOrder {
pub id: u64,
pub reason: String,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct LiquidationResponse {
pub successful_accounts: Vec<String>,
pub liquidation_orders: Vec<OrderPlacement>,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct SettlementEntry {
pub account: String,
pub price_denom: String,
pub asset_denom: String,
pub quantity: Decimal,
pub execution_cost_or_proceed: Decimal,
pub expected_cost_or_proceed: Decimal,
pub position_direction: PositionDirection,
pub order_type: OrderType,
pub order_id: u64,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct OrderPlacement {
pub id: u64,
pub status: i32,
pub account: String,
pub contract_address: String,
pub price_denom: String,
pub asset_denom: String,
pub price: Decimal,
pub quantity: Decimal,
pub order_type: i32,
pub position_direction: i32,
pub data: String,
pub status_description: String,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct OrderData {
pub leverage: Decimal,
pub position_effect: PositionEffect,
}
impl OrderPlacement {
pub fn to_order(&self) -> Result<Order, ContractError> {
let order_data: OrderData = match serde_json_wasm::from_str(&self.data) {
Ok(data) => data,
Err(_) => return Result::Err(ContractError::InvalidOrderData {}),
};
let order = Order {
id: self.id,
account: self.account.to_owned(),
price_denom: self.price_denom.to_owned(),
asset_denom: self.asset_denom.to_owned(),
price: SignedDecimal::new(self.price),
quantity: SignedDecimal::new(self.quantity),
remaining_quantity: SignedDecimal::new(self.quantity),
direction: i32_to_direction(self.position_direction),
order_type: i32_to_order_type(self.order_type),
effect: order_data.position_effect,
leverage: SignedDecimal::new(order_data.leverage),
};
Result::Ok(order)
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DepositInfo {
pub account: String,
pub denom: String,
pub amount: Decimal,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct LiquidationRequest {
pub requestor: String,
pub account: String,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct ContractOrderResult {
pub contract_address: String,
pub order_placement_results: Vec<OrderPlacementResult>,
pub order_execution_results: Vec<OrderExecutionResult>,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct OrderPlacementResult {
pub order_id: u64,
pub status_code: i32,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct OrderExecutionResult {
pub order_id: u64,
pub execution_price: Decimal,
pub executed_quantity: Decimal,
pub total_notional: Decimal,
pub position_direction: String,
}