erc20 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/Gemfile.lock +0 -1
- data/README.md +6 -4
- data/erc20.gemspec +0 -1
- data/lib/erc20/wallet.rb +18 -4
- data/lib/erc20.rb +1 -1
- data/test/erc20/test_wallet.rb +4 -6
- metadata +1 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b1ea9500a3c12a4b5f84d2aa2101e16c3cfcf1fef9f0d8ccb95433aba9798cb
|
4
|
+
data.tar.gz: dcd57712fa1fd1adb99511cbc42cb1c2315e7178b9d90862bddfb632ec83b0fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dada085699673561fff510ba8d2071d983c36634b7a7bdf0232536ee4319469a4cd19d7f6b5b48ec6572ec974eeaa5fe192052210b985fb3a094bfbf5b97c8be
|
7
|
+
data.tar.gz: 306828e368f07f87274eeaf1439075bc11a7b858260997419df4620b94682156b3d77000d9d5245aba5397507a0de107339f4d13b354cf3291cf592437884a69
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -11,13 +11,15 @@
|
|
11
11
|
[](https://hitsofcode.com/view/github/yegor256/erc20)
|
12
12
|
[](https://github.com/yegor256/erc20/blob/master/LICENSE.txt)
|
13
13
|
|
14
|
-
This small Ruby
|
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[
|
38
|
-
puts
|
39
|
-
puts
|
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
|
-
|
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
|
-
|
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
|
-
|
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
data/test/erc20/test_wallet.rb
CHANGED
@@ -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[
|
117
|
-
|
118
|
-
assert_equal(
|
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 >
|
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.
|
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
|