cns 0.1.1 → 0.1.2

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.
@@ -2,44 +2,65 @@
2
2
 
3
3
  # @author Hernani Rodrigues Vaz
4
4
  module Cns
5
- # (see Bigquery)
5
+ # classe para processar bigquery
6
6
  class Bigquery
7
+ private
8
+
7
9
  # @return [String] comando insert SQL formatado det (trades)
8
10
  def det_ins
9
- "insert #{BD}.det(tp,btc,eur,time,user,id,dtc,dias) VALUES#{apide.trades.map { |h| det_val1(h) }.join(',')}"
11
+ "insert #{BD}.det(txid,time,tp,user,btc,eur,dtc,dias) VALUES#{apide.trades.map { |h| det_val1(h) }.join(',')}"
10
12
  end
11
13
 
12
- # @example (see Apide#trades)
13
- # @param [Hash] htx transacao trade apide
14
+ # @example (see Apice#trades_de)
15
+ # @param [Hash] htx trade bitcoinde
14
16
  # @return [String] valores formatados det (trades parte1)
15
17
  def det_val1(htx)
16
- "('#{htx[:type]}'," \
17
- 'cast(' \
18
- "#{htx[:type] == 'buy' ? htx[:amount_currency_to_trade_after_fee] : "-#{htx[:amount_currency_to_trade]}"}" \
19
- ' as numeric),' \
20
- "cast(#{htx[:volume_currency_to_pay_after_fee]} as numeric)," \
18
+ "('#{htx[:trade_id]}'," \
21
19
  "DATETIME(TIMESTAMP('#{htx[:successfully_finished_at]}'))," \
20
+ "'#{htx[:type]}'," \
22
21
  "'#{htx[:trading_partner_information][:username]}'," \
23
22
  "#{det_val2(htx)}"
24
23
  end
25
24
 
26
- # @param (see #det_val1)
25
+ # @param (see det_val1)
27
26
  # @return [String] valores formatados det (trades parte2)
28
27
  def det_val2(htx)
29
- "'#{htx[:trade_id]}'," \
30
- "DATETIME(TIMESTAMP('#{htx[:trade_marked_as_paid_at]}'))," \
28
+ 'cast(' \
29
+ "#{htx[:type] == 'buy' ? htx[:amount_currency_to_trade_after_fee] : "-#{htx[:amount_currency_to_trade]}"}" \
30
+ ' as numeric),' \
31
+ "cast(#{htx[:volume_currency_to_pay_after_fee]} as numeric)," \
32
+ "DATETIME(TIMESTAMP('#{htx[:trade_marked_as_paid_at]}'))," \
31
33
  "#{Integer(ops[:h][htx[:trade_id]] || 0)})"
32
34
  end
33
35
 
36
+ # @return [String] comando insert SQL formatado del (ledger)
37
+ def del_ins
38
+ "insert #{BD}.del(txid,time,tp,add,moe,qt,fee) VALUES#{apide.ledger.map { |h| del_val(h) }.join(',')}"
39
+ end
40
+
41
+ # @example (see Apice#deposits_de)
42
+ # @example (see Apice#withdrawals_de)
43
+ # @param [Hash] hlx ledger (deposits + withdrawals) bitcoinde
44
+ # @return [String] valores formatados del (ledger)
45
+ def del_val(hlx)
46
+ "(#{hlx[:txid]}," \
47
+ "DATETIME(TIMESTAMP('#{hlx[:time].iso8601}'))," \
48
+ "'#{hlx[:tp]}'," \
49
+ "'#{hlx[:add]}'," \
50
+ "'#{hlx[:moe]}'," \
51
+ "cast(#{hlx[:tp] =='withdrawal' ? '-' : ''}#{hlx[:qt]} as numeric)," \
52
+ "cast(#{hlx[:fee]} as numeric))"
53
+ end
54
+
34
55
  # @return [String] comando insert SQL formatado ust (trades)
35
56
  def ust_ins
36
57
  "insert #{BD}.ust(txid,ordertxid,pair,time,type,ordertype,price,cost,fee,vol,margin,misc,ledgers,dias) " \
37
58
  "VALUES#{apius.trades.map { |k, v| ust_val1(k, v) }.join(',')}"
38
59
  end
39
60
 
40
- # @example (see Apius#trades)
61
+ # @example (see Apice#trades_us)
41
62
  # @param [String] idx identificador transacao
42
- # @param [Hash] htx transacao trade apius
63
+ # @param [Hash] htx trade kraken
43
64
  # @return [String] valores formatados ust (trades parte1)
44
65
  def ust_val1(idx, htx)
45
66
  "('#{idx}'," \
@@ -54,7 +75,7 @@ module Cns
54
75
  "#{ust_val2(idx, htx)}"
55
76
  end
56
77
 
57
- # @param (see #ust_val1)
78
+ # @param (see ust_val1)
58
79
  # @return [String] valores formatados ust (trades parte2)
59
80
  def ust_val2(idx, htx)
60
81
  "cast(#{htx[:vol]} as numeric)," \
@@ -64,34 +85,15 @@ module Cns
64
85
  "#{Integer(ops[:h][idx] || 0)})"
65
86
  end
66
87
 
67
- # @return [String] comando insert SQL formatado del (ledger)
68
- def del_ins
69
- "insert #{BD}.del(time,tp,qtxt,id,qt,fee,lgid) VALUES#{apide.ledger.map { |h| del_val(h) }.join(',')}"
70
- end
71
-
72
- # @example (see Apide#deposit_hash)
73
- # @example (see Apide#withdrawal_hash)
74
- # @param [Hash] hlx transacao uniformizada deposits + withdrawals apide
75
- # @return [String] valores formatados del (ledger)
76
- def del_val(hlx)
77
- "(DATETIME(TIMESTAMP('#{hlx[:time].iso8601}'))," \
78
- "'#{hlx[:tp]}'," \
79
- "'#{hlx[:qtxt]}'," \
80
- "'#{hlx[:id]}'," \
81
- "cast(#{hlx[:tp] == 'out' ? '-' : ''}#{hlx[:qt]} as numeric)," \
82
- "cast(#{hlx[:fee]} as numeric)," \
83
- "#{hlx[:lgid]})"
84
- end
85
-
86
88
  # @return [String] comando insert SQL formatado usl (ledger)
87
89
  def usl_ins
88
90
  "insert #{BD}.usl(txid,refid,time,type,aclass,asset,amount,fee) " \
89
91
  "VALUES#{apius.ledger.map { |k, v| usl_val(k, v) }.join(',')}"
90
92
  end
91
93
 
92
- # @example (see Apius#ledger)
93
- # @param idx (see #ust_val1)
94
- # @param [Hash] hlx transacao ledger apius
94
+ # @example (see Apice#ledger_us)
95
+ # @param idx (see ust_val1)
96
+ # @param [Hash] hlx ledger kraken
95
97
  # @return [String] valores formatados usl (ledger)
96
98
  def usl_val(idx, hlx)
97
99
  "('#{idx}'," \
@@ -109,8 +111,8 @@ module Cns
109
111
  "insert #{BD}.fr(uuid,tipo,valor,moe,time,dias) VALUES#{apifr.ledger.map { |h| frl_val(h) }.join(',')}"
110
112
  end
111
113
 
112
- # @example (see Apifr#ledger)
113
- # @param [Hash] hlx transacao ledger apifr
114
+ # @example (see Apice#ledger_fr)
115
+ # @param [Hash] hlx ledger paymium
114
116
  # @return [String] valores formatados frl (ledger)
115
117
  def frl_val(hlx)
116
118
  "('#{hlx[:uuid]}'," \
@@ -127,8 +129,8 @@ module Cns
127
129
  "VALUES#{apimt.ledger.map { |h| mtl_val1(h) }.join(',')}"
128
130
  end
129
131
 
130
- # @example (see Apimt#ledger)
131
- # @param [Hash] hlx transacao ledger apimt
132
+ # @example (see Apice#ledger_mt)
133
+ # @param [Hash] hlx ledger therock
132
134
  # @return [String] valores formatados mtl (ledger parte1)
133
135
  def mtl_val1(hlx)
134
136
  "(#{hlx[:id]}," \
@@ -140,7 +142,7 @@ module Cns
140
142
  "#{mtl_val2(hlx)}"
141
143
  end
142
144
 
143
- # @param hlx (see #mtl_val1)
145
+ # @param (see mtl_val1)
144
146
  # @return [String] valores formatados mtl (ledger parte2)
145
147
  def mtl_val2(hlx)
146
148
  "#{hlx[:note].to_s.empty? ? 'null' : "'#{hlx[:note]}'"}," \
@@ -4,12 +4,12 @@ require('bigdecimal/util')
4
4
 
5
5
  # @author Hernani Rodrigues Vaz
6
6
  module Cns
7
- # classe para processar saldos & transacoes trades e ledger do bitcoinde
7
+ # classe para processar transacoes trades/ledger do bitcoinde
8
8
  class Bitcoinde
9
9
  # @return [Apius] API bitcoinde
10
10
  attr_reader :api
11
11
  # @return [Array<Hash>] todos os dados bigquery
12
- attr_reader :dbq
12
+ attr_reader :bqd
13
13
  # @return [Thor::CoreExt::HashWithIndifferentAccess] opcoes trabalho
14
14
  attr_reader :ops
15
15
 
@@ -20,47 +20,59 @@ module Cns
20
20
  # @option pop [Boolean] :t (false) mostra transacoes todas ou somente novas?
21
21
  # @return [Bitcoinde] API bitcoinde - obter saldos & transacoes trades e ledger
22
22
  def initialize(dad, pop)
23
- # API bitcoinde base
24
- @api = Apide.new
25
- @dbq = dad
23
+ @api = Apice.new
24
+ @bqd = dad
26
25
  @ops = pop
27
26
  end
28
27
 
29
- # @return [Hash] dados exchange bitcoinde - saldos & transacoes trades e ledger
28
+ # @return [Array<Hash>] lista trades bitcoinde novos
29
+ def trades
30
+ @trades ||= exd[:tt].select { |h| kyt.include?(h[:trade_id]) }
31
+ end
32
+
33
+ # @return [Array<Hash>] lista ledger (deposits + withdrawals) bitcoinde novos
34
+ def ledger
35
+ @ledger ||= exd[:tl].select { |h| kyl.include?(h[:txid]) }
36
+ end
37
+
38
+ # @return [String] texto saldos & transacoes & ajuste dias
39
+ def mostra_resumo
40
+ puts("\nBITCOINDE\ntipo bitcoinde bigquery")
41
+ exd[:sl].each { |k, v| puts(formata_saldos(k, v)) }
42
+ mostra_totais
43
+
44
+ mostra_trades
45
+ mostra_ledger
46
+ return if trades.empty?
47
+
48
+ puts("\nstring ajuste dias dos trades\n-h=#{kyt.map { |e| "#{e}:0" }.join(' ')}")
49
+ end
50
+
51
+ # @return [Hash] dados exchange bitcoinde - saldos & trades & deposits & withdrawals
30
52
  def exd
31
53
  @exd ||= {
32
- sl: api.account,
33
- tt: api.trades,
34
- tl: api.deposits + api.withdrawals
54
+ sl: api.account_de,
55
+ tt: api.trades_de,
56
+ tl: api.deposits_de + api.withdrawals_de
35
57
  }
36
58
  end
37
59
 
38
- # @return [Array<String>] lista txid de transacoes trades
60
+ # @return [Array<String>] lista txid dos trades novos
39
61
  def kyt
40
- @kyt ||= exd[:tt].map { |h| h[:trade_id] }.flatten - (ops[:t] ? [] : dbq[:nt].map { |e| e[:txid] })
62
+ @kyt ||= exd[:tt].map { |h| h[:trade_id] }.flatten - (ops[:t] ? [] : bqd[:nt].map { |e| e[:txid] })
41
63
  end
42
64
 
43
- # @return [Array<Integer>] lista txid de transacoes ledger
65
+ # @return [Array<Integer>] lista txid dos ledger novos
44
66
  def kyl
45
- @kyl ||= exd[:tl].map { |h| h[:lgid] }.flatten - (ops[:t] ? [] : dbq[:nl].map { |e| e[:txid] })
46
- end
47
-
48
- # @return [Hash] transacoes trades
49
- def trades
50
- @trades ||= exd[:tt].select { |h| kyt.include?(h[:trade_id]) }
51
- end
52
-
53
- # @return [Hash] transacoes ledger
54
- def ledger
55
- @ledger ||= exd[:tl].select { |h| kyl.include?(h[:lgid]) }
67
+ @kyl ||= exd[:tl].map { |h| h[:txid] }.flatten - (ops[:t] ? [] : bqd[:nl].map { |e| e[:txid] })
56
68
  end
57
69
 
58
- # @example (see Apide#account)
70
+ # @example (see Apice#account_de)
59
71
  # @param [String] moe codigo bitcoinde da moeda
60
72
  # @param [Hash] hsx saldo bitcoinde da moeda
61
- # @return [String] texto formatado saldos (bitcoinde)
73
+ # @return [String] texto formatado saldos
62
74
  def formata_saldos(moe, hsx)
63
- b = dbq[:sl][moe.downcase.to_sym].to_d
75
+ b = bqd[:sl][moe.downcase.to_sym].to_d
64
76
  e = hsx[:total_amount].to_d
65
77
  format(
66
78
  '%<mo>-5.5s %<ex>21.9f %<bq>21.9f %<ok>3.3s',
@@ -71,9 +83,9 @@ module Cns
71
83
  )
72
84
  end
73
85
 
74
- # @example (see Apide#trades)
86
+ # @example (see Apice#trades_de)
75
87
  # @param (see Bigquery#det_val1)
76
- # @return [String] texto formatado transacao trade
88
+ # @return [String] texto formatado trade
77
89
  def formata_trades(htx)
78
90
  format(
79
91
  '%<ky>-6.6s %<dt>19.19s %<dp>10.10s %<ty>-5.5s %<mo>-8.8s %<vl>18.8f %<co>8.2f',
@@ -87,48 +99,47 @@ module Cns
87
99
  )
88
100
  end
89
101
 
90
- # @example (see Apide#deposit_hash)
91
- # @example (see Apide#withdrawal_hash)
102
+ # @example (see Apice#deposits_unif_de)
103
+ # @example (see Apice#withdrawals_unif_de)
92
104
  # @param (see Bigquery#del_val)
93
- # @return [String] texto formatado transacao ledger
105
+ # @return [String] texto formatado ledger
94
106
  def formata_ledger(hlx)
95
107
  format(
96
108
  '%<ky>6i %<dt>19.19s %<ty>-10.10s %<mo>-3.3s %<pr>19.8f %<vl>18.8f',
97
- ky: hlx[:lgid],
109
+ ky: hlx[:txid],
98
110
  dt: hlx[:time],
99
111
  ty: hlx[:tp],
100
- mo: hlx[:qtxt].upcase,
112
+ mo: hlx[:moe].upcase,
101
113
  pr: hlx[:qt].to_d,
102
114
  vl: hlx[:fee].to_d
103
115
  )
104
116
  end
105
117
 
106
- # @return [String] texto saldos & transacoes & ajuste dias
107
- def mostra_resumo
108
- puts("\nBITCOINDE\nmoeda saldo bitcoinde saldo bigquery")
109
- exd[:sl].each { |k, v| puts(formata_saldos(k, v)) }
118
+ # @return [String] texto numero de transacoes
119
+ def mostra_totais
120
+ a = exd[:tt].count
121
+ b = bqd[:nt].count
122
+ c = exd[:tl].count
123
+ d = bqd[:nl].count
110
124
 
111
- mostra_trades
112
- mostra_ledger
113
- return unless trades.count.positive?
114
-
115
- puts("\nstring ajuste dias dos trades\n-h=#{kyt.map { |e| "#{e}:0" }.join(' ')}")
125
+ puts("TRADES #{format('%<a>20i %<b>21i %<o>3.3s', a: a, b: b, o: a == b ? 'OK' : 'NOK')}")
126
+ puts("LEDGER #{format('%<c>20i %<d>21i %<o>3.3s', c: c, d: d, o: c == d ? 'OK' : 'NOK')}")
116
127
  end
117
128
 
118
129
  # @return [String] texto transacoes trades
119
130
  def mostra_trades
120
- return unless ops[:v] && trades.count.positive?
131
+ return unless ops[:v] && !trades.empty?
121
132
 
122
- puts("\ntrades data hora dt criacao tipo par ---------------qtd -----eur")
133
+ puts("\ntrades data hora dt criacao tipo par qtd eur")
123
134
  trades.sort { |a, b| Time.parse(b[:successfully_finished_at]) <=> Time.parse(a[:successfully_finished_at]) }
124
135
  .each { |h| puts(formata_trades(h)) }
125
136
  end
126
137
 
127
138
  # @return [String] texto transacoes ledger
128
139
  def mostra_ledger
129
- return unless ops[:v] && ledger.count.positive?
140
+ return unless ops[:v] && !ledger.empty?
130
141
 
131
- puts("\nledger data hora tipo moe ---------quantidade -------------custo")
142
+ puts("\nledger data hora tipo moe quantidade custo")
132
143
  ledger.sort { |a, b| b[:time] <=> a[:time] }.each { |h| puts(formata_ledger(h)) }
133
144
  end
134
145
  end
@@ -9,10 +9,10 @@ module Cns
9
9
 
10
10
  # (see Etherscan)
11
11
  class Etherscan
12
- # @return [Apies] API etherscan
12
+ # @return [Apibc] API blockchains
13
13
  attr_reader :api
14
14
  # @return [Array<Hash>] todos os dados bigquery
15
- attr_reader :dbq
15
+ attr_reader :bqd
16
16
  # @return [Thor::CoreExt::HashWithIndifferentAccess] opcoes trabalho
17
17
  attr_reader :ops
18
18
 
@@ -22,77 +22,78 @@ module Cns
22
22
  # @option pop [Boolean] :v (false) mostra dados transacoes normais & tokens?
23
23
  # @return [Etherscan] API etherscan - processar transacoes normais e tokens
24
24
  def initialize(dad, pop)
25
- @api = Apies.new
26
- @dbq = dad
25
+ @api = Apibc.new
26
+ @bqd = dad
27
27
  @ops = pop
28
28
  end
29
29
 
30
+ # @return [Array<Hash>] lista transacoes normais novas
31
+ def novtx
32
+ @novtx ||= bcd.map { |e| e[:tx].select { |n| idt.include?(n[:itx]) } }.flatten
33
+ end
34
+
35
+ # @return [Array<Hash>] lista transacoes token novas
36
+ def novkx
37
+ @novkx ||= bcd.map { |e| e[:kx].select { |n| idk.include?(n[:itx]) } }.flatten
38
+ end
39
+
30
40
  # @return [Array<String>] lista dos meus enderecos
31
41
  def lax
32
- @lax ||= dbq[:wb].map { |h| h[:ax] }
42
+ @lax ||= bqd[:wb].map { |h| h[:ax] }
33
43
  end
34
44
 
35
45
  # @return [Array<Hash>] todos os dados etherscan - saldos & transacoes
36
- def dbc
37
- @dbc ||= api.account(lax).map { |e| base_bc(e) }
46
+ def bcd
47
+ @bcd ||= api.account_es(lax).map { |e| base_bc(e) }
38
48
  end
39
49
 
40
50
  # @return [Array<Hash>] todos os dados juntos bigquery & etherscan
41
51
  def dados
42
- @dados ||= dbq[:wb].map { |b| bq_bc(b, dbc.select { |s| b[:ax] == s[:ax] }.first) }
52
+ @dados ||= bqd[:wb].map { |b| bq_bc(b, bcd.select { |s| b[:ax] == s[:ax] }.first) }
43
53
  end
44
54
 
45
55
  # @return [Array<Integer>] lista indices transacoes normais novas
46
- def bnt
47
- @bnt ||= (dbc.map { |e| e[:tx].map { |n| n[:itx] } }.flatten - (ops[:t] ? [] : dbq[:nt].map { |t| t[:itx] }))
56
+ def idt
57
+ @idt ||= (bcd.map { |e| e[:tx].map { |n| n[:itx] } }.flatten - (ops[:t] ? [] : bqd[:nt].map { |t| t[:itx] }))
48
58
  end
49
59
 
50
60
  # @return [Array<Integer>] lista indices transacoes token novas
51
- def bnk
52
- @bnk ||= (dbc.map { |e| e[:kx].map { |n| n[:itx] } }.flatten - (ops[:t] ? [] : dbq[:nk].map { |t| t[:itx] }))
53
- end
54
-
55
- # @return [Array<Hash>] lista transacoes normais novas
56
- def novtx
57
- @novtx ||= dbc.map { |e| e[:tx].select { |n| bnt.include?(n[:itx]) } }.flatten
58
- end
59
-
60
- # @return [Array<Hash>] lista transacoes token novas
61
- def novkx
62
- @novkx ||= dbc.map { |e| e[:kx].select { |n| bnk.include?(n[:itx]) } }.flatten
61
+ def idk
62
+ @idk ||= (bcd.map { |e| e[:kx].map { |n| n[:itx] } }.flatten - (ops[:t] ? [] : bqd[:nk].map { |t| t[:itx] }))
63
63
  end
64
64
 
65
- # @param [Hash] hbc dados etherscan
65
+ # @example (see Apibc#account_es)
66
+ # @param [Hash] abc account etherscan
66
67
  # @return [Hash] dados etherscan - address, saldo & transacoes
67
- def base_bc(hbc)
68
- a = hbc[:account]
68
+ def base_bc(abc)
69
+ a = abc[:account]
69
70
  {
70
71
  ax: a,
71
- sl: (hbc[:balance].to_d / 10**18).round(10),
72
- tx: filtrar_tx(a, api.norml_tx(a)),
73
- kx: filtrar_tx(a, api.token_tx(a))
72
+ sl: (abc[:balance].to_d / 10**18).round(10),
73
+ tx: filtrar_tx(a, api.norml_es(a)),
74
+ kx: filtrar_tx(a, api.token_es(a))
74
75
  }
75
76
  end
76
77
 
77
- # @param [Hash] hbq dados bigquery
78
- # @param hbc (see base_bc)
78
+ # @param [Hash] wbq wallet bigquery
79
+ # @param [Hash] hbc dados etherscan - address, saldo & transacoes
79
80
  # @return [Hash] dados juntos bigquery & etherscan
80
- def bq_bc(hbq, hbc)
81
+ def bq_bc(wbq, hbc)
81
82
  {
82
- id: hbq[:id],
83
- ax: hbq[:ax],
84
- bs: hbq[:sl],
85
- bt: dbq[:nt].select { |t| t[:iax] == hbq[:ax] },
86
- bk: dbq[:nk].select { |t| t[:iax] == hbq[:ax] },
83
+ id: wbq[:id],
84
+ ax: wbq[:ax],
85
+ bs: wbq[:sl],
86
+ bt: bqd[:nt].select { |t| t[:iax] == wbq[:ax] },
87
+ bk: bqd[:nk].select { |t| t[:iax] == wbq[:ax] },
87
88
  es: hbc[:sl],
88
89
  et: hbc[:tx],
89
90
  ek: hbc[:kx]
90
91
  }
91
92
  end
92
93
 
93
- # @param [String] add endereco carteira ETH
94
- # @param [Array<Hash>] ary lista das transacoes
95
- # @return [Array<Hash>] devolve lista de transacoes/token transfer events filtrada
94
+ # @param add (see Apibc#norml_es)
95
+ # @param [Array<Hash>] ary lista transacoes/token events
96
+ # @return [Array<Hash>] lista transacoes/token events filtrada
96
97
  def filtrar_tx(add, ary)
97
98
  # elimina transferencia from: (lax) to: (add) - esta transferencia aparece em from: (add) to: (lax)
98
99
  # elimina chaves irrelevantes (DL) & adiciona chave indice itx & adiciona identificador da carteira iax