Skip to content
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.

Commit

Permalink
feat: add the new margin open estimation as a query (#67)
Browse files Browse the repository at this point in the history
* feat: add the new margin open estimation as a query

* fix: remove raw dicount field and use user address

* docs: add comment to why user_address is not used

* docs: typo fix

* fix: add the user address field to the swap estimation query

* docs: typo fix

---------

Co-authored-by: Cosmic Vagabond <[email protected]>
  • Loading branch information
politeWall and cosmic-vagabond authored Dec 18, 2023
1 parent 62bcdf3 commit 69595e6
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 31 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trade_shield_contract"
version = "0.11.0"
version = "0.12.0"
edition = "2021"

[lib]
Expand All @@ -14,13 +14,13 @@ thiserror = "1"
schemars = "0.8.1"
cosmwasm-schema = "1.1.4"
cw-utils = "0.13"
elys-bindings = { version = "0.9.0", git = "https://github.com/elys-network/bindings", tag = "v0.9.0" }
elys-bindings = { version = "0.10.0", git = "https://github.com/elys-network/bindings", tag = "v0.10.0" }

[dev-dependencies]

cw-multi-test = "0.13.4"
serde_json = "1.0.107"
elys-bindings = { version = "0.9.0", git = "https://github.com/elys-network/bindings", tag = "v0.9.0", features = [
elys-bindings = { version = "0.10.0", git = "https://github.com/elys-network/bindings", tag = "v0.10.0", features = [
"testing",
] }
elys-bindings-test = { version = "0.9.0", git = "https://github.com/elys-network/bindings", tag = "v0.9.0" }
elys-bindings-test = { version = "0.10.0", git = "https://github.com/elys-network/bindings", tag = "v0.10.0" }
71 changes: 56 additions & 15 deletions front_end_script/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ createSpotOrder(
);
```

#### Exemple
#### Example

```js
createSpotOrder(
Expand Down Expand Up @@ -70,7 +70,7 @@ This function allows you to cancel an existing order by sending a transaction to
cancelSpotOrder("your_order_id_here");
```

#### Exemple
#### Example

```js
cancelSpotOrder("8");
Expand All @@ -92,7 +92,7 @@ This function retrieves information about multiple order by querying a CosmWasm
cancelSpotOrders("order_ids", "order_type", "order_owner");
```

#### Exemple
#### Example

```js
cancelSpotOrders(
Expand All @@ -116,7 +116,7 @@ This function retrieves information about a specific order by querying a CosmWas
getSpotOrder("your_order_id_here");
```

#### Exemple
#### Example

```js
getSpotOrder("1");
Expand Down Expand Up @@ -180,7 +180,7 @@ createMarginOrder(
);
```

#### Exemple
#### Example

```javascript
createMarginOrder(
Expand Down Expand Up @@ -209,7 +209,7 @@ This function allows you to cancel a margin order by sending a transaction to th
cancelMarginOrder("your_order_id_here");
```

#### Exemple
#### Example

```js
cancelMarginOrder("1");
Expand All @@ -229,7 +229,7 @@ This function retrieves information about a specific margin order by querying a
getMarginOrder("your_order_id_here");
```

#### Exemple
#### Example

```js
getMarginOrder("2");
Expand All @@ -250,7 +250,7 @@ This function retrieves information about a specific margin order by querying a
getMarginPosition("your_address", "your_order_id_here");
```

#### Exemple
#### Example

```js
getMarginPosition("elys1x5fehwug2vtkyn4vpunwkfn9zxkpxl8jg0lwuu", "255");
Expand All @@ -270,35 +270,37 @@ This function retrieves multiple margin orders by querying a CosmWasm contract o
getMarginPositions("pagination");
```

#### Exemple
#### Example

```js
getMarginPositions({ count_total: true, limit: 10, reverse: false, key: null });
```

### 11. SwapEstimationByDenom(amount, denom_in, denom_out)
### 11. SwapEstimationByDenom(amount, denom_in, denom_out, user_address)

This function retrieves an estimation of the value obtained by swapping one asset for another.

#### Parameters

- `amount` {Coin} : the amount of the value that you want to send or recive.
- `denom_in` {String} : The asset that you will send.
- `denom_out` {String} : The asset that you will recive.
- `denom_in` (String) : The asset that you will send.
- `denom_out` (String) : The asset that you will recive.
- `user_address` (String): user_address to calculate the discount that the user have access

#### Usage

```js
SwapEstimationByDenom({"amount", "denom"}, "denom_in", "denom_out")
SwapEstimationByDenom({"amount", "denom"}, "denom_in", "denom_out", "user_address")
```

#### Exemple
#### Example

```js
SwapEstimationByDenom({
amount: { amount: 200, denom: "usdc" },
denom_in: "usdc",
denom_out: "atom",
user_address: "elys12tzylat4udvjj56uuhu3vj2n4vgp7cf9fwna9w",
});
```

Expand All @@ -319,7 +321,7 @@ This function retrieves information about multiple order by querying a CosmWasm
getMarginOrders({"count_total", "limit", "reverse", "key"}, "order_type", "order_owner", status)
```

####
#### Example

```js
getMarginOrders(
Expand All @@ -330,6 +332,45 @@ getMarginOrders(
);
```

### 13. marginOpenEstimation (position,leverage,trading_asset,collateral,take_profit_price,user_address)

this function query an estimation on opening a MarginPosition.

#### Parameters

- `position` (String): The type of position for the margin order (e.g., "long", "short")
- `leverage` (String): The leverage for the margin position
- `trading_asset` (String): The trading asset
- `collateral` (Coin {`denom`: String, `amount`: String}) The amount that the user would like to send as a collateral
- `take_profit_price` (String): the take profit price for the open position
- `user_address` (String): user_address to calculate the discount that the user have access

#### Usage

```js
marginOpenEstimation(
"position",
"leverage",
"trading_asset",
"collateral",
"take_profit_price",
"user_address"
);
```

#### Example

```js
marginOpenEstimation(
"long",
"2.5",
"ubtc",
{ denom: "uusdc", amount: "20000" },
"1.5",
"elys12tzylat4udvjj56uuhu3vj2n4vgp7cf9fwna9w"
);
```

## Configuration

Before using these functions, you need to configure the following parameters in the script:
Expand Down
40 changes: 39 additions & 1 deletion front_end_script/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,12 @@ async function getSpotOrders(
console.log(`Result: `, result);
}

async function SwapEstimationByDenom(amount, denom_in, denom_out) {
async function SwapEstimationByDenom(
amount,
denom_in,
denom_out,
user_address
) {
const sender_wallet = await DirectSecp256k1HdWallet.fromMnemonic(
sender.mnemonic,
{ prefix: "elys" }
Expand All @@ -207,6 +212,7 @@ async function SwapEstimationByDenom(amount, denom_in, denom_out) {
amount: amount,
denom_in: denom_in,
denom_out: denom_out,
user_address: user_address,
},
}
);
Expand Down Expand Up @@ -343,3 +349,35 @@ async function getMarginPositions(pagination) {
);
console.log(`Result: `, result);
}

async function marginOpenEstimation(
position,
leverage,
trading_asset,
collateral,
take_profit_price,
user_address
) {
const sender_wallet = await DirectSecp256k1HdWallet.fromMnemonic(
sender.mnemonic,
{ prefix: "elys" }
);
const sender_client = await SigningCosmWasmClient.connectWithSigner(
rpcEndpoint,
sender_wallet
);
const result = await sender_client.queryContractSmart(
trade_shield_contract_addr,
{
margin_open_estimation: {
position: position,
leverage: leverage,
trading_asset: trading_asset,
collateral: collateral,
take_profit_price: take_profit_price,
user_address: user_address,
},
}
);
console.log(`Result: `, result);
}
8 changes: 5 additions & 3 deletions src/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod query {
mod get_margin_positions;
mod get_spot_order;
mod get_spot_orders;
mod margin_open_estimation;
mod swap_estimation_by_denom;

use super::*;
Expand All @@ -28,6 +29,7 @@ pub mod query {
pub use get_margin_positions::get_margin_positions;
pub use get_spot_order::get_spot_order;
pub use get_spot_orders::get_spot_orders;
pub use margin_open_estimation::margin_open_estimation;
pub use swap_estimation_by_denom::swap_estimation_by_denom;
}

Expand All @@ -49,8 +51,8 @@ pub mod execute {
mod eden_vest_request;
mod elys_cancel_unstake_request;
mod elys_redelegation_request;
mod join_amm_pool_request;
mod exit_amm_pool_request;
mod join_amm_pool_request;

use super::*;

Expand All @@ -68,10 +70,10 @@ pub mod execute {
pub use eden_vest_request::eden_vest_request;
pub use elys_cancel_unstake_request::elys_cancel_unstake_request;
pub use elys_redelegation_request::elys_redelegation_request;
pub use exit_amm_pool_request::exit_amm_pool_request;
pub use join_amm_pool_request::join_amm_pool_request;
pub use stake_request::stake_request;
pub use unstake_request::unstake_request;
pub use join_amm_pool_request::join_amm_pool_request;
pub use exit_amm_pool_request::exit_amm_pool_request;
}

pub mod reply {
Expand Down
24 changes: 24 additions & 0 deletions src/action/query/margin_open_estimation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use super::*;
use cosmwasm_std::{Coin, Decimal, StdResult};
use elys_bindings::query_resp::MarginOpenEstimationResponse;

pub fn margin_open_estimation(
deps: Deps<ElysQuery>,
position: MarginPosition,
leverage: Decimal,
trading_asset: String,
collateral: Coin,
take_profit_price: Decimal,
_user_address: String, // Parameter unused until account history work
) -> StdResult<MarginOpenEstimationResponse> {
let querier = ElysQuerier::new(&deps.querier);

querier.margin_open_estimation(
position,
leverage,
trading_asset,
collateral,
take_profit_price,
Decimal::zero(),
)
}
1 change: 1 addition & 0 deletions src/action/query/swap_estimation_by_denom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub fn swap_estimation_by_denom(
amount: Coin,
denom_in: String,
denom_out: String,
_user_address: String,
) -> Result<AmmSwapEstimationByDenomResponse, ContractError> {
let querier = ElysQuerier::new(&deps.querier);

Expand Down
23 changes: 22 additions & 1 deletion src/entry_point/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,30 @@ pub fn query(deps: Deps<ElysQuery>, _env: Env, msg: QueryMsg) -> Result<Binary,
amount,
denom_in,
denom_out,
user_address,
} => Ok(to_json_binary(&query::swap_estimation_by_denom(
deps, amount, denom_in, denom_out,
deps,
amount,
denom_in,
denom_out,
user_address,
)?)?),
GetMarginOrder { id } => Ok(to_json_binary(&query::get_margin_order(deps, id)?)?),
MarginOpenEstimation {
position,
leverage,
trading_asset,
collateral,
take_profit_price,
user_address,
} => Ok(to_json_binary(&query::margin_open_estimation(
deps,
position,
leverage,
trading_asset,
collateral,
take_profit_price,
user_address,
)?)?),
}
}
Loading

0 comments on commit 69595e6

Please sign in to comment.