class Etht::Etherscan

classe para tratar pedidos etherscan com chave API

Attributes

key[R]
url[R]

Public Class Methods

new(key: ENV['ETHERSCAN_API_KEY'], url: 'https://api.etherscan.io/') click to toggle source

@param [String] :key apikey a juntar aos pedidos HTTP url: @param [String] :url base URL to use as a prefix for all requests @return [Etherscan] API etherscan base

# File lib/etht/etherscan.rb, line 15
def initialize(key: ENV['ETHERSCAN_API_KEY'], url: 'https://api.etherscan.io/')
  @key = key
  @url = url
end

Public Instance Methods

adapter() click to toggle source

@return [<Symbol>] adapter for the connection - default :net_http

# File lib/etht/etherscan.rb, line 21
def adapter
  @adapter ||= Faraday.default_adapter
end
address_balance(add) click to toggle source

get ether balance for a single address

@param [String] add address for balance @return [<String>] devolve saldo

# 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() click to toggle source

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

@return [<Faraday::Connection>] connection object with an URL & adapter

# 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) click to toggle source

@return [<Hash>] resultado json do GET HTTP request

# 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) click to toggle source

get ether balance for multiple addresses

@param [String] ads lista de addresses (max 20) @return [Array<Hash>] devolve lista com contas & saldo

# 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) click to toggle source

get a list of normal transactions by address

@param [String] add address for transactions @param [Hash] ars opcoes trabalho @option ars [String] :start_block starting blockNo to retrieve results @option ars [String] :end_block ending blockNo to retrieve results @option ars [String] :sort asc -> ascending order, desc -> descending order @option ars [String] :page to get paginated results @option ars [String] :offset max records to return @return [Array<Hash>] devolve ate max 10000 das ultimas transacoes

# 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) click to toggle source

get ERC20-token account balance

@param add (see address_balance) @param [String] cdd token contract address @return (see address_balance)

# 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) click to toggle source

get a list of ERC20 - token transfer events

@param add (see normal_tx) @param [String] cdd token address (nil to get a list of all ERC20 transactions) @param ars (see normal_tx) @option ars (see normal_tx) @return (see normal_tx)

# 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

Private Instance Methods

transcations(act, add, cdd, **ars) click to toggle source

get a list of transactions

@param [String] act accao a executar @param add (see normal_tx) @param cdd (see token_tx) @param ars (see normal_tx) @option ars (see normal_tx) @return (see normal_tx)

# File lib/etht/etherscan.rb, line 118
def transcations(act, add, cdd, **ars)
  get(**{ module: 'account', action: act, address: add }.merge({
    contractaddress: cdd,
    startblock: ars[:start_block],
    endblock: ars[:end_block],
    page: ars[:page],
    offset: ars[:offset],
    sort: ars[:sort]
  }.reject { |_, v| v.nil? }))
end