etherscan 0.2.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e554929b14bae7f3a3f2c5da47aa57775a4e1088dd8f108cfe84f2c857ae088
4
- data.tar.gz: 833280f97f3d0649c0f12be779b93d0da8ee347681d22f96942b7b040486fb9d
3
+ metadata.gz: 2ae5a3c27f579a927ac49bf966f739638880883ea3427329e68f77a6d94fa84f
4
+ data.tar.gz: 2a88c324132a2a6cad32e36b290d50410abb9d19a54293566454ed26dc0615d6
5
5
  SHA512:
6
- metadata.gz: 7cb3a7981cf01a151b2e2ea262810f81096dc9ee3a7634b806118bbc947f628ddbab97446d474a7c1093b4f43f3b940f9958a847cd42178d85317c9ec80ffff4
7
- data.tar.gz: 755c15c09278bd308ecd07c7359bed0677eea3d333b817fa2666e41788d9ce6e88c58446abeb0d4ffc81f478eafedfa1cbcf1f07f84563aba83515ffa8d5d33d
6
+ metadata.gz: a0ae0a1a1092fae3b479260a95f1725724b685a305c8aab475a573855f5226b74e88fc67b8f62b8c85662f7e615d55470735971eaff8a4cab9eb3bf51b9a427e
7
+ data.tar.gz: 599c757ea7a9812a3a9a46f045708d936858bfc7670049d75d7ed9a1453b7e54409b9b9119544349e4fd28e69ebd5ab37cd203f184842fc6b91ed5a88a89b27e
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Etherscan
4
- VERSION = '0.2.4'
4
+ VERSION = '0.4.0'
5
5
  end
data/lib/etherscan.rb CHANGED
@@ -3,11 +3,46 @@ require 'logger'
3
3
  require 'net/http'
4
4
  require 'etherscan/api'
5
5
  require 'tronscan/api'
6
+ require 'subscan/api'
6
7
 
7
8
  require 'active_support'
8
9
  require 'active_support/core_ext/string'
9
10
 
10
11
  module Etherscan
12
+ CHAIN_SHORT_NAME_BY_ID = {
13
+ 1 => 'eth',
14
+ 5 => 'gor',
15
+ 11_155_111 => 'sep',
16
+ 42_161 => 'arb1',
17
+ 421_613 => 'arb_goerli',
18
+ 421_614 => 'arb_sep',
19
+ 56 => 'bnb',
20
+ 97 => 'bnbt',
21
+ 137 => 'matic',
22
+ 80_001 => 'maticmum',
23
+ 1101 => 'zkevm',
24
+ 14_422 => 'testnet_zk_evm_mango',
25
+ 250 => 'ftm',
26
+ 4002 => 'tftm',
27
+ 10 => 'oeth',
28
+ 1284 => 'mbeam',
29
+ 1285 => 'mriver',
30
+ 1287 => 'mbase',
31
+ 25 => 'cronos',
32
+ 199 => 'btt',
33
+ 1028 => 'tbtt',
34
+ 100 => 'gnosis',
35
+ 42_170 => 'arb_nova',
36
+ 59_144 => 'linea',
37
+ 59_140 => 'linea_testnet',
38
+ 8453 => 'base',
39
+ 84_531 => 'basegor',
40
+ 1111 => 'wemix',
41
+ 534_352 => 'scr',
42
+ 534_351 => 'scr_sepolia',
43
+ 204 => 'obnb'
44
+ }
45
+
11
46
  # https://chainid.network/chains.json
12
47
  # key is the underscore(short name) of the chain in chains.json
13
48
  # https://blockscan.com/
@@ -49,23 +84,46 @@ module Etherscan
49
84
 
50
85
  # https://tronscan.org/#/developer/api
51
86
  TRON_CHAINS = {
52
- 'tron' => 'https://apilist.tronscanapi.com/api',
87
+ 'tron' => 'https://apilist.tronscanapi.com/api'
88
+ }
89
+
90
+ SUBSCAN_CHAINS = {
91
+ 'pangolin' => 'https://pangolin.api.subscan.io/api',
92
+ 'crab' => 'https://crab.api.subscan.io/api',
93
+ 'darwinia' => 'https://darwinia.api.subscan.io/api'
53
94
  }
54
95
 
55
96
  class << self
56
97
  attr_accessor :logger
57
98
 
58
- def api(chain_short_name, api_key = nil)
99
+ def api(chain_id_or_short_name, api_key = nil)
100
+ if chain_id_or_short_name.is_a?(Integer)
101
+ chain_short_name = CHAIN_SHORT_NAME_BY_ID[chain_id_or_short_name]
102
+ raise "Chain id `#{chain_id_or_short_name}` is not supported." if chain_short_name.nil?
103
+ else
104
+ chain_short_name = chain_id_or_short_name
105
+ end
106
+
59
107
  url = CHAINS[chain_short_name]
60
108
  url = CHAINS[chain_short_name.underscore] if url.nil?
61
109
 
62
110
  tron_url = TRON_CHAINS[chain_short_name]
63
111
  tron_url = TRON_CHAINS[chain_short_name.underscore] if url.nil?
64
112
 
65
- raise "Chain `#{chain_short_name}` is not supported. Only Etherscan [#{CHAINS.keys}] & Tronscan [#{TRON_CHAINS.keys}] are supported." if url.nil? && tron_url.nil?
113
+ subscan_url = SUBSCAN_CHAINS[chain_short_name]
114
+ subscan_url = SUBSCAN_CHAINS[chain_short_name.underscore] if url.nil?
115
+
116
+ if url.nil? && tron_url.nil? && subscan_url.nil?
117
+ raise "Chain `#{chain_short_name}` is not supported. Only " \
118
+ "ETHERSCAN #{CHAINS.keys} & " \
119
+ "SUBSCAN #{SUBSCAN_CHAINS.keys} & " \
120
+ "TRONSCAN #{TRON_CHAINS.keys} are supported."
121
+ end
66
122
 
67
123
  return Etherscan::Api.new(url, api_key) if url
68
124
  return Tronscan::Api.new(tron_url, api_key) if tron_url
125
+
126
+ Subscan::Api.new(subscan_url, api_key) if subscan_url
69
127
  end
70
128
 
71
129
  # 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
data/lib/tronscan/api.rb CHANGED
@@ -21,13 +21,11 @@ module Tronscan
21
21
  request = Net::HTTP::Get.new(uri.request_uri)
22
22
  request['TRON-PRO-API-KEY'] = @api_key unless @api_key.nil?
23
23
 
24
- resp = http.request(request).body
24
+ response = http.request(request)
25
25
  # Etherscan.logger.debug "Rsp: #{resp}"
26
- resp = JSON.parse(resp)
26
+ raise response.body if response.code != '200'
27
27
 
28
- # raise resp['result'] if resp['status'] == '0'
29
-
30
- resp
28
+ JSON.parse(response.body)
31
29
  rescue StandardError => e
32
30
  puts e.message
33
31
  puts e.backtrace.inspect
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: etherscan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aki Wu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-04 00:00:00.000000000 Z
11
+ date: 2024-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -37,6 +37,7 @@ files:
37
37
  - lib/etherscan.rb
38
38
  - lib/etherscan/api.rb
39
39
  - lib/etherscan/version.rb
40
+ - lib/subscan/api.rb
40
41
  - lib/tronscan/api.rb
41
42
  homepage: https://github.com/wuminzhe/etherscan
42
43
  licenses: