Class: Honeymaker::Clients::Bybit

Inherits:
Honeymaker::Client show all
Defined in:
lib/honeymaker/clients/bybit.rb

Constant Summary collapse

URL =
"https://api.bybit.com"
RECV_WINDOW =
"5000"
RATE_LIMITS =
{ default: 100, orders: 200 }.freeze

Constants inherited from Honeymaker::Client

Honeymaker::Client::OPTIONS

Instance Attribute Summary

Attributes inherited from Honeymaker::Client

#api_key, #api_secret

Instance Method Summary collapse

Methods inherited from Honeymaker::Client

#initialize, rate_limits, #validate

Constructor Details

This class inherits a constructor from Honeymaker::Client

Instance Method Details

#borrow_history(currency: nil, start_time: nil, end_time: nil, limit: nil, cursor: nil) ⇒ Object

— Margin —



135
136
137
138
139
# File 'lib/honeymaker/clients/bybit.rb', line 135

def borrow_history(currency: nil, start_time: nil, end_time: nil, limit: nil, cursor: nil)
  get_authenticated("/v5/account/borrow-history", {
    currency: currency, startTime: start_time, endTime: end_time, limit: limit, cursor: cursor
  })
end

#cancel_order(category:, symbol:, order_id: nil, order_link_id: nil) ⇒ Object



92
93
94
95
96
97
# File 'lib/honeymaker/clients/bybit.rb', line 92

def cancel_order(category:, symbol:, order_id: nil, order_link_id: nil)
  post_authenticated("/v5/order/cancel", {
    category: category, symbol: symbol,
    orderId: order_id, orderLinkId: order_link_id
  })
end

#create_order(category:, symbol:, side:, order_type:, qty:, price: nil, time_in_force: nil, market_unit: nil, order_link_id: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/honeymaker/clients/bybit.rb', line 77

def create_order(category:, symbol:, side:, order_type:, qty:, price: nil,
                 time_in_force: nil, market_unit: nil, order_link_id: nil)
  result = post_authenticated("/v5/order/create", {
    category: category, symbol: symbol, side: side,
    orderType: order_type, qty: qty, price: price,
    timeInForce: time_in_force, marketUnit: market_unit,
    orderLinkId: order_link_id
  })
  return result if result.failure?
  return Result::Failure.new(result.data["retMsg"]) unless result.data["retCode"]&.zero?

  order_id = result.data.dig("result", "orderId")
  Result::Success.new({ order_id: order_id, raw: result.data })
end

#delivery_records(category:, symbol: nil, exp_date: nil, limit: nil, cursor: nil) ⇒ Object

— Futures —



149
150
151
152
153
# File 'lib/honeymaker/clients/bybit.rb', line 149

def delivery_records(category:, symbol: nil, exp_date: nil, limit: nil, cursor: nil)
  get_authenticated("/v5/asset/delivery-record", {
    category: category, symbol: symbol, expDate: exp_date, limit: limit, cursor: cursor
  })
end

#deposit_records(coin: nil, start_time: nil, end_time: nil, limit: nil, cursor: nil) ⇒ Object



113
114
115
116
117
# File 'lib/honeymaker/clients/bybit.rb', line 113

def deposit_records(coin: nil, start_time: nil, end_time: nil, limit: nil, cursor: nil)
  get_authenticated("/v5/asset/deposit/query-record", {
    coin: coin, startTime: start_time, endTime: end_time, limit: limit, cursor: cursor
  })
end

#earn_order_records(order_id: nil, order_type: nil, start_time: nil, end_time: nil, limit: nil, cursor: nil) ⇒ Object

— Earn —



163
164
165
166
167
168
# File 'lib/honeymaker/clients/bybit.rb', line 163

def earn_order_records(order_id: nil, order_type: nil, start_time: nil, end_time: nil, limit: nil, cursor: nil)
  get_authenticated("/v5/earn/order-records", {
    orderId: order_id, orderType: order_type,
    startTime: start_time, endTime: end_time, limit: limit, cursor: cursor
  })
end

#earn_yield_history(product_id: nil, start_time: nil, end_time: nil, limit: nil, cursor: nil) ⇒ Object



170
171
172
173
174
# File 'lib/honeymaker/clients/bybit.rb', line 170

def earn_yield_history(product_id: nil, start_time: nil, end_time: nil, limit: nil, cursor: nil)
  get_authenticated("/v5/earn/yield-history", {
    productId: product_id, startTime: start_time, endTime: end_time, limit: limit, cursor: cursor
  })
end

#execution_list(category:, symbol: nil, order_id: nil, start_time: nil, end_time: nil, limit: nil, cursor: nil) ⇒ Object



106
107
108
109
110
111
# File 'lib/honeymaker/clients/bybit.rb', line 106

def execution_list(category:, symbol: nil, order_id: nil, start_time: nil, end_time: nil, limit: nil, cursor: nil)
  get_authenticated("/v5/execution/list", {
    category: category, symbol: symbol, orderId: order_id,
    startTime: start_time, endTime: end_time, limit: limit, cursor: cursor
  })
end

#get_balances(account_type: "UNIFIED") ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/honeymaker/clients/bybit.rb', line 42

def get_balances(account_type: "UNIFIED")
  result = wallet_balance(account_type: )
  return result if result.failure?

  return Result::Failure.new(result.data["retMsg"]) unless result.data["retCode"]&.zero?

  balances = {}
  accounts = result.data.dig("result", "list") || []
  accounts.each do ||
    (["coin"] || []).each do |coin|
      symbol = coin["coin"]
      free = BigDecimal((coin["availableToWithdraw"] || "0").to_s)
      locked = BigDecimal((coin["locked"] || "0").to_s)
      next if free.zero? && locked.zero?
      balances[symbol] = { free: free, locked: locked }
    end
  end

  Result::Success.new(balances)
end

#get_coin_query_infoObject



10
11
12
# File 'lib/honeymaker/clients/bybit.rb', line 10

def get_coin_query_info
  get_authenticated("/v5/asset/coin/query-info")
end

#get_order(category:, order_id: nil, symbol: nil, order_link_id: nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/honeymaker/clients/bybit.rb', line 63

def get_order(category:, order_id: nil, symbol: nil, order_link_id: nil)
  result = get_authenticated("/v5/order/realtime", {
    category: category, orderId: order_id, symbol: symbol, orderLinkId: order_link_id
  })
  return result if result.failure?
  return Result::Failure.new(result.data["retMsg"]) unless result.data["retCode"]&.zero?

  order_list = result.data.dig("result", "list") || []
  raw = order_list.first
  return Result::Failure.new("Order not found") unless raw

  Result::Success.new(normalize_order(raw["orderId"], raw))
end

#instruments_info(category:, symbol: nil, status: nil, base_coin: nil, limit: nil, cursor: nil) ⇒ Object



14
15
16
17
18
19
# File 'lib/honeymaker/clients/bybit.rb', line 14

def instruments_info(category:, symbol: nil, status: nil, base_coin: nil, limit: nil, cursor: nil)
  get_public("/v5/market/instruments-info", {
    category: category, symbol: symbol, status: status,
    baseCoin: base_coin, limit: limit, cursor: cursor
  })
end

#kline(category:, symbol:, interval:, start: nil, end_time: nil, limit: nil) ⇒ Object



31
32
33
34
35
36
# File 'lib/honeymaker/clients/bybit.rb', line 31

def kline(category:, symbol:, interval:, start: nil, end_time: nil, limit: nil)
  get_public("/v5/market/kline", {
    category: category, symbol: symbol, interval: interval,
    start: start, end: end_time, limit: limit
  })
end

#orderbook(category:, symbol:, limit: nil) ⇒ Object



27
28
29
# File 'lib/honeymaker/clients/bybit.rb', line 27

def orderbook(category:, symbol:, limit: nil)
  get_public("/v5/market/orderbook", { category: category, symbol: symbol, limit: limit })
end

#settlement_records(category:, symbol: nil, limit: nil, cursor: nil) ⇒ Object



155
156
157
158
159
# File 'lib/honeymaker/clients/bybit.rb', line 155

def settlement_records(category:, symbol: nil, limit: nil, cursor: nil)
  get_authenticated("/v5/asset/settlement-record", {
    category: category, symbol: symbol, limit: limit, cursor: cursor
  })
end

#spot_margin_repay_history(start_time: nil, end_time: nil, coin: nil, limit: nil, cursor: nil) ⇒ Object



141
142
143
144
145
# File 'lib/honeymaker/clients/bybit.rb', line 141

def spot_margin_repay_history(start_time: nil, end_time: nil, coin: nil, limit: nil, cursor: nil)
  get_authenticated("/v5/spot-cross-margin-trade/repay-history", {
    startTime: start_time, endTime: end_time, coin: coin, limit: limit, cursor: cursor
  })
end

#tickers(category:, symbol: nil, base_coin: nil, exp_date: nil) ⇒ Object



21
22
23
24
25
# File 'lib/honeymaker/clients/bybit.rb', line 21

def tickers(category:, symbol: nil, base_coin: nil, exp_date: nil)
  get_public("/v5/market/tickers", {
    category: category, symbol: symbol, baseCoin: base_coin, expDate: exp_date
  })
end

#transaction_log(account_type: nil, category: nil, currency: nil, type: nil, start_time: nil, end_time: nil, limit: nil, cursor: nil) ⇒ Object



99
100
101
102
103
104
# File 'lib/honeymaker/clients/bybit.rb', line 99

def transaction_log(account_type: nil, category: nil, currency: nil, type: nil, start_time: nil, end_time: nil, limit: nil, cursor: nil)
  get_authenticated("/v5/account/transaction-log", {
    accountType: , category: category, currency: currency, type: type,
    startTime: start_time, endTime: end_time, limit: limit, cursor: cursor
  })
end

#wallet_balance(account_type:, coin: nil) ⇒ Object



38
39
40
# File 'lib/honeymaker/clients/bybit.rb', line 38

def wallet_balance(account_type:, coin: nil)
  get_authenticated("/v5/account/wallet-balance", { accountType: , coin: coin })
end

#withdraw(coin:, chain:, address:, amount:, tag: nil, force_chain: nil) ⇒ Object



126
127
128
129
130
131
# File 'lib/honeymaker/clients/bybit.rb', line 126

def withdraw(coin:, chain:, address:, amount:, tag: nil, force_chain: nil)
  post_authenticated("/v5/asset/withdraw/create", {
    coin: coin, chain: chain, address: address, amount: amount,
    tag: tag, forceChain: force_chain, timestamp: timestamp_ms
  })
end

#withdraw_records(coin: nil, start_time: nil, end_time: nil, limit: nil, cursor: nil, withdraw_type: nil) ⇒ Object



119
120
121
122
123
124
# File 'lib/honeymaker/clients/bybit.rb', line 119

def withdraw_records(coin: nil, start_time: nil, end_time: nil, limit: nil, cursor: nil, withdraw_type: nil)
  get_authenticated("/v5/asset/withdraw/query-record", {
    coin: coin, startTime: start_time, endTime: end_time,
    limit: limit, cursor: cursor, withdrawType: withdraw_type
  })
end