Skip to main content

df_hyperliquid_core/transfer/
bridge.rs

1//! USDC in and out of Hyperliquid through the Arbitrum bridge. In: a USDC `transfer` to the bridge
2//! on Arbitrum, credited to the same address on Hyperliquid in about a minute; under
3//! [`MINIMUM_DEPOSIT`] it is lost. Out: `withdraw3` on the venue ([`crate::transfer::Withdraw3`]).
4//! The reads before a deposit are [`crate::client::evm::rpc`] queries, sent by the caller.
5
6use crate::client::Network;
7use crate::client::evm::rpc::{BlockTag, CallRequest, Query, RpcError, eth};
8use crate::client::evm::u256::NATIVE_DECIMALS;
9use crate::client::evm::u256::U256;
10use crate::transfer::erc20;
11use crate::utils::{Address, Dec, USDC_DECIMALS, hex};
12
13const fn address(text: &str) -> Address {
14    match Address::parse_const(text) {
15        Some(address) => address,
16        None => Address::ZERO,
17    }
18}
19
20/// The Hyperliquid bridge on Arbitrum One.
21pub const MAINNET_BRIDGE: Address = address("0x2df1c51e09aecf9cacb7bc98cb1742757f163df7");
22/// Testnet's bridge, on Arbitrum Sepolia.
23pub const TESTNET_BRIDGE: Address = address("0x08cfc1B6b2dCF36A1480b99353A354AA8AC56f89");
24
25/// Native USDC on Arbitrum One.
26pub const MAINNET_USDC: Address = address("0xaf88d065e77c8cC2239327C5EDb3A432268e5831");
27/// The USDC testnet's bridge takes, on Arbitrum Sepolia.
28pub const TESTNET_USDC: Address = address("0x1baAbB04529D43a73232B713C0FE471f7c7334d5");
29
30/// Below this the bridge credits nothing and the USDC is gone.
31pub const MINIMUM_DEPOSIT: Dec = Dec::from_int(5);
32
33/// The bridge a deposit on `network` goes to.
34#[must_use]
35pub const fn bridge(network: Network) -> Address {
36    match network {
37        Network::Mainnet => MAINNET_BRIDGE,
38        Network::Testnet => TESTNET_BRIDGE,
39    }
40}
41
42/// The USDC contract a deposit on `network` is a transfer of.
43#[must_use]
44pub const fn usdc(network: Network) -> Address {
45    match network {
46        Network::Mainnet => MAINNET_USDC,
47        Network::Testnet => TESTNET_USDC,
48    }
49}
50
51/// A public JSON-RPC for the Arbitrum chain the bridge is on ([`Network::arbitrum_chain_id`]).
52#[must_use]
53pub const fn rpc_url(network: Network) -> &'static str {
54    match network {
55        Network::Mainnet => "https://arb1.arbitrum.io/rpc",
56        Network::Testnet => "https://sepolia-rollup.arbitrum.io/rpc",
57    }
58}
59
60/// A deposit amount refused before any calldata is made.
61#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
62pub enum BadDeposit {
63    /// More than six decimal places, which USDC does not count.
64    #[error("{0} USDC has more than six decimal places")]
65    TooFine(Dec),
66    /// Zero or below.
67    #[error("{0} USDC is not a positive amount")]
68    NotPositive(Dec),
69    /// Below [`MINIMUM_DEPOSIT`], which the bridge would not credit.
70    #[error("{0} USDC is below the bridge's minimum of {MINIMUM_DEPOSIT}; less is lost")]
71    BelowMinimum(Dec),
72    /// Too large to fit a `uint256` in six decimals.
73    #[error("{0} USDC is too large")]
74    TooLarge(Dec),
75}
76
77/// `0x` calldata for USDC `transfer(to, amount)` ([`erc20::transfer`]). The amount is scaled to
78/// six decimals exactly; a seventh place is refused, never rounded.
79pub fn usdc_transfer_calldata(to: Address, amount: Dec) -> Result<String, BadDeposit> {
80    if !amount.is_positive() {
81        return Err(BadDeposit::NotPositive(amount));
82    }
83    if amount.places() > USDC_DECIMALS {
84        return Err(BadDeposit::TooFine(amount));
85    }
86    let units = U256::from_dec(amount, USDC_DECIMALS).map_err(|_| BadDeposit::TooLarge(amount))?;
87    Ok(hex::encode_prefixed(&erc20::transfer(to, units)))
88}
89
90/// [`usdc_transfer_calldata`] to the network's bridge, refusing what it would lose. The
91/// transaction goes to [`usdc`] on [`Network::arbitrum_chain_id`] with no value.
92pub fn deposit_calldata(network: Network, amount: Dec) -> Result<String, BadDeposit> {
93    if amount.is_positive() && amount < MINIMUM_DEPOSIT {
94        return Err(BadDeposit::BelowMinimum(amount));
95    }
96    usdc_transfer_calldata(bridge(network), amount)
97}
98
99/// The owner's USDC on the network's Arbitrum chain: `balanceOf` through `eth_call` at the latest
100/// block. Its answer reads with [`usdc_amount`].
101#[must_use]
102pub fn usdc_balance_request(network: Network, owner: Address) -> Query<Vec<u8>> {
103    eth::call(&CallRequest::to(usdc(network), erc20::balance_of(owner)), BlockTag::Latest)
104}
105
106/// The owner's ETH, for gas, at the latest block. Its answer reads with [`eth_amount`].
107#[must_use]
108pub fn eth_balance_request(owner: Address) -> Query<U256> {
109    eth::balance(owner, BlockTag::Latest)
110}
111
112/// A [`usdc_balance_request`] answer as USDC, exactly.
113pub fn usdc_amount(answer: &[u8]) -> Result<Dec, RpcError> {
114    let unreadable = |detail: String| RpcError::Unreadable { method: "eth_call", detail };
115    let units = erc20::read_amount(answer).map_err(|error| unreadable(error.to_string()))?;
116    units.to_dec(USDC_DECIMALS).map_err(|error| unreadable(error.to_string()))
117}
118
119/// An [`eth_balance_request`] answer as ETH, exactly.
120pub fn eth_amount(wei: U256) -> Result<Dec, RpcError> {
121    wei.to_dec(NATIVE_DECIMALS)
122        .map_err(|error| RpcError::Unreadable { method: "eth_getBalance", detail: error.to_string() })
123}
124
125#[cfg(test)]
126#[path = "tests/bridge.rs"]
127mod tests;