erc20 0.0.2 → 0.0.3

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: bf1f0dbee9d090079bd263a79eeafc0093866fc5503d7a1130b26e7b77ca8630
4
- data.tar.gz: 105f04b7df7e9b8f45a3c34acb739fb6b74238e9f7180b624ceb724f8a4c1570
3
+ metadata.gz: 9b1ea9500a3c12a4b5f84d2aa2101e16c3cfcf1fef9f0d8ccb95433aba9798cb
4
+ data.tar.gz: dcd57712fa1fd1adb99511cbc42cb1c2315e7178b9d90862bddfb632ec83b0fc
5
5
  SHA512:
6
- metadata.gz: e86283d48937fca5b956c0963b19c6b25b25226d6a60bd737e37d6201cc5dc82879d33ab79ff3ef919728762da601aa9d1bc56aaa85b94532f7edcafd29f04eb
7
- data.tar.gz: f11fd8b7d8b017c4fbdc70b4442aa054344fee9451fd029e7826da8fe12961ac60447522945e83b941ab7ccf28a67299a9077c47b114f8736336d42c6b16088f
6
+ metadata.gz: dada085699673561fff510ba8d2071d983c36634b7a7bdf0232536ee4319469a4cd19d7f6b5b48ec6572ec974eeaa5fe192052210b985fb3a094bfbf5b97c8be
7
+ data.tar.gz: 306828e368f07f87274eeaf1439075bc11a7b858260997419df4620b94682156b3d77000d9d5245aba5397507a0de107339f4d13b354cf3291cf592437884a69
data/.rubocop.yml CHANGED
@@ -44,7 +44,7 @@ Layout/MultilineMethodCallIndentation:
44
44
  Metrics/AbcSize:
45
45
  Enabled: false
46
46
  Metrics/BlockLength:
47
- Max: 50
47
+ Max: 100
48
48
  Metrics/CyclomaticComplexity:
49
49
  Max: 25
50
50
  Metrics/PerceivedComplexity:
data/Gemfile.lock CHANGED
@@ -6,7 +6,6 @@ PATH
6
6
  eth (~> 0.4)
7
7
  jsonrpc-client (> 0)
8
8
  loog (> 0)
9
- tago (> 0)
10
9
  websocket-client-simple (> 0)
11
10
 
12
11
  GEM
data/README.md CHANGED
@@ -11,13 +11,15 @@
11
11
  [![Hits-of-Code](https://hitsofcode.com/github/yegor256/erc20)](https://hitsofcode.com/view/github/yegor256/erc20)
12
12
  [![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/yegor256/erc20/blob/master/LICENSE.txt)
13
13
 
14
- This small Ruby library makes manipulations with [Etherium] [ERC20] tokens
14
+ This small Ruby [gem](https://rubygems.org/gems/erc20)
15
+ makes manipulations with [Etherium] [ERC20] tokens
15
16
  as simple as they can be, if you have a provider of
16
17
  [JSON-RPC] and [WebSocket] Etherium APIs, for example
17
18
  [Infura], [GetBlock], or [Alchemy]:
18
19
 
19
20
  ```ruby
20
21
  # Create a wallet:
22
+ require 'erc20'
21
23
  w = ERC20::Wallet.new(
22
24
  contract: ERC20::Wallet.USDT, # hex of it
23
25
  rpc: 'https://mainnet.infura.io/v3/<your-key>',
@@ -34,9 +36,9 @@ txn = w.pay(private_key, to_address, amount)
34
36
  # Stay waiting, and trigger the block when transactions arrive:
35
37
  addresses = ['0x...', '0x...']
36
38
  w.accept(addresses) do |event|
37
- puts event['data'].to_i(16)) # how much
38
- puts "0x#{event['topics'][1][26..]}" # who sent the payment
39
- puts "0x#{event['topics'][2][26..]}" # who was the receiver
39
+ puts event[:amount] # how much
40
+ puts event[:from] # who sent the payment
41
+ puts event[:to] # who was the receiver
40
42
  end
41
43
  ```
42
44
 
data/erc20.gemspec CHANGED
@@ -43,7 +43,6 @@ Gem::Specification.new do |s|
43
43
  s.add_dependency 'eth', '~>0.4'
44
44
  s.add_dependency 'jsonrpc-client', '>0'
45
45
  s.add_dependency 'loog', '>0'
46
- s.add_dependency 'tago', '>0'
47
46
  s.add_dependency 'websocket-client-simple', '>0'
48
47
  s.metadata['rubygems_mfa_required'] = 'true'
49
48
  end
data/lib/erc20/wallet.rb CHANGED
@@ -59,7 +59,9 @@ class ERC20::Wallet
59
59
  padded = "000000000000000000000000#{hex[2..].downcase}"
60
60
  data = "0x#{func}#{padded}"
61
61
  r = jsonrpc.eth_call({ to: @contract, data: data }, 'latest')
62
- r[2..].to_i(16)
62
+ b = r[2..].to_i(16)
63
+ @log.debug("Balance of #{hex} is #{b}")
64
+ b
63
65
  end
64
66
 
65
67
  # Send a single payment from a private address to a public one.
@@ -92,7 +94,10 @@ class ERC20::Wallet
92
94
  }
93
95
  )
94
96
  tx.sign(key)
95
- jsonrpc.eth_sendRawTransaction("0x#{tx.hex}")
97
+ hex = "0x#{tx.hex}"
98
+ jsonrpc.eth_sendRawTransaction(hex)
99
+ @log.debug("Sent #{amount} from #{from} to #{address}: #{hex}")
100
+ hex
96
101
  end
97
102
 
98
103
  # Wait for incoming transactions and let the block know when they
@@ -101,7 +106,8 @@ class ERC20::Wallet
101
106
  #
102
107
  # @param [Array<String>] addresses Addresses to monitor
103
108
  # @param [Array] ready When connected, TRUE will be added to this array
104
- def accept(addresses, connected: [])
109
+ # @param [Boolean] raw TRUE if you need to get JSON events as they arrive from Websockets
110
+ def accept(addresses, connected: [], raw: false)
105
111
  WebSocket::Client::Simple.connect(@wss) do |ws|
106
112
  log = @log
107
113
  contract = @contract
@@ -126,6 +132,7 @@ class ERC20::Wallet
126
132
  }
127
133
  ws.send(msg.to_json)
128
134
  connected.append(1)
135
+ log.debug("Subscribed to #{addresses.count} addresses")
129
136
  end
130
137
  ws.on(:message) do |msg|
131
138
  data =
@@ -136,7 +143,14 @@ class ERC20::Wallet
136
143
  end
137
144
  if data['method'] == 'eth_subscription' && data.dig('params', 'result')
138
145
  event = data['params']['result']
139
- log.debug("New transaction from: #{event['address']}")
146
+ unless raw
147
+ event = {
148
+ amount: event['data'].to_i(16),
149
+ from: "0x#{event['topics'][1][26..].downcase}",
150
+ to: "0x#{event['topics'][2][26..].downcase}"
151
+ }
152
+ end
153
+ log.debug("New event arrived from #{event['address']}")
140
154
  yield event
141
155
  end
142
156
  end
data/lib/erc20.rb CHANGED
@@ -27,5 +27,5 @@
27
27
  # License:: MIT
28
28
  module ERC20
29
29
  # Current version of the gem (changed by .rultor.yml on every release)
30
- VERSION = '0.0.2'
30
+ VERSION = '0.0.3'
31
31
  end
@@ -113,11 +113,9 @@ class TestWallet < Minitest::Test
113
113
  wait_for { !event.nil? }
114
114
  daemon.kill
115
115
  daemon.join(30)
116
- assert_equal(sum, event['data'].to_i(16))
117
- from = "0x#{event['topics'][1][26..].downcase}"
118
- assert_equal(jeff, from)
119
- to = "0x#{event['topics'][2][26..].downcase}"
120
- assert_equal(walter, to)
116
+ assert_equal(sum, event[:amount])
117
+ assert_equal(jeff, event[:from])
118
+ assert_equal(walter, event[:to])
121
119
  end
122
120
  end
123
121
 
@@ -128,7 +126,7 @@ class TestWallet < Minitest::Test
128
126
  loop do
129
127
  sleep(0.1)
130
128
  break if yield
131
- raise 'timeout' if Time.now - start > 15
129
+ raise 'timeout' if Time.now - start > 60
132
130
  rescue Errno::ECONNREFUSED
133
131
  retry
134
132
  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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - ">"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: tago
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">"
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">"
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: websocket-client-simple
85
71
  requirement: !ruby/object:Gem::Requirement