Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit 82f104d

Browse files
Dev 1969 v4 bindings fix (#453)
* fix: remove options * feat: remove min collateral * feat: update interfaces * fix: replace mtp wth mtpAndPrice * fix: add mtp and price * update * open est fix * handle negative amount
1 parent 250d49d commit 82f104d

22 files changed

+215
-61
lines changed

bindings-test/src/multitest.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use elys_bindings::{
2121
},
2222
query_resp::{
2323
AmmSwapEstimationByDenomResponse, AmmSwapEstimationResponse, AuthAddressesResponse,
24-
BalanceBorrowed, Commitments, Entry, EstakingRewardsResponse,
24+
BalanceBorrowed, CoinNeg, Commitments, Entry, EstakingRewardsResponse,
2525
LeveragelpIsWhitelistedResponse, LeveragelpParams, LeveragelpParamsResponse,
2626
LeveragelpStatusResponse, LeveragelpWhitelistResponse, MasterchefUserPendingRewardResponse,
2727
OracleAssetInfoResponse, PerpetualGetPositionsForAddressResponse, PerpetualMtpResponse,
@@ -33,8 +33,8 @@ use elys_bindings::{
3333
StableStakeParamsData, StableStakeParamsResp, TierCalculateDiscountResponse,
3434
},
3535
types::{
36-
BalanceAvailable, Mtp, OracleAssetInfo, PageResponse, Price, SwapAmountInRoute,
37-
SwapAmountOutRoute,
36+
BalanceAvailable, Mtp, MtpAndPrice, OracleAssetInfo, PageResponse, Price,
37+
SwapAmountInRoute, SwapAmountOutRoute,
3838
},
3939
ElysMsg, ElysQuery,
4040
};
@@ -330,7 +330,12 @@ impl Module for ElysModule {
330330
.find(|mtp| mtp.id == id && mtp.address == address)
331331
.cloned()
332332
{
333-
Ok(to_json_binary(&PerpetualMtpResponse { mtp: Some(mtp) })?)
333+
Ok(to_json_binary(&PerpetualMtpResponse {
334+
mtp: Some(MtpAndPrice {
335+
mtp: mtp,
336+
trading_asset_price: Decimal::zero(),
337+
}),
338+
})?)
334339
} else {
335340
return Err(Error::new(StdError::not_found(
336341
"perpetual trading position",
@@ -341,7 +346,14 @@ impl Module for ElysModule {
341346
let mtps = PERPETUAL_OPENED_POSITION.load(storage)?;
342347
let (mtps, page_resp) = pagination.filter(mtps)?;
343348
Ok(to_json_binary(&PerpetualQueryPositionsResponse {
344-
mtps: Some(mtps),
349+
mtps: Some(
350+
mtps.iter()
351+
.map(|mtp| MtpAndPrice {
352+
mtp: mtp.clone(),
353+
trading_asset_price: Decimal::default(),
354+
})
355+
.collect(),
356+
),
345357
pagination: page_resp,
346358
})?)
347359
}
@@ -382,7 +394,10 @@ impl Module for ElysModule {
382394
funding_rate: Some(Decimal::zero().to_string()),
383395
price_impact: Some(Decimal::zero().to_string()),
384396
borrow_fee: Some(Coin::new(0, "")),
385-
funding_fee: Some(Coin::new(0, "")),
397+
funding_fee: Some(CoinNeg {
398+
amount: Int128::zero(),
399+
denom: "".to_string(),
400+
}),
386401
})?);
387402
}
388403
ElysQuery::AssetProfileEntryAll { .. } => {
@@ -503,7 +518,13 @@ impl Module for ElysModule {
503518
.collect();
504519

505520
Ok(to_json_binary(&PerpetualGetPositionsForAddressResponse {
506-
mtps: user_mtps,
521+
mtps: user_mtps
522+
.iter()
523+
.map(|v| MtpAndPrice {
524+
trading_asset_price: Decimal::zero(),
525+
mtp: v.clone(),
526+
})
527+
.collect::<Vec<_>>(),
507528
pagination: PageResponse::empty(false),
508529
})?)
509530
}

bindings-test/src/tests.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,14 @@ fn query_positions() {
170170

171171
let mtps_found: PerpetualQueryPositionsResponse = app.wrap().query(&req).unwrap();
172172

173-
assert_eq!(mtps_found.mtps.unwrap(), mtps);
173+
let u_mtps = mtps_found
174+
.mtps
175+
.unwrap()
176+
.iter()
177+
.map(|v| v.get_mtp())
178+
.collect::<Vec<_>>();
179+
180+
assert_eq!(u_mtps, mtps);
174181
}
175182

176183
#[test]
@@ -218,7 +225,7 @@ fn query_single_mtp() {
218225

219226
let mtp_found: PerpetualMtpResponse = app.wrap().query(&req).unwrap();
220227

221-
assert_eq!(mtps[0], mtp_found.mtp.unwrap());
228+
assert_eq!(mtps[0], mtp_found.mtp.unwrap().mtp);
222229
}
223230

224231
#[test]

bindings/src/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub enum ElysQuery {
153153
PerpetualQueryPositions { pagination: PageRequest },
154154
#[returns(PerpetualMtpResponse)]
155155
PerpetualMtp { address: String, id: u64 },
156-
#[returns(PerpetualOpenEstimationResponse)]
156+
#[returns(PerpetualOpenEstimationRawResponse)]
157157
PerpetualOpenEstimation {
158158
position: i32,
159159
leverage: SignedDecimal,

bindings/src/query_resp.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ use crate::{
1111
AmmPool, AmmPoolRaw, CoinValue, PerpetualPosition, PoolExtraInfo, StakedPositionRaw,
1212
},
1313
types::{
14-
BalanceAvailable, Mtp, OracleAssetInfo, PageResponse, PoolAsset, Price, StakedPosition,
15-
SwapAmountInRoute, SwapAmountOutRoute, UnstakedPosition, ValidatorDetail, VestingDetail,
14+
BalanceAvailable, Mtp, MtpAndPrice, OracleAssetInfo, PageResponse, PoolAsset, Price,
15+
StakedPosition, SwapAmountInRoute, SwapAmountOutRoute, UnstakedPosition, ValidatorDetail,
16+
VestingDetail,
1617
},
1718
ElysQuerier,
1819
};
@@ -67,13 +68,13 @@ pub struct OracleAssetInfoResponse {
6768

6869
#[cw_serde]
6970
pub struct PerpetualQueryPositionsResponse {
70-
pub mtps: Option<Vec<Mtp>>,
71+
pub mtps: Option<Vec<MtpAndPrice>>,
7172
pub pagination: PageResponse,
7273
}
7374

7475
#[cw_serde]
7576
pub struct PerpetualMtpResponse {
76-
pub mtp: Option<Mtp>,
77+
pub mtp: Option<MtpAndPrice>,
7778
}
7879

7980
#[cw_serde]
@@ -100,6 +101,22 @@ pub struct AmmSwapEstimationByDenomResponse {
100101
pub price_impact: SignedDecimal,
101102
pub slippage: Decimal,
102103
}
104+
105+
#[cw_serde]
106+
pub struct CoinNeg {
107+
pub denom: String,
108+
pub amount: Int128,
109+
}
110+
111+
impl Default for CoinNeg {
112+
fn default() -> Self {
113+
Self {
114+
denom: "".to_string(),
115+
amount: Int128::zero(),
116+
}
117+
}
118+
}
119+
103120
#[cw_serde]
104121
pub struct PerpetualOpenEstimationRawResponse {
105122
pub position: Option<i32>,
@@ -122,7 +139,7 @@ pub struct PerpetualOpenEstimationRawResponse {
122139
pub funding_rate: Option<String>,
123140
pub price_impact: Option<String>,
124141
pub borrow_fee: Option<Coin>,
125-
pub funding_fee: Option<Coin>,
142+
pub funding_fee: Option<CoinNeg>,
126143
}
127144

128145
#[cw_serde]
@@ -147,7 +164,7 @@ pub struct PerpetualOpenEstimationResponse {
147164
pub funding_rate: SignedDecimal,
148165
pub price_impact: SignedDecimal,
149166
pub borrow_fee: Coin,
150-
pub funding_fee: Coin,
167+
pub funding_fee: CoinNeg,
151168
}
152169

153170
impl Into<StdResult<PerpetualOpenEstimationResponse>> for PerpetualOpenEstimationRawResponse {
@@ -192,23 +209,35 @@ impl Into<StdResult<PerpetualOpenEstimationResponse>> for PerpetualOpenEstimatio
192209
price_impact: SignedDecimal::from_str(self.price_impact.unwrap_or_default().as_str())
193210
.unwrap_or_default(),
194211
borrow_fee: self.borrow_fee.unwrap_or_default(),
195-
funding_fee: self.funding_fee.unwrap_or_default(),
212+
funding_fee: self.funding_fee.unwrap_or(CoinNeg {
213+
denom: "".to_string(),
214+
amount: Int128::zero(),
215+
}),
196216
})
197217
}
198218
}
199219

200220
#[cw_serde]
201221
pub struct PerpetualGetPositionsForAddressResponseRaw {
202-
pub mtps: Option<Vec<Mtp>>,
222+
pub mtps: Option<Vec<MtpAndPrice>>,
203223
pub pagination: PageResponse,
204224
}
205225

206226
#[cw_serde]
207227
pub struct PerpetualGetPositionsForAddressResponse {
208-
pub mtps: Vec<Mtp>,
228+
pub mtps: Vec<MtpAndPrice>,
209229
pub pagination: PageResponse,
210230
}
211231

232+
impl PerpetualGetPositionsForAddressResponse {
233+
pub fn get_mtp_vec(&self) -> Vec<Mtp> {
234+
self.mtps
235+
.iter()
236+
.map(|mtp_and_price| mtp_and_price.get_mtp())
237+
.collect()
238+
}
239+
}
240+
212241
#[cw_serde]
213242
pub struct TierCalculateDiscountResponse {
214243
pub discount: String,

bindings/src/trade_shield/types/fees.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use cosmwasm_schema::cw_serde;
22
use cosmwasm_std::Coin;
33

4+
use crate::query_resp::CoinNeg;
5+
46
#[cw_serde]
57
pub struct Fee {
68
pub percent: String,
@@ -15,3 +17,18 @@ impl Default for Fee {
1517
}
1618
}
1719
}
20+
21+
#[cw_serde]
22+
pub struct FeeNeg {
23+
pub percent: String,
24+
pub amount: CoinNeg,
25+
}
26+
27+
impl Default for FeeNeg {
28+
fn default() -> Self {
29+
Self {
30+
percent: "".to_string(),
31+
amount: CoinNeg::default(),
32+
}
33+
}
34+
}

bindings/src/trade_shield/types/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub use coin_value::CoinValue;
2929
pub use date::Date;
3030
pub use denom::ElysDenom;
3131
pub use fees::Fee;
32+
pub use fees::FeeNeg;
3233
pub use perpetual_assets::{PerpetualAsset, PerpetualAssets};
3334
pub use perpetual_order::PerpetualOrder;
3435
pub use perpetual_order_plus::PerpetualOrderPlus;

bindings/src/trade_shield/types/perpetual_order_v2.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use cosmwasm_std::{
77
Storage,
88
};
99

10-
use super::{Fee, OrderPrice, PerpetualOrderType, Status};
10+
use super::{fees::FeeNeg, Fee, OrderPrice, PerpetualOrderType, Status};
1111

1212
#[cw_serde]
1313
pub struct PerpetualOrderV2 {
@@ -25,7 +25,7 @@ pub struct PerpetualOrderV2 {
2525
pub size: Option<DecCoin>,
2626
pub liquidation: Option<SignedDecimal>,
2727
pub borrow_fee: Option<Fee>,
28-
pub funding_fee: Option<Fee>,
28+
pub funding_fee: Option<FeeNeg>,
2929
}
3030

3131
impl PerpetualOrderV2 {
@@ -42,7 +42,7 @@ impl PerpetualOrderV2 {
4242
size: DecCoin,
4343
liquidation: SignedDecimal,
4444
borrow_fee: Fee,
45-
funding_fee: Fee,
45+
funding_fee: FeeNeg,
4646
) -> StdResult<Self> {
4747
let status = if order_type == &PerpetualOrderType::MarketOpen {
4848
Status::Executed
@@ -109,7 +109,7 @@ impl PerpetualOrderV2 {
109109
size: Some(DecCoin::new(Decimal256::zero(), "")),
110110
liquidation: Some(SignedDecimal::zero()),
111111
borrow_fee: Some(Fee::default()),
112-
funding_fee: Some(Fee::default()),
112+
funding_fee: Some(FeeNeg::default()),
113113
};
114114

115115
Ok(order)

bindings/src/types.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,17 @@ impl FromStr for PerpetualPosition {
257257
}
258258
}
259259
#[cw_serde]
260+
pub struct MtpAndPrice {
261+
pub trading_asset_price: Decimal,
262+
pub mtp: Mtp,
263+
}
264+
265+
impl MtpAndPrice {
266+
pub fn get_mtp(&self) -> Mtp {
267+
self.mtp.clone()
268+
}
269+
}
270+
#[cw_serde]
260271
pub struct Mtp {
261272
pub address: String,
262273
pub collateral_asset: String,

contracts/trade-shield-contract/src/action/execute/create_perpetual_order.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ fn create_perpetual_open_order(
189189
percent: open_estimation.borrow_interest_rate.to_string(),
190190
amount: open_estimation.borrow_fee,
191191
},
192-
Fee {
192+
FeeNeg {
193193
percent: open_estimation.funding_rate.to_string(),
194194
amount: open_estimation.funding_fee,
195195
},
@@ -326,7 +326,7 @@ fn create_perpetual_close_order(
326326
.into());
327327
}
328328

329-
if price.quote_denom != mtp.trading_asset {
329+
if price.quote_denom != mtp.mtp.trading_asset {
330330
return Err(StdError::generic_err(
331331
"trigger_price: The quote denom should be the trading asset denom",
332332
)
@@ -371,14 +371,14 @@ fn create_perpetual_close_order(
371371

372372
let order = PerpetualOrderV2::new_close(
373373
&info.sender,
374-
mtp.position,
374+
mtp.mtp.position,
375375
&order_type,
376-
&coin(mtp.collateral.i128() as u128, &mtp.collateral_asset),
377-
&mtp.trading_asset,
378-
&mtp.leverage,
376+
&coin(mtp.mtp.collateral.i128() as u128, &mtp.mtp.collateral_asset),
377+
&mtp.mtp.trading_asset,
378+
&mtp.mtp.leverage,
379379
position_id,
380380
&trigger_price,
381-
&Some(mtp.take_profit_price),
381+
&Some(mtp.mtp.take_profit_price),
382382
&orders,
383383
)?;
384384

@@ -409,8 +409,12 @@ fn create_perpetual_close_order(
409409
let number_of_executed_order = NUMBER_OF_EXECUTED_ORDER.load(deps.storage)? + 1;
410410
NUMBER_OF_EXECUTED_ORDER.save(deps.storage, &number_of_executed_order)?;
411411

412-
let msg =
413-
ElysMsg::perpetual_close_position(creator, position_id, mtp.custody.i128(), &info.sender);
412+
let msg = ElysMsg::perpetual_close_position(
413+
creator,
414+
position_id,
415+
mtp.mtp.custody.i128(),
416+
&info.sender,
417+
);
414418

415419
let reply_info_max_id = MAX_REPLY_ID.load(deps.storage)?;
416420

contracts/trade-shield-contract/src/action/query/get_perpetual_asset.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use cosmwasm_std::{DecCoin, Decimal256, Deps, Env, StdResult};
22
use elys_bindings::{
3-
query_resp::PerpetualGetPositionsForAddressResponse,
43
trade_shield::types::{ElysDenom, PerpetualAsset, PerpetualAssets, PerpetualPositionPlus},
54
ElysQuerier, ElysQuery,
65
};
@@ -12,9 +11,11 @@ pub fn get_perpetuals_assets(
1211
) -> StdResult<PerpetualAssets> {
1312
let querier = ElysQuerier::new(&deps.querier);
1413

15-
let PerpetualGetPositionsForAddressResponse { mtps, .. } =
14+
let perpetual_get_position_for_address_result =
1615
querier.perpetual_get_position_for_address(address, None)?;
1716

17+
let mtps = perpetual_get_position_for_address_result.get_mtp_vec();
18+
1819
let mtps = PerpetualPositionPlus::news(mtps, deps.storage, &querier)?;
1920
let mut perpetual_vec: Vec<PerpetualAsset> = vec![];
2021

0 commit comments

Comments
 (0)