cns 0.1.0 → 0.1.1

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.
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require('bigdecimal/util')
4
+
5
+ # @author Hernani Rodrigues Vaz
6
+ module Cns
7
+ # (see Greymass)
8
+ class Greymass
9
+ # @return [Apigm] API greymass
10
+ attr_reader :api
11
+ # @return [Array<Hash>] todos os dados bigquery
12
+ attr_reader :dbq
13
+ # @return [Thor::CoreExt::HashWithIndifferentAccess] opcoes trabalho
14
+ attr_reader :ops
15
+
16
+ # @param [Hash] dad todos os dados bigquery
17
+ # @param [Thor::CoreExt::HashWithIndifferentAccess] pop opcoes trabalho
18
+ # @option pop [Hash] :h ({}) configuracao dias ajuste reposicionamento temporal
19
+ # @option pop [Boolean] :v (false) mostra dados transacoes?
20
+ # @option pop [Boolean] :t (false) mostra transacoes todas ou somente novas?
21
+ # @return [Greymass] API greymass - processar transacoes
22
+ def initialize(dad, pop)
23
+ @api = Apigm.new
24
+ @dbq = dad
25
+ @ops = pop
26
+ end
27
+
28
+ # @return [Array<String>] lista dos meus enderecos
29
+ def lax
30
+ @lax ||= dbq[:wb].map { |h| h[:ax] }
31
+ end
32
+
33
+ # @return [Array<Hash>] todos os dados greymass - saldos & transacoes
34
+ def dbc
35
+ @dbc ||= dbq[:wb].map { |e| base_bc(e) }
36
+ end
37
+
38
+ # @return [Array<Hash>] todos os dados juntos bigquery & greymass
39
+ def dados
40
+ @dados ||= dbq[:wb].map { |b| bq_bc(b, dbc.select { |s| b[:ax] == s[:ax] }.first) }
41
+ end
42
+
43
+ # @return [Array<Integer>] lista indices transacoes novas
44
+ def bnt
45
+ @bnt ||= (dbc.map { |e| e[:tx].map { |n| n[:itx] } }.flatten - (ops[:t] ? [] : dbq[:nt].map { |t| t[:itx] }))
46
+ end
47
+
48
+ # @return [Array<Hash>] lista transacoes novas
49
+ def novax
50
+ @novax ||= dbc.map { |e| e[:tx].select { |s| bnt.include?(s[:itx]) } }.flatten
51
+ end
52
+
53
+ # @param [Hash] hbq dados bigquery wallet
54
+ # @return [Hash] dados greymass - address, saldo & transacoes
55
+ def base_bc(hbq)
56
+ a = hbq[:ax]
57
+ {
58
+ ax: a,
59
+ sl: greymass_sl(a).inject(:+),
60
+ tx: filtrar_tx(a, api.all_tx(a))
61
+ }
62
+ end
63
+
64
+ # @param hbq (see base_bc)
65
+ # @param [Hash] hbc dados greymass
66
+ # @return [Hash] dados juntos bigquery & greymass
67
+ def bq_bc(hbq, hbc)
68
+ {
69
+ id: hbq[:id],
70
+ ax: hbq[:ax],
71
+ bs: hbq[:sl],
72
+ bt: dbq[:nt].select { |t| t[:iax] == hbq[:ax] },
73
+ es: hbc[:sl],
74
+ et: hbc[:tx]
75
+ }
76
+ end
77
+
78
+ # @param (see filtrar_tx)
79
+ # @return [Array<BigDecimal>] lista recursos - liquido, net, spu
80
+ def greymass_sl(add)
81
+ v = api.account(account_name: add)
82
+ [
83
+ v[:core_liquid_balance].to_d,
84
+ v[:total_resources][:net_weight].to_d,
85
+ v[:total_resources][:cpu_weight].to_d
86
+ ]
87
+ end
88
+
89
+ # @param [String] add endereco carteira EOS
90
+ # @param [Array<Hash>] ary lista das transacoes
91
+ # @return [Array<Hash>] lista transacoes ligadas a uma carteira filtrada
92
+ def filtrar_tx(add, ary)
93
+ # elimina transferencia from: (lax) to: (add) - esta transferencia aparece em from: (add) to: (lax)
94
+ # adiciona chave indice itx & adiciona identificador da carteira iax
95
+ ary.delete_if { |h| act_data(h)[:to] == add && lax.include?(act_data(h)[:from]) }
96
+ .map { |h| h.merge(itx: h[:global_action_seq], iax: add) }
97
+ end
98
+
99
+ # @return [Array<Hash>] lista ordenada transacoes novas
100
+ def sorax
101
+ novax.sort { |a, b| b[:itx] <=> a[:itx] }
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @author Hernani Rodrigues Vaz
4
+ module Cns
5
+ # classe para processar carteiras & transacoes
6
+ class Greymass
7
+ # @param [Hash] hjn dados juntos bigquery & greymass
8
+ # @return [String] texto formatado duma carteira
9
+ def formata_carteira(hjn)
10
+ format(
11
+ '%<s1>-12.12s %<v1>14.4f %<n1>4i %<v2>14.4f %<n2>4i %<ok>-3s',
12
+ s1: hjn[:ax],
13
+ v1: hjn[:bs],
14
+ n1: hjn[:bt].count,
15
+ v2: hjn[:es],
16
+ n2: hjn[:et].count,
17
+ ok: ok?(hjn) ? 'OK' : 'NOK'
18
+ )
19
+ end
20
+
21
+ # @param (see formata_carteira)
22
+ # @return [Boolean] carteira tem transacoes novas(sim=NOK, nao=OK)?
23
+ def ok?(hjn)
24
+ hjn[:bs] == hjn[:es] && hjn[:bt].count == hjn[:et].count
25
+ end
26
+
27
+ # @param [Hash] htx transacao
28
+ # @return [String] texto formatado transacao
29
+ def formata_transacao(htx)
30
+ format(
31
+ '%<bn>12i %<fr>-12.12s %<to>-12.12s %<ac>-10.10s %<dt>10.10s %<vl>12.4f %<sy>-6.6s',
32
+ bn: htx[:itx],
33
+ fr: act_data(htx)[:from],
34
+ to: act_data(htx)[:to],
35
+ ac: act(htx)[:name],
36
+ dt: Date.parse(htx[:block_time]),
37
+ vl: act_data_quantity(htx).to_d,
38
+ sy: act_data_quantity(htx)[/[[:upper:]]+/]
39
+ )
40
+ end
41
+
42
+ # @param (see formata_transacao)
43
+ # @return [Hash] dados da acao
44
+ def act(htx)
45
+ htx[:action_trace][:act]
46
+ end
47
+
48
+ # @param (see formata_transacao)
49
+ # @return [Hash] dados da acao
50
+ def act_data(htx)
51
+ act(htx)[:data]
52
+ end
53
+
54
+ # @param (see formata_transacao)
55
+ # @return [String] dados da quantidade
56
+ def act_data_quantity(htx)
57
+ act_data(htx)[:quantity].to_s
58
+ end
59
+
60
+ # @return [String] texto carteiras & transacoes & ajuste dias
61
+ def mostra_resumo
62
+ return unless dados.count.positive?
63
+
64
+ puts("\naddress bigquery ntx greymass ntx")
65
+ dados.each { |e| puts(formata_carteira(e)) }
66
+ mostra_transacoes_novas
67
+ mostra_configuracao_ajuste_dias
68
+ end
69
+
70
+ # @return [String] texto transacoes
71
+ def mostra_transacoes_novas
72
+ return unless ops[:v] && novax.count.positive?
73
+
74
+ puts("\nsequence num from to accao data valor moeda")
75
+ sorax.each { |e| puts(formata_transacao(e)) }
76
+ end
77
+
78
+ # @return [String] texto configuracao ajuste dias das transacoes
79
+ def mostra_configuracao_ajuste_dias
80
+ return unless novax.count.positive?
81
+
82
+ puts("\nstring ajuste dias\n-h=#{sorax.map { |e| "#{e[:itx]}:0" }.join(' ')}")
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ require('bigdecimal/util')
4
+
5
+ # @author Hernani Rodrigues Vaz
6
+ module Cns
7
+ # classe para processar saldos & transacoes trades e ledger
8
+ class Kraken
9
+ # @return [Apius] API kraken
10
+ attr_reader :api
11
+ # @return [Array<Hash>] todos os dados bigquery
12
+ attr_reader :dbq
13
+ # @return [Thor::CoreExt::HashWithIndifferentAccess] opcoes trabalho
14
+ attr_reader :ops
15
+
16
+ # @param [Hash] dad todos os dados bigquery
17
+ # @param [Thor::CoreExt::HashWithIndifferentAccess] pop opcoes trabalho
18
+ # @option pop [Hash] :h ({}) configuracao dias ajuste reposicionamento temporal
19
+ # @option pop [Boolean] :v (false) mostra dados transacoes trades & ledger?
20
+ # @option pop [Boolean] :t (false) mostra transacoes todas ou somente novas?
21
+ # @return [Kraken] API kraken - obter saldos & transacoes trades e ledger
22
+ def initialize(dad, pop)
23
+ # API kraken base
24
+ @api = Apius.new
25
+ @dbq = dad
26
+ @ops = pop
27
+ end
28
+
29
+ # @return [Hash] dados exchange kraken - saldos & transacoes trades e ledger
30
+ def exd
31
+ @exd ||= {
32
+ sl: api.account,
33
+ kt: api.trades,
34
+ kl: api.ledger
35
+ }
36
+ end
37
+
38
+ # @return [Array<String>] lista txid de transacoes trades
39
+ def kyt
40
+ @kyt ||= exd[:kt].keys - (ops[:t] ? [] : dbq[:nt].map { |e| e[:txid].to_sym })
41
+ end
42
+
43
+ # @return [Array<String>] lista txid de transacoes ledger
44
+ def kyl
45
+ @kyl ||= exd[:kl].keys - (ops[:t] ? [] : dbq[:nl].map { |e| e[:txid].to_sym })
46
+ end
47
+
48
+ # @return [Hash] transacoes trades
49
+ def trades
50
+ @trades ||= exd[:kt].select { |k, _| kyt.include?(k) }
51
+ end
52
+
53
+ # @return [Hash] transacoes ledger
54
+ def ledger
55
+ @ledger ||= exd[:kl].select { |k, _| kyl.include?(k) }
56
+ end
57
+
58
+ # @example (see Apius#account)
59
+ # @param [String] moe codigo kraken da moeda
60
+ # @param [BigDecimal] sal saldo kraken da moeda
61
+ # @return [String] texto formatado saldos (kraken/bigquery) & iguais/ok/nok?
62
+ def formata_saldos(moe, sal)
63
+ t = dbq[:sl][moe.downcase.to_sym].to_d
64
+ format(
65
+ '%<mo>-5.5s %<kr>21.9f %<bq>21.9f %<ok>3.3s',
66
+ mo: moe.upcase,
67
+ kr: sal,
68
+ bq: t,
69
+ ok: t == sal ? 'OK' : 'NOK'
70
+ )
71
+ end
72
+
73
+ # @example (see Apius#trades)
74
+ # @param (see Bigquery#ust_val1)
75
+ # @return [String] texto formatado transacao trade
76
+ def formata_trades(idx, htx)
77
+ format(
78
+ '%<ky>-6.6s %<dt>19.19s %<ty>-10.10s %<mo>-8.8s %<pr>8.2f %<vl>15.7f %<co>8.2f',
79
+ ky: idx,
80
+ dt: Time.at(htx[:time]),
81
+ ty: "#{htx[:type]}/#{htx[:ordertype]}",
82
+ mo: htx[:pair].upcase,
83
+ pr: htx[:price].to_d,
84
+ vl: htx[:vol].to_d,
85
+ co: htx[:cost].to_d
86
+ )
87
+ end
88
+
89
+ # @example (see Apius#ledger)
90
+ # @param (see Bigquery#usl_val)
91
+ # @return [String] texto formatado transacao ledger
92
+ def formata_ledger(idx, hlx)
93
+ format(
94
+ '%<ky>-6.6s %<dt>19.19s %<ty>-10.10s %<mo>-4.4s %<pr>18.7f %<vl>18.7f',
95
+ ky: idx,
96
+ dt: Time.at(hlx[:time]),
97
+ ty: hlx[:type],
98
+ mo: hlx[:asset].upcase,
99
+ pr: hlx[:amount].to_d,
100
+ vl: hlx[:fee].to_d
101
+ )
102
+ end
103
+
104
+ # @return [String] texto saldos & transacoes & ajuste dias
105
+ def mostra_resumo
106
+ puts("\nKRAKEN\nmoeda saldo kraken saldo bigquery")
107
+ exd[:sl].each { |k, v| puts(formata_saldos(k, v.to_d)) }
108
+
109
+ mostra_trades
110
+ mostra_ledger
111
+ return unless trades.count.positive?
112
+
113
+ puts("\nstring ajuste dias dos trades\n-h=#{kyt.map { |e| "#{e}:0" }.join(' ')}")
114
+ end
115
+
116
+ # @return [String] texto transacoes trades
117
+ def mostra_trades
118
+ return unless ops[:v] && trades.count.positive?
119
+
120
+ puts("\ntrade data hora tipo par ---preco ---------volume ---custo")
121
+ trades.sort { |a, b| b[1][:time] <=> a[1][:time] }.each { |k, v| puts(formata_trades(k, v)) }
122
+ end
123
+
124
+ # @return [String] texto transacoes ledger
125
+ def mostra_ledger
126
+ return unless ops[:v] && ledger.count.positive?
127
+
128
+ puts("\nledger data hora tipo moeda -------quantidade -------------custo")
129
+ ledger.sort { |a, b| b[1][:time] <=> a[1][:time] }.each { |k, v| puts(formata_ledger(k, v)) }
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require('bigdecimal/util')
4
+
5
+ # @author Hernani Rodrigues Vaz
6
+ module Cns
7
+ # classe para processar saldos & transacoes ledger
8
+ class Paymium
9
+ # @return [Apius] API paymium
10
+ attr_reader :api
11
+ # @return [Array<Hash>] todos os dados bigquery
12
+ attr_reader :dbq
13
+ # @return [Thor::CoreExt::HashWithIndifferentAccess] opcoes trabalho
14
+ attr_reader :ops
15
+
16
+ # @param [Hash] dad todos os dados bigquery
17
+ # @param [Thor::CoreExt::HashWithIndifferentAccess] pop opcoes trabalho
18
+ # @option pop [Hash] :h ({}) configuracao dias ajuste reposicionamento temporal
19
+ # @option pop [Boolean] :v (false) mostra dados transacoes trades & ledger?
20
+ # @option pop [Boolean] :t (false) mostra transacoes todas ou somente novas?
21
+ # @return [Paymium] API paymium - obter saldos & transacoes ledger
22
+ def initialize(dad, pop)
23
+ # API paymium base
24
+ @api = Apifr.new
25
+ @dbq = dad
26
+ @ops = pop
27
+ end
28
+
29
+ # @return [Hash] dados exchange paymium - saldos & transacoes ledger
30
+ def exd
31
+ @exd ||= {
32
+ sl: api.account,
33
+ kl: api.ledger
34
+ }
35
+ end
36
+
37
+ # @return [Array<String>] lista txid de transacoes ledger
38
+ def kyl
39
+ @kyl ||= exd[:kl].map { |h| h[:account_operations].map { |o| o[:uuid] } }.flatten -
40
+ (ops[:t] ? [] : dbq[:nl].map { |e| e[:txid] })
41
+ end
42
+
43
+ # @return [Hash] transacoes ledger
44
+ def ledger
45
+ @ledger ||= exd[:kl].map { |h| h[:account_operations].select { |o| kyl.include?(o[:uuid]) } }.flatten
46
+ end
47
+
48
+ # @example (see Apifr#account)
49
+ # @param [Symbol] bqm symbol paymium da moeda
50
+ # @return [String] texto formatado saldos (paymium/bigquery) & iguais/ok/nok?
51
+ def formata_saldos(bqm)
52
+ b = dbq[:sl][bqm].to_d
53
+ t = exd[:sl]["balance_#{bqm}".to_sym].to_d
54
+ format(
55
+ '%<mo>-5.5s %<kr>21.9f %<bq>21.9f %<ok>3.3s',
56
+ mo: bqm.upcase,
57
+ kr: t,
58
+ bq: b,
59
+ ok: t == b ? 'OK' : 'NOK'
60
+ )
61
+ end
62
+
63
+ # @example (see Apifr#ledger)
64
+ # @param (see Bigquery#frl_val)
65
+ # @return [String] texto formatado transacao ledger
66
+ def formata_ledger(hlx)
67
+ format(
68
+ '%<ky>-18.18s %<dt>19.19s %<ty>-17.17s %<mo>-4.4s %<vl>18.7f',
69
+ ky: formata_uuid(hlx[:uuid], 18),
70
+ dt: Time.at(hlx[:created_at_int]),
71
+ ty: hlx[:name],
72
+ mo: hlx[:currency].upcase,
73
+ vl: hlx[:amount].to_d
74
+ )
75
+ end
76
+
77
+ # @example (see Apifr#ledger)
78
+ # @param [String] uid identificacor da ledger apifr
79
+ # @param [Integer] max chars a mostrar
80
+ # @return [String] texto formatado identificacor da ledger apifr
81
+ def formata_uuid(uid, max)
82
+ i = Integer(max / 2)
83
+ max < 7 ? 'erro' : "#{uid[0, i]}#{uid[-i..]}"
84
+ end
85
+
86
+ # @return [String] texto saldos & transacoes & ajuste dias
87
+ def mostra_resumo
88
+ puts("\nPAYMIUM\nmoeda saldo paymium saldo bigquery")
89
+ puts(formata_saldos(:btc))
90
+ puts(formata_saldos(:eur))
91
+
92
+ mostra_ledger
93
+ return unless ledger.count.positive?
94
+
95
+ puts("\nstring ajuste dias da ledger\n-h=#{kyl.map { |e| "#{e}:0" }.join(' ')}")
96
+ end
97
+
98
+ # @return [String] texto transacoes ledger
99
+ def mostra_ledger
100
+ return unless ops[:v] && ledger.count.positive?
101
+
102
+ puts("\nledger data hora tipo moeda -------quantidade")
103
+ ledger.sort { |a, b| b[:created_at_int] <=> a[:created_at_int] }.each { |o| puts(formata_ledger(o)) }
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require('bigdecimal/util')
4
+
5
+ # @author Hernani Rodrigues Vaz
6
+ module Cns
7
+ # classe para processar saldos & transacoes ledger
8
+ class TheRock
9
+ # @return [Apius] API therock
10
+ attr_reader :api
11
+ # @return [Array<Hash>] todos os dados bigquery
12
+ attr_reader :dbq
13
+ # @return [Thor::CoreExt::HashWithIndifferentAccess] opcoes trabalho
14
+ attr_reader :ops
15
+
16
+ # @param [Hash] dad todos os dados bigquery
17
+ # @param [Thor::CoreExt::HashWithIndifferentAccess] pop opcoes trabalho
18
+ # @option pop [Hash] :h ({}) configuracao dias ajuste reposicionamento temporal
19
+ # @option pop [Boolean] :v (false) mostra dados transacoes trades & ledger?
20
+ # @option pop [Boolean] :t (false) mostra transacoes todas ou somente novas?
21
+ # @return [TheRock] API therock - obter saldos & transacoes ledger
22
+ def initialize(dad, pop)
23
+ # API therock base
24
+ @api = Apimt.new
25
+ @dbq = dad
26
+ @ops = pop
27
+ end
28
+
29
+ # @return [Hash] dados exchange therock - saldos & transacoes ledger
30
+ def exd
31
+ @exd ||= {
32
+ sl: api.account,
33
+ kl: api.ledger
34
+ }
35
+ end
36
+
37
+ # @return [Array<String>] lista txid de transacoes ledger
38
+ def kyl
39
+ @kyl ||= exd[:kl].map { |h| h[:id] } - (ops[:t] ? [] : dbq[:nl].map { |e| e[:txid] })
40
+ end
41
+
42
+ # @return [Hash] transacoes ledger
43
+ def ledger
44
+ @ledger ||= exd[:kl].select { |o| kyl.include?(o[:id]) }
45
+ end
46
+
47
+ # @example (see Apimt#account)
48
+ # @param [Hash] hsl saldo therock da moeda
49
+ # @return [String] texto formatado saldos (therock/bigquery) & iguais/ok/nok?
50
+ def formata_saldos(hsl)
51
+ b = dbq[:sl][hsl[:currency].downcase.to_sym].to_d
52
+ k = hsl[:balance].to_d
53
+ format(
54
+ '%<mo>-5.5s %<kr>21.9f %<bq>21.9f %<ok>3.3s',
55
+ mo: hsl[:currency].upcase,
56
+ kr: k,
57
+ bq: b,
58
+ ok: k == b ? 'OK' : 'NOK'
59
+ )
60
+ end
61
+
62
+ # @example (see Apimt#ledger)
63
+ # @param (see Bigquery#mtl_val1)
64
+ # @return [String] texto formatado transacao ledger
65
+ def formata_ledger(hlx)
66
+ format(
67
+ '%<ky>6i %<dt>19.19s %<ty>-27.27s %<mo>-4.4s %<vl>20.7f',
68
+ ky: hlx[:id],
69
+ dt: Time.parse(hlx[:date]),
70
+ ty: hlx[:type],
71
+ mo: hlx[:currency].upcase,
72
+ vl: hlx[:price].to_d
73
+ )
74
+ end
75
+
76
+ # @return [String] texto saldos & transacoes & ajuste dias
77
+ def mostra_resumo
78
+ puts("\nTHEROCK\nmoeda saldo therock saldo bigquery")
79
+ exd[:sl].each { |h| puts(formata_saldos(h)) }
80
+
81
+ mostra_ledger
82
+ return unless ledger.count.positive?
83
+
84
+ puts("\nstring ajuste dias da ledger\n-h=#{kyl.map { |e| "#{e}:0" }.join(' ')}")
85
+ end
86
+
87
+ # @return [String] texto transacoes ledger
88
+ def mostra_ledger
89
+ return unless ops[:v] && ledger.count.positive?
90
+
91
+ puts("\nledger data hora tipo moeda ---------quantidade")
92
+ ledger.sort { |a, b| b[:id] <=> a[:id] }.each { |o| puts(formata_ledger(o)) }
93
+ end
94
+ end
95
+ end