Skip to content

Commit

Permalink
add bitget service
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Jan 7, 2024
1 parent d9f1838 commit 1c7c4c1
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions app/services/bitget_spots_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'openssl'
require 'rest-client'

class BitgetSpotsService
BASE_URL = ENV['BITGET_API_URL']
attr_reader :user_id

def initialize(user_id: nil)
@user = User.find_by id: user_id

if @user.present?
@api_key = @user.bitget_api_key
@secret_key = Base64.decode64(@user.bitget_secret_key) rescue nil
@passphrase = Base64.decode64(@user.bitget_passphrase) rescue nil
else
@api_key = ENV["BITGET_KEY"]
@secret_key = ENV["BITGET_SECRET"]
@passphrase = ENV["BITGET_PASSPHRASE"]
end
end

def get_orders(start_time: nil, end_time: DateTime.now)
begin
start_time ||= end_time - 2.weeks
request_path = "/api/v2/tax/spot-record?endTime=#{end_time.strftime('%Q')}&startTime=#{start_time.strftime('%Q')}"
do_request("get", request_path)
rescue => e
format_error_msg(e)
end
end

def get_price(instId)
begin
request_path = "/api/v5/market/ticker?instId=#{instId}"
do_request("get", request_path)
rescue => e
format_error_msg(e)
end
end

private
def do_request(method, request_path)
url = BASE_URL + request_path
timestamp = '1685013478665' #get_timestamp
sign = signed_data("#{timestamp}#{method.upcase}#{request_path}")
headers = {
"ACCESS-KEY" => @api_key,
"ACCESS-SIGN" => sign,
"ACCESS-TIMESTAMP" => timestamp,
"ACCESS-PASSPHRASE" => @passphrase,
"Content-Type" => "application/json",
"locale" => "en-US"
}

response = RestClient.get(url, headers)
JSON.parse(response)
end

def signed_data(data)
Base64.strict_encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), @secret_key, data))
end

def get_timestamp
DateTime.now.strftime('%Q')
end

def format_error_msg(e)
"Error: #{e}"
end
end

0 comments on commit 1c7c4c1

Please sign in to comment.