Struct hyperliquid::Exchange
source · pub struct Exchange {
pub client: Client,
pub chain: Chain,
}
Expand description
Endpoint to interact with and trade on the Hyperliquid chain.
Fields§
§client: Client
§chain: Chain
Implementations§
source§impl Exchange
impl Exchange
sourcepub async fn place_order(
&self,
wallet: Arc<LocalWallet>,
orders: Vec<OrderRequest>,
vault_address: Option<Address>
) -> Result<Response>
pub async fn place_order( &self, wallet: Arc<LocalWallet>, orders: Vec<OrderRequest>, vault_address: Option<Address> ) -> Result<Response>
Place an order
§Arguments
wallet
- The wallet to sign the order withorders
- The orders to placevault_address
- If trading on behalf of a vault, its onchain address in 42-character hexadecimal format e.g.0x0000000000000000000000000000000000000000
§Note
cloid
in argumentorder
is an optional 128 bit hex string, e.g.0x1234567890abcdef1234567890abcdef
Examples found in repository?
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
async fn order_status(info: &Info, exchange: &Exchange, wallet: Arc<LocalWallet>) {
let user = wallet.address();
let vault_address = None;
let cloid = Uuid::new_v4();
let order = OrderRequest {
asset: 4,
is_buy: true,
reduce_only: false,
limit_px: parse_price(2800.0),
sz: parse_size(0.0331, 4),
order_type: OrderType::Limit(Limit { tif: Tif::Gtc }),
cloid: Some(cloid),
};
println!("Placing order with cloid: {}{SEP}", cloid.simple());
let response = exchange
.place_order(wallet, vec![order], vault_address)
.await
.expect("Failed to place order");
println!("Response: {:?}", response);
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
let order_status = info.order_status(user, Oid::Cloid(cloid)).await.unwrap();
println!(
"Order status for {} \n{:?}{SEP}",
cloid.simple(),
order_status
);
}
More examples
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
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::ArbitrumTestnet);
// Create a new wallet with the agent. This agent can't transfer or withdraw funds
// but can place orders.
let agent = LocalWallet::new(&mut thread_rng());
let agent_address = agent.address();
println!("Agent address: {:?}", agent_address);
let res = exchange
.approve_agent(wallet.clone(), agent_address, Some("WETH".to_string()))
.await
.unwrap();
println!("Response: {:?}", res);
// place order with agent
let order_type = OrderType::Limit(Limit { tif: Tif::Gtc });
let order = OrderRequest {
asset: 4,
is_buy: true,
reduce_only: false,
limit_px: "1700".to_string(),
sz: "0.1".to_string(),
order_type,
cloid: None,
};
let vault_address = None;
println!("Placing order with agent...");
let response = exchange
.place_order(wallet.clone(), vec![order], vault_address)
.await
.expect("Failed to place order");
println!("Response: {:?}", response);
}
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
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 info: Info = Hyperliquid::new(Chain::Dev);
let asset = 4;
let sz_decimals = 4;
let order_type = OrderType::Limit(Limit { tif: Tif::Gtc });
let order = OrderRequest {
asset,
is_buy: true,
reduce_only: false,
limit_px: parse_price(2800.0),
sz: parse_size(0.0331, sz_decimals),
order_type,
cloid: None,
};
let vault_address = None;
println!("Placing order...");
let response = exchange
.place_order(wallet.clone(), vec![order], vault_address)
.await
.expect("Failed to place order");
let response = match response {
Response::Ok(order) => order,
Response::Err(error) => panic!("Failed to place order: {:?}", error),
};
println!("Response: {:?}", response.data);
let status_type = &response.data.unwrap();
let status = match status_type {
StatusType::Statuses(statuses) => &statuses[0],
_ => {
panic!("Failed to place order: {:?}", status_type);
}
};
let oid = match status {
Status::Filled(order) => order.oid,
Status::Resting(order) => order.oid,
_ => panic!("Order is not filled or resting"),
};
println!("-----------------");
println!("Fetching order {} status...", oid);
let status = info
.order_status(wallet.address(), Oid::Order(oid))
.await
.expect("Failed to fetch order status");
println!("Order status: {:#?}", status.order);
}
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
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 order_type = OrderType::Limit(Limit { tif: Tif::Gtc });
let cloid = Uuid::new_v4();
let order = OrderRequest {
asset: 4,
is_buy: true,
reduce_only: false,
limit_px: "1800".to_string(),
sz: "0.1".to_string(),
order_type,
cloid: Some(cloid),
};
let vault_address = None;
println!("Placing order...");
let response = exchange
.place_order(wallet.clone(), vec![order], vault_address)
.await
.expect("Failed to place order");
let response = match response {
Response::Ok(order) => order,
Response::Err(error) => panic!("Failed to place order: {:?}", error),
};
let status_type = &response.data.unwrap();
let status = match status_type {
StatusType::Statuses(statuses) => &statuses[0],
_ => {
panic!("Failed to place order: {:?}", status_type);
}
};
let oid = match status {
Status::Filled(order) => order.oid,
Status::Resting(order) => order.oid,
_ => panic!("Order is not filled or resting"),
};
println!("Order placed: {:?}", oid);
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
let limit_px = "1710".to_string();
// Modifying the order
println!("Modifying order {oid} limit price to {limit_px}.");
let order = OrderRequest {
asset: 4,
is_buy: true,
reduce_only: false,
limit_px,
sz: "0.1".to_string(),
order_type: OrderType::Limit(Limit { tif: Tif::Gtc }),
cloid: Some(cloid),
};
let order = ModifyRequest { order, oid };
let response = exchange
.modify_order(wallet.clone(), order, vault_address)
.await
.expect("Failed to modify order");
println!("Response: {:?}", response);
}
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
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 order_type = OrderType::Limit(Limit { tif: Tif::Gtc });
let order = OrderRequest {
asset,
is_buy: true,
reduce_only: false,
limit_px: parse_price(2800.0),
sz: parse_size(0.0331, sz_decimals),
order_type,
cloid: None,
};
let vault_address = None;
println!("Placing order...");
let response = exchange
.place_order(wallet.clone(), vec![order], vault_address)
.await
.expect("Failed to place order");
println!("Response: {:?}", response);
println!("-----------------");
println!("Placing an order with cloid...");
let order_type = OrderType::Limit(Limit { tif: Tif::Gtc });
let cloid = Uuid::new_v4();
let order = OrderRequest {
asset,
is_buy: true,
reduce_only: false,
limit_px: parse_price(2800.0),
sz: parse_size(0.0331, sz_decimals),
order_type,
cloid: Some(cloid),
};
let response = exchange
.place_order(wallet.clone(), vec![order], vault_address)
.await
.expect("Failed to place order");
println!("Response: {:?}", response);
println!("-----------------");
println!("Placing a trigger order with tpsl...");
let order_type = OrderType::Trigger(Trigger {
is_market: false,
trigger_px: parse_price(2800.0),
tpsl: TpSl::Tp,
});
let order = OrderRequest {
asset,
is_buy: true,
reduce_only: false,
limit_px: parse_price(2800.0),
sz: parse_size(0.0331, sz_decimals),
order_type,
cloid: Some(cloid),
};
let response = exchange
.place_order(wallet.clone(), vec![order], vault_address)
.await
.expect("Failed to place order");
println!("Response: {:?}", response);
}
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
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 order_type = OrderType::Limit(Limit { tif: Tif::Gtc });
let order = OrderRequest {
asset: 4,
is_buy: true,
reduce_only: false,
limit_px: "1800".to_string(),
sz: "0.1".to_string(),
order_type,
cloid: None,
};
let vault_address = None;
println!("Placing order...");
let response = exchange
.place_order(wallet.clone(), vec![order], vault_address)
.await
.expect("Failed to place order");
let response = match response {
Response::Ok(order) => order,
Response::Err(error) => panic!("Failed to place order: {:?}", error),
};
let status_type = &response.data.unwrap();
let status = match status_type {
StatusType::Statuses(statuses) => &statuses[0],
_ => {
panic!("Failed to place order: {:?}", status_type);
}
};
let oid = match status {
Status::Filled(order) => order.oid,
Status::Resting(order) => order.oid,
_ => {
panic!("Order is not filled or resting, status: {:?}", status);
}
};
println!("Order placed: {:?}", oid);
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
// Cancel order
println!("Cancelling order with oid {oid}.");
let cancel = CancelRequest { asset: 4, oid };
let vault_address = None;
let response = exchange
.cancel_order(wallet.clone(), vec![cancel], vault_address)
.await
.expect("Failed to cancel order");
println!("Response: {:?}", response);
let cloid = Uuid::new_v4();
println!("Placing order with cloid: {}", cloid.simple());
let order_type = OrderType::Limit(Limit { tif: Tif::Gtc });
let order = OrderRequest {
asset: 4,
is_buy: true,
reduce_only: false,
limit_px: "1700".to_string(),
sz: "0.1".to_string(),
order_type,
cloid: Some(cloid),
};
let vault_address = None;
let response = exchange
.place_order(wallet.clone(), vec![order], vault_address)
.await
.expect("Failed to place order");
let response = match response {
Response::Ok(order) => order,
Response::Err(error) => panic!("Failed to place order: {:?}", error),
};
let data = &response.data.unwrap();
let status = match data {
StatusType::Statuses(statuses) => &statuses[0],
_ => {
panic!("Failed to place order: {:?}", data);
}
};
let oid = match status {
Status::Filled(order) => order.oid,
Status::Resting(order) => order.oid,
_ => panic!("Order is not filled or resting"),
};
println!("Order placed: {:?}", oid);
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
println!("Cancelling order with cloid: {}", cloid.simple());
let cancel = CancelByCloidRequest { asset: 4, cloid };
let response = exchange
.cancel_order_by_cloid(wallet.clone(), vec![cancel], vault_address)
.await
.expect("Failed to cancel order");
println!("Response: {:?}", response);
}
sourcepub async fn cancel_order(
&self,
wallet: Arc<LocalWallet>,
cancels: Vec<CancelRequest>,
vault_address: Option<Address>
) -> Result<Response>
pub async fn cancel_order( &self, wallet: Arc<LocalWallet>, cancels: Vec<CancelRequest>, vault_address: Option<Address> ) -> Result<Response>
Cancel an order
§Arguments
wallet
- The wallet to sign the order withcancels
- The orders to cancelvault_address
- If trading on behalf of a vault, its onchain address in 42-character hexadecimal format e.g.0x0000000000000000000000000000000000000000
Examples found in repository?
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
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 order_type = OrderType::Limit(Limit { tif: Tif::Gtc });
let order = OrderRequest {
asset: 4,
is_buy: true,
reduce_only: false,
limit_px: "1800".to_string(),
sz: "0.1".to_string(),
order_type,
cloid: None,
};
let vault_address = None;
println!("Placing order...");
let response = exchange
.place_order(wallet.clone(), vec![order], vault_address)
.await
.expect("Failed to place order");
let response = match response {
Response::Ok(order) => order,
Response::Err(error) => panic!("Failed to place order: {:?}", error),
};
let status_type = &response.data.unwrap();
let status = match status_type {
StatusType::Statuses(statuses) => &statuses[0],
_ => {
panic!("Failed to place order: {:?}", status_type);
}
};
let oid = match status {
Status::Filled(order) => order.oid,
Status::Resting(order) => order.oid,
_ => {
panic!("Order is not filled or resting, status: {:?}", status);
}
};
println!("Order placed: {:?}", oid);
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
// Cancel order
println!("Cancelling order with oid {oid}.");
let cancel = CancelRequest { asset: 4, oid };
let vault_address = None;
let response = exchange
.cancel_order(wallet.clone(), vec![cancel], vault_address)
.await
.expect("Failed to cancel order");
println!("Response: {:?}", response);
let cloid = Uuid::new_v4();
println!("Placing order with cloid: {}", cloid.simple());
let order_type = OrderType::Limit(Limit { tif: Tif::Gtc });
let order = OrderRequest {
asset: 4,
is_buy: true,
reduce_only: false,
limit_px: "1700".to_string(),
sz: "0.1".to_string(),
order_type,
cloid: Some(cloid),
};
let vault_address = None;
let response = exchange
.place_order(wallet.clone(), vec![order], vault_address)
.await
.expect("Failed to place order");
let response = match response {
Response::Ok(order) => order,
Response::Err(error) => panic!("Failed to place order: {:?}", error),
};
let data = &response.data.unwrap();
let status = match data {
StatusType::Statuses(statuses) => &statuses[0],
_ => {
panic!("Failed to place order: {:?}", data);
}
};
let oid = match status {
Status::Filled(order) => order.oid,
Status::Resting(order) => order.oid,
_ => panic!("Order is not filled or resting"),
};
println!("Order placed: {:?}", oid);
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
println!("Cancelling order with cloid: {}", cloid.simple());
let cancel = CancelByCloidRequest { asset: 4, cloid };
let response = exchange
.cancel_order_by_cloid(wallet.clone(), vec![cancel], vault_address)
.await
.expect("Failed to cancel order");
println!("Response: {:?}", response);
}
sourcepub async fn cancel_order_by_cloid(
&self,
wallet: Arc<LocalWallet>,
cancels: Vec<CancelByCloidRequest>,
vault_address: Option<Address>
) -> Result<Response>
pub async fn cancel_order_by_cloid( &self, wallet: Arc<LocalWallet>, cancels: Vec<CancelByCloidRequest>, vault_address: Option<Address> ) -> Result<Response>
Cancel order(s) by client order id (cloid)
§Arguments
wallet
- The wallet to sign the order withcancels
- The client orders to cancelvault_address
- If trading on behalf of a vault, its onchain address in 42-character hexadecimal format e.g.0x0000000000000000000000000000000000000000
Note: cloid
in argument cancel
is a 128 bit hex string, e.g. 0x1234567890abcdef1234567890abcdef
Examples found in repository?
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
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 order_type = OrderType::Limit(Limit { tif: Tif::Gtc });
let order = OrderRequest {
asset: 4,
is_buy: true,
reduce_only: false,
limit_px: "1800".to_string(),
sz: "0.1".to_string(),
order_type,
cloid: None,
};
let vault_address = None;
println!("Placing order...");
let response = exchange
.place_order(wallet.clone(), vec![order], vault_address)
.await
.expect("Failed to place order");
let response = match response {
Response::Ok(order) => order,
Response::Err(error) => panic!("Failed to place order: {:?}", error),
};
let status_type = &response.data.unwrap();
let status = match status_type {
StatusType::Statuses(statuses) => &statuses[0],
_ => {
panic!("Failed to place order: {:?}", status_type);
}
};
let oid = match status {
Status::Filled(order) => order.oid,
Status::Resting(order) => order.oid,
_ => {
panic!("Order is not filled or resting, status: {:?}", status);
}
};
println!("Order placed: {:?}", oid);
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
// Cancel order
println!("Cancelling order with oid {oid}.");
let cancel = CancelRequest { asset: 4, oid };
let vault_address = None;
let response = exchange
.cancel_order(wallet.clone(), vec![cancel], vault_address)
.await
.expect("Failed to cancel order");
println!("Response: {:?}", response);
let cloid = Uuid::new_v4();
println!("Placing order with cloid: {}", cloid.simple());
let order_type = OrderType::Limit(Limit { tif: Tif::Gtc });
let order = OrderRequest {
asset: 4,
is_buy: true,
reduce_only: false,
limit_px: "1700".to_string(),
sz: "0.1".to_string(),
order_type,
cloid: Some(cloid),
};
let vault_address = None;
let response = exchange
.place_order(wallet.clone(), vec![order], vault_address)
.await
.expect("Failed to place order");
let response = match response {
Response::Ok(order) => order,
Response::Err(error) => panic!("Failed to place order: {:?}", error),
};
let data = &response.data.unwrap();
let status = match data {
StatusType::Statuses(statuses) => &statuses[0],
_ => {
panic!("Failed to place order: {:?}", data);
}
};
let oid = match status {
Status::Filled(order) => order.oid,
Status::Resting(order) => order.oid,
_ => panic!("Order is not filled or resting"),
};
println!("Order placed: {:?}", oid);
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
println!("Cancelling order with cloid: {}", cloid.simple());
let cancel = CancelByCloidRequest { asset: 4, cloid };
let response = exchange
.cancel_order_by_cloid(wallet.clone(), vec![cancel], vault_address)
.await
.expect("Failed to cancel order");
println!("Response: {:?}", response);
}
sourcepub async fn modify_order(
&self,
wallet: Arc<LocalWallet>,
order: ModifyRequest,
vault_address: Option<Address>
) -> Result<Response>
pub async fn modify_order( &self, wallet: Arc<LocalWallet>, order: ModifyRequest, vault_address: Option<Address> ) -> Result<Response>
Modify an order
§Arguments
wallet
- The wallet to sign the order withorder
- The orders to modifyvault_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
Examples found in repository?
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
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 order_type = OrderType::Limit(Limit { tif: Tif::Gtc });
let cloid = Uuid::new_v4();
let order = OrderRequest {
asset: 4,
is_buy: true,
reduce_only: false,
limit_px: "1800".to_string(),
sz: "0.1".to_string(),
order_type,
cloid: Some(cloid),
};
let vault_address = None;
println!("Placing order...");
let response = exchange
.place_order(wallet.clone(), vec![order], vault_address)
.await
.expect("Failed to place order");
let response = match response {
Response::Ok(order) => order,
Response::Err(error) => panic!("Failed to place order: {:?}", error),
};
let status_type = &response.data.unwrap();
let status = match status_type {
StatusType::Statuses(statuses) => &statuses[0],
_ => {
panic!("Failed to place order: {:?}", status_type);
}
};
let oid = match status {
Status::Filled(order) => order.oid,
Status::Resting(order) => order.oid,
_ => panic!("Order is not filled or resting"),
};
println!("Order placed: {:?}", oid);
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
let limit_px = "1710".to_string();
// Modifying the order
println!("Modifying order {oid} limit price to {limit_px}.");
let order = OrderRequest {
asset: 4,
is_buy: true,
reduce_only: false,
limit_px,
sz: "0.1".to_string(),
order_type: OrderType::Limit(Limit { tif: Tif::Gtc }),
cloid: Some(cloid),
};
let order = ModifyRequest { order, oid };
let response = exchange
.modify_order(wallet.clone(), order, vault_address)
.await
.expect("Failed to modify order");
println!("Response: {:?}", response);
}
sourcepub async fn batch_modify_orders(
&self,
wallet: Arc<LocalWallet>,
orders: Vec<ModifyRequest>,
vault_address: Option<Address>
) -> Result<Response>
pub async fn batch_modify_orders( &self, wallet: Arc<LocalWallet>, orders: Vec<ModifyRequest>, vault_address: Option<Address> ) -> Result<Response>
Batch modify orders
§Arguments
wallet
- The wallet to sign the order withorders
- The orders to modifyvault_address
- If trading on behalf of a vault, its onchain address in 42-character hexadecimal format e.g.0x0000000000000000000000000000000000000000
sourcepub async fn update_leverage(
&self,
wallet: Arc<LocalWallet>,
leverage: u32,
asset: u32,
is_cross: bool
) -> Result<Response>
pub async fn update_leverage( &self, wallet: Arc<LocalWallet>, leverage: u32, asset: u32, is_cross: bool ) -> Result<Response>
Update cross or isolated leverage on a coin
§Arguments
wallet
- The wallet to sign the order withleverage
- The new leverage to setasset
- The asset to set the leverage foris_cross
- true if cross leverage, false if isolated leverage
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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 leverage = 2;
let asset = 4;
let is_cross = false;
println!("Updating leverage to {}x ...", leverage);
let res = exchange
.update_leverage(wallet.clone(), leverage, asset, is_cross)
.await
.unwrap();
println!("Response: {:?}", res);
let margin = 1;
println!("--\nUpdating isolated margin for ETH to {margin}% ...");
let res = exchange
.update_isolated_margin(wallet.clone(), margin, asset)
.await
.unwrap();
println!("Response: {:?}", res);
let info: Info = Hyperliquid::new(Chain::Dev);
// user state
let res = info.user_state(wallet.address()).await.unwrap();
println!("--\nUser state: {:?}", res);
}
sourcepub async fn update_isolated_margin(
&self,
wallet: Arc<LocalWallet>,
margin: i64,
asset: u32
) -> Result<Response>
pub async fn update_isolated_margin( &self, wallet: Arc<LocalWallet>, margin: i64, asset: u32 ) -> Result<Response>
Add or remove margin from isolated position
§Arguments
wallet
- The wallet to sign the order withmargin
- The new margin to setasset
- The asset to set the margin for
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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 leverage = 2;
let asset = 4;
let is_cross = false;
println!("Updating leverage to {}x ...", leverage);
let res = exchange
.update_leverage(wallet.clone(), leverage, asset, is_cross)
.await
.unwrap();
println!("Response: {:?}", res);
let margin = 1;
println!("--\nUpdating isolated margin for ETH to {margin}% ...");
let res = exchange
.update_isolated_margin(wallet.clone(), margin, asset)
.await
.unwrap();
println!("Response: {:?}", res);
let info: Info = Hyperliquid::new(Chain::Dev);
// user state
let res = info.user_state(wallet.address()).await.unwrap();
println!("--\nUser state: {:?}", res);
}
sourcepub async fn twap_order(
&self,
wallet: Arc<LocalWallet>,
twap: TwapRequest,
vault_address: Option<Address>
) -> Result<Response>
pub async fn twap_order( &self, wallet: Arc<LocalWallet>, twap: TwapRequest, vault_address: Option<Address> ) -> Result<Response>
Place a TWAP order
§Arguments
wallet
- The wallet to sign the order withtwap
- The twap order to placevault_address
- If trading on behalf of a vault, its onchain address in 42-character hexadecimal format e.g.0x0000000000000000000000000000000000000000
Examples found in repository?
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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 = 0;
let sz_decimals = 2;
let twap = TwapRequest {
asset,
is_buy: true,
reduce_only: false,
duration: 10,
sz: parse_size(13.85, sz_decimals),
randomize: true,
};
let vault_address = None;
println!("Placing a TWAP order...");
let response = exchange
.twap_order(wallet.clone(), twap, vault_address)
.await
.expect("Failed to place twap order");
println!("Response: {:?}", response);
}
sourcepub async fn usdc_transfer(
&self,
from: Arc<LocalWallet>,
destination: Address,
amount: String
) -> Result<Response>
pub async fn usdc_transfer( &self, from: Arc<LocalWallet>, destination: Address, amount: String ) -> Result<Response>
Send usd to another address. This transfer does not touch the EVM bridge. The signature format is human readable for wallet interfaces.
§Arguments
from
- The wallet to sign the transfer withdestination
- The address to send the usd toamount
- The amount of usd to send
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
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::ArbitrumTestnet);
let destination = "0x0D1d9635D0640821d15e323ac8AdADfA9c111414"
.parse()
.expect("Invalid address");
let amount = "1".to_string(); // USD
println!(
"Transferring ${} from {:?} to {:?}",
amount,
wallet.address(),
destination
);
let res = exchange
.usdc_transfer(wallet.clone(), destination, amount)
.await
.unwrap();
println!("Response: {:?}", res);
}
sourcepub async fn withdraw_from_bridge(
&self,
wallet: Arc<LocalWallet>,
destination: Address,
usd: String
) -> Result<Response>
pub async fn withdraw_from_bridge( &self, wallet: Arc<LocalWallet>, destination: Address, usd: String ) -> Result<Response>
Withdraw from bridge
§Arguments
wallet
- The wallet to sign the withdrawal withdestination
- The address to send the usd tousd
- The amount of usd to send
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
async fn main() {
// Key was randomly generated for testing and shouldn't be used with any real funds
let wallet: Arc<LocalWallet> = Arc::new(
"8547bf37e4ac35e85d1e8afc2a2ba5c7f352b8a11ae916e9f14737737e8e0e47"
.parse()
.unwrap(),
);
let exchange: Exchange = Hyperliquid::new(Chain::ArbitrumGoerli);
let destination = "0x0D1d9635D0640821d15e323ac8AdADfA9c111414"
.parse()
.expect("Invalid address");
let usd = "10".to_string(); // USD
println!("Withdrawing ${} from bridge to {:?}", usd, destination);
let res = exchange
.withdraw_from_bridge(wallet.clone(), destination, usd)
.await
.unwrap();
println!("Response: {:?}", res);
}
sourcepub async fn approve_agent(
&self,
wallet: Arc<LocalWallet>,
agent_address: Address,
extra_agent_name: Option<String>
) -> Result<Response>
pub async fn approve_agent( &self, wallet: Arc<LocalWallet>, agent_address: Address, extra_agent_name: Option<String> ) -> Result<Response>
Approve an agent to trade on behalf of the user
§Arguments
wallet
- The wallet to sign the approval withagent_address
- The address of the agent to approveextra_agent_name
- An optional name for the agent
Examples found in repository?
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
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::ArbitrumTestnet);
// Create a new wallet with the agent. This agent can't transfer or withdraw funds
// but can place orders.
let agent = LocalWallet::new(&mut thread_rng());
let agent_address = agent.address();
println!("Agent address: {:?}", agent_address);
let res = exchange
.approve_agent(wallet.clone(), agent_address, Some("WETH".to_string()))
.await
.unwrap();
println!("Response: {:?}", res);
// place order with agent
let order_type = OrderType::Limit(Limit { tif: Tif::Gtc });
let order = OrderRequest {
asset: 4,
is_buy: true,
reduce_only: false,
limit_px: "1700".to_string(),
sz: "0.1".to_string(),
order_type,
cloid: None,
};
let vault_address = None;
println!("Placing order with agent...");
let response = exchange
.place_order(wallet.clone(), vec![order], vault_address)
.await
.expect("Failed to place order");
println!("Response: {:?}", response);
}
sourcepub async fn withdraw(
&self,
from: Arc<LocalWallet>,
usd: String
) -> Result<Response>
pub async fn withdraw( &self, from: Arc<LocalWallet>, usd: String ) -> Result<Response>
Initiate a withdrawal request
§Arguments
from
- The wallet to sign the withdrawal withusd
- The amount of usd to send
sourcepub async fn create_subaccount(
&self,
wallet: Arc<LocalWallet>,
name: String
) -> Result<Response>
pub async fn create_subaccount( &self, wallet: Arc<LocalWallet>, name: String ) -> Result<Response>
Create subaccount for the user
§Arguments
wallet
- The wallet to create the subaccount withname
- The name of the subaccount
sourcepub async fn subaccount_transfer() -> Result<Response>
pub async fn subaccount_transfer() -> Result<Response>
Transfer funds between subaccounts
§Arguments
wallet
- The wallet to sign the transfer withfrom
- The subaccount to transfer from
sourcepub async fn set_referrer(
&self,
wallet: Arc<LocalWallet>,
code: String
) -> Result<Response>
pub async fn set_referrer( &self, wallet: Arc<LocalWallet>, code: String ) -> Result<Response>
Set referrer for the user
§Arguments
wallet
- The wallet to sign the transfer withcode
- The referrer code
sourcepub async fn schedule_cancel(
&self,
wallet: Arc<LocalWallet>,
time: Option<u64>
) -> Result<Response>
pub async fn schedule_cancel( &self, wallet: Arc<LocalWallet>, time: Option<u64> ) -> Result<Response>
Schedule a time in (UTC ms) to cancel all open orders
§Arguments
wallet
- The wallet to sign the transaction withtime
- Optional time in milliseconds to cancel all open orders
§Note
- If
time
isNone
, then unsets any cancel time in the future.time
must be atleast 5 seconds after the current time - Once the time is reached, all open orders will be cancelled and trigger count will be incremented. The max number of triggers is 10 per day. Trigger count resets at 00:00 UTC
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Exchange
impl !RefUnwindSafe for Exchange
impl Send for Exchange
impl Sync for Exchange
impl Unpin for Exchange
impl !UnwindSafe for Exchange
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> FmtForward for T
impl<T> FmtForward for T
source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> R
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.