etherscan 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e12314597d854f9d264641830082a829f3f99f40a49cbfa1283cac848527416
4
- data.tar.gz: 825813db2b4c6debd2cf9c96a59ebbe8f5aacf13cdb84f5cbd9ecd85f44fd814
3
+ metadata.gz: '0227579db92861939267b0cac5f56646f809f4a4d1db84800f77401e68404717'
4
+ data.tar.gz: f1451aa9dc8117abe7070f80908b6cc764bcba3407968b02aaa9af36f371961d
5
5
  SHA512:
6
- metadata.gz: 3ddbff16ed0c4d471b68bdccd621b1bb6c0f7a70209e93aa5d340f131f0f2e194cb58410b8cb2e50106f91f5a8b68ea75e104f1981a4ef49f53ec1d39a627acc
7
- data.tar.gz: 368ab98f97fff3c59808fe6433029bc41b2dd59e60fb0776cfc8d8b863c5461bbec194040371ca9b4f584fcdb249b755903bf48163dc1527c5eb74ab6dda5e0d
6
+ metadata.gz: 674a4e2c6fb06d13f1a075706658014f141ca6be9ffa74cfbcfa985ce49e0a3b476086c06a3c2759f88124d38872912df7d0776a316d0e01102650d09a0f158e
7
+ data.tar.gz: 711af9556a2a5efbf870fa90168ca39f5cecafc1c918b8c45b0830a82e65bc65be389ab361c16679027695d67de8c3ea15ddd65c93bd617af7c67a3778321b53
data/README.md CHANGED
@@ -6,7 +6,7 @@ Access to the API of etherscan.io and the compatible APIs.
6
6
  ## Installation
7
7
 
8
8
  ```bash
9
- gem 'etherscan', '~> 0.2.0'
9
+ gem 'etherscan', '~> 0.2.3'
10
10
  ```
11
11
 
12
12
  ```bash
data/lib/etherscan/api.rb CHANGED
@@ -33,7 +33,7 @@ module Etherscan
33
33
 
34
34
  def method_missing(method, *args)
35
35
  module_name, action = method.to_s.split('_')
36
- request(module_name, action, args[0])
36
+ request(module_name, action, args[0] || {})
37
37
  end
38
38
 
39
39
  #########################################
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Etherscan
4
- VERSION = "0.2.3"
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/etherscan.rb CHANGED
@@ -2,6 +2,8 @@ require 'json'
2
2
  require 'logger'
3
3
  require 'net/http'
4
4
  require 'etherscan/api'
5
+ require 'tronscan/api'
6
+ require 'subscan/api'
5
7
 
6
8
  require 'active_support'
7
9
  require 'active_support/core_ext/string'
@@ -46,15 +48,38 @@ module Etherscan
46
48
  'obnb' => 'https://api-opbnb.bscscan.com/api'
47
49
  }
48
50
 
51
+ # https://tronscan.org/#/developer/api
52
+ TRON_CHAINS = {
53
+ 'tron' => 'https://apilist.tronscanapi.com/api',
54
+ }
55
+
56
+ SUBSCAN_CHAINS = {
57
+ 'pangolin' => 'https://pangolin.api.subscan.io/api',
58
+ 'crab' => 'https://crab.api.subscan.io/api',
59
+ 'darwinia' => 'https://darwinia.api.subscan.io/api',
60
+ }
61
+
49
62
  class << self
50
63
  attr_accessor :logger
51
64
 
52
65
  def api(chain_short_name, api_key = nil)
53
66
  url = CHAINS[chain_short_name]
54
67
  url = CHAINS[chain_short_name.underscore] if url.nil?
55
- raise "Chain `#{chain_short_name}` is not supported. Only #{CHAINS.keys} are supported." if url.nil?
56
68
 
57
- Etherscan::Api.new(url, api_key)
69
+ tron_url = TRON_CHAINS[chain_short_name]
70
+ tron_url = TRON_CHAINS[chain_short_name.underscore] if url.nil?
71
+
72
+ subscan_url = SUBSCAN_CHAINS[chain_short_name]
73
+ subscan_url = SUBSCAN_CHAINS[chain_short_name.underscore] if url.nil?
74
+
75
+ raise "Chain `#{chain_short_name}` is not supported. Only " \
76
+ "Etherscan [#{CHAINS.keys}] & " \
77
+ "Subscan [#{SUBSCAN_CHAINS.keys}] & " \
78
+ "Tronscan [#{TRON_CHAINS.keys}] are supported." if url.nil? && tron_url.nil? && subscan_url.nil?
79
+
80
+ return Etherscan::Api.new(url, api_key) if url
81
+ return Tronscan::Api.new(tron_url, api_key) if tron_url
82
+ return Subscan::Api.new(subscan_url, api_key) if subscan_url
58
83
  end
59
84
 
60
85
  # for example: Etherscan.eth('your_api_key')
@@ -0,0 +1,50 @@
1
+ module Subscan
2
+ class Api
3
+ def initialize(url, api_key = nil)
4
+ @url = url
5
+ @api_key = api_key
6
+ end
7
+
8
+ def request(module_name, action, params = {})
9
+ params = params.reject { |_k, v| v.nil? } # filter out nil values
10
+
11
+ url = "#{@url}/scan/#{module_name}/#{action}"
12
+ uri = URI(url)
13
+ Etherscan.logger.debug "Req: #{uri}"
14
+
15
+ http = Net::HTTP.new(uri.host, uri.port)
16
+ http.use_ssl = (uri.scheme == 'https')
17
+
18
+ request = Net::HTTP::Post.new(uri)
19
+ request['Content-Type'] = 'application/json'
20
+ request['X-API-Key'] = @api_key unless @api_key.nil?
21
+ request.body = params.to_json
22
+ Etherscan.logger.debug "Body: #{request.body}"
23
+
24
+ response = http.request(request)
25
+
26
+ raise response.body if response.code != '200'
27
+
28
+ json_response = JSON.parse(response.body)
29
+
30
+ if json_response.key?('status')
31
+ raise json_response['message'] if json_response['status'] != '1'
32
+
33
+ json_response['result'] || json_response['message']
34
+ elsif json_response.key?('code') # normal response
35
+ raise json_response['message'] if json_response['code'] != 0
36
+
37
+ json_response['data']
38
+ end
39
+ end
40
+
41
+ def respond_to_missing?(*_args)
42
+ true
43
+ end
44
+
45
+ def method_missing(method, *args)
46
+ module_name, action = method.to_s.split('_')
47
+ request(module_name, action, args[0] || {})
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,54 @@
1
+ module Tronscan
2
+ class Api
3
+ def initialize(url, api_key = nil)
4
+ @url = url
5
+ @api_key = api_key
6
+ end
7
+
8
+ def request(module_name, action = nil, params = {})
9
+ action_path = action.nil? ? '' : "/#{action}"
10
+
11
+ params = params.reject { |_k, v| v.nil? } # filter out nil values
12
+ params_query = params.keys.map { |key| "#{key}=#{params[key]}" }.join('&').strip
13
+ params_query = '' if params_query.empty?
14
+
15
+ uri = URI "#{@url}/#{module_name}#{action_path}?#{params_query}"
16
+ Etherscan.logger.debug "Req: #{uri}"
17
+
18
+ http = Net::HTTP.new(uri.host, uri.port)
19
+ http.use_ssl = (uri.scheme == 'https')
20
+
21
+ request = Net::HTTP::Get.new(uri.request_uri)
22
+ request['TRON-PRO-API-KEY'] = @api_key unless @api_key.nil?
23
+
24
+ response = http.request(request)
25
+ # Etherscan.logger.debug "Rsp: #{resp}"
26
+ raise response.body if response.code != '200'
27
+
28
+ JSON.parse(response.body)
29
+ rescue StandardError => e
30
+ puts e.message
31
+ puts e.backtrace.inspect
32
+ end
33
+
34
+ def respond_to_missing?(*_args)
35
+ true
36
+ end
37
+
38
+ def method_missing(method, *args)
39
+ module_name, action = method.to_s.split('_')
40
+ request(module_name, action, args[0] || {})
41
+ end
42
+
43
+ # def contract_getcontractcreation(contractaddresses:)
44
+ # if contractaddresses.is_a? Array
45
+ # request('contract', 'getcontractcreation', contractaddresses: contractaddresses.join(','))
46
+ # elsif contractaddresses.is_a? String
47
+ # request('contract', 'getcontractcreation', contractaddresses: contractaddresses)
48
+ # else
49
+ # raise 'contractaddresses must be an array or a string'
50
+ # end
51
+ # end
52
+
53
+ end
54
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: etherscan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aki Wu
@@ -37,6 +37,8 @@ files:
37
37
  - lib/etherscan.rb
38
38
  - lib/etherscan/api.rb
39
39
  - lib/etherscan/version.rb
40
+ - lib/subscan/api.rb
41
+ - lib/tronscan/api.rb
40
42
  homepage: https://github.com/wuminzhe/etherscan
41
43
  licenses:
42
44
  - MIT