Created
May 7, 2021 13:22
-
-
Save milk1000cc/3726d21e0f55bb5a7bfd86197ce3b6a6 to your computer and use it in GitHub Desktop.
Ruby Bybit API Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://bybit-exchange.github.io/docs/inverse/ | |
# https://github.com/bybit-exchange/api-connectors/blob/master/encryption_example/Encryption.rb | |
require 'open-uri' | |
require 'openssl' | |
require 'json' | |
require 'bigdecimal' | |
require 'bigdecimal/util' | |
API_ENDPOINT = 'https://api.bybit.com' | |
API_KEY = '' | |
API_SECRET = '' | |
def request(path, **params) | |
params.merge! api_key: API_KEY, timestamp: Time.now.to_i * 1000 | |
params.merge! sign: get_signature(URI.encode_www_form(params.sort), API_SECRET) | |
uri = URI(API_ENDPOINT + path) | |
uri.query = URI.encode_www_form(params) | |
JSON.parse uri.read | |
end | |
def get_signature(param_str, secret) | |
OpenSSL::HMAC.hexdigest('sha256', secret, param_str) | |
end | |
json = request('/v2/private/wallet/balance', coin: 'BTC') | |
wallet_balance_str = json['result']['BTC']['wallet_balance'].to_d.to_s('F') | |
puts "wallet balance: #{ wallet_balance_str } BTC" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment