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

Commit

Permalink
Update Documentation and Js Script (#34)
Browse files Browse the repository at this point in the history
* fix: remove warning

* docs: updated doc and js script
  • Loading branch information
politeWall authored Nov 17, 2023
1 parent ec28760 commit 9f531ef
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 11 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@ TradeShield smart contracts are built using the CosmWasm framework, which provid

## Features

### Spot Order

A spot order is fulfilled when the specified price set by the trader aligns with the prevailing market price, prompting the immediate execution of the purchase or sale of a financial asset.

- **Stop Loss Functionality**: Enable users to set automated orders that trigger when the asset's price reaches a specified lower limit, minimizing potential losses.
- **Limit Sell Functionality**: Allow users to set automated orders that execute when the asset's price reaches a specified upper limit, securing profits.
- **Limit Buy Fuctionality**: Allow user to set automated orders that execute when the limit price is reaches, securing profits.

### Margin Order

A margin order involves trading an asset using funds provided by a third party. It allows users to trade larger positions with a smaller initial capital outlay.

- **Long Position Functionality**: Allows users to borrow an asset to sell it, hoping to repurchase it later at a lower price, thus profiting from the difference.
- **Short Position Functionality**: Allows users to borrow an asset, selling it with the expectation of buying it back at a lower price to repay the loan and pocket the difference.
- **Unspecified Position Functionality**: A neutral or unspecified position in a margin order, potentially indicating no active trading or a position that isn't explicitly long or short.

## Getting Started

To start using TradeShield, follow these steps:
Expand Down
171 changes: 169 additions & 2 deletions front_end_script/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This function allows you to create a new spot order by sending a transaction to

#### Parameters

- `order_amm_routes` (Vec): The route for the AMM module to swap the token.
- `order_amm_routes` (Vec<{`pool_id` : u64, `token_out_denom` : String }>): The route for the AMM module to swap the token.
- `order_price` ({`base_denom`:String, `quote_denom`:String, `rate` :String}): Price relates two assets exchange rate that the user should define
- `order_type` (String): The type of the order (e.g., "stop_loss", "limit_sell", "limit_buy").
- `amount_send` (String): The amount of cryptocurrency to send in the order.
Expand All @@ -30,6 +30,19 @@ createSpotOrder(
);
```

#### Exemple

```js
createSpotOrder(
[{ pool_id: 4, token_out_denom: "BTC" }, "AMM_Route_2"],
{ base_denom: "BTC", quote_denom: "ETH", rate: "0.035" },
"limit_buy",
"2.5",
"ETH",
"BTC"
);
```

### 2. cancelSpotOrder(order_id)

This function allows you to cancel an existing order by sending a transaction to the CosmWasm contract.
Expand All @@ -44,7 +57,38 @@ This function allows you to cancel an existing order by sending a transaction to
cancelSpotOrder("your_order_id_here");
```

### 3. getSpotOrder(order_id)
#### Exemple

```js
cancelSpotOrder("8");
```

### 3. cancelSpotOrders(pagination, order_type, owner_address)

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

#### Parameters

- `order_type` (OrderType or null): select the order type that should be canceled
- `owner_address` (String): select the owner of the order that should be canceled

#### Usage

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

#### Exemple

```js
cancelSpotOrders(
"limit_sell",
"elys1x5fehwug2vtkyn4vpunwkfn9zxkpxl8jg0lwuu",
[5, 4, 6]
);
```

### 4. getSpotOrder(order_id)

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

Expand All @@ -58,6 +102,129 @@ This function retrieves information about a specific order by querying a CosmWas
getSpotOrder("your_order_id_here");
```

#### Exemple

```js
getSpotOrder("1");
```

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

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

#### Usage

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

####

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

### 6. createMarginOrder(position, collateral, leverage, borrow_asset, take_profit_price)

This function allows you to create a margin order by sending a transaction to the CosmWasm contract.

#### Parameters

- `position` (String): The type of position for the margin order (e.g., "long", "short").
- `collateral` (Coin): The amount of collateral for the margin order.
- `leverage` (String): The leverage for the margin order.
- `borrow_asset` (String): The asset to borrow for the margin order.
- `take_profit_price` (String): The price at which the order will take profit.

#### Usage

```javascript
createMarginOrder(
"position_type",
"collateral",
"leverage_value",
"borrow_asset",
"take_profit_price"
);
```

#### Exemple

```javascript
createMarginOrder("short", { denom: "BTC", amount: "2" }, "4.3", "ETH", "2.2");
```

### 7. cancelMarginOrder(order_id)

This function allows you to cancel a margin order by sending a transaction to the CosmWasm contract.

#### Parameters

- `order_id` (String): The unique identifier for the order you want to cancel.

#### Usage

```javascript
cancelMarginOrder("your_order_id_here");
```

#### Exemple

```js
cancelMarginOrder("1");
```

### 8. getMarginOrder(address,id)

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

#### Parameters

- `address` (String): The address associated with the margin order.
- `order_id` (String): The unique identifier for the order you want to retrieve.

#### Usage

```javascript
getMarginOrder("your_order_id_here");
```

#### Exemple

```js
getMarginOrder("255");
```

### 9. getMarginOrders(pagination)

This function retrieves multiple margin orders by querying a CosmWasm contract on the blockchain.

#### Parameters

- `pagination` {PageRequest} : Parameters for pagination.

#### Usage

```javascript
getMarginOrders("pagination");
```

#### Exemple

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

## Configuration

Before using these functions, you need to configure the following parameters in the script:
Expand Down
51 changes: 51 additions & 0 deletions front_end_script/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,54 @@ async function cancelSpotOrder(order_id) {
);
console.log("create_spot_order_res:", create_spot_order_res);
}

async function cancelSpotOrders(order_type, owner_address, order_ids) {
const gasPrice = GasPrice.fromString(GASPRICE);
const sender_wallet = await DirectSecp256k1HdWallet.fromMnemonic(
sender.mnemonic,
{ prefix: "elys" }
);
const sender_client = await SigningCosmWasmClient.connectWithSigner(
rpcEndpoint,
sender_wallet
);
const executeFee = calculateFee(300_000, gasPrice);
const msg = {
cancel_spot_orders: {
order_type: order_type,
owner_address: owner_address,
order_ids: order_ids,
},
};

const create_spot_orders_res = await sender_client.execute(
sender.address,
trade_shield_contract_addr,
msg,
executeFee,
""
);
console.log("create_spot_orders_res:", create_spot_orders_res);
}

async function getSpotOrders(pagination, order_type, owner_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,
{
get_spot_orders: {
pagination: pagination,
order_type: order_type,
owner_address: owner_address,
},
}
);
console.log(`Result: `, result);
}
4 changes: 2 additions & 2 deletions src/action/execute/cancel_spot_orders.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{to_binary, Addr, Coin, StdError};
use cosmwasm_std::{to_json_binary, Coin, StdError};

use super::*;

Expand Down Expand Up @@ -56,7 +56,7 @@ pub fn cancel_spot_orders(

Ok(Response::new()
.add_message(refund_msg)
.set_data(to_binary(&order_ids)?))
.set_data(to_json_binary(&order_ids)?))
}

fn filter_order_by_id(
Expand Down
2 changes: 1 addition & 1 deletion src/msg/query_msg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[allow(unused_imports)]
use super::query_resp::*;
use crate::types::{SpotOrderType};
use crate::types::SpotOrderType;
use cosmwasm_schema::{cw_serde, QueryResponses};
#[allow(unused_imports)]
use elys_bindings::query_resp::*;
Expand Down
4 changes: 2 additions & 2 deletions src/tests/cancel_spot_orders/successfully_cancel_orders.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{to_binary, Coin};
use cosmwasm_std::{to_json_binary, Coin};

use super::*;

Expand Down Expand Up @@ -127,7 +127,7 @@ fn successfully_cancel_orders() {
)
.unwrap();

assert_eq!(resp.data.unwrap(), to_binary(&vec![0, 1, 3]).unwrap());
assert_eq!(resp.data.unwrap(), to_json_binary(&vec![0, 1, 3]).unwrap());

assert_eq!(
app.wrap().query_balance("user", "btc").unwrap(),
Expand Down
4 changes: 2 additions & 2 deletions src/tests/cancel_spot_orders/successfully_cancel_orders_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{to_binary, Coin};
use cosmwasm_std::{to_json_binary, Coin};

use super::*;

Expand Down Expand Up @@ -127,7 +127,7 @@ fn successfully_cancel_orders_ids() {
)
.unwrap();

assert_eq!(resp.data.unwrap(), to_binary(&vec![1, 3]).unwrap());
assert_eq!(resp.data.unwrap(), to_json_binary(&vec![1, 3]).unwrap());

assert_eq!(
app.wrap().query_balance("user", "btc").unwrap(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{to_binary, Coin};
use cosmwasm_std::{to_json_binary, Coin};

use super::*;

Expand Down Expand Up @@ -127,7 +127,7 @@ fn successfully_cancel_orders_type() {
)
.unwrap();

assert_eq!(resp.data.unwrap(), to_binary(&vec![0]).unwrap());
assert_eq!(resp.data.unwrap(), to_json_binary(&vec![0]).unwrap());

assert_eq!(
app.wrap().query_balance("user", "btc").unwrap(),
Expand Down

0 comments on commit 9f531ef

Please sign in to comment.