df-hyperliquid-core 0.2.1

Hyperliquid SDK for Rust with full API parity: info, exchange, websockets, HIP-3, HIP-4, HyperEVM and CoreWriter. Exact decimals, the fastest order signing of the SDKs benchmarked, native and wasm.
Documentation
//! Refusals as HyperEVM mainnet wrote them, verbatim (`hyperevm_mainnet.json`), and answers
//! that do not read.

use crate::client::evm::fixtures;
use crate::client::evm::rpc::{BlockTag, CallRequest, ErrorKind, LogFilter, eth};

use super::*;

#[test]
fn a_refusal_is_a_typed_node_error() {
    let not_a_method = eth::chain_id().answer(&fixtures::answer("eth_notAMethod", 0)).unwrap_err();
    let RpcError::Node { method, error } = &not_a_method else { panic!("{not_a_method:?}") };
    assert_eq!(*method, "eth_chainId");
    assert_eq!(
        (error.code, error.message.as_str(), error.kind()),
        (-32601, "Method not found", ErrorKind::MethodNotFound)
    );
    let reverted =
        eth::call(&CallRequest::default(), BlockTag::Latest).answer(&fixtures::answer("eth_call", 2)).unwrap_err();
    let RpcError::Node { error, .. } = reverted else { panic!() };
    assert_eq!((error.code, error.kind(), error.revert()), (3, ErrorKind::Reverted, None));
    let timeout = eth::logs(&LogFilter::default()).answer(&fixtures::answer("eth_getLogs", 0)).unwrap_err();
    let RpcError::Node { error, .. } = timeout else { panic!() };
    assert_eq!((error.code, error.kind()), (-32000, ErrorKind::Server));
    assert_eq!(error.message, "execution aborted (timeout = 5s)");
    // A precompile asked about an account it has nothing for.
    let file = fixtures::mainnet();
    let borrow =
        file["precompiles"].as_array().unwrap().iter().find(|call| call["name"] == "borrowLendUserState").unwrap();
    let refused =
        eth::call(&CallRequest::default(), BlockTag::Latest).answer(fixtures::text(&borrow["answer"])).unwrap_err();
    let RpcError::Node { error, .. } = refused else { panic!() };
    assert_eq!((error.code, error.message.as_str()), (-32003, "EVM error: PrecompileError"));
    assert!(matches!(eth::chain_id().answer("<html>"), Err(RpcError::Unreadable { method: "eth_chainId", .. })));
    assert!(matches!(eth::chain_id().answer(r#"{"jsonrpc":"2.0","id":1}"#), Err(RpcError::Unreadable { .. })));
    assert!(matches!(
        eth::chain_id().answer(r#"{"jsonrpc":"2.0","id":1,"result":3}"#),
        Err(RpcError::Unreadable { .. })
    ));
}

/// `net_version` answers a decimal string, `eth_syncing` false or an object, and a count of a block
/// the node does not know is `null`.
#[test]
fn a_network_id_a_sync_state_and_a_missing_count_read() {
    let answer = |result: &str| format!(r#"{{"jsonrpc":"2.0","id":1,"result":{result}}}"#);
    assert_eq!(eth::net_version().answer(&answer(r#""999""#)), Ok(999));
    assert!(matches!(eth::net_version().answer(&answer(r#""0x3e7""#)), Err(RpcError::Unreadable { .. })));
    assert_eq!(eth::syncing().answer(&answer("false")), Ok(false));
    assert_eq!(eth::syncing().answer(&answer(r#"{"startingBlock":"0x0","currentBlock":"0x1"}"#)), Ok(true));
    assert!(matches!(eth::syncing().answer(&answer("1")), Err(RpcError::Unreadable { method: "eth_syncing", .. })));
    assert_eq!(eth::block_transaction_count(BlockTag::Latest).answer(&answer(r#""0x14""#)), Ok(Some(20)));
    assert_eq!(eth::block_transaction_count(BlockTag::Latest).answer(&answer("null")), Ok(None));
}