Skip to content
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

Add Builder Codes #66

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/bin/approve_builder_fee.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use ethers::signers::LocalWallet;
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 exchange_client =
ExchangeClient::new(None, wallet.clone(), Some(BaseUrl::Testnet), None, None)
.await
.unwrap();

let max_fee_rate = "10";
let builder = "0x1ab189B7801140900C711E458212F9c76F8dAC79";

let resp = exchange_client
.approve_builder_fee(builder.to_string(), max_fee_rate.to_string(), Some(&wallet))
.await;
info!("resp: {resp:#?}");
}
7 changes: 7 additions & 0 deletions src/exchange/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,10 @@ pub struct VaultTransfer {
pub struct SetReferrer {
pub code: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But every other action has the derive attribute traits in that order. Should I still change it?

#[serde(rename_all = "camelCase")]
pub struct ApproveBuilderFee {
pub max_fee_rate: String,
pub builder: String,
}
44 changes: 33 additions & 11 deletions src/exchange/exchange_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::signature::sign_typed_data;
use crate::{
exchange::{
actions::{
ApproveAgent, BulkCancel, BulkModify, BulkOrder, SetReferrer, UpdateIsolatedMargin,
UpdateLeverage, UsdSend,
ApproveAgent, ApproveBuilderFee, BulkCancel, BulkModify, BulkOrder, SetReferrer,
UpdateIsolatedMargin, UpdateLeverage, UsdSend,
},
cancel::{CancelRequest, CancelRequestCloid},
modify::{ClientModifyRequest, ModifyRequest},
Expand All @@ -23,7 +23,7 @@ use ethers::{
signers::{LocalWallet, Signer},
types::{Signature, H160, H256},
};
use log::debug;
use log::{debug, info};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
Expand Down Expand Up @@ -66,6 +66,7 @@ pub enum Actions {
VaultTransfer(VaultTransfer),
SpotSend(SpotSend),
SetReferrer(SetReferrer),
ApproveBuilderFee(ApproveBuilderFee),
}

impl Actions {
Expand Down Expand Up @@ -139,14 +140,12 @@ impl ExchangeClient {
.map_err(|e| Error::JsonParse(e.to_string()))?;
debug!("Sending request {res:?}");

serde_json::from_str(
&self
.http_client
.post("/exchange", res)
.await
.map_err(|e| Error::JsonParse(e.to_string()))?,
)
.map_err(|e| Error::JsonParse(e.to_string()))
let output = &self.http_client.post("/exchange", res).await.map_err(|e| {
info!("{e:#?}");
Error::JsonParse(e.to_string())
})?;
info!("{output:?}");
serde_json::from_str(output).map_err(|e| Error::JsonParse(e.to_string()))
}

pub async fn usdc_transfer(
Expand Down Expand Up @@ -662,6 +661,29 @@ impl ExchangeClient {
let signature = sign_l1_action(wallet, connection_id, is_mainnet)?;
self.post(action, signature, timestamp).await
}

pub async fn approve_builder_fee(
&self,
builder: String,
max_fee_rate: String,
wallet: Option<&LocalWallet>,
) -> Result<ExchangeResponseStatus> {
let wallet = wallet.unwrap_or(&self.wallet);
let timestamp = next_nonce();

let action = Actions::ApproveBuilderFee(ApproveBuilderFee {
builder,
max_fee_rate,
});

info!("{timestamp:?}");
let connection_id = action.hash(timestamp, self.vault_address)?;
let action = serde_json::to_value(&action).map_err(|e| Error::JsonParse(e.to_string()))?;

let is_mainnet = self.http_client.is_mainnet();
let signature = sign_l1_action(wallet, connection_id, is_mainnet)?;
self.post(action, signature, timestamp).await
}
}

fn round_to_decimals(value: f64, decimals: u32) -> f64 {
Expand Down
Loading