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

Commit

Permalink
feat: fix test, add map handling for order and reply, change status ,…
Browse files Browse the repository at this point in the history
… updated doc
  • Loading branch information
politeWall committed Dec 6, 2023
1 parent 3919bf4 commit 94d4f12
Show file tree
Hide file tree
Showing 47 changed files with 259 additions and 156 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.9.0"
version = "0.10.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.7.0", git = "https://github.com/elys-network/bindings", tag = "v0.7.0" }
elys-bindings = { version = "0.8.0", git = "https://github.com/elys-network/bindings", tag = "v0.8.0" }

[dev-dependencies]

cw-multi-test = "0.13.4"
serde_json = "1.0.107"
elys-bindings = { version = "0.7.0", git = "https://github.com/elys-network/bindings", tag = "v0.7.0", features = [
elys-bindings = { version = "0.8.0", git = "https://github.com/elys-network/bindings", tag = "v0.8.0", features = [
"testing",
] }
elys-bindings-test = { version = "0.7.0", git = "https://github.com/elys-network/bindings", tag = "v0.7.0" }
elys-bindings-test = { version = "0.8.0", git = "https://github.com/elys-network/bindings", tag = "v0.8.0" }
40 changes: 35 additions & 5 deletions front_end_script/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ This function retrieves information about multiple order by querying a CosmWasm
#### Usage

```javascript
cancelSpotOrders("order_ids", "order_type", "owner_address");
cancelSpotOrders("order_ids", "order_type", "order_owner");
```

#### Exemple
Expand Down Expand Up @@ -122,20 +122,21 @@ getSpotOrder("your_order_id_here");
getSpotOrder("1");
```

### 5. getSpotOrders(pagination, order_type, owner_address)
### 5. getSpotOrders(pagination, order_type, order_owner, order_status)

This function retrieves information about multiple order by querying a CosmWasm contract on the blockchain.

#### Parameters

- `pagination` {PageRequest} :
- `order_type` (OrderType or null): select the order type that should be querried
- `owner_address` (String or null): select the owner of the order that should be querried
- `order_owner` (String or null): select the owner of the order that should be querried
- `order_status` (String or null) : select the order staus that should be querried (Pending,Executed,Canceled)

#### Usage

```javascript
getSpotOrders({"count_total", "limit", "reverse", "key"}, "order_type", "owner_address")
getSpotOrders({"count_total", "limit", "reverse", "key"}, "order_type", "order_owner", "status")
```

####
Expand All @@ -144,7 +145,8 @@ getSpotOrders({"count_total", "limit", "reverse", "key"}, "order_type", "owner_a
getSpotOrders(
{ count_total: true, limit: 10, reverse: false, key: null },
"stop_loss",
"elys12tzylat4udvjj56uuhu3vj2n4vgp7cf9fwna9w"
"elys12tzylat4udvjj56uuhu3vj2n4vgp7cf9fwna9w",
null
);
```

Expand Down Expand Up @@ -300,6 +302,34 @@ SwapEstimationByDenom({
});
```

### 12. getMarginOrders(pagination, order_type, order_owner, order_status)

This function retrieves information about multiple order by querying a CosmWasm contract on the blockchain.

#### Parameters

- `pagination` {PageRequest} :
- `order_type` (OrderType or null): select the order type that should be querried
- `order_owner` (String or null): select the owner of the order that should be querried
- `order_status` (String or null) : select the order staus that should be querried (pending,executed,canceled)

#### Usage

```javascript
getMarginOrders({"count_total", "limit", "reverse", "key"}, "order_type", "order_owner", status)
```

####

```js
getMarginOrders(
{ count_total: true, limit: 10, reverse: false, key: null },
"stop_loss",
"elys12tzylat4udvjj56uuhu3vj2n4vgp7cf9fwna9w",
"pending"
);
```

## Configuration

Before using these functions, you need to configure the following parameters in the script:
Expand Down
38 changes: 36 additions & 2 deletions front_end_script/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,40 @@ async function cancelSpotOrders(order_type, owner_address, order_ids) {
console.log("create_spot_orders_res:", create_spot_orders_res);
}

async function getSpotOrders(pagination, order_type, owner_address) {
async function getSpotOrders(
pagination,
order_type,
order_owner,
order_status
) {
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,
{
get_spot_orders: {
pagination: pagination,
order_type: order_type,
order_owner: order_owner,
order_status: order_status,
},
}
);
console.log(`Result: `, result);
}

async function getSpotOrders(
pagination,
order_type,
order_owner,
order_status
) {
const sender_wallet = await DirectSecp256k1HdWallet.fromMnemonic(
sender.mnemonic,
{ prefix: "elys" }
Expand All @@ -150,7 +183,8 @@ async function getSpotOrders(pagination, order_type, owner_address) {
get_spot_orders: {
pagination: pagination,
order_type: order_type,
owner_address: owner_address,
order_owner: order_owner,
order_status: order_status,
},
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/action/execute/cancel_margin_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn cancel_margin_order(
});
}

if order.status != Status::NotProcessed {
if order.status != Status::Pending {
return Err(ContractError::CancelStatusError {
order_id,
status: order.status.clone(),
Expand Down
2 changes: 1 addition & 1 deletion src/action/execute/cancel_margin_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn cancel_margin_orders(

if let Some(order) = filtered_order
.iter()
.find(|order| order.status != Status::NotProcessed)
.find(|order| order.status != Status::Pending)
{
return Err(ContractError::CancelStatusError {
order_id: order.order_id,
Expand Down
2 changes: 1 addition & 1 deletion src/action/execute/cancel_spot_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn cancel_spot_order(
});
}

if order.status != Status::NotProcessed {
if order.status != Status::Pending {
return Err(ContractError::CancelStatusError {
order_id,
status: order.status.clone(),
Expand Down
2 changes: 1 addition & 1 deletion src/action/execute/cancel_spot_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn cancel_spot_orders(

if let Some(order) = filtered_order
.iter()
.find(|order| order.status != Status::NotProcessed)
.find(|order| order.status != Status::Pending)
{
return Err(ContractError::CancelStatusError {
order_id: order.order_id,
Expand Down
1 change: 0 additions & 1 deletion src/action/execute/claim_rewards_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::*;
use elys_bindings::types::EarnType;

pub fn claim_rewards_request(
env: Env,
info: MessageInfo,
_deps: DepsMut<ElysQuery>,
withdraw_type: EarnType,
Expand Down
1 change: 0 additions & 1 deletion src/action/execute/claim_validator_commission_request.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::*;
// delegator_address, validator_address, denom
pub fn claim_validator_commission_request(
env: Env,
info: MessageInfo,
_deps: DepsMut<ElysQuery>,
validator_address: String,
Expand Down
2 changes: 1 addition & 1 deletion src/action/execute/close_margin_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::*;

pub fn close_margin_position(
info: MessageInfo,
env: Env,

id: u64,
amount: Int128,
) -> Result<Response<ElysMsg>, ContractError> {
Expand Down
38 changes: 23 additions & 15 deletions src/action/execute/create_margin_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use MarginOrderType::*;
pub fn create_margin_order(
info: MessageInfo,
deps: DepsMut<ElysQuery>,
env: Env,
position: Option<MarginPosition>,
leverage: Option<Decimal>,
trading_asset: Option<String>,
Expand All @@ -31,7 +30,6 @@ pub fn create_margin_order(
create_margin_open_order(
info,
deps,
env,
order_type,
position.unwrap(),
trading_asset.unwrap(),
Expand All @@ -40,14 +38,7 @@ pub fn create_margin_order(
trigger_price,
)
} else {
create_margin_close_order(
info,
deps,
env,
order_type,
position_id.unwrap(),
trigger_price,
)
create_margin_close_order(info, deps, order_type, position_id.unwrap(), trigger_price)
}
}

Expand Down Expand Up @@ -101,7 +92,7 @@ fn check_order_type(
fn create_margin_open_order(
info: MessageInfo,
deps: DepsMut<ElysQuery>,
env: Env,

order_type: MarginOrderType,
position: MarginPosition,
trading_asset: String,
Expand All @@ -120,7 +111,20 @@ fn create_margin_open_order(
return Err(StdError::generic_err("margin position cannot be set at: Unspecified").into());
}

//TODO: include query to test if the collateral is valid
let querrier = ElysQuerier::new(&deps.querier);

let open_estimation = querrier.margin_open_estimation(
position.clone(),
leverage.clone(),
&trading_asset,
collateral.clone(),
take_profit_price.clone(),
Decimal::zero(),
)?;

if !open_estimation.valid_collateral {
return Err(StdError::generic_err("not valid collateral").into());
}

let order = MarginOrder::new_open(
&info.sender,
Expand Down Expand Up @@ -175,7 +179,7 @@ fn create_margin_open_order(
fn create_margin_close_order(
info: MessageInfo,
deps: DepsMut<ElysQuery>,
env: Env,

order_type: MarginOrderType,
position_id: u64,
trigger_price: Option<OrderPrice>,
Expand Down Expand Up @@ -231,14 +235,18 @@ fn create_margin_close_order(
return Ok(resp);
}

let msg = ElysMsg::margin_close_position(&info.sender, position_id, 0 /*AMOUNT */);
let msg = ElysMsg::margin_close_position(
&info.sender,
position_id,
mtp.custodies[0].amount.u128() as i128,
);

let reply_id = MAX_REPLY_ID.load(deps.storage)? + 1;
MAX_REPLY_ID.save(deps.storage, &reply_id)?;

let reply_info = ReplyInfo {
id: reply_id,
reply_type: ReplyType::MarginBrokerMarketOpen,
reply_type: ReplyType::MarginBrokerMarketClose,
data: Some(to_json_binary(&order_id)?),
};

Expand Down
1 change: 0 additions & 1 deletion src/action/execute/eden_cancel_vest_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::*;
use cosmwasm_std::Int128;

pub fn eden_cancel_vest_request(
env: Env,
info: MessageInfo,
_deps: DepsMut<ElysQuery>,
amount: u64,
Expand Down
1 change: 0 additions & 1 deletion src/action/execute/eden_vest_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::*;
use cosmwasm_std::Int128;

pub fn eden_vest_request(
env: Env,
info: MessageInfo,
_deps: DepsMut<ElysQuery>,
amount: u64,
Expand Down
1 change: 0 additions & 1 deletion src/action/execute/elys_cancel_unstake_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::*;
use cosmwasm_std::Coin;

pub fn elys_cancel_unstake_request(
env: Env,
info: MessageInfo,
_deps: DepsMut<ElysQuery>,
validator_address: String,
Expand Down
1 change: 0 additions & 1 deletion src/action/execute/elys_redelegation_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::*;
use cosmwasm_std::Coin;

pub fn elys_redelegation_request(
env: Env,
info: MessageInfo,
_deps: DepsMut<ElysQuery>,
// the amount to be staked in base denomination.
Expand Down
1 change: 0 additions & 1 deletion src/action/execute/stake_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::*;
use cosmwasm_std::Int128;

pub fn stake_request(
env: Env,
info: MessageInfo,
deps: DepsMut<ElysQuery>,
// the amount to be staked in base denomination.
Expand Down
1 change: 0 additions & 1 deletion src/action/execute/unstake_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::*;
use cosmwasm_std::Int128;

pub fn unstake_request(
env: Env,
info: MessageInfo,
_deps: DepsMut<ElysQuery>,
// the amount to be staked in base denomination.
Expand Down
Loading

0 comments on commit 94d4f12

Please sign in to comment.