Class: Etht::Etherscan

Inherits:
Object
  • Object
show all
Defined in:
lib/etht/etherscan.rb

Overview

classe para tratar pedidos etherscan com chave API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key: ENV['ETHERSCAN_API_KEY'], url: 'https://api.etherscan.io/') ⇒ Etherscan

Returns API etherscan base.

Parameters:

  • :key (String)

    apikey a juntar aos pedidos HTTP url:

  • :url (String)

    base URL to use as a prefix for all requests



15
16
17
18
# File 'lib/etht/etherscan.rb', line 15

def initialize(key: ENV['ETHERSCAN_API_KEY'], url: 'https://api.etherscan.io/')
  @key = key
  @url = url
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



10
11
12
# File 'lib/etht/etherscan.rb', line 10

def key
  @key
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/etht/etherscan.rb', line 10

def url
  @url
end

Instance Method Details

#adapter<Symbol>

Returns adapter for the connection - default :net_http.

Returns:

  • (<Symbol>)

    adapter for the connection - default :net_http



21
22
23
# File 'lib/etht/etherscan.rb', line 21

def adapter
  @adapter ||= Faraday.default_adapter
end

#address_balance(add) ⇒ <String>

get ether balance for a single address

Parameters:

  • add (String)

    address for balance

Returns:

  • (<String>)

    devolve saldo

Raises:



52
53
54
55
56
# File 'lib/etht/etherscan.rb', line 52

def address_balance(add)
  raise(Etht::Erro, 'address must be defined') if add.nil?

  get(module: 'account', action: 'balance', address: add, tag: 'latest')
end

#conn<Faraday::Connection>

manage the default properties and the middleware stack for fulfilling an HTTP request

Returns:

  • (<Faraday::Connection>)

    connection object with an URL & adapter



28
29
30
31
32
33
34
# File 'lib/etht/etherscan.rb', line 28

def conn
  @conn ||=
    Faraday.new(url: url) do |c|
      c.request(:url_encoded)
      c.adapter(adapter)
    end
end

#get(**ars) ⇒ <Hash>

Returns resultado json do GET HTTP request.

Returns:

  • (<Hash>)

    resultado json do GET HTTP request

Raises:



37
38
39
40
41
42
43
44
45
46
# File 'lib/etht/etherscan.rb', line 37

def get(**ars)
  r =
    conn.get('api') do |o|
      o.headers = { content_type: 'application/json', accept: 'application/json', user_agent: 'etherscan;ruby' }
      o.params = ars.merge({ apikey: key })
    end
  raise(Etht::Erro, r) if r.status != 200

  JSON(r.body)['result']
end

#multi_address_balance(ads) ⇒ Array<Hash>

get ether balance for multiple addresses

Parameters:

  • ads (String)

    lista de addresses (max 20)

Returns:

  • (Array<Hash>)

    devolve lista com contas & saldo

Raises:



62
63
64
65
66
# File 'lib/etht/etherscan.rb', line 62

def multi_address_balance(ads)
  raise(Etht::Erro, 'up to 20 accounts in a single batch') if ads.size > 20

  get(module: 'account', action: 'balancemulti', address: ads.join(','), tag: 'latest')
end

#normal_tx(add, **ars) ⇒ Array<Hash>

get a list of normal transactions by address

Parameters:

  • add (String)

    address for transactions

  • ars (Hash)

    opcoes trabalho

Options Hash (**ars):

  • :start_block (String)

    starting blockNo to retrieve results

  • :end_block (String)

    ending blockNo to retrieve results

  • :sort (String)

    asc -> ascending order, desc -> descending order

  • :page (String)

    to get paginated results

  • :offset (String)

    max records to return

Returns:

  • (Array<Hash>)

    devolve ate max 10000 das ultimas transacoes

Raises:



89
90
91
92
93
# File 'lib/etht/etherscan.rb', line 89

def normal_tx(add, **ars)
  raise(Etht::Erro, 'address must be defined') if add.nil?

  transcations('txlist', add, nil, **ars)
end

#token_balance(add, cdd) ⇒ <String>

get ERC20-token account balance

Parameters:

  • cdd (String)

    token contract address

  • add (String)

    address for balance

Returns:

  • (<String>)

    devolve saldo

Raises:



73
74
75
76
77
# File 'lib/etht/etherscan.rb', line 73

def token_balance(add, cdd)
  raise(Etht::Erro, 'contract or address must be defined') if (cdd || add).nil?

  get(module: 'account', action: 'tokenbalance', address: add, contractaddress: cdd)
end

#token_tx(add, cdd = nil, **ars) ⇒ Array<Hash>

get a list of ERC20 - token transfer events

Parameters:

  • cdd (String) (defaults to: nil)

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

  • add (String)

    address for transactions

  • ars (Hash)

    opcoes trabalho

Options Hash (**ars):

  • :start_block (String)

    starting blockNo to retrieve results

  • :end_block (String)

    ending blockNo to retrieve results

  • :sort (String)

    asc -> ascending order, desc -> descending order

  • :page (String)

    to get paginated results

  • :offset (String)

    max records to return

Returns:

  • (Array<Hash>)

    devolve ate max 10000 das ultimas transacoes

Raises:



102
103
104
105
106
# File 'lib/etht/etherscan.rb', line 102

def token_tx(add, cdd = nil, **ars)
  raise(Etht::Erro, 'contract or address must be defined') if (cdd || add).nil?

  transcations('tokentx', add, cdd, **ars)
end