cns 0.9.1 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/cns/bitcoinde.rb CHANGED
@@ -7,11 +7,9 @@ module Cns
7
7
  # classe para processar transacoes trades/ledger do bitcoinde
8
8
  class Bitcoinde
9
9
  # @return [Apius] API bitcoinde
10
- attr_reader :api
11
10
  # @return [Array<Hash>] todos os dados bigquery
12
- attr_reader :bqd
13
11
  # @return [Thor::CoreExt::HashWithIndifferentAccess] opcoes trabalho
14
- attr_reader :ops
12
+ attr_reader :api, :bqd, :ops
15
13
 
16
14
  # @param [Hash] dad todos os dados bigquery
17
15
  # @param [Thor::CoreExt::HashWithIndifferentAccess] pop opcoes trabalho
@@ -27,60 +25,55 @@ module Cns
27
25
 
28
26
  # @return [Hash] dados exchange bitcoinde - saldos & trades & deposits & withdrawals
29
27
  def exd
30
- @exd ||= { sl: pdea(api.account_de), tt: pdet(api.trades_de), tl: pdel(api.deposits_de + api.withdrawals_de) }
28
+ @exd ||= {sl: pdea(api.account_de), tt: pdet(api.trades_de), tl: pdel(api.deposits_de + api.withdrawals_de)}
31
29
  end
32
30
 
33
31
  # @return [String] texto saldos & transacoes & ajuste dias
34
32
  def mresumo
35
33
  puts("\nBITCOINDE\ntipo bitcoinde bigquery")
36
- exd[:sl].sort.each { |key, val| puts(formata_saldos(key, val)) }
34
+ exd[:sl].sort.each { |k, v| puts(fos(k, v)) }
37
35
  mtotais
38
36
 
39
37
  mtrades
40
38
  mledger
41
39
  return if novcdet.empty?
42
40
 
43
- puts("\nstring ajuste dias dos trades\n-h=#{kyt.map { |obj| "#{obj}:0" }.join(' ')}")
41
+ puts("\nstring ajuste dias dos trades\n-h=#{novcdet.sort_by { |i| -i[:srx] }.map { |o| "#{o[:trade_id]}:0" }.join(' ')}")
44
42
  end
45
43
 
46
44
  private
47
45
 
48
- def show_all?
49
- ops[:t] || false
50
- end
51
-
52
- def bqkyt
53
- @bqkyt ||= show_all? ? [] : (bqd[:nt]&.map { |t| t[:txid] } || [])
54
- end
46
+ # mosta contadores transacoes
47
+ def mtotais
48
+ vtt = exd[:tt].count
49
+ vnt = bqd[:nt].count
50
+ vtl = exd[:tl].count
51
+ vnl = bqd[:nl].count
55
52
 
56
- def bqkyl
57
- @bqkyl ||= show_all? ? [] : (bqd[:nl]&.map { |l| l[:txid] } || [])
53
+ puts("TRADES #{format('%<a>20i %<b>21i %<o>3.3s', a: vtt, b: vnt, o: vtt == vnt ? 'OK' : 'NOK')}")
54
+ puts("LEDGER #{format('%<c>20i %<d>21i %<o>3.3s', c: vtl, d: vnl, o: vtl == vnl ? 'OK' : 'NOK')}")
58
55
  end
59
56
 
60
- # @return [Array<String>] lista txid dos trades novos
61
- def kyt
62
- @kyt ||= exd[:tt].map { |oex| oex[:trade_id] } - bqkyt
63
- end
57
+ # mosta transacoes trades
58
+ def mtrades
59
+ return unless ops[:v] && novcdet.any?
64
60
 
65
- # @return [Array<Integer>] lista txid dos ledger novos
66
- def kyl
67
- @kyl ||= exd[:tl].map { |oex| oex[:txid] } - bqkyl
61
+ puts("\ntrades data hora dt criacao tipo par btc eur")
62
+ novcdet.sort_by { |i| -i[:srx] }.each { |o| puts(fot(o)) }
68
63
  end
69
64
 
70
- # @return [Array<Hash>] lista trades bitcoinde novos
71
- def novcdet
72
- @novcdet ||= exd[:tt].select { |obj| kyt.include?(obj[:trade_id]) }
73
- end
65
+ # mosta transacoes ledger
66
+ def mledger
67
+ return unless ops[:v] && novcdel.any?
74
68
 
75
- # @return [Array<Hash>] lista ledger (deposits + withdrawals) bitcoinde novos
76
- def novcdel
77
- @novcdel ||= exd[:tl].select { |obj| kyl.include?(obj[:txid]) }
69
+ puts("\nledger data hora tipo moe quantidade custo")
70
+ novcdel.sort_by { |i| -i[:srx] }.each { |o| puts(fol(o)) }
78
71
  end
79
72
 
80
73
  # @param [String] moe codigo bitcoinde da moeda
81
74
  # @param [Hash] hsx saldo bitcoinde da moeda
82
75
  # @return [String] texto formatado saldos
83
- def formata_saldos(moe, hsx)
76
+ def fos(moe, hsx)
84
77
  vbq = bqd[:sl][moe.downcase.to_sym].to_d
85
78
  vex = hsx[:total_amount]
86
79
  format(
@@ -92,9 +85,9 @@ module Cns
92
85
  )
93
86
  end
94
87
 
95
- # @param (see Bigquery#det_val1)
88
+ # @param [Hash] htn trades bitcoinde
96
89
  # @return [String] texto formatado trade
97
- def formata_trades(htx)
90
+ def fot(htx)
98
91
  format(
99
92
  '%<ky>-6.6s %<dt>19.19s %<dp>10.10s %<ty>-5.5s %<mo>-8.8s %<vl>18.8f %<co>8.2f',
100
93
  ky: htx[:trade_id],
@@ -102,82 +95,96 @@ module Cns
102
95
  dp: htx[:trade_marked_as_paid_at].strftime('%F'),
103
96
  ty: htx[:type],
104
97
  mo: htx[:trading_pair],
105
- vl: htx[:amount_currency_to_trade],
106
- co: htx[:volume_currency_to_pay]
98
+ vl: htx[:btc],
99
+ co: htx[:eur]
107
100
  )
108
101
  end
109
102
 
110
- # @param (see Bigquery#del_val)
103
+ # @param [Hash] htn ledger bitcoinde
111
104
  # @return [String] texto formatado ledger
112
- def formata_ledger(hlx)
105
+ def fol(hlx)
113
106
  format(
114
107
  '%<ky>6i %<dt>19.19s %<ty>-10.10s %<mo>-3.3s %<pr>19.8f %<vl>18.8f',
115
108
  ky: hlx[:txid],
116
109
  dt: hlx[:time].strftime('%F %T'),
117
110
  ty: hlx[:tp],
118
111
  mo: hlx[:moe],
119
- pr: hlx[:qt],
112
+ pr: hlx[:qtd],
120
113
  vl: hlx[:fee]
121
114
  )
122
115
  end
123
116
 
124
- # @return [String] texto numero de transacoes
125
- def mtotais
126
- vtt = exd[:tt].count
127
- vnt = bqd[:nt].count
128
- vtl = exd[:tl].count
129
- vnl = bqd[:nl].count
130
-
131
- puts("TRADES #{format('%<a>20i %<b>21i %<o>3.3s', a: vtt, b: vnt, o: vtt == vnt ? 'OK' : 'NOK')}")
132
- puts("LEDGER #{format('%<c>20i %<d>21i %<o>3.3s', c: vtl, d: vnl, o: vtl == vnl ? 'OK' : 'NOK')}")
133
- end
134
-
135
- # @return [String] texto transacoes trades
136
- def mtrades
137
- return unless ops[:v] && novcdet.any?
138
-
139
- puts("\ntrades data hora dt criacao tipo par qtd eur")
140
- novcdet.sort_by { |i| -i[:srx] }.each { |o| puts(formata_trades(o)) }
141
- end
142
-
143
- # @return [String] texto transacoes ledger
144
- def mledger
145
- return unless ops[:v] && novcdel.any?
146
-
147
- puts("\nledger data hora tipo moe quantidade custo")
148
- novcdel.sort_by { |i| -i[:srx] }.each { |o| puts(formata_ledger(o)) }
117
+ def show_all?
118
+ ops[:t] || false
149
119
  end
150
120
 
151
- # Processa os trades para garantir que as datas estejam no formato correto
121
+ # @param [Hash] itm recursos bitcoinde
122
+ # @return [Hash<BigDecimal>] moedas & sados
152
123
  def pdea(itm)
153
124
  itm.select { |k, _| EM.include?(k) }.transform_values { |o| o.merge(total_amount: o[:total_amount].to_d) }
154
125
  end
155
126
 
156
- # Processa time field somtimes is string
157
- def ptm(itm)
158
- itm.is_a?(String) ? Time.parse(itm) : itm
127
+ # @param [Object] val time bitcoinde
128
+ # @return [Time] processa time (somtimes is string)
129
+ def ptm(val)
130
+ val.is_a?(String) ? Time.parse(val) : val
159
131
  end
160
132
 
133
+ # @param [Hash] itm transacao bitcoinde
134
+ # @return [Hash] transaccao filtrada
161
135
  def pdes(key, itm)
162
136
  tym = ptm(itm[key])
163
137
  itm.merge(srx: Integer(tym), key => tym)
164
138
  end
165
139
 
166
- # Processa os trades para garantir que as datas estejam no formato correto
167
- def pdet(itm)
168
- itm.map do |t|
140
+ # @param [Array<Hash>] htx trade bitcoinde
141
+ # @return [Array<Hash>] transaccao filtrada
142
+ def pdet(htx)
143
+ htx.map do |t|
169
144
  pdes(:successfully_finished_at, t).merge(
170
145
  trade_marked_as_paid_at: ptm(t[:trade_marked_as_paid_at]),
171
- amount_currency_to_trade: t[:amount_currency_to_trade].to_d,
172
- volume_currency_to_pay: t[:volume_currency_to_pay].to_d,
146
+ username: t[:trading_partner_information][:username],
147
+ btc: t[:type] == 'buy' ? t[:amount_currency_to_trade_after_fee].to_d : -1 * t[:amount_currency_to_trade].to_d,
148
+ eur: t[:volume_currency_to_pay_after_fee].to_d,
173
149
  trading_pair: t[:trading_pair].upcase
174
150
  )
175
151
  end
176
152
  end
177
153
 
178
- # Processa os ledger entries para garantir que as datas estejam no formato correto
179
- def pdel(itm)
180
- itm.map { |t| pdes(:time, t).merge(qt: t[:qt].to_d, fee: t[:fee].to_d, moe: t[:moe].upcase) }
154
+ # @param [Array<Hash>] hlx ledger bitcoinde
155
+ # @return [Array<Hash>] transaccao filtrada
156
+ def pdel(hlx)
157
+ hlx.map { |t| pdes(:time, t).merge(qtd: (t[:tp] == 'withdrawal' ? -1 : 1) * t[:qt].to_d, nxid: t[:txid], fee: t[:fee].to_d, moe: t[:moe].upcase) }
158
+ end
159
+
160
+ # @return [Array<String>] indices trades bigquery
161
+ def bqkyt
162
+ @bqkyt ||= show_all? ? [] : (bqd[:nt]&.map { |t| t[:txid] } || [])
163
+ end
164
+
165
+ # @return [Array<Integer>] indices ledger bigquery
166
+ def bqkyl
167
+ @bqkyl ||= show_all? ? [] : (bqd[:nl]&.map { |l| l[:txid] } || [])
168
+ end
169
+
170
+ # @return [Array<String>] lista txid trades novos
171
+ def kyt
172
+ @kyt ||= exd[:tt].map { |t| t[:trade_id] } - bqkyt
173
+ end
174
+
175
+ # @return [Array<Integer>] lista txid ledger novos
176
+ def kyl
177
+ @kyl ||= exd[:tl].map { |t| t[:txid] } - bqkyl
178
+ end
179
+
180
+ # @return [Array<Hash>] lista trades bitcoinde novos
181
+ def novcdet
182
+ @novcdet ||= exd[:tt].select { |obj| kyt.include?(obj[:trade_id]) }
183
+ end
184
+
185
+ # @return [Array<Hash>] lista ledger (deposits + withdrawals) bitcoinde novos
186
+ def novcdel
187
+ @novcdel ||= exd[:tl].select { |obj| kyl.include?(obj[:txid]) }
181
188
  end
182
189
  end
183
190
  end