Class: Cns::Apice

Inherits:
Object
  • Object
show all
Defined in:
lib/cns/apice.rb

Overview

classe para acesso dados centralized exchanges

Constant Summary collapse

API =
{de: 'https://api.bitcoin.de/v4', us: 'https://api.kraken.com/0/private'}.freeze
MDE =
%w[btc eth].freeze

Instance Method Summary collapse

Constructor Details

#initializeApice

Returns a new instance of Apice.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cns/apice.rb', line 15

def initialize
  @curl =
    Curl::Easy.new.tap do |c|
      c.timeout = 30
      c.connect_timeout = 10
      c.follow_location = true
      c.ssl_verify_peer = true
    end
  @deky = ENV.fetch('BITCOINDE_API_KEY', nil)
  @desc = ENV.fetch('BITCOINDE_API_SECRET', nil)
  @usky = ENV.fetch('KRAKEN_API_KEY', nil)
  @ussc = ENV.fetch('KRAKEN_API_SECRET', nil)
end

Instance Method Details

#account_deHash

Get account balances from Bitcoin.de

Returns:

  • (Hash)

    saldos no bitcoinde



31
32
33
34
35
36
37
# File 'lib/cns/apice.rb', line 31

def 
  uri = "#{API[:de]}/account"
  rcrl(@curl, uri, headers: hde(uri))
  pjsn(@curl).dig(:data, :balances) || {}
rescue Curl::Err::CurlError
  {}
end

#account_usHash

Get account balances from Kraken

Returns:

  • (Hash)

    saldos kraken



75
76
77
78
79
80
81
82
# File 'lib/cns/apice.rb', line 75

def 
  uri = 'Balance'
  ops = {nonce: nnc}
  rcrl(@curl, "#{API[:us]}/#{uri}", method: 'POST', post_data: ops, headers: hus(uri, ops))
  pjsn(@curl).fetch(:result, {})
rescue Curl::Err::CurlError
  {}
end

#deposits_deArray<Hash>

Get deposits from Bitcoin.de, uniformly formatted

Returns:

  • (Array<Hash>)

    depositos uniformizados bitcoinde



53
54
55
56
57
58
59
60
# File 'lib/cns/apice.rb', line 53

def deposits_de
  MDE.flat_map do |cry|
    pag_de_req("#{API[:de]}/#{cry}/deposits", {}, :deposits) { |i| i.map { |h| deposit_unif(h, cry.upcase) } }
  end
  # pag_de_req("#{API[:de]}/btc/deposits", {}, :deposits) { |i| i.map { |h| deposit_unif(h) } }
rescue Curl::Err::CurlError
  []
end

#ledger_us(tsp = nil) ⇒ Hash

Get ledger from Kraken

Parameters:

  • tsp (Integer) (defaults to: nil)

    optional timestamp to fetch ledger from (last N seconds)

Returns:

  • (Hash)

    ledger kraken



102
103
104
105
106
# File 'lib/cns/apice.rb', line 102

def ledger_us(tsp = nil)
  pag_us_req('Ledgers', :ledger, tsp ? {start: tsp} : {})
rescue Curl::Err::CurlError
  []
end

#trades_de(tsp = nil) ⇒ Array<Hash>

Get trades from Bitcoin.de

Parameters:

  • tsp (Integer) (defaults to: nil)

    optional unix timestamp (start date) to fetch trades from

Returns:

  • (Array<Hash>)

    trades bitcoinde



42
43
44
45
46
47
48
49
# File 'lib/cns/apice.rb', line 42

def trades_de(tsp = nil)
  prm = {state: 1}
  # Bitcoin.de API needs round UTC Time down to nearest 15-minute mark (00,15,30,45) and format as RFC 3339 with +00:00
  prm[:date_start] = Time.at(tsp).utc.then { |t| Time.utc(t.year, t.month, t.day, t.hour, 0, 0).strftime('%Y-%m-%dT%H:%M:%S+00:00') } if tsp
  pag_de_req("#{API[:de]}/trades", prm, :trades)
rescue Curl::Err::CurlError
  []
end

#trades_us(tsp = nil) ⇒ Hash

Get trades from Kraken

Parameters:

  • tsp (Integer) (defaults to: nil)

    optional timestamp to fetch trades from (last N seconds)

Returns:

  • (Hash)

    trades kraken



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cns/apice.rb', line 87

def trades_us(tsp = nil)
  # last TradesHistory was 2023-06-03 (after that was discontinued by kraken)
  if tsp && tsp > Integer(Time.new(2023, 6, 3))
    # return empty array to avoid unnecessary API calls
    []
  else
    pag_us_req('TradesHistory', :trades, tsp ? {start: tsp} : {})
  end
rescue Curl::Err::CurlError
  []
end

#withdrawals_deArray<Hash>

Get withdrawals from Bitcoin.de, uniformly formatted

Returns:

  • (Array<Hash>)

    withdrawals uniformizadas bitcoinde



64
65
66
67
68
69
70
71
# File 'lib/cns/apice.rb', line 64

def withdrawals_de
  MDE.flat_map do |cry|
    pag_de_req("#{API[:de]}/#{cry}/withdrawals", {}, :withdrawals) { |i| i.map { |h| withdrawal_unif(h, cry.upcase) } }
  end
  # pag_de_req("#{API[:de]}/btc/withdrawals", {}, :withdrawals) { |i| i.map { |h| withdrawal_unif(h) } }
rescue Curl::Err::CurlError
  []
end