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
GMPS =

Greymass maximum actions per request

100

Instance Method Summary collapse

Constructor Details

#initializeApibc

Returns a new instance of Apibc.



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

def initialize
  @escn = bccn('https://api.etherscan.io')
  @gmcn = bccn('https://eos.greymass.com')
  @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



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

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

#account_gm(add) ⇒ Hash

Get EOS account information

Parameters:

  • add (String)

    EOS account name

Returns:

  • (Hash)

    Account details with resources



79
80
81
82
# File 'lib/cns/apibc.rb', line 79

def (add)
  res = gm_req('/v1/chain/get_account', account_name: add)
  res[:core_liquid_balance]&.to_d&.positive? ? res : gm_erro
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



56
57
58
# File 'lib/cns/apibc.rb', line 56

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



48
49
50
51
# File 'lib/cns/apibc.rb', line 48

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

#ledger_gm(add) ⇒ Array<Hash>

Get complete transaction history for EOS account

Parameters:

  • add (String)

    EOS account name

Returns:

  • (Array<Hash>)

    lista completa transacoes greymass



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cns/apibc.rb', line 87

def ledger_gm(add)
  trx = []
  pos = 0
  loop do
    res = gm_req('/v1/history/get_actions', account_name: add, pos: pos, offset: GMPS)
    bth = Array(res[:actions])
    trx.concat(bth)
    break if bth.size < GMPS

    pos += GMPS
  end
  trx
rescue StandardError
  trx
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



40
41
42
43
# File 'lib/cns/apibc.rb', line 40

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



71
72
73
74
# File 'lib/cns/apibc.rb', line 71

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



63
64
65
66
# File 'lib/cns/apibc.rb', line 63

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