#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use std::time::Duration;
use df_hyperliquid_core::account::LocalKey;
use df_hyperliquid_core::client::Transport;
use df_hyperliquid_core::client::native::{Native, clock};
use df_hyperliquid_core::utils::Json;
use df_hyperliquid_core::utils::sign::L1Action;
use df_hyperliquid_core::{Client, Network};
const FILES: [(&str, &str); 13] = [
("deploy_actions", include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/vectors/deploy_actions.json"))),
("l1", include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/vectors/l1.json"))),
("leverage", include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/vectors/leverage.json"))),
("nktkas-0.33.2-trade", include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/vectors/nktkas-0.33.2-trade.json"))),
("nktkas-0.33.2", include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/vectors/nktkas-0.33.2.json"))),
("nktkas-0.33.3-parity", include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/vectors/nktkas-0.33.3-parity.json"))),
("outcome_deploy", include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/vectors/outcome_deploy.json"))),
("outcomes", include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/vectors/outcomes.json"))),
("send_asset", include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/vectors/send_asset.json"))),
("session", include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/vectors/session.json"))),
("tpsl", include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/vectors/tpsl.json"))),
("validator_actions", include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/vectors/validator_actions.json"))),
("vault", include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/vectors/vault.json"))),
];
fn new_key() -> LocalKey {
let mut secret = [0u8; 32];
std::io::Read::read_exact(&mut std::fs::File::open("/dev/urandom").unwrap(), &mut secret).unwrap();
LocalKey::from_bytes(&secret).unwrap()
}
fn l1_actions(json: &Json, out: &mut Vec<Json>) {
match json {
Json::Object(entries) => {
for (key, value) in entries {
if key == "action" && is_l1(value) {
out.push(value.clone());
}
if key != "reference" {
l1_actions(value, out);
}
}
}
Json::Array(items) => items.iter().for_each(|item| l1_actions(item, out)),
_ => {}
}
}
fn is_l1(action: &Json) -> bool {
let kind = action.get("type").and_then(Json::as_str);
kind.is_some_and(|kind| kind != "dummy")
&& action.get("signatureChainId").is_none()
&& action.get("hyperliquidChain").is_none()
}
fn refused_by_the_node(wire: &str) -> bool {
["\"enableAlignedQuoteToken\"", "\"disableAlignedQuoteToken\"", "\"Ip\":\"2001:db8::1\""]
.iter()
.any(|part| wire.contains(part))
}
fn at_nonce(action: Json, nonce: u64) -> Json {
match action {
Json::Object(entries) => Json::Object(
entries
.into_iter()
.map(|(key, value)| match key.as_str() {
"nonce" => (key, Json::Number(nonce.to_string())),
_ => (key, value),
})
.collect(),
),
other => other,
}
}
fn label(action: &Json) -> String {
let kind = action.get("type").and_then(Json::as_str).unwrap_or("?");
match action {
Json::Object(entries) if kind == "spotDeploy" || kind == "perpDeploy" => {
entries.iter().find(|(key, _)| key != "type").map_or(kind.to_owned(), |(key, _)| format!("{kind}.{key}"))
}
_ => kind.to_owned(),
}
}
#[tokio::test]
#[ignore = "posts to testnet; run with `just testnet`"]
async fn testnet_reads_and_hashes_every_l1_action_as_signed() {
let key = new_key();
let me = key.address().to_string().to_lowercase();
let client = Client::reader(Native::new(), Network::Testnet, clock);
let mut seen = Vec::new();
let mut failed = Vec::new();
for (file, text) in FILES {
let mut found = Vec::new();
l1_actions(&Json::parse(text).unwrap(), &mut found);
for action in found {
let wire = action.compact();
if seen.contains(&wire) {
continue;
}
seen.push(wire.clone());
let nonce = clock();
let map = at_nonce(action.clone(), nonce).to_action().unwrap();
let signature = key.sign_digest(&map.digest(nonce, None, Network::Testnet)).unwrap();
let answer = match client.post("/exchange", map.body(nonce, &signature, None)).await {
Ok(body) => body,
Err(error) => format!("{error:?}"),
};
let lower = answer.to_lowercase();
let pass = match refused_by_the_node(&wire) {
true => lower.contains("status: 422") && lower.contains("failed to deserialize"),
false => {
lower.contains(&format!("user or api wallet {me} does not exist"))
|| lower.contains(&format!("must deposit before performing actions. user: {me}"))
}
};
let note = if refused_by_the_node(&wire) { " (refused by the node, as expected)" } else { "" };
println!("{} {file} {}{note}", if pass { "ok " } else { "FAIL" }, label(&action));
if !pass {
failed.push(format!("{file} {wire}\n {answer}"));
}
tokio::time::sleep(Duration::from_millis(120)).await;
}
}
println!("{} actions, {} failed", seen.len(), failed.len());
assert!(failed.is_empty(), "{}", failed.join("\n"));
}