erc20 0.0.13 → 0.0.14

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: 85e0628229d427776681b41f5937a4ac7259333c946866c6662ce6a6b9ac32d9
4
- data.tar.gz: 217938bd8b36af8844f3f3fc7f4f4f44d06af596cef01e3de46f017349f633ff
3
+ metadata.gz: ac1bd8d3ffe6bf19ceb44174514dca9abb57746110c864821de775b2f11f955a
4
+ data.tar.gz: 7cc7b1d4fe20626629ea99ca5bb6cbb02686c872e36106c4c05fab846ad9b24b
5
5
  SHA512:
6
- metadata.gz: 7f9e60bab73c14a1ff6cacc2ef49b1ee7c615e0e36747b53540ef0988a7adbbb2482ce14eec32a393de56e853c4ae781b59a29df13fc4687fb4558e33f192d57
7
- data.tar.gz: d472ce73882cf23a0a24f355961261cf03204c592eee0f8a439f2cb4e32ca76933e78cd563a4b2952c5050b1fe7eeef2ed2b9bceee7087ffda683a032abdeeaf
6
+ metadata.gz: 7212c53bce48a094d0eaa8359bed81cad82e81f883caa47da2da4851cff7c6d3956036a3919265925f540b125cede427f5a9c506a60e3b002469bed22096695a
7
+ data.tar.gz: 7a80add54d6ecf810bff74a265cf4dca8ae8b99ae8c8f9e046cfec537bc9256d7a6127159d52d0b04001a7c3b17f9895c4b64814e7d9b058789e2c267b648a21
data/lib/erc20/erc20.rb CHANGED
@@ -42,5 +42,5 @@
42
42
  # License:: MIT
43
43
  module ERC20
44
44
  # Current version of the gem (changed by the +.rultor.yml+ on every release)
45
- VERSION = '0.0.13'
45
+ VERSION = '0.0.14'
46
46
  end
data/lib/erc20/wallet.rb CHANGED
@@ -131,8 +131,7 @@ class ERC20::Wallet
131
131
  raise 'Address must be a String' unless hex.is_a?(String)
132
132
  raise 'Invalid format of the address' unless /^0x[0-9a-fA-F]{40}$/.match?(hex)
133
133
  func = '70a08231' # balanceOf
134
- padded = "000000000000000000000000#{hex[2..].downcase}"
135
- data = "0x#{func}#{padded}"
134
+ data = "0x#{func}000000000000000000000000#{hex[2..].downcase}"
136
135
  r = jsonrpc.eth_call({ to: @contract, data: data }, 'latest')
137
136
  b = r[2..].to_i(16)
138
137
  @log.debug("Balance of #{hex} is #{b}")
@@ -214,24 +213,27 @@ class ERC20::Wallet
214
213
  # @param [Array] active List of addresses that we are actually listening to
215
214
  # @param [Boolean] raw TRUE if you need to get JSON events as they arrive from Websockets
216
215
  # @param [Integer] delay How many seconds to wait between +eth_subscribe+ calls
217
- def accept(addresses, active = [], raw: false, delay: 1)
216
+ # @param [Integer] subscription_id Unique ID of the subscription
217
+ def accept(addresses, active = [], raw: false, delay: 1, subscription_id: rand(99_999))
218
218
  raise 'Addresses can\'t be nil' unless addresses
219
219
  raise 'Addresses must respond to .to_a()' unless addresses.respond_to?(:to_a)
220
220
  raise 'Active can\'t be nil' unless active
221
- raise 'Active must respond to .append()' unless addresses.respond_to?(:append)
221
+ raise 'Active must respond to .append()' unless active.respond_to?(:append)
222
222
  raise 'Amount must be an Integer' unless delay.is_a?(Integer)
223
223
  raise 'Amount must be a positive Integer' unless delay.positive?
224
+ raise 'Subscription ID must be an Integer' unless subscription_id.is_a?(Integer)
225
+ raise 'Subscription ID must be a positive Integer' unless subscription_id.positive?
224
226
  EventMachine.run do
225
227
  u = url(http: false)
226
228
  @log.debug("Connecting to #{u.hostname}:#{u.port}...")
227
229
  ws = Faye::WebSocket::Client.new(u.to_s, [], proxy: @proxy ? { origin: @proxy } : {})
228
230
  log = @log
229
231
  contract = @contract
230
- id = rand(99_999)
231
232
  attempt = []
233
+ log_url = "ws#{@ssl ? 's' : ''}://#{u.hostname}:#{u.port}"
232
234
  ws.on(:open) do
233
235
  verbose do
234
- log.debug("Connected to ws://#{u.hostname}:#{u.port}")
236
+ log.debug("Connected to #{log_url}")
235
237
  end
236
238
  end
237
239
  ws.on(:message) do |msg|
@@ -243,7 +245,7 @@ class ERC20::Wallet
243
245
  active.append(a) unless before.include?(a)
244
246
  end
245
247
  log.debug(
246
- "Subscribed ##{id} to #{active.to_a.size} addresses: " \
248
+ "Subscribed ##{subscription_id} to #{active.to_a.size} addresses at #{log_url}: " \
247
249
  "#{active.to_a.map { |a| a[0..6] }.join(', ')}"
248
250
  )
249
251
  elsif data['method'] == 'eth_subscription' && data.dig('params', 'result')
@@ -257,7 +259,10 @@ class ERC20::Wallet
257
259
  to: "0x#{event['topics'][2][26..].downcase}",
258
260
  txn: event['transactionHash'].downcase
259
261
  }
260
- log.debug("Payment of #{event[:amount]} tokens arrived from #{event[:from]} to #{event[:to]}")
262
+ log.debug(
263
+ "Payment of #{event[:amount]} tokens arrived" \
264
+ "from #{event[:from]} to #{event[:to]} in #{event[:txn]}"
265
+ )
261
266
  end
262
267
  yield event
263
268
  end
@@ -265,12 +270,12 @@ class ERC20::Wallet
265
270
  end
266
271
  ws.on(:close) do
267
272
  verbose do
268
- log.debug("Disconnected from ws://#{u.hostname}:#{u.port}")
273
+ log.debug("Disconnected from #{log_url}")
269
274
  end
270
275
  end
271
276
  ws.on(:error) do |e|
272
277
  verbose do
273
- log.debug("Error at #{u.hostname}: #{e.message}")
278
+ log.debug("Error at #{log_url}: #{e.message}")
274
279
  end
275
280
  end
276
281
  EventMachine.add_periodic_timer(delay) do
@@ -279,7 +284,7 @@ class ERC20::Wallet
279
284
  ws.send(
280
285
  {
281
286
  jsonrpc: '2.0',
282
- id:,
287
+ id: subscription_id,
283
288
  method: 'eth_subscribe',
284
289
  params: [
285
290
  'logs',
@@ -295,7 +300,7 @@ class ERC20::Wallet
295
300
  }.to_json
296
301
  )
297
302
  log.debug(
298
- "Requested to subscribe ##{id} to #{addresses.to_a.size} addresses: " \
303
+ "Requested to subscribe ##{subscription_id} to #{addresses.to_a.size} addresses at #{log_url}: " \
299
304
  "#{addresses.to_a.map { |a| a[0..6] }.join(', ')}"
300
305
  )
301
306
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: erc20
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko