-
Notifications
You must be signed in to change notification settings - Fork 127
Add Alloy-rs and remove ethers #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
androolloyd
wants to merge
1
commit into
hyperliquid-dex:master
Choose a base branch
from
androolloyd:ethers-alloy-migrate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,24 @@ | ||
use alloy_primitives::{Address, U256}; | ||
use alloy_signer_local::PrivateKeySigner; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And then use alloy::primitives:... |
||
use hyperliquid_rust_sdk::{BaseUrl, ExchangeClient}; | ||
use log::info; | ||
|
||
use ethers::signers::{LocalWallet, Signer}; | ||
use hyperliquid_rust_sdk::{BaseUrl, ClientLimit, ClientOrder, ClientOrderRequest, ExchangeClient}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
env_logger::init(); | ||
// Key was randomly generated for testing and shouldn't be used with any real funds | ||
let wallet: LocalWallet = "e908f86dbb4d55ac876378565aafeabc187f6690f046459397b17d9b9a19688e" | ||
.parse() | ||
.unwrap(); | ||
|
||
let exchange_client = ExchangeClient::new(None, wallet, Some(BaseUrl::Testnet), None, None) | ||
.await | ||
.unwrap(); | ||
|
||
/* | ||
Create a new wallet with the agent. | ||
This agent cannot transfer or withdraw funds, but can for example place orders. | ||
*/ | ||
|
||
let (private_key, response) = exchange_client.approve_agent(None).await.unwrap(); | ||
info!("Agent creation response: {response:?}"); | ||
|
||
let wallet: LocalWallet = private_key.parse().unwrap(); | ||
|
||
info!("Agent address: {:?}", wallet.address()); | ||
let priv_key = "e908f86dbb4d55ac876378565aafeabc187f6690f046459397b17d9b9a19688e"; | ||
let wallet = priv_key.parse::<PrivateKeySigner>().unwrap(); | ||
|
||
let exchange_client = ExchangeClient::new(None, wallet, Some(BaseUrl::Testnet), None, None) | ||
.await | ||
.unwrap(); | ||
let exchange_client = ExchangeClient::new(BaseUrl::Testnet.get_url()); | ||
|
||
let order = ClientOrderRequest { | ||
asset: "ETH".to_string(), | ||
is_buy: true, | ||
reduce_only: false, | ||
limit_px: 1795.0, | ||
sz: 0.01, | ||
cloid: None, | ||
order_type: ClientOrder::Limit(ClientLimit { | ||
tif: "Gtc".to_string(), | ||
}), | ||
}; | ||
let agent = "0x1234567890123456789012345678901234567890".parse::<Address>().unwrap(); | ||
|
||
let response = exchange_client.order(order, None).await.unwrap(); | ||
info!("Approving agent {}", agent); | ||
|
||
info!("Order placed: {response:?}"); | ||
let res = exchange_client.approve_agent(agent, "Testnet".to_string()).await; | ||
match res { | ||
Ok(_) => info!("Successfully approved agent"), | ||
Err(e) => eprintln!("Failed to approve agent: {}", e), | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,21 @@ | ||
use ethers::signers::LocalWallet; | ||
use hyperliquid_rust_sdk::{BaseUrl, ExchangeClient}; | ||
use alloy_primitives::{Address, U256}; | ||
use alloy_signer_local::LocalWallet; | ||
use hyperliquid_rust_sdk::{BaseUrl, ExchangeClient, BuilderInfo}; | ||
use log::info; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
env_logger::init(); | ||
// Key was randomly generated for testing and shouldn't be used with any real funds | ||
let wallet: LocalWallet = "e908f86dbb4d55ac876378565aafeabc187f6690f046459397b17d9b9a19688e" | ||
.parse() | ||
.unwrap(); | ||
let priv_key = "e908f86dbb4d55ac876378565aafeabc187f6690f046459397b17d9b9a19688e"; | ||
let wallet = priv_key.parse::<LocalWallet>().unwrap(); | ||
|
||
let exchange_client = | ||
ExchangeClient::new(None, wallet.clone(), Some(BaseUrl::Testnet), None, None) | ||
.await | ||
.unwrap(); | ||
let exchange_client = ExchangeClient::new(BaseUrl::Testnet.get_url()); | ||
|
||
let max_fee_rate = "0.1%"; | ||
let builder = "0x1ab189B7801140900C711E458212F9c76F8dAC79".to_lowercase(); | ||
let builder = BuilderInfo { | ||
builder: "0x1ab189B7801140900C711E458212F9c76F8dAC79".to_string(), | ||
fee: 1, | ||
}; | ||
|
||
let resp = exchange_client | ||
.approve_builder_fee(builder.to_string(), max_fee_rate.to_string(), Some(&wallet)) | ||
.await; | ||
info!("resp: {resp:#?}"); | ||
info!("Builder info: {:?}", builder); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,28 @@ | ||
use ethers::signers::LocalWallet; | ||
use alloy_primitives::U256; | ||
use alloy_signer_local::PrivateKeySigner; | ||
use hyperliquid_rust_sdk::{BaseUrl, ExchangeClient}; | ||
use log::info; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
env_logger::init(); | ||
// Key was randomly generated for testing and shouldn't be used with any real funds | ||
let wallet: LocalWallet = "e908f86dbb4d55ac876378565aafeabc187f6690f046459397b17d9b9a19688e" | ||
.parse() | ||
.unwrap(); | ||
let priv_key = "e908f86dbb4d55ac876378565aafeabc187f6690f046459397b17d9b9a19688e"; | ||
let wallet = priv_key.parse::<PrivateKeySigner>().unwrap(); | ||
|
||
let exchange_client = ExchangeClient::new(None, wallet, Some(BaseUrl::Testnet), None, None) | ||
.await | ||
.unwrap(); | ||
let exchange_client = ExchangeClient::new(BaseUrl::Testnet.get_url()); | ||
|
||
let usdc = 1.0; // 1 USD | ||
let to_perp = false; | ||
let usdc = 1000; // 1000 USDC | ||
let to_perp = true; // Transfer to perp account | ||
|
||
let res = exchange_client | ||
.class_transfer(usdc, to_perp, None) | ||
info!("Transferring {} USDC to {} account", usdc, if to_perp { "perp" } else { "spot" }); | ||
|
||
let amount = U256::from(usdc); | ||
|
||
exchange_client | ||
.class_transfer(amount, to_perp, "Testnet".to_string()) | ||
.await | ||
.unwrap(); | ||
info!("Class transfer result: {res:?}"); | ||
|
||
info!("Class transfer completed"); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,17 @@ | ||
use ethers::signers::{LocalWallet, Signer}; | ||
use hyperliquid_rust_sdk::{BaseUrl, ExchangeClient, InfoClient}; | ||
use alloy_primitives::U256; | ||
use alloy_signer_local::PrivateKeySigner; | ||
use hyperliquid_rust_sdk::{BaseUrl, ExchangeClient}; | ||
use log::info; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Example assumes you already have a position on ETH so you can update margin | ||
env_logger::init(); | ||
// Key was randomly generated for testing and shouldn't be used with any real funds | ||
let wallet: LocalWallet = "e908f86dbb4d55ac876378565aafeabc187f6690f046459397b17d9b9a19688e" | ||
.parse() | ||
.unwrap(); | ||
let priv_key = "e908f86dbb4d55ac876378565aafeabc187f6690f046459397b17d9b9a19688e"; | ||
let wallet = priv_key.parse::<PrivateKeySigner>().unwrap(); | ||
|
||
let address = wallet.address(); | ||
let exchange_client = ExchangeClient::new(None, wallet, Some(BaseUrl::Testnet), None, None) | ||
.await | ||
.unwrap(); | ||
let info_client = InfoClient::new(None, Some(BaseUrl::Testnet)).await.unwrap(); | ||
let exchange_client = ExchangeClient::new(BaseUrl::Testnet.get_url()); | ||
|
||
let response = exchange_client | ||
.update_leverage(5, "ETH", false, None) | ||
.await | ||
.unwrap(); | ||
info!("Update leverage response: {response:?}"); | ||
|
||
let response = exchange_client | ||
.update_isolated_margin(1.0, "ETH", None) | ||
.await | ||
.unwrap(); | ||
|
||
info!("Update isolated margin response: {response:?}"); | ||
|
||
let user_state = info_client.user_state(address).await.unwrap(); | ||
info!("User state: {user_state:?}"); | ||
// TODO: Implement leverage functionality using the new API | ||
info!("Leverage functionality not yet implemented"); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,56 @@ | ||
/* | ||
This is an example of a basic market making strategy. | ||
|
||
We subscribe to the current mid price and build a market around this price. Whenever our market becomes outdated, we place and cancel orders to renew it. | ||
*/ | ||
use ethers::signers::LocalWallet; | ||
|
||
use hyperliquid_rust_sdk::{MarketMaker, MarketMakerInput}; | ||
use alloy_primitives::Address; | ||
use alloy_signer_local::PrivateKeySigner; | ||
use hyperliquid_rust_sdk::{ | ||
BaseUrl, ExchangeClient, ClientLimit, ClientOrder, ClientOrderRequest, BuilderInfo, | ||
}; | ||
use log::info; | ||
use std::time::Duration; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
env_logger::init(); | ||
// Key was randomly generated for testing and shouldn't be used with any real funds | ||
let wallet: LocalWallet = "e908f86dbb4d55ac876378565aafeabc187f6690f046459397b17d9b9a19688e" | ||
.parse() | ||
.unwrap(); | ||
let market_maker_input = MarketMakerInput { | ||
let priv_key = "e908f86dbb4d55ac876378565aafeabc187f6690f046459397b17d9b9a19688e"; | ||
let wallet = priv_key.parse::<PrivateKeySigner>().unwrap(); | ||
|
||
let exchange_client = ExchangeClient::new(BaseUrl::Testnet.get_url()); | ||
|
||
let builder_info = BuilderInfo { | ||
builder: "test".to_string(), | ||
fee: 1, | ||
}; | ||
|
||
// Place a limit buy order | ||
let buy_order = ClientOrderRequest { | ||
asset: "ETH".to_string(), | ||
target_liquidity: 0.25, | ||
max_bps_diff: 2, | ||
half_spread: 1, | ||
max_absolute_position_size: 0.5, | ||
decimals: 1, | ||
wallet, | ||
is_buy: true, | ||
reduce_only: false, | ||
limit_px: 2000.0, | ||
sz: 0.1, | ||
cloid: None, | ||
order_type: ClientOrder::Limit(ClientLimit { | ||
tif: "Gtc".to_string(), | ||
}), | ||
}; | ||
MarketMaker::new(market_maker_input).await.start().await | ||
|
||
let response = exchange_client.order(buy_order, Some(builder_info.clone())).await.unwrap(); | ||
info!("Buy order response: {response:?}"); | ||
|
||
tokio::time::sleep(Duration::from_secs(5)).await; | ||
|
||
// Place a limit sell order | ||
let sell_order = ClientOrderRequest { | ||
asset: "ETH".to_string(), | ||
is_buy: false, | ||
reduce_only: false, | ||
limit_px: 2100.0, | ||
sz: 0.1, | ||
cloid: None, | ||
order_type: ClientOrder::Limit(ClientLimit { | ||
tif: "Gtc".to_string(), | ||
}), | ||
}; | ||
|
||
let response = exchange_client.order(sell_order, Some(builder_info)).await.unwrap(); | ||
info!("Sell order response: {response:?}"); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use alloy with features instead of everything as a single dependency?
It's easier for dependency management and you don't end up with a mess of dependencies.
Not 100% sure, but this should be it. Alloy core should have remaining deps
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I also agree with this, much cleaner this way