Class: Etht::Etherscan
- Inherits:
-
Object
- Object
- Etht::Etherscan
- Defined in:
- lib/etht/etherscan.rb
Overview
classe para tratar pedidos etherscan com chave API
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#adapter ⇒ <Symbol>
Adapter for the connection - default :net_http.
-
#address_balance(add) ⇒ <String>
get ether balance for a single address.
-
#conn ⇒ <Faraday::Connection>
manage the default properties and the middleware stack for fulfilling an HTTP request.
-
#get(**ars) ⇒ <Hash>
Resultado json do GET HTTP request.
-
#initialize(key: ENV['ETHERSCAN_API_KEY'], url: 'https://api.etherscan.io/') ⇒ Etherscan
constructor
API etherscan base.
-
#multi_address_balance(ads) ⇒ Array<Hash>
get ether balance for multiple addresses.
-
#normal_tx(add, **ars) ⇒ Array<Hash>
get a list of normal transactions by address.
-
#token_balance(add, cdd) ⇒ <String>
get ERC20-token account balance.
-
#token_tx(add, cdd = nil, **ars) ⇒ Array<Hash>
get a list of ERC20 - token transfer events.
Constructor Details
#initialize(key: ENV['ETHERSCAN_API_KEY'], url: 'https://api.etherscan.io/') ⇒ Etherscan
Returns API etherscan base.
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
#key ⇒ Object (readonly)
Returns the value of attribute key.
10 11 12 |
# File 'lib/etht/etherscan.rb', line 10 def key @key end |
#url ⇒ Object (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.
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
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
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.
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
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
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
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
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 |