Class: Bct::Apies

Inherits:
Object
  • Object
show all
Defined in:
lib/bct/apies.rb

Overview

classe para acesso dados blockchain ETH

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chv: ENV['ETHERSCAN_API_KEY'], www: 'https://api.etherscan.io/') ⇒ Apies

Returns API etherscan base.

Parameters:

  • chv (String) (defaults to: ENV['ETHERSCAN_API_KEY'])

    apikey a juntar aos pedidos HTTP url:

  • www (String) (defaults to: 'https://api.etherscan.io/')

    base URL to use as a prefix for all requests



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

#keyString (readonly)

Returns apikey a juntar aos pedidos HTTP url:.

Returns:

  • (String)

    apikey a juntar aos pedidos HTTP url:



11
12
13
# File 'lib/bct/apies.rb', line 11

def key
  @key
end

#urlString (readonly)

Returns base URL to use as a prefix for all requests.

Returns:

  • (String)

    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.

Examples:

[
  { account: '0x...', balance: '4000000000000000000' },
  { account: '0x...', balance: '87000000000000000000' }
]

Parameters:

  • ads (String)

    lista enderecos carteiras ETH (max 20)

Returns:

  • (Array<Hash>)

    devolve lista com dados & saldo de carteiras ETH

Raises:



44
45
46
47
48
# File 'lib/bct/apies.rb', line 44

def (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.

Returns:

  • (<Symbol>)

    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.

Returns:

  • (<Faraday::Connection>)

    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.

Parameters:

  • pag (Integer) (defaults to: 0)

    pagina das transacoes a devolver

  • ary (Array<Hash>) (defaults to: [])

    lista acumuladora das transacoes a devolver

  • arg (Hash)

    argumentos trabalho

Options Hash (**arg):

  • :start_block (Integer)

    starting blockNo to retrieve results

  • :end_block (Integer)

    ending blockNo to retrieve results

  • :sort (String)

    asc -> ascending order, desc -> descending order

  • :page (Integer)

    to get paginated results

  • :offset (Integer)

    max records to return

Returns:

  • (Array<Hash>)

    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.

Examples:

[
  {
    blockNumber: '4984535',
    timeStamp: '1517094794',
    hash: '0x...',
    nonce: '10',
    blockHash: '0x...',
    transactionIndex: '17',
    from: '0x...',
    to: '0x...',
    value: '52627271000000000000',
    gas: '21000',
    gasPrice: '19000000000',
    isError: '0',
    txreceipt_status: '1',
    input: '0x',
    contractAddress: '',
    gasUsed: '21000',
    cumulativeGasUsed: '566293',
    confirmations: '5848660'
  },
  {}
]

Parameters:

  • add (String)

    endereco carteira ETH

  • arg (Hash)

    argumentos trabalho

Options Hash (**arg):

  • :start_block (Integer)

    starting blockNo to retrieve results

  • :end_block (Integer)

    ending blockNo to retrieve results

  • :sort (String)

    asc -> ascending order, desc -> descending order

  • :page (Integer)

    to get paginated results

  • :offset (Integer)

    max records to return

Returns:

  • (Array<Hash>)

    lista de transacoes

Raises:



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.

Examples:

registo duplicado

[
  {
    blockNumber: '3967652',
    timeStamp: '1499081515',
    hash: '0x registo duplicado com todos os dados iguais',
    nonce: '3',
    blockHash: '0x00a49e999036dc13dc6c4244bb1d51d3146fe7f00bfb500a7624d82e299c7328',
    from: '0xd0a6e6c54dbc68db5db3a091b171a77407ff7ccf',
    contractAddress: '0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0',
    to: '0x...',
    value: '0',
    tokenName: 'EOS',
    tokenSymbol: 'EOS',
    tokenDecimal: '18',
    transactionIndex: '83',
    gas: '173399',
    gasPrice: '21000000000',
    gasUsed: '173398',
    input: 'deprecated',
    cumulativeGasUsed: '7484878',
    confirmations: '3442641'
  },
  {}
]

Parameters:

  • cdd (String) (defaults to: nil)

    token address (nil to get a list of all ERC20 transactions)

  • add (String)

    endereco carteira ETH

  • arg (Hash)

    argumentos trabalho

Options Hash (**arg):

  • :start_block (Integer)

    starting blockNo to retrieve results

  • :end_block (Integer)

    ending blockNo to retrieve results

  • :sort (String)

    asc -> ascending order, desc -> descending order

  • :page (Integer)

    to get paginated results

  • :offset (Integer)

    max records to return

Returns:

  • (Array<Hash>)

    lista de token transfer events

Raises:



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