Class: Cns::Apibc

Inherits:
Object
  • Object
show all
Defined in:
lib/cns/apibc.rb

Overview

classe para acesso dados blockchains

Constant Summary collapse

ESPS =

Etherscan maximum records per page

10_000

Instance Method Summary collapse

Constructor Details

#initializeApibc

Returns a new instance of Apibc.



13
14
15
16
17
# File 'lib/cns/apibc.rb', line 13

def initialize
  @escn = bccn('https://api.etherscan.io')
  @esky = ENV.fetch('ETHERSCAN_API_KEY', nil)
  @blks = {} # Cache to store block numbers so we don't ask API repeatedly
end

Instance Method Details

#account_es(ads) ⇒ Array<Hash>

Get account balances for multiple ETH addresses

Parameters:

  • ads (Array<String>)

    List of ETH addresses (max 20)

Returns:

  • (Array<Hash>)

    List of addresses with balances



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cns/apibc.rb', line 22

def (ads)
  return [] if ads.nil? || ads.empty?

  # Batch addresses into groups of 20 (Etherscan limit) and fetch balances
  ads.each_slice(20).flat_map do |b|
    res = es_req('balancemulti', b.join(','), 1, tag: 'latest')
    res[:status] == '1' ? Array(res[:result]) : []
  end
rescue StandardError
  []
end

#block_es(add) ⇒ Array<Hash>

Get mined blocks for ETH address

Parameters:

  • add (String)

    endereco ETH

  • days (Integer)

    (Optional) Fetch only last N days

Returns:

  • (Array<Hash>)

    lista blocos etherscan



54
55
56
# File 'lib/cns/apibc.rb', line 54

def block_es(add)
  pag_es_req('getminedblocks', add, blocktype: 'blocks')
end

#inter_es(add, days: nil) ⇒ Array<Hash>

Get internal transactions for ETH address

Parameters:

  • add (String)

    endereco ETH

  • days (Integer) (defaults to: nil)

    (Optional) Fetch only last N days

Returns:

  • (Array<Hash>)

    lista transacoes internas etherscan



46
47
48
49
# File 'lib/cns/apibc.rb', line 46

def inter_es(add, days: nil)
  prm = days ? {startblock: start_block(days)} : {}
  pag_es_req('txlistinternal', add, prm)
end

#norml_es(add, days: nil) ⇒ Array<Hash>

Get normal transactions for ETH address

Parameters:

  • add (String)

    endereco ETH

  • days (Integer) (defaults to: nil)

    (Optional) Fetch only last N days

Returns:

  • (Array<Hash>)

    lista transacoes normais etherscan



38
39
40
41
# File 'lib/cns/apibc.rb', line 38

def norml_es(add, days: nil)
  prm = days ? {startblock: start_block(days)} : {}
  pag_es_req('txlist', add, prm)
end

#token_es(add, days: nil) ⇒ Array<Hash>

Get token transfers for ETH address

Parameters:

  • add (String)

    endereco ETH

  • days (Integer) (defaults to: nil)

    (Optional) Fetch only last N days

Returns:

  • (Array<Hash>)

    lista token transfer events etherscan



69
70
71
72
# File 'lib/cns/apibc.rb', line 69

def token_es(add, days: nil)
  prm = days ? {startblock: start_block(days)} : {}
  pag_es_req('tokentx', add, prm)
end

#withw_es(add, days: nil) ⇒ Array<Hash>

Get withdrawals for ETH address

Parameters:

  • add (String)

    endereco ETH

  • days (Integer) (defaults to: nil)

    (Optional) Fetch only last N days

Returns:

  • (Array<Hash>)

    lista blocos etherscan



61
62
63
64
# File 'lib/cns/apibc.rb', line 61

def withw_es(add, days: nil)
  prm = days ? {startblock: start_block(days)} : {}
  pag_es_req('txsBeaconWithdrawal', add, prm)
end