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.
@@ -1,161 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require('faraday')
4
- require('json')
5
-
6
- # @author Hernani Rodrigues Vaz
7
- module Cns
8
- # classe para acesso dados blockchain ETH
9
- class Apies
10
- # @return [String] apikey a juntar aos pedidos HTTP url:
11
- attr_reader :key
12
- # @return [String] base URL to use as a prefix for all requests
13
- attr_reader :url
14
-
15
- # @param [String] chv apikey a juntar aos pedidos HTTP url:
16
- # @param [String] www base URL to use as a prefix for all requests
17
- # @return [Apies] API etherscan base
18
- def initialize(chv: ENV['ETHERSCAN_API_KEY'], www: 'https://api.etherscan.io/')
19
- @key = chv
20
- @url = www
21
- end
22
-
23
- # @return [<Symbol>] adapter for the connection - default :net_http
24
- def adapter
25
- @adapter ||= Faraday.default_adapter
26
- end
27
-
28
- # @return [<Faraday::Connection>] connection object with an URL & adapter
29
- def conn
30
- @conn ||=
31
- Faraday.new(url: url) do |c|
32
- c.request(:url_encoded)
33
- c.adapter(adapter)
34
- end
35
- end
36
-
37
- # @example
38
- # [
39
- # { account: '0x...', balance: '4000000000000000000' },
40
- # { account: '0x...', balance: '87000000000000000000' }
41
- # ]
42
- # @param [String] ads lista enderecos carteiras ETH (max 20)
43
- # @return [Array<Hash>] devolve lista com dados & saldo de carteiras ETH
44
- def account(ads)
45
- raise(Erro, 'maximo de 20 enderecos') if ads.size > 20
46
-
47
- get(action: 'balancemulti', address: ads.join(','), tag: 'latest')[:result]
48
- end
49
-
50
- # @example
51
- # [
52
- # {
53
- # blockNumber: '4984535',
54
- # timeStamp: '1517094794',
55
- # hash: '0x...',
56
- # nonce: '10',
57
- # blockHash: '0x...',
58
- # transactionIndex: '17',
59
- # from: '0x...',
60
- # to: '0x...',
61
- # value: '52627271000000000000',
62
- # gas: '21000',
63
- # gasPrice: '19000000000',
64
- # isError: '0',
65
- # txreceipt_status: '1',
66
- # input: '0x',
67
- # contractAddress: '',
68
- # gasUsed: '21000',
69
- # cumulativeGasUsed: '566293',
70
- # confirmations: '5848660'
71
- # },
72
- # {}
73
- # ]
74
- # @param [String] add endereco carteira ETH
75
- # @param [Hash] arg argumentos trabalho
76
- # @option arg [Integer] :start_block starting blockNo to retrieve results
77
- # @option arg [Integer] :end_block ending blockNo to retrieve results
78
- # @option arg [String] :sort asc -> ascending order, desc -> descending order
79
- # @option arg [Integer] :page to get paginated results
80
- # @option arg [Integer] :offset max records to return
81
- # @return [Array<Hash>] lista de transacoes
82
- def norml_tx(add, **arg)
83
- raise(Erro, 'endereco tem de ser definido') if add.nil? || add.empty?
84
-
85
- ledger(**arg.merge(action: 'txlist', address: add))
86
- end
87
-
88
- # @example registo duplicado
89
- # [
90
- # {
91
- # blockNumber: '3967652',
92
- # timeStamp: '1499081515',
93
- # hash: '0x registo duplicado com todos os dados iguais',
94
- # nonce: '3',
95
- # blockHash: '0x00a49e999036dc13dc6c4244bb1d51d3146fe7f00bfb500a7624d82e299c7328',
96
- # from: '0xd0a6e6c54dbc68db5db3a091b171a77407ff7ccf',
97
- # contractAddress: '0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0',
98
- # to: '0x...',
99
- # value: '0',
100
- # tokenName: 'EOS',
101
- # tokenSymbol: 'EOS',
102
- # tokenDecimal: '18',
103
- # transactionIndex: '83',
104
- # gas: '173399',
105
- # gasPrice: '21000000000',
106
- # gasUsed: '173398',
107
- # input: 'deprecated',
108
- # cumulativeGasUsed: '7484878',
109
- # confirmations: '3442641'
110
- # },
111
- # {}
112
- # ]
113
- # @param add (see norml_tx)
114
- # @param [String] cdd token address (nil to get a list of all ERC20 transactions)
115
- # @param arg (see norml_tx)
116
- # @option arg (see norml_tx)
117
- # @return [Array<Hash>] lista de token transfer events
118
- def token_tx(add, cdd = nil, **arg)
119
- raise(Erro, 'contrato ou endereco tem de estar definido') if (cdd || add).nil? || (cdd || add).empty?
120
-
121
- # registos duplicados aparecem em token events (ver exemplo acima)
122
- # -quando ha erros na blockchain (acho)
123
- # -quando ha token events identicos no mesmo block (acho)
124
- ledger(**arg.merge(action: 'tokentx', address: add, contractaddress: cdd))
125
- end
126
-
127
- # @param [Integer] pag pagina das transacoes a devolver
128
- # @param [Array<Hash>] ary lista acumuladora das transacoes a devolver
129
- # @param arg (see norml_tx)
130
- # @option arg (see norml_tx)
131
- # @return [Array<Hash>] devolve lista de transacoes/token transfer events
132
- def ledger(pag = 0, ary = [], **arg)
133
- r = get(**arg.merge(page: pag + 1, offset: 10_000))[:result]
134
- ary += r
135
- r.count < 10_000 ? ary : ledger(pag + 1, ary, **arg)
136
- rescue StandardError
137
- ary
138
- end
139
-
140
- private
141
-
142
- # @example
143
- # {
144
- # status: '1',
145
- # message: 'OK',
146
- # result: []
147
- # }
148
- # @return [Hash] resultado do HTTP request
149
- def get(**arg)
150
- JSON.parse(
151
- (conn.get('api') do |o|
152
- o.headers = { content_type: 'application/json', accept: 'application/json', user_agent: 'etherscan;ruby' }
153
- o.params = arg.merge(module: 'account', apikey: key).reject { |_, v| v.nil? }
154
- end).body,
155
- symbolize_names: true
156
- )
157
- rescue StandardError
158
- { result: [] }
159
- end
160
- end
161
- end
@@ -1,139 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require('openssl')
4
- require('base64')
5
- require('curb')
6
- require('json')
7
-
8
- # @author Hernani Rodrigues Vaz
9
- module Cns
10
- # classe para processar dados no paymium
11
- class Apifr
12
- # @return [String] API key
13
- attr_reader :aky
14
- # @return [String] API secret
15
- attr_reader :asc
16
- # @return [String] API url base
17
- attr_reader :urb
18
-
19
- # @param [String] pky API key
20
- # @param [String] psc API secret
21
- # @param [Hash] ops parametrizacao base da API
22
- # @return [Apius] API paymium base
23
- def initialize(
24
- pky: ENV['PAYMIUM_API_KEY'],
25
- psc: ENV['PAYMIUM_API_SECRET'],
26
- ops: { www: 'https://paymium.com', ver: 1 }
27
- )
28
- @aky = pky
29
- @asc = psc
30
- @urb = "#{ops[:www]}/api/v#{ops[:ver]}"
31
- end
32
-
33
- # @example
34
- # {
35
- # name: '...',
36
- # email: '...',
37
- # locale: 'en',
38
- # channel_id: '...',
39
- # meta_state: 'approved',
40
- # balance_eur: '0.0',
41
- # locked_eur: '0.0',
42
- # balance_btc: '0.0',
43
- # locked_btc: '0.0',
44
- # balance_lbtc: '0.0',
45
- # locked_lbtc: '0.0'
46
- # }
47
- # @return [Hash] saldos no paymium
48
- def account
49
- api_get('user')
50
- end
51
-
52
- # @example
53
- # [
54
- # {
55
- # uuid: '50551e61-4e74-4ae7-85fd-9c2040542818',
56
- # currency_amount: nil,
57
- # state: 'executed',
58
- # btc_fee: '0.0',
59
- # currency_fee: '0.0',
60
- # created_at: '2014-03-04T09:00Z',
61
- # updated_at: '2014-03-04T09:00Z',
62
- # currency: 'EUR',
63
- # comment: '5723',
64
- # amount: '100.0',
65
- # type: 'WireDeposit',
66
- # account_operations: [{
67
- # uuid: 'b5058a68-cf99-4438-86d3-e773eba418ec',
68
- # name: 'wire_deposit',
69
- # amount: '100.0',
70
- # currency: 'EUR',
71
- # created_at: '2014-03-04T09:00Z',
72
- # created_at_int: 1_393_923_644,
73
- # is_trading_account: false
74
- # }, {}]
75
- # }, {}
76
- # ]
77
- # @return [Hash] ledger no paymium
78
- def ledger(pag = 0, ary = [])
79
- r = api_get('user/orders', offset: pag)
80
- r.empty? ? ary : ledger(pag + r.size, ary + r)
81
- rescue StandardError
82
- ary
83
- end
84
-
85
- private
86
-
87
- # HTTP GET request for public paymium API queries.
88
- def api_get(uri, **ops)
89
- resposta(Curl.get("#{urb}/#{uri}", ops) { |r| r.headers = hdrs(url(uri, ops), nonce, {}) })
90
- end
91
-
92
- # HTTP POST request for private paymium API queries involving user credentials.
93
- def api_post(uri, **ops)
94
- resposta(Curl.post("#{urb}/#{uri}", ops) { |r| r.headers = hdrs(uri, nonce, ops) })
95
- end
96
-
97
- # @return [String] URL do pedido formatado com todos os parametros
98
- def url(uri, ops)
99
- ops.empty? ? uri : "#{uri}?#{URI.encode_www_form(ops)}"
100
- end
101
-
102
- # @return [Hash] headers necessarios para pedido HTTP
103
- def hdrs(qry, non, ops)
104
- {
105
- content_type: 'application/json',
106
- 'Api-Key': aky,
107
- 'Api-Nonce': non,
108
- 'Api-Signature': auth(qry, non, URI.encode_www_form(ops))
109
- }
110
- end
111
-
112
- # @return [String] assinarura codificada dos pedidos HTTP
113
- def auth(qry, non, par)
114
- raise(ArgumentError, 'API Key is not set') unless aky
115
- raise(ArgumentError, 'API Secret is not set') unless asc
116
-
117
- OpenSSL::HMAC.hexdigest('sha256', asc, [non, "#{urb}/#{qry}", par].join)
118
- end
119
-
120
- # @return [Integer] continually-increasing unsigned integer nonce from the current Unix Time
121
- def nonce
122
- Integer(Float(Time.now) * 1e6)
123
- end
124
-
125
- # @return [Hash] resposta do pedido HTTP
126
- def resposta(http)
127
- http.response_code == 200 ? JSON.parse(http.body, symbolize_names: true) : http.status
128
- rescue JSON::ParserError,
129
- EOFError,
130
- Errno::ECONNRESET,
131
- Errno::EINVAL,
132
- Net::HTTPBadResponse,
133
- Net::HTTPHeaderSyntaxError,
134
- Net::ProtocolError,
135
- Timeout::Error => e
136
- "Erro da API paymium #{e.inspect}"
137
- end
138
- end
139
- end
@@ -1,181 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require('faraday')
4
- require('json')
5
-
6
- module Cns
7
- # classe para acesso dados blockchain EOS
8
- class Apigm
9
- # @return [String] base URL to use as a prefix for all requests
10
- attr_reader :url
11
-
12
- # @param [String] www base URL to use as a prefix for all requests
13
- # @return [Apigm] acesso dados blockchain EOS
14
- def initialize(www: 'https://eos.greymass.com')
15
- @url = www
16
- end
17
-
18
- # @return [<Symbol>] adapter for the connection - default :net_http
19
- def adapter
20
- @adapter ||= Faraday.default_adapter
21
- end
22
-
23
- # @return [<Faraday::Connection>] connection object with an URL & adapter
24
- def conn
25
- @conn ||=
26
- Faraday.new(url: url) do |c|
27
- c.request(:url_encoded)
28
- c.adapter(adapter)
29
- end
30
- end
31
-
32
- # @example
33
- # {
34
- # account_name: '...',
35
- # head_block_num: 141_391_122,
36
- # head_block_time: '2020-09-11T16:05:51.000',
37
- # privileged: false,
38
- # last_code_update: '1970-01-01T00:00:00.000',
39
- # created: '2018-06-09T13:14:37.000',
40
- # core_liquid_balance: '1232.0228 EOS',
41
- # ram_quota: 9548,
42
- # net_weight: 10_001_142,
43
- # cpu_weight: 10_001_144,
44
- # net_limit: { used: 0, available: 1_066_648_346, max: 1_066_648_346 },
45
- # cpu_limit: { used: 338, available: 88_498, max: 88_836 },
46
- # ram_usage: 3574,
47
- # permissions: [
48
- # {
49
- # perm_name: 'active',
50
- # parent: 'owner',
51
- # required_auth: {
52
- # threshold: 1, keys: [{ key: 'EOS...', weight: 1 }], accounts: [], waits: []
53
- # }
54
- # },
55
- # {
56
- # perm_name: 'owner',
57
- # parent: '',
58
- # required_auth: {
59
- # threshold: 1, keys: [{ key: 'EOS...', weight: 1 }], accounts: [], waits: []
60
- # }
61
- # }
62
- # ],
63
- # total_resources: { owner: '...', net_weight: '1000.1142 EOS', cpu_weight: '1000.1144 EOS', ram_bytes: 8148 },
64
- # self_delegated_bandwidth: { from: '...', to: '...', net_weight: '1000.1142 EOS', cpu_weight: '1000.1144 EOS' },
65
- # refund_request: nil,
66
- # voter_info: {
67
- # owner: '...',
68
- # proxy: '...',
69
- # producers: [],
70
- # staked: 20_002_286,
71
- # last_vote_weight: '17172913021904.12109375000000000',
72
- # proxied_vote_weight: '0.00000000000000000',
73
- # is_proxy: 0,
74
- # flags1: 0,
75
- # reserved2: 0,
76
- # reserved3: '0.0000 EOS'
77
- # },
78
- # rex_info: nil
79
- # }
80
- # @param [Hash] arg argumentos trabalho
81
- # @option arg [String] :account_name endereco carteira EOS
82
- # @return [Hash] dados & saldo duma carteira EOS
83
- def account(**arg)
84
- raise(Erro, 'endereco tem de ser definido') if arg[:account_name].nil? || arg[:account_name].empty?
85
-
86
- get('/v1/chain/get_account', **arg)
87
- end
88
-
89
- # @example
90
- # {
91
- # actions: [
92
- # {
93
- # account_action_seq: 964,
94
- # action_trace: {
95
- # account_ram_deltas: [],
96
- # act: {
97
- # account: 'voicebestapp',
98
- # authorization: [
99
- # { actor: 'thetruevoice', permission: 'active' },
100
- # { actor: 'voicebestapp', permission: 'active' }
101
- # ],
102
- # data: { from: 'voicebestapp', memo: '...', quantity: '1.0001 MESSAGE', to: '...' },
103
- # hex_data: '...',
104
- # name: 'transfer'
105
- # },
106
- # action_ordinal: 10,
107
- # block_num: 141_345_345,
108
- # block_time: '2020-09-11T09:44:04.500',
109
- # closest_unnotified_ancestor_action_ordinal: 5,
110
- # context_free: false,
111
- # creator_action_ordinal: 5,
112
- # elapsed: 6,
113
- # producer_block_id: '...',
114
- # receipt: {
115
- # abi_sequence: 1,
116
- # act_digest: '...',
117
- # auth_sequence: [['thetruevoice', 6_778_215], ['voicebestapp', 435_346]],
118
- # code_sequence: 1,
119
- # global_sequence: 233_283_589_258,
120
- # receiver: '...',
121
- # recv_sequence: 927
122
- # },
123
- # receiver: '...',
124
- # trx_id: '...'
125
- # },
126
- # block_num: 141_345_345,
127
- # block_time: '2020-09-11T09:44:04.500',
128
- # global_action_seq: 233_283_589_258,
129
- # irreversible: true
130
- # },
131
- # {}
132
- # ],
133
- # head_block_num: 141_721_698,
134
- # last_irreversible_block: 141_721_371
135
- # }
136
- # @param [String] add endereco carteira EOS
137
- # @param [Hash] arg argumentos trabalho
138
- # @option arg [String] :account_name endereco carteira EOS
139
- # @option arg [Integer] :pos posicao da primeira transacao a devolver
140
- # @option arg [Integer] :offset numero maximo transacoes a devolver
141
- # @option arg [String] :filter filtro a aplicar na resposta
142
- # @option arg [String] :sort ordenacao asc/desc
143
- # @option arg [String] :after time inicio "2020-09-13T13:44:03.105Z"
144
- # @option arg [String] :before time fim "2020-09-13T13:44:03.105Z"
145
- # @option arg [Integer] :parent transacao pai
146
- # @return [Array<Hash>] devolve lista de transacoes
147
- def all_tx(add, **arg)
148
- raise(Erro, 'endereco tem de ser definido') if add.nil? || add.empty?
149
-
150
- ledger(**arg.merge(account_name: add))
151
- end
152
-
153
- # @param [Integer] pos posicao das transacoes a devolver
154
- # @param [Array<Hash>] ary lista acumuladora das transacoes a devolver
155
- # @param arg (see all_tx)
156
- # @option arg (see all_tx)
157
- # @return [Array<Hash>] lista das transacoes ligadas a uma carteira EOS
158
- def ledger(pos = 0, ary = [], **arg)
159
- r = get('/v1/history/get_actions', **arg.merge(pos: pos, offset: 100))[:actions]
160
- ary += r
161
- r.count < 100 ? ary : ledger(pos + r.count, ary, **arg)
162
- rescue StandardError
163
- ary
164
- end
165
-
166
- private
167
-
168
- # @param [String] uri identificacao do recurso a questionar
169
- # @param arg (see all_tx)
170
- # @option arg (see all_tx)
171
- # @return [Hash] resultado do HTTP request
172
- def get(uri, **arg)
173
- JSON.parse(
174
- conn.post(uri, arg.to_json, content_type: 'application/json').body,
175
- symbolize_names: true
176
- )
177
- rescue StandardError
178
- { actions: [] }
179
- end
180
- end
181
- end