Class: Hyperliquid::Exchange
- Inherits:
-
Object
- Object
- Hyperliquid::Exchange
- 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 Attribute Summary collapse
-
#expires_after ⇒ Object
writeonly
Update the global expiration timestamp applied to subsequent L1 actions.
Instance Method Summary collapse
-
#address ⇒ String
Get the wallet address.
-
#agent_enable_dex_abstraction(vault_address: nil) ⇒ Hash
Enable HIP-3 DEX abstraction via agent (L1 action, enable only) This allows agents to enable DEX abstraction for the account they’re trading on behalf of.
-
#agent_set_abstraction(abstraction:, vault_address: nil) ⇒ Hash
Set the agent abstraction mode (L1 action ‘agentSetAbstraction`).
-
#approve_agent(agent_address:, agent_name: nil) ⇒ Hash
Authorize an agent wallet to trade on behalf of this account.
-
#approve_builder_fee(builder:, max_fee_rate:) ⇒ Hash
Approve a builder fee rate for a builder address Users must approve a builder before orders with that builder can be placed.
-
#batch_modify(modifies:, vault_address: nil) ⇒ Hash
Modify multiple orders at once.
-
#bulk_cancel(cancels:, vault_address: nil) ⇒ Hash
Cancel multiple orders by order ID.
-
#bulk_cancel_by_cloid(cancels:, vault_address: nil) ⇒ Hash
Cancel multiple orders by client order ID.
-
#bulk_orders(orders:, grouping: 'na', vault_address: nil, builder: nil) ⇒ Hash
Place multiple orders in a batch.
-
#cancel(coin:, oid:, vault_address: nil) ⇒ Hash
Cancel a single order by order ID.
-
#cancel_by_cloid(coin:, cloid:, vault_address: nil) ⇒ Hash
Cancel a single order by client order ID.
-
#convert_to_multi_sig_user(authorized_users:, threshold:) ⇒ Hash
Convert this account into a multi-sig user (‘convertToMultiSigUser` user-signed action).
-
#create_sub_account(name:) ⇒ Hash
Create a sub-account.
-
#gossip_priority_bid(slot_id:, ip:, max_gas:, vault_address: nil) ⇒ Hash
Submit a priority-bid gossip message (L1 action ‘gossipPriorityBid`).
-
#initialize(client:, signer:, info:, testnet: false, expires_after: nil) ⇒ Exchange
constructor
Initialize the exchange client.
-
#market_close(coin:, size: nil, slippage: DEFAULT_SLIPPAGE, cloid: nil, vault_address: nil, builder: nil) ⇒ Hash
Close a position at market price.
-
#market_order(coin:, is_buy:, size:, slippage: DEFAULT_SLIPPAGE, vault_address: nil, builder: nil) ⇒ Hash
Place a market order (aggressive limit IoC with slippage).
-
#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.
-
#noop(nonce: nil, vault_address: nil) ⇒ Hash
No-op L1 action — useful for burning a specific nonce slot without side effects.
-
#order(coin:, is_buy:, size:, limit_px:, order_type: { limit: { tif: 'Gtc' } }, reduce_only: false, cloid: nil, vault_address: nil, builder: nil) ⇒ Hash
Place a single order.
-
#reload_metadata! ⇒ Object
Clear the asset metadata cache Call this if metadata has been updated.
-
#schedule_cancel(time: nil, vault_address: nil) ⇒ Hash
Schedule automatic cancellation of all orders.
-
#send_asset(destination:, source_dex:, destination_dex:, token:, amount:) ⇒ Hash
Move assets between DEX instances.
-
#set_referrer(code:) ⇒ Hash
Set referral code.
-
#spot_send(amount:, destination:, token:) ⇒ Hash
Transfer a spot token to another address.
-
#sub_account_spot_transfer(sub_account_user:, is_deposit:, token:, amount:) ⇒ Hash
Transfer spot tokens to/from a sub-account.
-
#sub_account_transfer(sub_account_user:, is_deposit:, usd:) ⇒ Hash
Transfer USDC to/from a sub-account.
-
#token_delegate(validator:, wei:, is_undelegate:) ⇒ Hash
Delegate or undelegate HYPE tokens to a validator.
-
#update_isolated_margin(coin:, amount:, vault_address: nil) ⇒ Hash
Add or remove isolated margin for a position.
-
#update_leverage(coin:, leverage:, is_cross: true, vault_address: nil) ⇒ Hash
Set cross or isolated leverage for a coin.
-
#usd_class_transfer(amount:, to_perp:) ⇒ Hash
Move USDC between perp and spot accounts.
-
#usd_send(amount:, destination:) ⇒ Hash
Transfer USDC to another address.
-
#use_big_blocks(enable:) ⇒ Hash
Toggle EVM big-blocks mode for this account (‘evmUserModify` L1 action).
-
#user_dex_abstraction(enabled:, user: nil) ⇒ Hash
Enable or disable HIP-3 DEX abstraction for automatic collateral transfers When enabled, collateral is automatically transferred to HIP-3 dexes when trading.
-
#user_set_abstraction(user:, abstraction:) ⇒ Hash
Set the abstraction mode for a user (‘userSetAbstraction` user-signed action).
-
#vault_transfer(vault_address:, is_deposit:, usd:) ⇒ Hash
Deposit or withdraw USDC to/from a vault.
-
#withdraw_from_bridge(amount:, destination:) ⇒ Hash
Withdraw USDC via the bridge.
Constructor Details
#initialize(client:, signer:, info:, testnet: false, expires_after: nil) ⇒ Exchange
Initialize the exchange client
22 23 24 25 26 27 28 29 |
# File 'lib/hyperliquid/exchange.rb', line 22 def initialize(client:, signer:, info:, testnet: false, expires_after: nil) @client = client @signer = signer @info = info @testnet = testnet @expires_after = expires_after @asset_cache = nil end |
Instance Attribute Details
#expires_after=(value) ⇒ Object (writeonly)
Update the global expiration timestamp applied to subsequent L1 actions. ‘expires_after` is not supported on user-signed actions (e.g. `usd_send`, `withdraw_from_bridge`) and must be nil for those calls to succeed.
722 723 724 |
# File 'lib/hyperliquid/exchange.rb', line 722 def expires_after=(value) @expires_after = value end |
Instance Method Details
#address ⇒ String
Get the wallet address
33 34 35 |
# File 'lib/hyperliquid/exchange.rb', line 33 def address @signer.address end |
#agent_enable_dex_abstraction(vault_address: nil) ⇒ Hash
Enable HIP-3 DEX abstraction via agent (L1 action, enable only) This allows agents to enable DEX abstraction for the account they’re trading on behalf of
668 669 670 671 672 673 674 675 676 677 |
# File 'lib/hyperliquid/exchange.rb', line 668 def agent_enable_dex_abstraction(vault_address: nil) nonce = action = { type: 'agentEnableDexAbstraction' } signature = @signer.sign_l1_action( action, nonce, vault_address: vault_address, expires_after: @expires_after ) post_action(action, signature, nonce, vault_address) end |
#agent_set_abstraction(abstraction:, vault_address: nil) ⇒ Hash
Set the agent abstraction mode (L1 action ‘agentSetAbstraction`).
683 684 685 686 687 688 689 690 691 692 |
# File 'lib/hyperliquid/exchange.rb', line 683 def agent_set_abstraction(abstraction:, vault_address: nil) nonce = action = { type: 'agentSetAbstraction', abstraction: abstraction } signature = @signer.sign_l1_action( action, nonce, vault_address: vault_address, expires_after: @expires_after ) post_action(action, signature, nonce, vault_address) end |
#approve_agent(agent_address:, agent_name: nil) ⇒ Hash
Authorize an agent wallet to trade on behalf of this account
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 |
# File 'lib/hyperliquid/exchange.rb', line 573 def approve_agent(agent_address:, agent_name: nil) nonce = action = { type: 'approveAgent', signatureChainId: '0x66eee', hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @testnet), agentAddress: agent_address, nonce: nonce } # agentName is always included in the signed message (empty string if nil), # but only included in the posted action if a name was provided (matches Python SDK) action[:agentName] = agent_name if agent_name signature = @signer.sign_user_signed_action( { agentAddress: agent_address, agentName: agent_name || '', nonce: nonce }, 'HyperliquidTransaction:ApproveAgent', Signing::EIP712::APPROVE_AGENT_TYPES ) post_action(action, signature, nonce, nil) end |
#approve_builder_fee(builder:, max_fee_rate:) ⇒ Hash
Approve a builder fee rate for a builder address Users must approve a builder before orders with that builder can be placed.
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 |
# File 'lib/hyperliquid/exchange.rb', line 598 def approve_builder_fee(builder:, max_fee_rate:) nonce = action = { type: 'approveBuilderFee', signatureChainId: '0x66eee', hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @testnet), maxFeeRate: max_fee_rate, builder: builder, nonce: nonce } signature = @signer.sign_user_signed_action( { maxFeeRate: max_fee_rate, builder: builder, nonce: nonce }, 'HyperliquidTransaction:ApproveBuilderFee', Signing::EIP712::APPROVE_BUILDER_FEE_TYPES ) post_action(action, signature, nonce, nil) end |
#batch_modify(modifies:, vault_address: nil) ⇒ Hash
Modify multiple orders at once
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/hyperliquid/exchange.rb', line 255 def batch_modify(modifies:, vault_address: nil) nonce = 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
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/hyperliquid/exchange.rb', line 190 def bulk_cancel(cancels:, vault_address: nil) nonce = 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
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/hyperliquid/exchange.rb', line 210 def bulk_cancel_by_cloid(cancels:, vault_address: nil) nonce = 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, builder: nil) ⇒ Hash
Place multiple orders in a batch
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/hyperliquid/exchange.rb', line 84 def bulk_orders(orders:, grouping: 'na', vault_address: nil, builder: nil) nonce = 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 } action[:builder] = normalize_builder(builder) if builder 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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/hyperliquid/exchange.rb', line 148 def cancel(coin:, oid:, vault_address: nil) nonce = 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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/hyperliquid/exchange.rb', line 169 def cancel_by_cloid(coin:, cloid:, vault_address: nil) nonce = 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 |
#convert_to_multi_sig_user(authorized_users:, threshold:) ⇒ Hash
Convert this account into a multi-sig user (‘convertToMultiSigUser` user-signed action). The set of authorized signers and the threshold are JSON-encoded into the action’s ‘signers` field as required by the Hyperliquid protocol.
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 |
# File 'lib/hyperliquid/exchange.rb', line 777 def convert_to_multi_sig_user(authorized_users:, threshold:) nonce = sorted_users = .sort signers_json = JSON.generate({ authorizedUsers: sorted_users, threshold: threshold }) action = { type: 'convertToMultiSigUser', signatureChainId: '0x66eee', hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @testnet), signers: signers_json, nonce: nonce } signature = @signer.sign_user_signed_action( { signers: signers_json, nonce: nonce }, 'HyperliquidTransaction:ConvertToMultiSigUser', Signing::EIP712::CONVERT_TO_MULTI_SIG_USER_TYPES ) post_action(action, signature, nonce, nil) end |
#create_sub_account(name:) ⇒ Hash
Create a sub-account
499 500 501 502 503 504 |
# File 'lib/hyperliquid/exchange.rb', line 499 def create_sub_account(name:) nonce = action = { type: 'createSubAccount', name: name } signature = @signer.sign_l1_action(action, nonce) post_action(action, signature, nonce, nil) end |
#gossip_priority_bid(slot_id:, ip:, max_gas:, vault_address: nil) ⇒ Hash
Submit a priority-bid gossip message (L1 action ‘gossipPriorityBid`). Used by validators / priority bidders to gossip a bid for a given slot.
760 761 762 763 764 765 766 767 768 769 |
# File 'lib/hyperliquid/exchange.rb', line 760 def gossip_priority_bid(slot_id:, ip:, max_gas:, vault_address: nil) nonce = action = { type: 'gossipPriorityBid', slotId: slot_id, ip: ip, maxGas: max_gas } signature = @signer.sign_l1_action( action, nonce, vault_address: vault_address, expires_after: @expires_after ) post_action(action, signature, nonce, vault_address) end |
#market_close(coin:, size: nil, slippage: DEFAULT_SLIPPAGE, cloid: nil, vault_address: nil, builder: nil) ⇒ Hash
Close a position at market price
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/hyperliquid/exchange.rb', line 357 def market_close(coin:, size: nil, slippage: DEFAULT_SLIPPAGE, cloid: nil, vault_address: nil, builder: nil) address = vault_address || @signer.address position = find_position(coin, address) 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 slippage_price = calculate_slippage_price(coin, get_mid_price(coin), 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, builder: builder ) end |
#market_order(coin:, is_buy:, size:, slippage: DEFAULT_SLIPPAGE, vault_address: nil, builder: nil) ⇒ Hash
Place a market order (aggressive limit IoC with slippage)
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/hyperliquid/exchange.rb', line 122 def market_order(coin:, is_buy:, size:, slippage: DEFAULT_SLIPPAGE, vault_address: nil, builder: nil) # Get current mid price (use dex-specific endpoint for HIP-3 assets) dex_prefix = extract_dex_prefix(coin) mids = dex_prefix ? @info.all_mids(dex: dex_prefix) : @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, builder: builder ) 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
237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/hyperliquid/exchange.rb', line 237 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 |
#noop(nonce: nil, vault_address: nil) ⇒ Hash
No-op L1 action — useful for burning a specific nonce slot without side effects.
742 743 744 745 746 747 748 749 750 751 |
# File 'lib/hyperliquid/exchange.rb', line 742 def noop(nonce: nil, vault_address: nil) nonce ||= action = { type: 'noop' } signature = @signer.sign_l1_action( action, nonce, vault_address: vault_address, expires_after: @expires_after ) post_action(action, signature, nonce, vault_address) end |
#order(coin:, is_buy:, size:, limit_px:, order_type: { limit: { tif: 'Gtc' } }, reduce_only: false, cloid: nil, vault_address: nil, builder: nil) ⇒ Hash
Place a single order
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/hyperliquid/exchange.rb', line 48 def order(coin:, is_buy:, size:, limit_px:, order_type: { limit: { tif: 'Gtc' } }, reduce_only: false, cloid: nil, vault_address: nil, builder: nil) nonce = 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' } action[:builder] = normalize_builder(builder) if builder 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
798 799 800 |
# File 'lib/hyperliquid/exchange.rb', line 798 def @asset_cache = nil end |
#schedule_cancel(time: nil, vault_address: nil) ⇒ Hash
Schedule automatic cancellation of all orders
335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/hyperliquid/exchange.rb', line 335 def schedule_cancel(time: nil, vault_address: nil) nonce = 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
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/hyperliquid/exchange.rb', line 471 def send_asset(destination:, source_dex:, destination_dex:, token:, amount:) nonce = action = { type: 'sendAsset', signatureChainId: '0x66eee', hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @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
562 563 564 565 566 567 |
# File 'lib/hyperliquid/exchange.rb', line 562 def set_referrer(code:) nonce = 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
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/hyperliquid/exchange.rb', line 401 def spot_send(amount:, destination:, token:) nonce = action = { type: 'spotSend', signatureChainId: '0x66eee', hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @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
529 530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/hyperliquid/exchange.rb', line 529 def sub_account_spot_transfer(sub_account_user:, is_deposit:, token:, amount:) nonce = action = { type: 'subAccountSpotTransfer', subAccountUser: sub_account_user, 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
511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/hyperliquid/exchange.rb', line 511 def sub_account_transfer(sub_account_user:, is_deposit:, usd:) nonce = action = { type: 'subAccountTransfer', subAccountUser: sub_account_user, isDeposit: is_deposit, usd: float_to_usd_int(usd) } signature = @signer.sign_l1_action(action, nonce) post_action(action, signature, nonce, nil) end |
#token_delegate(validator:, wei:, is_undelegate:) ⇒ Hash
Delegate or undelegate HYPE tokens to a validator
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
# File 'lib/hyperliquid/exchange.rb', line 621 def token_delegate(validator:, wei:, is_undelegate:) nonce = action = { type: 'tokenDelegate', signatureChainId: '0x66eee', hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @testnet), validator: validator, wei: wei, isUndelegate: is_undelegate, nonce: nonce } signature = @signer.sign_user_signed_action( { validator: validator, wei: wei, isUndelegate: is_undelegate, nonce: nonce }, 'HyperliquidTransaction:TokenDelegate', Signing::EIP712::TOKEN_DELEGATE_TYPES ) post_action(action, signature, nonce, nil) end |
#update_isolated_margin(coin:, amount:, vault_address: nil) ⇒ Hash
Add or remove isolated margin for a position
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/hyperliquid/exchange.rb', line 313 def update_isolated_margin(coin:, amount:, vault_address: nil) nonce = 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
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/hyperliquid/exchange.rb', line 290 def update_leverage(coin:, leverage:, is_cross: true, vault_address: nil) nonce = 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
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/hyperliquid/exchange.rb', line 424 def usd_class_transfer(amount:, to_perp:) nonce = action = { type: 'usdClassTransfer', signatureChainId: '0x66eee', hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @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
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/hyperliquid/exchange.rb', line 378 def usd_send(amount:, destination:) nonce = action = { type: 'usdSend', signatureChainId: '0x66eee', hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @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 |
#use_big_blocks(enable:) ⇒ Hash
Toggle EVM big-blocks mode for this account (‘evmUserModify` L1 action). When enabled, EVM transactions from this account are routed to big blocks.
728 729 730 731 732 733 734 735 736 |
# File 'lib/hyperliquid/exchange.rb', line 728 def use_big_blocks(enable:) nonce = action = { type: 'evmUserModify', usingBigBlocks: enable } signature = @signer.sign_l1_action( action, nonce, expires_after: @expires_after ) post_action(action, signature, nonce, nil) end |
#user_dex_abstraction(enabled:, user: nil) ⇒ Hash
Enable or disable HIP-3 DEX abstraction for automatic collateral transfers When enabled, collateral is automatically transferred to HIP-3 dexes when trading
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 |
# File 'lib/hyperliquid/exchange.rb', line 645 def user_dex_abstraction(enabled:, user: nil) nonce = user_address = user || @signer.address action = { type: 'userDexAbstraction', signatureChainId: '0x66eee', hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @testnet), user: user_address, enabled: enabled, nonce: nonce } signature = @signer.sign_user_signed_action( { user: user_address, enabled: enabled, nonce: nonce }, 'HyperliquidTransaction:UserDexAbstraction', Signing::EIP712::USER_DEX_ABSTRACTION_TYPES ) post_action(action, signature, nonce, nil) end |
#user_set_abstraction(user:, abstraction:) ⇒ Hash
Set the abstraction mode for a user (‘userSetAbstraction` user-signed action). The `user` address is lowercased to match the Python SDK and protocol expectations.
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 |
# File 'lib/hyperliquid/exchange.rb', line 699 def user_set_abstraction(user:, abstraction:) nonce = user_lower = user.downcase action = { type: 'userSetAbstraction', signatureChainId: '0x66eee', hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @testnet), user: user_lower, abstraction: abstraction, nonce: nonce } signature = @signer.sign_user_signed_action( { user: user_lower, abstraction: abstraction, nonce: nonce }, 'HyperliquidTransaction:UserSetAbstraction', Signing::EIP712::USER_SET_ABSTRACTION_TYPES ) post_action(action, signature, nonce, nil) end |
#vault_transfer(vault_address:, is_deposit:, usd:) ⇒ Hash
Deposit or withdraw USDC to/from a vault
547 548 549 550 551 552 553 554 555 556 557 |
# File 'lib/hyperliquid/exchange.rb', line 547 def vault_transfer(vault_address:, is_deposit:, usd:) nonce = 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
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
# File 'lib/hyperliquid/exchange.rb', line 446 def withdraw_from_bridge(amount:, destination:) nonce = action = { type: 'withdraw3', signatureChainId: '0x66eee', hyperliquidChain: Signing::EIP712.hyperliquid_chain(testnet: @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 |