erc20 0.1.2 → 0.1.4
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 +4 -4
- data/Gemfile +1 -0
- data/Gemfile.lock +12 -0
- data/lib/erc20/erc20.rb +1 -1
- data/lib/erc20/wallet.rb +71 -50
- data/test/erc20/test_fake_wallet.rb +1 -1
- data/test/erc20/test_wallet.rb +67 -18
- data/test/test__helper.rb +3 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71eb4c1f0b7e6778be6d2b121792c20aa190cdfdfc62948ce39e1c4ea1cb288f
|
4
|
+
data.tar.gz: 9dbb23249587fe95bf6681ab9b87a131b4f946343c515df036c1fa1c7f45b200
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aeb83a009f276a4ead1a30e1d1cb052816067fb1571cca6cb20cc39a7840422342cbf246b498932be850c2330663d7e08356c2b3f35f2365acc933511462a4ab
|
7
|
+
data.tar.gz: '08b280c073e3d8bd1cc7b335b8d520e1808e190b50aaa227ee5fed17ae78ebc622637cd28c037e5991ea2dd8356cddb4f794ba240313f441cde0e79c3e7163d3'
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -12,6 +12,8 @@ PATH
|
|
12
12
|
GEM
|
13
13
|
remote: https://rubygems.org/
|
14
14
|
specs:
|
15
|
+
addressable (2.8.7)
|
16
|
+
public_suffix (>= 2.0.2, < 7.0)
|
15
17
|
ansi (1.5.0)
|
16
18
|
ast (2.4.3)
|
17
19
|
backtrace (0.4.0)
|
@@ -19,6 +21,9 @@ GEM
|
|
19
21
|
bigdecimal (3.1.9)
|
20
22
|
builder (3.3.0)
|
21
23
|
concurrent-ruby (1.3.5)
|
24
|
+
crack (1.0.0)
|
25
|
+
bigdecimal
|
26
|
+
rexml
|
22
27
|
cucumber (9.2.1)
|
23
28
|
builder (~> 3.2)
|
24
29
|
cucumber-ci-environment (> 9, < 11)
|
@@ -81,6 +86,7 @@ GEM
|
|
81
86
|
ffi (>= 1.15.5)
|
82
87
|
rake
|
83
88
|
forwardable (1.3.3)
|
89
|
+
hashdiff (1.1.2)
|
84
90
|
json (2.10.2)
|
85
91
|
jsonrpc-client (0.1.4)
|
86
92
|
faraday
|
@@ -113,6 +119,7 @@ GEM
|
|
113
119
|
racc
|
114
120
|
pkg-config (1.6.1)
|
115
121
|
prism (1.4.0)
|
122
|
+
public_suffix (6.0.1)
|
116
123
|
qbash (0.4.0)
|
117
124
|
backtrace (> 0)
|
118
125
|
elapsed (> 0)
|
@@ -184,6 +191,10 @@ GEM
|
|
184
191
|
unicode-emoji (~> 4.0, >= 4.0.4)
|
185
192
|
unicode-emoji (4.0.4)
|
186
193
|
uri (1.0.3)
|
194
|
+
webmock (3.25.1)
|
195
|
+
addressable (>= 2.8.0)
|
196
|
+
crack (>= 0.3.2)
|
197
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
187
198
|
websocket-driver (0.7.7)
|
188
199
|
base64
|
189
200
|
websocket-extensions (>= 0.1.0)
|
@@ -222,6 +233,7 @@ DEPENDENCIES
|
|
222
233
|
simplecov-cobertura (~> 2.1)
|
223
234
|
threads (~> 0.4)
|
224
235
|
typhoeus (> 0)
|
236
|
+
webmock (~> 3.23)
|
225
237
|
yard (~> 0.9)
|
226
238
|
|
227
239
|
BUNDLED WITH
|
data/lib/erc20/erc20.rb
CHANGED
data/lib/erc20/wallet.rb
CHANGED
@@ -121,7 +121,7 @@ class ERC20::Wallet
|
|
121
121
|
data = "0x#{func}000000000000000000000000#{address[2..].downcase}"
|
122
122
|
r = jsonrpc.eth_call({ to: @contract, data: data }, 'latest')
|
123
123
|
b = r[2..].to_i(16)
|
124
|
-
|
124
|
+
log_it(:debug, "The balance of #{address} is #{b} ERC20 tokens")
|
125
125
|
b
|
126
126
|
end
|
127
127
|
|
@@ -138,7 +138,7 @@ class ERC20::Wallet
|
|
138
138
|
raise 'Invalid format of the address' unless /^0x[0-9a-fA-F]{40}$/.match?(address)
|
139
139
|
r = jsonrpc.eth_getBalance(address, 'latest')
|
140
140
|
b = r[2..].to_i(16)
|
141
|
-
|
141
|
+
log_it(:debug, "The balance of #{address} is #{b} ETHs")
|
142
142
|
b
|
143
143
|
end
|
144
144
|
|
@@ -159,7 +159,7 @@ class ERC20::Wallet
|
|
159
159
|
raise "Amount (#{amount}) must be an Integer" unless amount.is_a?(Integer)
|
160
160
|
raise "Amount (#{amount}) must be a positive Integer" unless amount.positive?
|
161
161
|
gas = jsonrpc.eth_estimateGas({ from:, to: @contract, data: to_pay_data(to, amount) }, 'latest').to_i(16)
|
162
|
-
|
162
|
+
log_it(:debug, "It would take #{gas} gas units to send #{amount} tokens from #{from} to #{to}")
|
163
163
|
gas
|
164
164
|
end
|
165
165
|
|
@@ -177,7 +177,7 @@ class ERC20::Wallet
|
|
177
177
|
block = jsonrpc.eth_getBlockByNumber('latest', false)
|
178
178
|
raise "Can't get gas price, try again later" if block.nil?
|
179
179
|
gwei = block['baseFeePerGas'].to_i(16)
|
180
|
-
|
180
|
+
log_it(:debug, "The cost of one gas unit is #{gwei} gwei")
|
181
181
|
gwei
|
182
182
|
end
|
183
183
|
|
@@ -235,7 +235,7 @@ class ERC20::Wallet
|
|
235
235
|
hex = "0x#{tx.hex}"
|
236
236
|
jsonrpc.eth_sendRawTransaction(hex)
|
237
237
|
end
|
238
|
-
|
238
|
+
log_it(:debug, "Sent #{amount} ERC20 tokens from #{from} to #{address}: #{tnx}")
|
239
239
|
tnx.downcase
|
240
240
|
end
|
241
241
|
|
@@ -279,7 +279,7 @@ class ERC20::Wallet
|
|
279
279
|
hex = "0x#{tx.hex}"
|
280
280
|
jsonrpc.eth_sendRawTransaction(hex)
|
281
281
|
end
|
282
|
-
|
282
|
+
log_it(:debug, "Sent #{amount} ETHs from #{from} to #{address}: #{tnx}")
|
283
283
|
tnx.downcase
|
284
284
|
end
|
285
285
|
|
@@ -319,61 +319,66 @@ class ERC20::Wallet
|
|
319
319
|
raise 'Subscription ID must be a positive Integer' unless subscription_id.positive?
|
320
320
|
EventMachine.run do
|
321
321
|
u = url(http: false)
|
322
|
-
|
322
|
+
log_it(:debug, "Connecting to #{u.hostname}:#{u.port}...")
|
323
323
|
ws = Faye::WebSocket::Client.new(u.to_s, [], proxy: @proxy ? { origin: @proxy } : {})
|
324
|
-
log = @log
|
325
324
|
contract = @contract
|
326
325
|
attempt = []
|
327
326
|
log_url = "ws#{@ssl ? 's' : ''}://#{u.hostname}:#{u.port}"
|
328
327
|
ws.on(:open) do
|
329
|
-
|
330
|
-
|
328
|
+
safe do
|
329
|
+
verbose do
|
330
|
+
log_it(:debug, "Connected to #{log_url}")
|
331
|
+
end
|
331
332
|
end
|
332
333
|
end
|
333
334
|
ws.on(:message) do |msg|
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
event = data['params']['result']
|
347
|
-
if raw
|
348
|
-
log.debug("New event arrived from #{event['address']}")
|
349
|
-
else
|
350
|
-
event = {
|
351
|
-
amount: event['data'].to_i(16),
|
352
|
-
from: "0x#{event['topics'][1][26..].downcase}",
|
353
|
-
to: "0x#{event['topics'][2][26..].downcase}",
|
354
|
-
txn: event['transactionHash'].downcase
|
355
|
-
}
|
356
|
-
log.debug(
|
357
|
-
"Payment of #{event[:amount]} tokens arrived " \
|
358
|
-
"from #{event[:from]} to #{event[:to]} in #{event[:txn]}"
|
335
|
+
safe do
|
336
|
+
verbose do
|
337
|
+
data = to_json(msg)
|
338
|
+
if data['id']
|
339
|
+
before = active.to_a
|
340
|
+
attempt.each do |a|
|
341
|
+
active.append(a) unless before.include?(a)
|
342
|
+
end
|
343
|
+
log_it(
|
344
|
+
:debug,
|
345
|
+
"Subscribed ##{subscription_id} to #{active.to_a.size} addresses at #{log_url}: " \
|
346
|
+
"#{active.to_a.map { |a| a[0..6] }.join(', ')}"
|
359
347
|
)
|
360
|
-
|
361
|
-
|
348
|
+
elsif data['method'] == 'eth_subscription' && data.dig('params', 'result')
|
349
|
+
event = data['params']['result']
|
350
|
+
if raw
|
351
|
+
log_it(:debug, "New event arrived from #{event['address']}")
|
352
|
+
else
|
353
|
+
event = {
|
354
|
+
amount: event['data'].to_i(16),
|
355
|
+
from: "0x#{event['topics'][1][26..].downcase}",
|
356
|
+
to: "0x#{event['topics'][2][26..].downcase}",
|
357
|
+
txn: event['transactionHash'].downcase
|
358
|
+
}
|
359
|
+
log_it(
|
360
|
+
:debug,
|
361
|
+
"Payment of #{event[:amount]} tokens arrived " \
|
362
|
+
"from #{event[:from]} to #{event[:to]} in #{event[:txn]}"
|
363
|
+
)
|
364
|
+
end
|
362
365
|
yield event
|
363
|
-
rescue StandardError => e
|
364
|
-
@log.error(Backtrace.new(e).to_s)
|
365
366
|
end
|
366
367
|
end
|
367
368
|
end
|
368
369
|
end
|
369
370
|
ws.on(:close) do
|
370
|
-
|
371
|
-
|
371
|
+
safe do
|
372
|
+
verbose do
|
373
|
+
log_it(:debug, "Disconnected from #{log_url}")
|
374
|
+
end
|
372
375
|
end
|
373
376
|
end
|
374
377
|
ws.on(:error) do |e|
|
375
|
-
|
376
|
-
|
378
|
+
safe do
|
379
|
+
verbose do
|
380
|
+
log_it(:debug, "Error at #{log_url}: #{e.message}")
|
381
|
+
end
|
377
382
|
end
|
378
383
|
end
|
379
384
|
EventMachine.add_periodic_timer(delay) do
|
@@ -397,7 +402,8 @@ class ERC20::Wallet
|
|
397
402
|
]
|
398
403
|
}.to_json
|
399
404
|
)
|
400
|
-
|
405
|
+
log_it(
|
406
|
+
:debug,
|
401
407
|
"Requested to subscribe ##{subscription_id} to #{addresses.to_a.size} addresses at #{log_url}: " \
|
402
408
|
"#{addresses.to_a.map { |a| a[0..6] }.join(', ')}"
|
403
409
|
)
|
@@ -416,19 +422,26 @@ class ERC20::Wallet
|
|
416
422
|
def verbose
|
417
423
|
yield
|
418
424
|
rescue StandardError => e
|
419
|
-
|
425
|
+
log_it(:error, Backtrace.new(e).to_s)
|
420
426
|
raise e
|
421
427
|
end
|
422
428
|
|
429
|
+
def safe
|
430
|
+
yield
|
431
|
+
rescue StandardError
|
432
|
+
# ignore it
|
433
|
+
end
|
434
|
+
|
423
435
|
def url(http: true)
|
424
436
|
URI.parse("#{http ? 'http' : 'ws'}#{@ssl ? 's' : ''}://#{@host}:#{@port}#{http ? @http_path : @ws_path}")
|
425
437
|
end
|
426
438
|
|
427
439
|
def jsonrpc
|
428
440
|
JSONRPC.logger = Loog::NULL
|
429
|
-
|
430
|
-
|
431
|
-
|
441
|
+
opts = {}
|
442
|
+
if @proxy
|
443
|
+
uri = URI.parse(@proxy)
|
444
|
+
opts[:connection] =
|
432
445
|
Faraday.new do |f|
|
433
446
|
f.adapter(Faraday.default_adapter)
|
434
447
|
f.proxy = {
|
@@ -437,8 +450,8 @@ class ERC20::Wallet
|
|
437
450
|
password: uri.password
|
438
451
|
}
|
439
452
|
end
|
440
|
-
|
441
|
-
JSONRPC::Client.new(url,
|
453
|
+
end
|
454
|
+
JSONRPC::Client.new(url.to_s, opts)
|
442
455
|
end
|
443
456
|
|
444
457
|
def to_pay_data(address, amount)
|
@@ -449,4 +462,12 @@ class ERC20::Wallet
|
|
449
462
|
amt_padded = ('0' * (64 - amt_hex.size)) + amt_hex
|
450
463
|
"0x#{func}#{to_padded}#{amt_padded}"
|
451
464
|
end
|
465
|
+
|
466
|
+
def log_it(method, msg)
|
467
|
+
if @log.respond_to?(method)
|
468
|
+
@log.__send__(method, msg)
|
469
|
+
elsif @log.respond_to?(:puts)
|
470
|
+
@log.puts(msg)
|
471
|
+
end
|
472
|
+
end
|
452
473
|
end
|
data/test/erc20/test_wallet.rb
CHANGED
@@ -7,7 +7,7 @@ require 'backtrace'
|
|
7
7
|
require 'donce'
|
8
8
|
require 'eth'
|
9
9
|
require 'faraday'
|
10
|
-
require '
|
10
|
+
require 'json'
|
11
11
|
require 'random-port'
|
12
12
|
require 'shellwords'
|
13
13
|
require 'threads'
|
@@ -31,18 +31,21 @@ class TestWallet < ERC20::Test
|
|
31
31
|
WALTER = '91f9111b1744d55361e632771a4e53839e9442a9fef45febc0a5c838c686a15b'
|
32
32
|
|
33
33
|
def test_checks_balance_on_mainnet
|
34
|
+
WebMock.enable_net_connect!
|
34
35
|
b = mainnet.balance(STABLE)
|
35
36
|
refute_nil(b)
|
36
37
|
assert_equal(8_000_000, b) # this is $8 USDT
|
37
38
|
end
|
38
39
|
|
39
40
|
def test_checks_eth_balance_on_mainnet
|
41
|
+
WebMock.enable_net_connect!
|
40
42
|
b = mainnet.eth_balance(STABLE)
|
41
43
|
refute_nil(b)
|
42
44
|
assert_equal(4_200_000_000_000_000, b) # this is 0.0042 ETH
|
43
45
|
end
|
44
46
|
|
45
47
|
def test_checks_balance_of_absent_address
|
48
|
+
WebMock.enable_net_connect!
|
46
49
|
a = '0xEB2fE8872A6f1eDb70a2632Effffffffffffffff'
|
47
50
|
b = mainnet.balance(a)
|
48
51
|
refute_nil(b)
|
@@ -50,6 +53,7 @@ class TestWallet < ERC20::Test
|
|
50
53
|
end
|
51
54
|
|
52
55
|
def test_checks_gas_estimate_on_mainnet
|
56
|
+
WebMock.enable_net_connect!
|
53
57
|
b = mainnet.gas_estimate(STABLE, Eth::Key.new(priv: JEFF).address.to_s, 44_000)
|
54
58
|
refute_nil(b)
|
55
59
|
assert_predicate(b, :positive?)
|
@@ -57,28 +61,45 @@ class TestWallet < ERC20::Test
|
|
57
61
|
end
|
58
62
|
|
59
63
|
def test_fails_with_invalid_infura_key
|
64
|
+
WebMock.enable_net_connect!
|
60
65
|
skip('Apparently, even with invalid key, Infura returns balance')
|
61
66
|
w = ERC20::Wallet.new(
|
62
67
|
contract: ERC20::Wallet.USDT,
|
63
68
|
host: 'mainnet.infura.io',
|
64
69
|
http_path: '/v3/invalid-key-here',
|
65
|
-
log:
|
70
|
+
log: fake_loog
|
66
71
|
)
|
67
|
-
assert_raises(StandardError) {
|
72
|
+
assert_raises(StandardError) { w.balance(STABLE) }
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_logs_to_stdout
|
76
|
+
WebMock.disable_net_connect!
|
77
|
+
stub_request(:post, 'https://example.org/').to_return(
|
78
|
+
body: { jsonrpc: '2.0', id: 42, result: '0x1F1F1F' }.to_json,
|
79
|
+
headers: { 'Content-Type' => 'application/json' }
|
80
|
+
)
|
81
|
+
w = ERC20::Wallet.new(
|
82
|
+
host: 'example.org',
|
83
|
+
http_path: '/',
|
84
|
+
log: $stdout
|
85
|
+
)
|
86
|
+
w.balance(STABLE)
|
68
87
|
end
|
69
88
|
|
70
89
|
def test_checks_balance_on_testnet
|
90
|
+
WebMock.enable_net_connect!
|
71
91
|
b = testnet.balance(STABLE)
|
72
92
|
refute_nil(b)
|
73
93
|
assert_predicate(b, :zero?)
|
74
94
|
end
|
75
95
|
|
76
96
|
def test_checks_balance_on_polygon
|
97
|
+
WebMock.enable_net_connect!
|
77
98
|
w = ERC20::Wallet.new(
|
78
99
|
contract: '0xc2132D05D31c914a87C6611C10748AEb04B58e8F',
|
79
100
|
host: 'polygon-mainnet.infura.io',
|
80
101
|
http_path: "/v3/#{env('INFURA_KEY')}",
|
81
|
-
log:
|
102
|
+
log: fake_loog
|
82
103
|
)
|
83
104
|
b = w.balance(STABLE)
|
84
105
|
refute_nil(b)
|
@@ -86,6 +107,7 @@ class TestWallet < ERC20::Test
|
|
86
107
|
end
|
87
108
|
|
88
109
|
def test_checks_gas_estimate_on_hardhat
|
110
|
+
WebMock.enable_net_connect!
|
89
111
|
sum = 100_000
|
90
112
|
on_hardhat do |wallet|
|
91
113
|
b1 = wallet.gas_estimate(
|
@@ -98,6 +120,7 @@ class TestWallet < ERC20::Test
|
|
98
120
|
end
|
99
121
|
|
100
122
|
def test_checks_balance_on_hardhat
|
123
|
+
WebMock.enable_net_connect!
|
101
124
|
on_hardhat do |wallet|
|
102
125
|
b = wallet.balance(Eth::Key.new(priv: JEFF).address.to_s)
|
103
126
|
assert_equal(123_000_100_000, b)
|
@@ -105,6 +128,7 @@ class TestWallet < ERC20::Test
|
|
105
128
|
end
|
106
129
|
|
107
130
|
def test_checks_eth_balance_on_hardhat
|
131
|
+
WebMock.enable_net_connect!
|
108
132
|
on_hardhat do |wallet|
|
109
133
|
b = wallet.balance(Eth::Key.new(priv: WALTER).address.to_s)
|
110
134
|
assert_equal(456_000_000_000, b)
|
@@ -112,6 +136,7 @@ class TestWallet < ERC20::Test
|
|
112
136
|
end
|
113
137
|
|
114
138
|
def test_checks_balance_on_hardhat_in_threads
|
139
|
+
WebMock.enable_net_connect!
|
115
140
|
on_hardhat do |wallet|
|
116
141
|
Threads.new.assert do
|
117
142
|
b = wallet.balance(Eth::Key.new(priv: JEFF).address.to_s)
|
@@ -121,6 +146,7 @@ class TestWallet < ERC20::Test
|
|
121
146
|
end
|
122
147
|
|
123
148
|
def test_pays_on_hardhat
|
149
|
+
WebMock.enable_net_connect!
|
124
150
|
on_hardhat do |wallet|
|
125
151
|
to = Eth::Key.new(priv: WALTER).address.to_s
|
126
152
|
before = wallet.balance(to)
|
@@ -135,6 +161,7 @@ class TestWallet < ERC20::Test
|
|
135
161
|
end
|
136
162
|
|
137
163
|
def test_eth_pays_on_hardhat
|
164
|
+
WebMock.enable_net_connect!
|
138
165
|
on_hardhat do |wallet|
|
139
166
|
to = Eth::Key.new(priv: WALTER).address.to_s
|
140
167
|
before = wallet.eth_balance(to)
|
@@ -149,6 +176,7 @@ class TestWallet < ERC20::Test
|
|
149
176
|
end
|
150
177
|
|
151
178
|
def test_pays_on_hardhat_in_threads
|
179
|
+
WebMock.enable_net_connect!
|
152
180
|
on_hardhat do |wallet|
|
153
181
|
to = Eth::Key.new(priv: WALTER).address.to_s
|
154
182
|
before = wallet.balance(to)
|
@@ -162,6 +190,7 @@ class TestWallet < ERC20::Test
|
|
162
190
|
end
|
163
191
|
|
164
192
|
def test_pays_eth_on_hardhat_in_threads
|
193
|
+
WebMock.enable_net_connect!
|
165
194
|
on_hardhat do |wallet|
|
166
195
|
to = Eth::Key.new(priv: WALTER).address.to_s
|
167
196
|
before = wallet.eth_balance(to)
|
@@ -175,6 +204,7 @@ class TestWallet < ERC20::Test
|
|
175
204
|
end
|
176
205
|
|
177
206
|
def test_accepts_payments_on_hardhat
|
207
|
+
WebMock.enable_net_connect!
|
178
208
|
walter = Eth::Key.new(priv: WALTER).address.to_s.downcase
|
179
209
|
jeff = Eth::Key.new(priv: JEFF).address.to_s.downcase
|
180
210
|
on_hardhat do |wallet|
|
@@ -186,7 +216,7 @@ class TestWallet < ERC20::Test
|
|
186
216
|
event = e
|
187
217
|
end
|
188
218
|
rescue StandardError => e
|
189
|
-
|
219
|
+
fake_loog.error(Backtrace.new(e))
|
190
220
|
end
|
191
221
|
wait_for { !active.empty? }
|
192
222
|
sum = 77_000
|
@@ -202,6 +232,7 @@ class TestWallet < ERC20::Test
|
|
202
232
|
end
|
203
233
|
|
204
234
|
def test_accepts_many_payments_on_hardhat
|
235
|
+
WebMock.enable_net_connect!
|
205
236
|
walter = Eth::Key.new(priv: WALTER).address.to_s.downcase
|
206
237
|
on_hardhat do |wallet|
|
207
238
|
active = []
|
@@ -213,7 +244,7 @@ class TestWallet < ERC20::Test
|
|
213
244
|
events.add(e)
|
214
245
|
end
|
215
246
|
rescue StandardError => e
|
216
|
-
|
247
|
+
fake_loog.error(Backtrace.new(e))
|
217
248
|
end
|
218
249
|
wait_for { !active.empty? }
|
219
250
|
sum = 1_234
|
@@ -228,6 +259,7 @@ class TestWallet < ERC20::Test
|
|
228
259
|
end
|
229
260
|
|
230
261
|
def test_accepts_payments_with_failures_on_hardhat
|
262
|
+
WebMock.enable_net_connect!
|
231
263
|
walter = Eth::Key.new(priv: WALTER).address.to_s.downcase
|
232
264
|
on_hardhat do |wallet|
|
233
265
|
active = []
|
@@ -253,6 +285,7 @@ class TestWallet < ERC20::Test
|
|
253
285
|
end
|
254
286
|
|
255
287
|
def test_accepts_payments_on_changing_addresses_on_hardhat
|
288
|
+
WebMock.enable_net_connect!
|
256
289
|
walter = Eth::Key.new(priv: WALTER).address.to_s.downcase
|
257
290
|
jeff = Eth::Key.new(priv: JEFF).address.to_s.downcase
|
258
291
|
addresses = Primitivo.new([walter])
|
@@ -265,7 +298,7 @@ class TestWallet < ERC20::Test
|
|
265
298
|
event = e
|
266
299
|
end
|
267
300
|
rescue StandardError => e
|
268
|
-
|
301
|
+
fake_loog.error(Backtrace.new(e))
|
269
302
|
end
|
270
303
|
wait_for { active.to_a.include?(walter) }
|
271
304
|
sum1 = 453_000
|
@@ -285,6 +318,7 @@ class TestWallet < ERC20::Test
|
|
285
318
|
end
|
286
319
|
|
287
320
|
def test_accepts_payments_on_hardhat_via_proxy
|
321
|
+
WebMock.enable_net_connect!
|
288
322
|
via_proxy do |proxy|
|
289
323
|
walter = Eth::Key.new(priv: WALTER).address.to_s.downcase
|
290
324
|
jeff = Eth::Key.new(priv: JEFF).address.to_s.downcase
|
@@ -298,7 +332,7 @@ class TestWallet < ERC20::Test
|
|
298
332
|
event = e
|
299
333
|
end
|
300
334
|
rescue StandardError => e
|
301
|
-
|
335
|
+
fake_loog.error(Backtrace.new(e))
|
302
336
|
end
|
303
337
|
wait_for { !active.empty? }
|
304
338
|
sum = 55_000
|
@@ -312,6 +346,7 @@ class TestWallet < ERC20::Test
|
|
312
346
|
end
|
313
347
|
|
314
348
|
def test_accepts_payments_on_mainnet
|
349
|
+
WebMock.enable_net_connect!
|
315
350
|
active = []
|
316
351
|
failed = false
|
317
352
|
net = mainnet
|
@@ -322,7 +357,7 @@ class TestWallet < ERC20::Test
|
|
322
357
|
end
|
323
358
|
rescue StandardError => e
|
324
359
|
failed = true
|
325
|
-
|
360
|
+
fake_loog.error(Backtrace.new(e))
|
326
361
|
end
|
327
362
|
wait_for { !active.empty? }
|
328
363
|
daemon.kill
|
@@ -331,27 +366,31 @@ class TestWallet < ERC20::Test
|
|
331
366
|
end
|
332
367
|
|
333
368
|
def test_checks_balance_via_proxy
|
369
|
+
WebMock.enable_net_connect!
|
370
|
+
b = nil
|
334
371
|
via_proxy do |proxy|
|
335
372
|
on_hardhat do |w|
|
336
373
|
wallet = through_proxy(w, proxy)
|
337
374
|
b = wallet.balance(Eth::Key.new(priv: JEFF).address.to_s)
|
338
|
-
assert_equal(123_000_100_000, b)
|
339
375
|
end
|
340
376
|
end
|
377
|
+
assert_equal(123_000_100_000, b)
|
341
378
|
end
|
342
379
|
|
343
380
|
def test_checks_balance_via_proxy_on_mainnet
|
381
|
+
WebMock.enable_net_connect!
|
344
382
|
via_proxy do |proxy|
|
345
383
|
w = ERC20::Wallet.new(
|
346
384
|
host: 'mainnet.infura.io',
|
347
385
|
http_path: "/v3/#{env('INFURA_KEY')}",
|
348
|
-
proxy:, log:
|
386
|
+
proxy:, log: fake_loog
|
349
387
|
)
|
350
388
|
assert_equal(8_000_000, w.balance(STABLE))
|
351
389
|
end
|
352
390
|
end
|
353
391
|
|
354
392
|
def test_pays_on_mainnet
|
393
|
+
WebMock.enable_net_connect!
|
355
394
|
skip('This is live, must be run manually')
|
356
395
|
w = mainnet
|
357
396
|
print 'Enter Ethereum ERC20 private key (64 chars): '
|
@@ -383,7 +422,12 @@ class TestWallet < ERC20::Test
|
|
383
422
|
ws_path: "/#{env('GETBLOCK_WS_KEY')}"
|
384
423
|
}
|
385
424
|
].map do |server|
|
386
|
-
ERC20::Wallet.new(
|
425
|
+
ERC20::Wallet.new(
|
426
|
+
host: server[:host],
|
427
|
+
http_path: server[:http_path],
|
428
|
+
ws_path: server[:ws_path],
|
429
|
+
log: fake_loog
|
430
|
+
)
|
387
431
|
end.sample
|
388
432
|
end
|
389
433
|
|
@@ -400,7 +444,12 @@ class TestWallet < ERC20::Test
|
|
400
444
|
ws_path: "/#{env('GETBLOCK_SEPOILA_KEY')}"
|
401
445
|
}
|
402
446
|
].map do |server|
|
403
|
-
ERC20::Wallet.new(
|
447
|
+
ERC20::Wallet.new(
|
448
|
+
host: server[:host],
|
449
|
+
http_path: server[:http_path],
|
450
|
+
ws_path: server[:ws_path],
|
451
|
+
log: fake_loog
|
452
|
+
)
|
404
453
|
end.sample
|
405
454
|
end
|
406
455
|
|
@@ -408,7 +457,7 @@ class TestWallet < ERC20::Test
|
|
408
457
|
ERC20::Wallet.new(
|
409
458
|
contract: wallet.contract, chain: wallet.chain,
|
410
459
|
host: donce_host, port: wallet.port, http_path: wallet.http_path, ws_path: wallet.ws_path,
|
411
|
-
ssl: wallet.ssl, proxy:, log:
|
460
|
+
ssl: wallet.ssl, proxy:, log: fake_loog
|
412
461
|
)
|
413
462
|
end
|
414
463
|
|
@@ -418,7 +467,7 @@ class TestWallet < ERC20::Test
|
|
418
467
|
image: 'yegor256/squid-proxy:latest',
|
419
468
|
ports: { port => 3128 },
|
420
469
|
env: { 'USERNAME' => 'jeffrey', 'PASSWORD' => 'swordfish' },
|
421
|
-
root: true, log:
|
470
|
+
root: true, log: fake_loog
|
422
471
|
) do
|
423
472
|
yield "http://jeffrey:swordfish@localhost:#{port}"
|
424
473
|
end
|
@@ -431,7 +480,7 @@ class TestWallet < ERC20::Test
|
|
431
480
|
home: File.join(__dir__, '../../hardhat'),
|
432
481
|
ports: { port => 8545 },
|
433
482
|
command: 'npx hardhat node',
|
434
|
-
log:
|
483
|
+
log: fake_loog
|
435
484
|
) do
|
436
485
|
wait_for_port(port)
|
437
486
|
cmd = [
|
@@ -444,13 +493,13 @@ class TestWallet < ERC20::Test
|
|
444
493
|
home: File.join(__dir__, '../../hardhat'),
|
445
494
|
command: "/bin/bash -c #{Shellwords.escape(cmd)}",
|
446
495
|
build_args: { 'HOST' => donce_host, 'PORT' => port },
|
447
|
-
log:
|
496
|
+
log: fake_loog,
|
448
497
|
root: true
|
449
498
|
).split("\n").last
|
450
499
|
wallet = ERC20::Wallet.new(
|
451
500
|
contract:, chain: 4242,
|
452
501
|
host: 'localhost', port:, http_path: '/', ws_path: '/', ssl: false,
|
453
|
-
log:
|
502
|
+
log: fake_loog
|
454
503
|
)
|
455
504
|
yield wallet
|
456
505
|
end
|
data/test/test__helper.rb
CHANGED
@@ -51,12 +51,14 @@ class Primitivo
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
+
require 'webmock/minitest'
|
55
|
+
|
54
56
|
# Test.
|
55
57
|
# Author:: Yegor Bugayenko (yegor256@gmail.com)
|
56
58
|
# Copyright:: Copyright (c) 2025 Yegor Bugayenko
|
57
59
|
# License:: MIT
|
58
60
|
class ERC20::Test < Minitest::Test
|
59
|
-
def
|
61
|
+
def fake_loog
|
60
62
|
ENV['RAKE'] ? Loog::ERRORS : Loog::VERBOSE
|
61
63
|
end
|
62
64
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: erc20
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yegor Bugayenko
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-04-
|
10
|
+
date: 2025-04-21 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: eth
|