Class: Bct::Apies
- Inherits:
-
Object
- Object
- Bct::Apies
- Defined in:
- lib/bct/apies.rb
Overview
classe para acesso dados blockchain ETH
Instance Attribute Summary collapse
-
#key ⇒ String
readonly
Apikey a juntar aos pedidos HTTP url:.
-
#url ⇒ String
readonly
Base URL to use as a prefix for all requests.
Instance Method Summary collapse
-
#account(ads) ⇒ Array<Hash>
Devolve lista com dados & saldo de carteiras ETH.
-
#adapter ⇒ <Symbol>
Adapter for the connection - default :net_http.
-
#conn ⇒ <Faraday::Connection>
Connection object with an URL & adapter.
-
#initialize(chv: ENV['ETHERSCAN_API_KEY'], www: 'https://api.etherscan.io/') ⇒ Apies
constructor
API etherscan base.
-
#ledger(pag = 0, ary = [], **arg) ⇒ Array<Hash>
Devolve lista de transacoes/token transfer events.
-
#norml_tx(add, **arg) ⇒ Array<Hash>
Lista de transacoes.
-
#token_tx(add, cdd = nil, **arg) ⇒ Array<Hash>
Lista de token transfer events.
Constructor Details
#initialize(chv: ENV['ETHERSCAN_API_KEY'], www: 'https://api.etherscan.io/') ⇒ Apies
Returns API etherscan base.
18 19 20 21 |
# File 'lib/bct/apies.rb', line 18 def initialize(chv: ENV['ETHERSCAN_API_KEY'], www: 'https://api.etherscan.io/') @key = chv @url = www end |
Instance Attribute Details
#key ⇒ String (readonly)
Returns apikey a juntar aos pedidos HTTP url:.
11 12 13 |
# File 'lib/bct/apies.rb', line 11 def key @key end |
#url ⇒ String (readonly)
Returns base URL to use as a prefix for all requests.
13 14 15 |
# File 'lib/bct/apies.rb', line 13 def url @url end |
Instance Method Details
#account(ads) ⇒ Array<Hash>
Returns devolve lista com dados & saldo de carteiras ETH.
44 45 46 47 48 |
# File 'lib/bct/apies.rb', line 44 def account(ads) raise(Erro, 'maximo de 20 enderecos') if ads.size > 20 get(action: 'balancemulti', address: ads.join(','), tag: 'latest')[:result] end |
#adapter ⇒ <Symbol>
Returns adapter for the connection - default :net_http.
24 25 26 |
# File 'lib/bct/apies.rb', line 24 def adapter @adapter ||= Faraday.default_adapter end |
#conn ⇒ <Faraday::Connection>
Returns connection object with an URL & adapter.
29 30 31 32 33 34 35 |
# File 'lib/bct/apies.rb', line 29 def conn @conn ||= Faraday.new(url: url) do |c| c.request(:url_encoded) c.adapter(adapter) end end |
#ledger(pag = 0, ary = [], **arg) ⇒ Array<Hash>
Returns devolve lista de transacoes/token transfer events.
132 133 134 135 136 137 138 |
# File 'lib/bct/apies.rb', line 132 def ledger(pag = 0, ary = [], **arg) r = get(**arg.merge(page: pag + 1, offset: 10_000))[:result] ary += r r.count < 10_000 ? ary : ledger(pag + 1, ary, **arg) rescue StandardError ary end |
#norml_tx(add, **arg) ⇒ Array<Hash>
Returns lista de transacoes.
82 83 84 85 86 |
# File 'lib/bct/apies.rb', line 82 def norml_tx(add, **arg) raise(Erro, 'endereco tem de ser definido') if add.nil? || add.empty? ledger(**arg.merge(action: 'txlist', address: add)) end |
#token_tx(add, cdd = nil, **arg) ⇒ Array<Hash>
Returns lista de token transfer events.
118 119 120 121 122 123 124 125 |
# File 'lib/bct/apies.rb', line 118 def token_tx(add, cdd = nil, **arg) raise(Erro, 'contrato ou endereco tem de estar definido') if (cdd || add).nil? || (cdd || add).empty? # registos duplicados aparecem em token events (ver exemplo acima) # -quando ha erros na blockchain (acho) # -quando ha token events identicos no mesmo block (acho) ledger(**arg.merge(action: 'tokentx', address: add, contractaddress: cdd)) end |