decommas 0.0.1 → 0.0.2

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: a4e249274a9bc7cdb02982d278d31cd66e5ba6dc3a3f54210cd8b9291346a4a4
4
- data.tar.gz: ae79e5304e2b264021b45116fa989ad61c74527cb207674d94fae09fc5ae3208
3
+ metadata.gz: e137cc3a096be5f4f9dca46a98ea40e81f8d364b4ed1d465f6cb825ce203366a
4
+ data.tar.gz: 88c9b1c66a2e16c406e99b71de9c05756dad49e92bad7b619f619c3be4fd944f
5
5
  SHA512:
6
- metadata.gz: 00fa84a733dcf4c376d4c9e20c345c3921f6f475cff0b8b964923cf1ee702a69eebd5a2683fc40c27f9b181da7897bf4c4361202a3803505e8859beff613196f
7
- data.tar.gz: 66e36ff7e57071750b3b53e59052feea51730207800135ecd42bba6e7daf8632ea49b1e38d5bbd5e8494e9224cec3884111a810d5917c21fbc0c9d328054612a
6
+ metadata.gz: fa98297fbf0b9d7806330dea315eddce22226c6ea8dab54f104078fca2b77cbf870354bfc3fc5ab8b11cea85a1862b7ccf26f9b99dc446229446d06070853672
7
+ data.tar.gz: 8daf091f16336ee6f81905490c3fcc05fd3c6cbdcb075b3f6a7ef94634449ed260febfaafffe326afafd551e4d7abe32503e2deac7ac0c964acbb97f86be8dca
@@ -0,0 +1,110 @@
1
+ module Decommas
2
+ class Api
3
+ include HTTParty
4
+ base_uri 'https://datalayer.decommas.net/datalayer/api/v1/'
5
+
6
+ class << self
7
+ PAGINATION_LIMIT = 100
8
+
9
+ def coins(address:, options: {})
10
+ wget("/coins/#{address}", query: options)
11
+ end
12
+
13
+ def tokens(address:, options: {})
14
+ wget("/tokens/#{address}", query: options)
15
+ end
16
+
17
+ def nfts(address:, options: {})
18
+ wget("/nfts/#{address}", query: options)
19
+ end
20
+
21
+ def transactions(address:, limit: nil, offset: nil)
22
+ query_params = {
23
+ "limit" => limit || PAGINATION_LIMIT,
24
+ "offset" => (offset if offset)
25
+ }.compact
26
+ wget("/transactions/#{address}", query: query_params)
27
+ end
28
+
29
+ def transfers_erc20(address:, limit: nil, offset: nil)
30
+ query_params = {
31
+ "limit" => limit || PAGINATION_LIMIT,
32
+ "offset" => (offset if offset)
33
+ }.compact
34
+ wget("/transfers_erc20/#{address}", query: query_params)
35
+ end
36
+
37
+ def transfers_nft(address:, options: {})
38
+ wget("/transfers_nft/#{address}", query: options)
39
+ end
40
+
41
+ def protocols(address:, options: {})
42
+ wget("/protocols/#{address}", query: options)
43
+ end
44
+
45
+ def transaction_erc20_transfers(chain_name:, tx:)
46
+ wget("/transaction_erc20_transfers/#{chain_name}/#{tx}")
47
+ end
48
+
49
+ def all_tokens_metadata
50
+ wget("/all_tokens_metadata")
51
+ end
52
+
53
+ def all_coins_metadata
54
+ wget("/all_coins_metadata")
55
+ end
56
+
57
+ def token_metadata(chain_name:, contract_address:)
58
+ wget("/token_metadata/#{chain_name}/#{contract_address}")
59
+ end
60
+
61
+ private
62
+
63
+ def with_retry(max_retries: 5, base_delay: 0.7, &block)
64
+ def attempt(retries_left, delay, &block)
65
+ block.call
66
+ rescue => e
67
+ if retries_left > 0
68
+ puts "#{e} has been raised, retries_left: #{retries_left}"
69
+
70
+ sleep(delay)
71
+ attempt(retries_left - 1, delay * 2, &block)
72
+ else
73
+ raise e
74
+ end
75
+ end
76
+
77
+ attempt(max_retries, base_delay, &block)
78
+ end
79
+
80
+ def wget(url, options = {})
81
+ raise Decommas::TokenNotProvidedError.new unless token
82
+
83
+ if options[:query]
84
+ options[:query].merge!("api-key" => token)
85
+ else
86
+ options[:query] = { "api-key" => token }
87
+ end
88
+
89
+ if safe_mode?
90
+ with_retry do
91
+ response = Response.new(get(url, **options))
92
+ raise ThrottledResponseError.new(response) if response.throttled?
93
+ response
94
+ end
95
+ else
96
+ Response.new(get(url, **options))
97
+ end
98
+ end
99
+
100
+ def token
101
+ @token ||= Decommas.configuration.token
102
+ end
103
+
104
+ def safe_mode?
105
+ @safe_mode ||= Decommas.configuration.safe_mode
106
+ end
107
+ end
108
+ end
109
+ end
110
+
@@ -0,0 +1,10 @@
1
+ module Decommas
2
+ class Configuration
3
+ attr_accessor :token
4
+ attr_accessor :safe_mode
5
+
6
+ def initialize
7
+ @safe_mode ||= false
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ module Decommas
2
+ class TokenNotProvidedError < StandardError
3
+ def initialize
4
+ super("Please provide token in configuration!")
5
+ end
6
+ end
7
+
8
+ class ResponseError < StandardError
9
+ def initialize(response)
10
+ super("DecommasApi returned: #{response.code} #{response.message}")
11
+ end
12
+ end
13
+
14
+ class ThrottledResponseError < StandardError
15
+ def initialize(response)
16
+ super("DecommasApi throttled you with: #{response.code} #{response.message}")
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ module Decommas
2
+ class Response
3
+ VALID_RESPONSES = [200]
4
+ THROTTLED_RESPONSES = [429]
5
+
6
+ attr_reader :code
7
+ attr_reader :data
8
+ attr_reader :message
9
+
10
+ def initialize(response)
11
+ # Workaround for 429 response that returns text/html content-type for some reason
12
+ response_data = if response.parsed_response.kind_of?(String)
13
+ JSON.parse(response.parsed_response)
14
+ else
15
+ response.parsed_response
16
+ end
17
+ @code = response.code
18
+ @data = response_data["result"]
19
+ @message = response_data["message"]
20
+ end
21
+
22
+ def success?
23
+ @code.in?(VALID_RESPONSES)
24
+ end
25
+
26
+ def throttled?
27
+ @code.in?(THROTTLED_RESPONSES)
28
+ end
29
+ end
30
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decommas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Takarlikov
@@ -31,6 +31,10 @@ extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
33
  - lib/decommas.rb
34
+ - lib/decommas/api.rb
35
+ - lib/decommas/configuration.rb
36
+ - lib/decommas/errors.rb
37
+ - lib/decommas/response.rb
34
38
  homepage: https://rubygems.org/gems/decommas
35
39
  licenses:
36
40
  - MIT