Module: Hyperliquid::Utils

Defined in:
lib/hyperliquid/utils.rb

Class Method Summary collapse

Class Method Details

.address_to_bytes(address) ⇒ Object

Convert hex address string to 20-byte binary.



74
75
76
# File 'lib/hyperliquid/utils.rb', line 74

def address_to_bytes(address)
  [address.delete_prefix("0x")].pack("H40")
end

.float_to_int(x, power) ⇒ Object

Convert float to integer by multiplying by 10^power, validating precision.

Raises:



23
24
25
26
27
28
29
# File 'lib/hyperliquid/utils.rb', line 23

def float_to_int(x, power)
  with_decimals = x * (10**power)
  rounded = with_decimals.round
  raise SigningError, "float_to_int causes rounding: #{x}" if (rounded - with_decimals).abs >= 1e-3

  rounded
end

.float_to_int_for_hashing(x) ⇒ Object

Convert float to int * 10^8 for action hashing.



32
33
34
# File 'lib/hyperliquid/utils.rb', line 32

def float_to_int_for_hashing(x)
  float_to_int(x, 8)
end

.float_to_usd_int(x) ⇒ Object

Convert USD float to int * 10^6.



37
38
39
# File 'lib/hyperliquid/utils.rb', line 37

def float_to_usd_int(x)
  float_to_int(x, 6)
end

.float_to_wire(x) ⇒ Object

Convert float to wire format string: 8 decimal precision, trailing zeros removed. Examples: 100 -> “100”, 1670.1 -> “1670.1”, 0.0147 -> “0.0147”

Raises:



11
12
13
14
15
16
17
18
19
20
# File 'lib/hyperliquid/utils.rb', line 11

def float_to_wire(x)
  rounded = format("%.8f", x)
  raise SigningError, "float_to_wire causes rounding: #{x}" if (Float(rounded) - x).abs >= 1e-12

  rounded = "0" if rounded == "-0"
  # Normalize via BigDecimal to strip trailing zeros, format as fixed-point
  normalized = BigDecimal(rounded).to_s("F")
  # BigDecimal("100.00000000").to_s("F") => "100.0", we want "100"
  normalized.sub(/\.0$/, "")
end

.order_request_to_order_wire(order, asset) ⇒ Object

Convert an OrderRequest hash to wire format for signing.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hyperliquid/utils.rb', line 42

def order_request_to_order_wire(order, asset)
  wire = {
    "a" => asset,
    "b" => order[:is_buy],
    "p" => float_to_wire(order[:limit_px]),
    "s" => float_to_wire(order[:sz]),
    "r" => order[:reduce_only],
    "t" => order_type_to_wire(order[:order_type])
  }
  wire["c"] = order[:cloid].to_raw if order[:cloid]
  wire
end

.order_type_to_wire(order_type) ⇒ Object

Convert order type to wire format.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hyperliquid/utils.rb', line 56

def order_type_to_wire(order_type)
  if order_type[:limit]
    { "limit" => order_type[:limit] }
  elsif order_type[:trigger]
    t = order_type[:trigger]
    {
      "trigger" => {
        "isMarket" => t[:isMarket],
        "triggerPx" => float_to_wire(t[:triggerPx]),
        "tpsl" => t[:tpsl]
      }
    }
  else
    raise SigningError, "Unknown order type: #{order_type}"
  end
end