Skip to content

Commit

Permalink
add normal tpsl
Browse files Browse the repository at this point in the history
  • Loading branch information
dennohpeter committed May 13, 2024
1 parent 1fc43fd commit b908dd0
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
75 changes: 75 additions & 0 deletions examples/normal-tpsl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::sync::Arc;

use ethers::signers::LocalWallet;
use hyperliquid::{
types::{
exchange::request::{Limit, OrderRequest, OrderType, Tif, TpSl, Trigger},
Chain,
},
utils::{parse_price, parse_size},
Exchange, Hyperliquid,
};
#[tokio::main]
async fn main() {
// Key was randomly generated for testing and shouldn't be used with any real funds
let wallet: Arc<LocalWallet> = Arc::new(
"e908f86dbb4d55ac876378565aafeabc187f6690f046459397b17d9b9a19688e"
.parse()
.unwrap(),
);

let exchange: Exchange = Hyperliquid::new(Chain::Dev);

let asset = 4;
let sz_decimals = 4;

let normal = OrderRequest {
asset,
is_buy: true,
reduce_only: false,
limit_px: parse_price(2800.0),
sz: parse_size(0.0331, sz_decimals),
order_type: OrderType::Limit(Limit { tif: Tif::Gtc }),
cloid: None,
};

let tp = OrderRequest {
asset,
is_buy: false,
reduce_only: true,
limit_px: parse_price(2810.0),
sz: parse_size(0.0331, sz_decimals),
order_type: OrderType::Trigger(Trigger {
is_market: true,
trigger_px: parse_price(2810.0),
tpsl: TpSl::Tp,
}),
cloid: None,
};

let sl = OrderRequest {
asset,
is_buy: false,
reduce_only: true,
limit_px: parse_price(2750.0),
sz: parse_size(0.0331, sz_decimals),
order_type: OrderType::Trigger(Trigger {
is_market: true,
trigger_px: parse_price(2750.0),
tpsl: TpSl::Tp,
}),
cloid: None,
};

let vault_address = None;

println!("Placing normal tpsl order...");
let response = exchange
.normal_tpsl(wallet.clone(), vec![normal, tp, sl], vault_address)
.await
.expect("Failed to place order");

println!("Response: {:?}", response);

println!("-----------------");
}
37 changes: 37 additions & 0 deletions src/exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,43 @@ impl Exchange {
self.client.post(&API::Exchange, &request).await
}

/// Place a normal order with tpsl order
///
/// # Arguments
/// * `wallet` - The wallet to sign the order with
/// * `orders` - The orders to place
/// * `vault_address` - If trading on behalf of a vault, its onchain address in 42-character hexadecimal format
/// e.g. `0x0000000000000000000000000000000000000000`
///
/// # Note
/// * `cloid` in argument `order` is an optional 128 bit hex string, e.g. `0x1234567890abcdef1234567890abcdef`
pub async fn normal_tpsl(
&self,
wallet: Arc<LocalWallet>,
orders: Vec<OrderRequest>,
vault_address: Option<Address>,
) -> Result<Response> {
let nonce = self.nonce()?;

let action = Action::Order {
grouping: Grouping::NormalTpsl,
orders,
};

let connection_id = action.connection_id(vault_address, nonce)?;

let signature = self.sign(wallet, connection_id).await?;

let request = Request {
action,
nonce,
signature,
vault_address,
};

self.client.post(&API::Exchange, &request).await
}

/// Cancel an order
///
/// # Arguments
Expand Down

0 comments on commit b908dd0

Please sign in to comment.