Class: Hyperliquid::Exchange

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperliquid/exchange.rb

Overview

Exchange API client for write operations (orders, cancels, etc.) Requires a private key for signing transactions

Constant Summary collapse

DEFAULT_SLIPPAGE =

Default slippage for market orders (5%)

0.05
SPOT_ASSET_THRESHOLD =

Spot assets have indices >= 10000

10_000

Instance Method Summary collapse

Constructor Details

#initialize(client:, signer:, info:, expires_after: nil) ⇒ Exchange

Initialize the exchange client

Parameters:



20
21
22
23
24
25
26
# File 'lib/hyperliquid/exchange.rb', line 20

def initialize(client:, signer:, info:, expires_after: nil)
  @client = client
  @signer = signer
  @info = info
  @expires_after = expires_after
  @asset_cache = nil
end

Instance Method Details

#addressString

Get the wallet address

Returns:

  • (String)

    Checksummed Ethereum address



30
31
32
# File 'lib/hyperliquid/exchange.rb', line 30

def address
  @signer.address
end

#batch_modify(modifies:, vault_address: nil) ⇒ Hash

Modify multiple orders at once

Parameters:

  • modifies (Array<Hash>)

    Array of modify hashes with keys: :oid, :coin, :is_buy, :size, :limit_px, :order_type, :reduce_only, :cloid

  • vault_address (String, nil) (defaults to: nil)

    Vault address for vault trading (optional)

Returns:

  • (Hash)

    Batch modify response



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/hyperliquid/exchange.rb', line 245

def batch_modify(modifies:, vault_address: nil)
  nonce = timestamp_ms

  modify_wires = modifies.map do |m|
    order_wire = build_order_wire(
      coin: m[:coin],
      is_buy: m[:is_buy],
      size: m[:size],
      limit_px: m[:limit_px],
      order_type: m[:order_type] || { limit: { tif: 'Gtc' } },
      reduce_only: m[:reduce_only] || false,
      cloid: m[:cloid]
    )
    { oid: normalize_oid(m[:oid]), order: order_wire }
  end

  action = {
    type: 'batchModify',
    modifies: modify_wires
  }

  signature = @signer.sign_l1_action(
    action, nonce,
    vault_address: vault_address,
    expires_after: @expires_after
  )
  post_action(action, signature, nonce, vault_address)
end

#bulk_cancel(cancels:, vault_address: nil) ⇒ Hash

Cancel multiple orders by order ID

Parameters:

  • cancels (Array<Hash>)

    Array of cancel hashes with :coin and :oid

  • vault_address (String, nil) (defaults to: nil)

    Vault address for vault trading (optional)

Returns:

  • (Hash)

    Bulk cancel response



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/hyperliquid/exchange.rb', line 180

def bulk_cancel(cancels:, vault_address: nil)
  nonce = timestamp_ms

  cancel_wires = cancels.map do |c|
    { a: asset_index(c[:coin]), o: c[:oid] }
  end
  action = { type: 'cancel', cancels: cancel_wires }

  signature = @signer.sign_l1_action(
    action, nonce,
    vault_address: vault_address,
    expires_after: @expires_after
  )
  post_action(action, signature, nonce, vault_address)
end

#bulk_cancel_by_cloid(cancels:, vault_address: nil) ⇒ Hash

Cancel multiple orders by client order ID

Parameters:

  • cancels (Array<Hash>)

    Array of cancel hashes with :coin and :cloid

  • vault_address (String, nil) (defaults to: nil)

    Vault address for vault trading (optional)

Returns:

  • (Hash)

    Bulk cancel by cloid response



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/hyperliquid/exchange.rb', line 200

def bulk_cancel_by_cloid(cancels:, vault_address: nil)
  nonce = timestamp_ms

  cancel_wires = cancels.map do |c|
    { asset: asset_index(c[:coin]), cloid: normalize_cloid(c[:cloid]) }
  end
  action = { type: 'cancelByCloid', cancels: cancel_wires }

  signature = @signer.sign_l1_action(
    action, nonce,
    vault_address: vault_address,
    expires_after: @expires_after
  )
  post_action(action, signature, nonce, vault_address)
end

#bulk_orders(orders:, grouping: 'na', vault_address: nil) ⇒ Hash

Place multiple orders in a batch

Parameters:

  • orders (Array<Hash>)

    Array of order hashes with keys: :coin, :is_buy, :size, :limit_px, :order_type, :reduce_only, :cloid

  • grouping (String) (defaults to: 'na')

    Order grouping (“na”, “normalTpsl”, “positionTpsl”)

  • vault_address (String, nil) (defaults to: nil)

    Vault address for vault trading (optional)

Returns:

  • (Hash)

    Bulk order response



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/hyperliquid/exchange.rb', line 78

def bulk_orders(orders:, grouping: 'na', vault_address: nil)
  nonce = timestamp_ms

  order_wires = orders.map do |o|
    build_order_wire(
      coin: o[:coin],
      is_buy: o[:is_buy],
      size: o[:size],
      limit_px: o[:limit_px],
      order_type: o[:order_type] || { limit: { tif: 'Gtc' } },
      reduce_only: o[:reduce_only] || false,
      cloid: o[:cloid]
    )
  end

  action = {
    type: 'order',
    orders: order_wires,
    grouping: grouping
  }

  signature = @signer.sign_l1_action(
    action, nonce,
    vault_address: vault_address,
    expires_after: @expires_after
  )
  post_action(action, signature, nonce, vault_address)
end

#cancel(coin:, oid:, vault_address: nil) ⇒ Hash

Cancel a single order by order ID

Parameters:

  • coin (String)

    Asset symbol

  • oid (Integer)

    Order ID

  • vault_address (String, nil) (defaults to: nil)

    Vault address for vault trading (optional)

Returns:

  • (Hash)

    Cancel response



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/hyperliquid/exchange.rb', line 138

def cancel(coin:, oid:, vault_address: nil)
  nonce = timestamp_ms

  action = {
    type: 'cancel',
    cancels: [{ a: asset_index(coin), o: oid }]
  }

  signature = @signer.sign_l1_action(
    action, nonce,
    vault_address: vault_address,
    expires_after: @expires_after
  )
  post_action(action, signature, nonce, vault_address)
end

#cancel_by_cloid(coin:, cloid:, vault_address: nil) ⇒ Hash

Cancel a single order by client order ID

Parameters:

  • coin (String)

    Asset symbol

  • cloid (Cloid, String)

    Client order ID

  • vault_address (String, nil) (defaults to: nil)

    Vault address for vault trading (optional)

Returns:

  • (Hash)

    Cancel response



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/hyperliquid/exchange.rb', line 159

def cancel_by_cloid(coin:, cloid:, vault_address: nil)
  nonce = timestamp_ms
  cloid_raw = normalize_cloid(cloid)

  action = {
    type: 'cancelByCloid',
    cancels: [{ asset: asset_index(coin), cloid: cloid_raw }]
  }

  signature = @signer.sign_l1_action(
    action, nonce,
    vault_address: vault_address,
    expires_after: @expires_after
  )
  post_action(action, signature, nonce, vault_address)
end

#create_sub_account(name:) ⇒ Hash

Create a sub-account

Parameters:

  • name (String)

    Sub-account name

Returns:

  • (Hash)

    Creation response



502
503
504
505
506
507
# File 'lib/hyperliquid/exchange.rb', line 502

def (name:)
  nonce = timestamp_ms
  action = { type: 'createSubAccount', name: name }
  signature = @signer.sign_l1_action(action, nonce)
  post_action(action, signature, nonce, nil)
end

#market_close(coin:, size: nil, slippage: DEFAULT_SLIPPAGE, cloid: nil, vault_address: nil) ⇒ Hash

Close a position at market price

Parameters:

  • coin (String)

    Asset symbol (perps only)

  • size (Numeric, nil) (defaults to: nil)

    Size to close (nil = close entire position)

  • slippage (Float) (defaults to: DEFAULT_SLIPPAGE)

    Slippage tolerance (default: 5%)

  • cloid (Cloid, String, nil) (defaults to: nil)

    Client order ID (optional)

  • vault_address (String, nil) (defaults to: nil)

    Vault address for vault trading (optional)

Returns:

  • (Hash)

    Order response

Raises:

  • (ArgumentError)


346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/hyperliquid/exchange.rb', line 346

def market_close(coin:, size: nil, slippage: DEFAULT_SLIPPAGE, cloid: nil, vault_address: nil)
  address = vault_address || @signer.address
  state = @info.user_state(address)

  position = state['assetPositions']&.find do |pos|
    pos.dig('position', 'coin') == coin
  end
  raise ArgumentError, "No open position found for #{coin}" unless position

  szi = position.dig('position', 'szi').to_f
  is_buy = szi.negative?
  close_size = size || szi.abs

  mids = @info.all_mids
  mid = mids[coin]&.to_f
  raise ArgumentError, "Unknown asset or no price available: #{coin}" unless mid&.positive?

  slippage_price = calculate_slippage_price(coin, mid, is_buy, slippage)

  order(
    coin: coin,
    is_buy: is_buy,
    size: close_size,
    limit_px: slippage_price,
    order_type: { limit: { tif: 'Ioc' } },
    reduce_only: true,
    cloid: cloid,
    vault_address: vault_address
  )
end

#market_order(coin:, is_buy:, size:, slippage: DEFAULT_SLIPPAGE, vault_address: nil) ⇒ Hash

Place a market order (aggressive limit IoC with slippage)

Parameters:

  • coin (String)

    Asset symbol

  • is_buy (Boolean)

    True for buy, false for sell

  • size (String, Numeric)

    Order size

  • slippage (Float) (defaults to: DEFAULT_SLIPPAGE)

    Slippage tolerance (default: 0.05 = 5%)

  • vault_address (String, nil) (defaults to: nil)

    Vault address for vault trading (optional)

Returns:

  • (Hash)

    Order response

Raises:

  • (ArgumentError)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/hyperliquid/exchange.rb', line 114

def market_order(coin:, is_buy:, size:, slippage: DEFAULT_SLIPPAGE, vault_address: nil)
  # Get current mid price
  mids = @info.all_mids
  mid = mids[coin]&.to_f
  raise ArgumentError, "Unknown asset or no price available: #{coin}" unless mid&.positive?

  # Apply slippage and round to appropriate precision
  slippage_price = calculate_slippage_price(coin, mid, is_buy, slippage)

  order(
    coin: coin,
    is_buy: is_buy,
    size: size,
    limit_px: slippage_price,
    order_type: { limit: { tif: 'Ioc' } },
    vault_address: vault_address
  )
end

#modify_order(oid:, coin:, is_buy:, size:, limit_px:, order_type: { limit: { tif: 'Gtc' } }, reduce_only: false, cloid: nil, vault_address: nil) ⇒ Hash

Modify a single existing order

Parameters:

  • oid (Integer, Cloid, String)

    Order ID or client order ID to modify

  • coin (String)

    Asset symbol (e.g., “BTC”)

  • is_buy (Boolean)

    True for buy, false for sell

  • size (String, Numeric)

    New order size

  • limit_px (String, Numeric)

    New limit price

  • order_type (Hash) (defaults to: { limit: { tif: 'Gtc' } })

    Order type config (default: { limit: { tif: “Gtc” } })

  • reduce_only (Boolean) (defaults to: false)

    Reduce-only flag (default: false)

  • cloid (Cloid, String, nil) (defaults to: nil)

    Client order ID for the modified order (optional)

  • vault_address (String, nil) (defaults to: nil)

    Vault address for vault trading (optional)

Returns:

  • (Hash)

    Modify response



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/hyperliquid/exchange.rb', line 227

def modify_order(oid:, coin:, is_buy:, size:, limit_px:,
                 order_type: { limit: { tif: 'Gtc' } },
                 reduce_only: false, cloid: nil, vault_address: nil)
  batch_modify(
    modifies: [{
      oid: oid, coin: coin, is_buy: is_buy, size: size,
      limit_px: limit_px, order_type: order_type,
      reduce_only: reduce_only, cloid: cloid
    }],
    vault_address: vault_address
  )
end

#order(coin:, is_buy:, size:, limit_px:, order_type: { limit: { tif: 'Gtc' } }, reduce_only: false, cloid: nil, vault_address: nil) ⇒ Hash

Place a single order

Parameters:

  • coin (String)

    Asset symbol (e.g., “BTC”)

  • is_buy (Boolean)

    True for buy, false for sell

  • size (String, Numeric)

    Order size

  • limit_px (String, Numeric)

    Limit price

  • order_type (Hash) (defaults to: { limit: { tif: 'Gtc' } })

    Order type config (default: { limit: { tif: “Gtc” } })

  • reduce_only (Boolean) (defaults to: false)

    Reduce-only flag (default: false)

  • cloid (Cloid, String, nil) (defaults to: nil)

    Client order ID (optional)

  • vault_address (String, nil) (defaults to: nil)

    Vault address for vault trading (optional)

Returns:

  • (Hash)

    Order response



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hyperliquid/exchange.rb', line 44

def order(coin:, is_buy:, size:, limit_px:, order_type: { limit: { tif: 'Gtc' } },
          reduce_only: false, cloid: nil, vault_address: nil)
  nonce = timestamp_ms

  order_wire = build_order_wire(
    coin: coin,
    is_buy: is_buy,
    size: size,
    limit_px: limit_px,
    order_type: order_type,
    reduce_only: reduce_only,
    cloid: cloid
  )

  action = {
    type: 'order',
    orders: [order_wire],
    grouping: 'na'
  }

  signature = @signer.sign_l1_action(
    action, nonce,
    vault_address: vault_address,
    expires_after: @expires_after
  )
  post_action(action, signature, nonce, vault_address)
end

#reload_metadata!Object

Clear the asset metadata cache Call this if metadata has been updated



574
575
576
# File 'lib/hyperliquid/exchange.rb', line 574

def reload_metadata!
  @asset_cache = nil
end

#schedule_cancel(time: nil, vault_address: nil) ⇒ Hash

Schedule automatic cancellation of all orders

Parameters:

  • time (Integer, nil) (defaults to: nil)

    UTC timestamp in milliseconds to cancel at (nil to activate with server default)

  • vault_address (String, nil) (defaults to: nil)

    Vault address for vault trading (optional)

Returns:

  • (Hash)

    Schedule cancel response



325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/hyperliquid/exchange.rb', line 325

def schedule_cancel(time: nil, vault_address: nil)
  nonce = timestamp_ms

  action = { type: 'scheduleCancel' }
  action[:time] = time if time

  signature = @signer.sign_l1_action(
    action, nonce,
    vault_address: vault_address,
    expires_after: @expires_after
  )
  post_action(action, signature, nonce, vault_address)
end

#send_asset(destination:, source_dex:, destination_dex:, token:, amount:) ⇒ Hash

Move assets between DEX instances

Parameters:

  • destination (String)

    Destination wallet address

  • source_dex (String)

    Source DEX identifier

  • destination_dex (String)

    Destination DEX identifier

  • token (String)

    Token identifier

  • amount (String, Numeric)

    Amount to send

Returns:

  • (Hash)

    Transfer response



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/hyperliquid/exchange.rb', line 474

def send_asset(destination:, source_dex:, destination_dex:, token:, amount:)
  nonce = timestamp_ms
  action = {
    type: 'sendAsset',
    signatureChainId: '0x66eee',
    hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @signer.instance_variable_get(:@testnet)),
    destination: destination,
    sourceDex: source_dex,
    destinationDex: destination_dex,
    token: token,
    amount: amount.to_s,
    fromSubAccount: '',
    nonce: nonce
  }
  signature = @signer.sign_user_signed_action(
    {
      destination: destination, sourceDex: source_dex, destinationDex: destination_dex,
      token: token, amount: amount.to_s, fromSubAccount: '', nonce: nonce
    },
    'HyperliquidTransaction:SendAsset',
    Signing::EIP712::SEND_ASSET_TYPES
  )
  post_action(action, signature, nonce, nil)
end

#set_referrer(code:) ⇒ Hash

Set referral code

Parameters:

  • code (String)

    Referral code

Returns:

  • (Hash)

    Set referrer response



565
566
567
568
569
570
# File 'lib/hyperliquid/exchange.rb', line 565

def set_referrer(code:)
  nonce = timestamp_ms
  action = { type: 'setReferrer', code: code }
  signature = @signer.sign_l1_action(action, nonce)
  post_action(action, signature, nonce, nil)
end

#spot_send(amount:, destination:, token:) ⇒ Hash

Transfer a spot token to another address

Parameters:

  • amount (String, Numeric)

    Amount to send

  • destination (String)

    Destination wallet address

  • token (String)

    Token identifier

Returns:

  • (Hash)

    Transfer response



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/hyperliquid/exchange.rb', line 404

def spot_send(amount:, destination:, token:)
  nonce = timestamp_ms
  action = {
    type: 'spotSend',
    signatureChainId: '0x66eee',
    hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @signer.instance_variable_get(:@testnet)),
    destination: destination,
    token: token,
    amount: amount.to_s,
    time: nonce
  }
  signature = @signer.sign_user_signed_action(
    { destination: destination, token: token, amount: amount.to_s, time: nonce },
    'HyperliquidTransaction:SpotSend',
    Signing::EIP712::SPOT_SEND_TYPES
  )
  post_action(action, signature, nonce, nil)
end

#sub_account_spot_transfer(sub_account_user:, is_deposit:, token:, amount:) ⇒ Hash

Transfer spot tokens to/from a sub-account

Parameters:

  • sub_account_user (String)

    Sub-account wallet address

  • is_deposit (Boolean)

    True to deposit into sub-account, false to withdraw

  • token (String)

    Token identifier

  • amount (String, Numeric)

    Amount to transfer

Returns:

  • (Hash)

    Transfer response



532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/hyperliquid/exchange.rb', line 532

def (sub_account_user:, is_deposit:, token:, amount:)
  nonce = timestamp_ms
  action = {
    type: 'subAccountSpotTransfer',
    subAccountUser: ,
    isDeposit: is_deposit,
    token: token,
    amount: amount.to_s
  }
  signature = @signer.sign_l1_action(action, nonce)
  post_action(action, signature, nonce, nil)
end

#sub_account_transfer(sub_account_user:, is_deposit:, usd:) ⇒ Hash

Transfer USDC to/from a sub-account

Parameters:

  • sub_account_user (String)

    Sub-account wallet address

  • is_deposit (Boolean)

    True to deposit into sub-account, false to withdraw

  • usd (Numeric)

    Amount in USD

Returns:

  • (Hash)

    Transfer response



514
515
516
517
518
519
520
521
522
523
524
# File 'lib/hyperliquid/exchange.rb', line 514

def (sub_account_user:, is_deposit:, usd:)
  nonce = timestamp_ms
  action = {
    type: 'subAccountTransfer',
    subAccountUser: ,
    isDeposit: is_deposit,
    usd: float_to_usd_int(usd)
  }
  signature = @signer.sign_l1_action(action, nonce)
  post_action(action, signature, nonce, nil)
end

#update_isolated_margin(coin:, amount:, vault_address: nil) ⇒ Hash

Add or remove isolated margin for a position

Parameters:

  • coin (String)

    Asset symbol (perps only)

  • amount (Numeric)

    Amount in USD (positive to add, negative to remove)

  • vault_address (String, nil) (defaults to: nil)

    Vault address for vault trading (optional)

Returns:

  • (Hash)

    Margin update response



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/hyperliquid/exchange.rb', line 303

def update_isolated_margin(coin:, amount:, vault_address: nil)
  nonce = timestamp_ms

  action = {
    type: 'updateIsolatedMargin',
    asset: asset_index(coin),
    isBuy: true,
    ntli: float_to_usd_int(amount)
  }

  signature = @signer.sign_l1_action(
    action, nonce,
    vault_address: vault_address,
    expires_after: @expires_after
  )
  post_action(action, signature, nonce, vault_address)
end

#update_leverage(coin:, leverage:, is_cross: true, vault_address: nil) ⇒ Hash

Set cross or isolated leverage for a coin

Parameters:

  • coin (String)

    Asset symbol (perps only)

  • leverage (Integer)

    Leverage value

  • is_cross (Boolean) (defaults to: true)

    True for cross margin, false for isolated (default: true)

  • vault_address (String, nil) (defaults to: nil)

    Vault address for vault trading (optional)

Returns:

  • (Hash)

    Leverage update response



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/hyperliquid/exchange.rb', line 280

def update_leverage(coin:, leverage:, is_cross: true, vault_address: nil)
  nonce = timestamp_ms

  action = {
    type: 'updateLeverage',
    asset: asset_index(coin),
    isCross: is_cross,
    leverage: leverage
  }

  signature = @signer.sign_l1_action(
    action, nonce,
    vault_address: vault_address,
    expires_after: @expires_after
  )
  post_action(action, signature, nonce, vault_address)
end

#usd_class_transfer(amount:, to_perp:) ⇒ Hash

Move USDC between perp and spot accounts

Parameters:

  • amount (String, Numeric)

    Amount to transfer

  • to_perp (Boolean)

    True to move to perp, false to move to spot

Returns:

  • (Hash)

    Transfer response



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/hyperliquid/exchange.rb', line 427

def usd_class_transfer(amount:, to_perp:)
  nonce = timestamp_ms
  action = {
    type: 'usdClassTransfer',
    signatureChainId: '0x66eee',
    hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @signer.instance_variable_get(:@testnet)),
    amount: amount.to_s,
    toPerp: to_perp,
    nonce: nonce
  }
  signature = @signer.sign_user_signed_action(
    { amount: amount.to_s, toPerp: to_perp, nonce: nonce },
    'HyperliquidTransaction:UsdClassTransfer',
    Signing::EIP712::USD_CLASS_TRANSFER_TYPES
  )
  post_action(action, signature, nonce, nil)
end

#usd_send(amount:, destination:) ⇒ Hash

Transfer USDC to another address

Parameters:

  • amount (String, Numeric)

    Amount to send

  • destination (String)

    Destination wallet address

Returns:

  • (Hash)

    Transfer response



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/hyperliquid/exchange.rb', line 381

def usd_send(amount:, destination:)
  nonce = timestamp_ms
  action = {
    type: 'usdSend',
    signatureChainId: '0x66eee',
    hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @signer.instance_variable_get(:@testnet)),
    destination: destination,
    amount: amount.to_s,
    time: nonce
  }
  signature = @signer.sign_user_signed_action(
    { destination: destination, amount: amount.to_s, time: nonce },
    'HyperliquidTransaction:UsdSend',
    Signing::EIP712::USD_SEND_TYPES
  )
  post_action(action, signature, nonce, nil)
end

#vault_transfer(vault_address:, is_deposit:, usd:) ⇒ Hash

Deposit or withdraw USDC to/from a vault

Parameters:

  • vault_address (String)

    Vault wallet address

  • is_deposit (Boolean)

    True to deposit, false to withdraw

  • usd (Numeric)

    Amount in USD

Returns:

  • (Hash)

    Vault transfer response



550
551
552
553
554
555
556
557
558
559
560
# File 'lib/hyperliquid/exchange.rb', line 550

def vault_transfer(vault_address:, is_deposit:, usd:)
  nonce = timestamp_ms
  action = {
    type: 'vaultTransfer',
    vaultAddress: vault_address,
    isDeposit: is_deposit,
    usd: float_to_usd_int(usd)
  }
  signature = @signer.sign_l1_action(action, nonce)
  post_action(action, signature, nonce, nil)
end

#withdraw_from_bridge(amount:, destination:) ⇒ Hash

Withdraw USDC via the bridge

Parameters:

  • amount (String, Numeric)

    Amount to withdraw

  • destination (String)

    Destination wallet address

Returns:

  • (Hash)

    Withdrawal response



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/hyperliquid/exchange.rb', line 449

def withdraw_from_bridge(amount:, destination:)
  nonce = timestamp_ms
  action = {
    type: 'withdraw3',
    signatureChainId: '0x66eee',
    hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @signer.instance_variable_get(:@testnet)),
    destination: destination,
    amount: amount.to_s,
    time: nonce
  }
  signature = @signer.sign_user_signed_action(
    { destination: destination, amount: amount.to_s, time: nonce },
    'HyperliquidTransaction:Withdraw',
    Signing::EIP712::WITHDRAW_TYPES
  )
  post_action(action, signature, nonce, nil)
end