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
# 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)
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



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

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



73
74
75
76
# File 'lib/cns/apibc.rb', line 73

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

Returns:

  • (Array<Hash>)

    lista blocos etherscan



52
53
54
# File 'lib/cns/apibc.rb', line 52

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

#inter_es(add) ⇒ Array<Hash>

Get internal transactions for ETH address

Parameters:

  • add (String)

    endereco ETH

Returns:

  • (Array<Hash>)

    lista transacoes internas etherscan



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

def inter_es(add)
  pag_es_req('txlistinternal', add)
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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cns/apibc.rb', line 81

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) ⇒ Array<Hash>

Get normal transactions for ETH address

Parameters:

  • add (String)

    endereco ETH

Returns:

  • (Array<Hash>)

    lista transacoes normais etherscan



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

def norml_es(add)
  pag_es_req('txlist', add)
end

#token_es(add) ⇒ Array<Hash>

Get token transfers for ETH address

Parameters:

  • add (String)

    endereco ETH

Returns:

  • (Array<Hash>)

    lista token transfer events etherscan



66
67
68
# File 'lib/cns/apibc.rb', line 66

def token_es(add)
  pag_es_req('tokentx', add)
end

#withw_es(add) ⇒ Array<Hash>

Get withdrawals for ETH address

Parameters:

  • add (String)

    endereco ETH

Returns:

  • (Array<Hash>)

    lista blocos etherscan



59
60
61
# File 'lib/cns/apibc.rb', line 59

def withw_es(add)
  pag_es_req('txsBeaconWithdrawal', add)
end