Skip to content

Instantly share code, notes, and snippets.

@agustinf
Last active February 6, 2019 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agustinf/a455d19928d8c0c3cee38367b07aecb8 to your computer and use it in GitHub Desktop.
Save agustinf/a455d19928d8c0c3cee38367b07aecb8 to your computer and use it in GitHub Desktop.
require 'JSON'
require 'httpparty'
HOST = 'https://www.buda.com'
def new_order(symbol, amount, type, side, price = nil)
side = side == :buy ? 'Bid' : 'Ask'
payload = { order: { type: side, price_type: type.to_s,
limit: price.to_s, amount: amount.to_s } }
path = "/api/v2/markets/#{symbol}/orders"
url = "#{HOST}#{path}"
HTTParty.post(url, body: payload, headers: headers('POST', path, payload.to_json))
end
# Authentication.
def generate_nonce
(Time.now.to_f.round(3) * 1000).to_i.to_s
end
def signature(request_type, path, nonce, payload = nil)
if payload.nil?
"#{request_type} #{path} #{nonce}"
else
"#{request_type} #{path} #{Base64.strict_encode64(payload)} #{nonce}"
end
end
def headers(request_type, path, payload = nil)
nonce = generate_nonce
encrypted_signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha384'),
ENV.fetch('BUDA_API_SECRET'), signature(request_type, path, nonce, payload))
{
'X-SBTC-APIKEY' => ENV.fetch('BUDA_API_KEY'),
'X-SBTC-NONCE' => nonce,
'X-SBTC-SIGNATURE' => encrypted_signature,
'Content-Type' => 'application/json'
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment