lib/krkt/client.rb in krkt-0.1.1 vs lib/krkt/client.rb in krkt-0.1.2
- old
+ new
@@ -3,83 +3,109 @@
require('openssl')
require('base64')
require('curb')
require('json')
+# @author Hernani Rodrigues Vaz
module Krkt
- # class Client
+ # classe para processar dados no kraken
class Client
# @return [String] API key
- attr_reader :key
+ attr_reader :aky
# @return [String] API secret
- attr_reader :sec
+ attr_reader :asc
# @return [String] API public url
attr_reader :puu
# @return [String] API private url
attr_reader :pru
# @return [String] API private path
attr_reader :pth
- # def initialize(api_key: nil, api_secret: nil, options: {})
- def initialize(api_key: ENV['KRAKEN_API_KEY'], api_secret: ENV['KRAKEN_API_SECRET'], options: {})
+ # @param [String] pky API key
+ # @param [String] psc API secret
+ # @return [Client] API kraken base
+ def initialize(pky: ENV['KRAKEN_API_KEY'], psc: ENV['KRAKEN_API_SECRET'], options: {})
b = options.fetch(:base_uri, 'https://api.kraken.com')
v = options.fetch(:version, 0)
- @key = api_key
- @sec = api_secret
+ @aky = pky
+ @asc = psc
@puu = "#{b}/#{v}/public/"
@pru = "#{b}/#{v}/private/"
@pth = "/#{v}/private/"
end
- # type = type of trade (optional)
- # all = all types (default)
- # any position = any position (open or closed)
- # closed position = positions that have been closed
- # closing position = any trade closing all or part of a position
- # no position = non-positional trades
- # trades = whether or not to include trades related to position in output (optional. default = false)
- # start = starting unix timestamp or trade tx id of results (optional. exclusive)
- # end = ending unix timestamp or trade tx id of results (optional. inclusive)
- # ofs = result offset
- # Result: array of trade info
- # trades = array of trade info with txid as the key
- # ordertxid = order responsible for execution of trade
- # pair = asset pair
- # time = unix timestamp of trade
- # type = type of order (buy/sell)
- # ordertype = order type
- # price = average price order was executed at (quote currency)
- # cost = total cost of order (quote currency)
- # fee = total fee (quote currency)
- # vol = volume (base currency)
- # margin = initial margin (quote currency)
- # misc = comma delimited list of miscellaneous info
- # closing = trade closes all or part of a position
- # count = amount of available trades info matching criteria
- def trades_history(**opts)
- post_private('TradesHistory', **opts)['result']
+ # @return [Hash] resultados ultimas transacoes trades no kraken
+ # @example
+ # {
+ # 'trades' => {
+ # 'TLADFV-QIUTN-QDNCBR' => {
+ # 'ordertxid' => 'O2CRFG-5PCJA-EMX7VC',
+ # 'pair' => 'XETHXXBT',
+ # 'time' => 1_463_422_494.7069,
+ # 'type' => 'buy',
+ # 'ordertype' => 'market',
+ # 'price' => '0.024400',
+ # 'cost' => '1.358424',
+ # 'fee' => '0.003532',
+ # 'vol' => '55.67311475',
+ # 'margin' => '0.000000',
+ # 'misc' => ''
+ # },
+ # ...
+ # },
+ # 'count' => 156
+ # }
+ def trades_history
+ post_private('TradesHistory')['result']
end
- # def ledgers
- def ledgers(**opts)
- post_private('Ledgers', **opts)['result']
+ # @return [Hash] resultados ultimas transacoes ledger no kraken
+ # @example
+ # {
+ # 'ledger' => {
+ # 'LUK3QC-QW6TA-27B7NI' => {
+ # 'refid' => 'A2BNCQS-CHCETZ-JYCER6',
+ # 'time' => 1_584_349_306.9479,
+ # 'type' => 'withdrawal',
+ # 'subtype' => '',
+ # 'aclass' => 'currency',
+ # 'asset' => 'XETH',
+ # 'amount' => '-29.2659039100',
+ # 'fee' => '0.0050000000',
+ # 'balance' => '1.1806988100'
+ # }
+ # ,
+ # ...
+ # },
+ # 'count' => 373
+ # }
+ def ledger
+ post_private('Ledgers')['result']
end
- # def balance
+ # @return [Hash] saldos no kraken
+ # @example
+ # {
+ # 'ZEUR' => '0.0000',
+ # 'XXBT' => '0.0000000000',
+ # 'XETH' => '1.1806988100',
+ # 'XETC' => '0.0000000000',
+ # 'EOS' => '0.0000001700',
+ # 'BCH' => '0.0000000000'
+ # }
def balance
post_private('Balance')['result']
end
- # Get server time
- # URL: https://api.kraken.com/0/public/Time
- # Returns a hash with keys +error+ and +result+.
- # +result+ is an array of hashes with keys:
- # +unixtime+ = unix timestamp
- # +rfc1123+ = RFC 1123 time format
- #
+ # @return [Hash] data hora kraken
+ # @example
+ # {
+ # 'unixtime' => 1_598_956_727,
+ # 'rfc1123' => 'Tue, 1 Sep 20 10:38:47 +0000'
+ # }
def server_time
- get_public('Time')
+ get_public('Time')['result']
end
private
# HTTP GET request for public API queries.
@@ -88,26 +114,26 @@
end
# HTTP POST request for private API queries involving user credentials.
def post_private(method, **opts)
# Generate a continually-increasing unsigned 51-bit integer nonce from the current Unix Time.
- t = opts.merge!({ nonce: Integer(Time.now.to_f * 1_000_000) }).map { |p| p.join('=') }.join('&')
+ opts.merge!({ nonce: Integer(Time.now) * 1_000_000 })
- parse_response(Curl.post("#{pru}#{method}", t) do |r|
+ parse_response(Curl.post("#{pru}#{method}", opts) do |r|
r.headers = {
- 'api-key': key,
- 'api-sign': authenticate(method, opts[:nonce], t)
+ 'api-key': aky,
+ 'api-sign': authenticate(method, opts[:nonce], opts.map { |p| p.join('=') }.join('&'))
}
end)
end
def authenticate(method, nonce, params)
- raise(ArgumentError, 'API Key is not set') unless key
- raise(ArgumentError, 'API Secret is not set') unless sec
+ raise(ArgumentError, 'API Key is not set') unless aky
+ raise(ArgumentError, 'API Secret is not set') unless asc
Base64.strict_encode64(OpenSSL::HMAC.digest(
'sha512',
- Base64.decode64(sec),
+ Base64.decode64(asc),
"#{pth}#{method}#{Digest::SHA256.digest("#{nonce}#{params}")}"
))
end
def parse_response(http)