erc20 0.0.9 → 0.0.11
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/lib/erc20/erc20.rb +1 -1
- data/lib/erc20/fake_wallet.rb +88 -0
- data/lib/erc20/wallet.rb +1 -1
- data/lib/erc20.rb +1 -0
- data/test/erc20/test_fake_wallet.rb +75 -0
- data/test/erc20/test_wallet.rb +0 -33
- data/test/test__helper.rb +40 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50eba9549bf56943891bef40214396c749f1af24a5cbaa5ea05974bda67da502
|
4
|
+
data.tar.gz: 96849e0e0853943acfb4477dcf652ffabd5f87ad9dd66c38505d49ce6ae74a02
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30717a9c5ee86f82ccee0da5b332a82953e7ef6df2ff0f7c72e628dcda9abc06322e134ab01568a47bdd009f9f4e6cde12409f4eefcbb210b09600d91c920235
|
7
|
+
data.tar.gz: 230efabd9195277fa94d86f90a3802b354cfb78a1090da1329d813a1713720176c9aae3243842c034100550b55974951312728a1f8382aff6717a2b1395efd8f
|
data/lib/erc20/erc20.rb
CHANGED
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright (c) 2025 Yegor Bugayenko
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
# SOFTWARE.
|
22
|
+
|
23
|
+
require_relative 'erc20'
|
24
|
+
require_relative 'wallet'
|
25
|
+
|
26
|
+
# A fake wallet that behaves like a +ERC20::Wallet+.
|
27
|
+
#
|
28
|
+
# Author:: Yegor Bugayenko (yegor256@gmail.com)
|
29
|
+
# Copyright:: Copyright (c) 2025 Yegor Bugayenko
|
30
|
+
# License:: MIT
|
31
|
+
class ERC20::FakeWallet
|
32
|
+
# Fakes:
|
33
|
+
attr_reader :host, :port, :ssl, :chain, :contract, :ws_path, :http_path
|
34
|
+
|
35
|
+
# Ctor.
|
36
|
+
def initialize
|
37
|
+
@host = 'example.com'
|
38
|
+
@port = 443
|
39
|
+
@ssl = true
|
40
|
+
@chain = 1
|
41
|
+
@contract = ERC20::Wallet::USDT
|
42
|
+
@ws_path = '/'
|
43
|
+
@http_path = '/'
|
44
|
+
end
|
45
|
+
|
46
|
+
# Get balance of a public address.
|
47
|
+
#
|
48
|
+
# @param [String] _hex Public key, in hex, starting from '0x'
|
49
|
+
# @return [Integer] Balance, in tokens
|
50
|
+
def balance(_hex)
|
51
|
+
42_000_000
|
52
|
+
end
|
53
|
+
|
54
|
+
# Send a single payment from a private address to a public one.
|
55
|
+
#
|
56
|
+
# @param [String] _priv Private key, in hex
|
57
|
+
# @param [String] _address Public key, in hex
|
58
|
+
# @param [Integer] _amount The amount of ERC20 tokens to send
|
59
|
+
# @return [String] Transaction hash
|
60
|
+
def pay(_priv, _address, _amount, *)
|
61
|
+
'0x172de9cda30537eae68ab4a96163ebbb8f8a85293b8737dd2e5deb4714b14623'
|
62
|
+
end
|
63
|
+
|
64
|
+
# Wait and accept.
|
65
|
+
#
|
66
|
+
# @param [Array<String>] addresses Addresses to monitor
|
67
|
+
# @param [Array] active List of addresses that we are actually listening to
|
68
|
+
# @param [Boolean] raw TRUE if you need to get JSON events as they arrive from Websockets
|
69
|
+
# @param [Integer] delay How many seconds to wait between +eth_subscribe+ calls
|
70
|
+
def accept(addresses, active = [], raw: false, delay: 1)
|
71
|
+
addresses.to_a.each { |a| active.append(a) }
|
72
|
+
loop do
|
73
|
+
event =
|
74
|
+
if raw
|
75
|
+
{}
|
76
|
+
else
|
77
|
+
{
|
78
|
+
amount: 424_242,
|
79
|
+
from: '0xd5ff1bfcde7a03da61ad229d962c74f1ea2f16a5',
|
80
|
+
to: addresses.to_a.sample,
|
81
|
+
txn: '0x172de9cda30537eae68ab4a96163ebbb8f8a85293b8737dd2e5deb4714b14623'
|
82
|
+
}
|
83
|
+
end
|
84
|
+
yield event
|
85
|
+
sleep(delay)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/lib/erc20/wallet.rb
CHANGED
@@ -108,7 +108,7 @@ class ERC20::Wallet
|
|
108
108
|
# Get balance of a public address.
|
109
109
|
#
|
110
110
|
# @param [String] hex Public key, in hex, starting from '0x'
|
111
|
-
# @return [Integer] Balance, in
|
111
|
+
# @return [Integer] Balance, in tokens
|
112
112
|
def balance(hex)
|
113
113
|
func = '70a08231' # balanceOf
|
114
114
|
padded = "000000000000000000000000#{hex[2..].downcase}"
|
data/lib/erc20.rb
CHANGED
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright (c) 2025 Yegor Bugayenko
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
# SOFTWARE.
|
22
|
+
|
23
|
+
require 'backtrace'
|
24
|
+
require 'donce'
|
25
|
+
require 'eth'
|
26
|
+
require 'faraday'
|
27
|
+
require 'loog'
|
28
|
+
require 'minitest/autorun'
|
29
|
+
require 'random-port'
|
30
|
+
require 'shellwords'
|
31
|
+
require 'threads'
|
32
|
+
require 'typhoeus'
|
33
|
+
require_relative '../../lib/erc20/fake_wallet'
|
34
|
+
require_relative '../test__helper'
|
35
|
+
|
36
|
+
# Test.
|
37
|
+
# Author:: Yegor Bugayenko (yegor256@gmail.com)
|
38
|
+
# Copyright:: Copyright (c) 2025 Yegor Bugayenko
|
39
|
+
# License:: MIT
|
40
|
+
class TestFakeWallet < Minitest::Test
|
41
|
+
def test_checks_fake_balance
|
42
|
+
b = ERC20::FakeWallet.new.balance('0xEB2fE8872A6f1eDb70a2632Effffffffffffffff')
|
43
|
+
refute_nil(b)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_returns_host
|
47
|
+
assert_equal('example.com', ERC20::FakeWallet.new.host)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_pays_fake_money
|
51
|
+
priv = '81a9b2114d53731ecc84b261ef6c0387dde34d5907fe7b441240cc21d61bf80a'
|
52
|
+
to = '0xfadef8ba4a5d709a2bf55b7a8798c9b438c640c1'
|
53
|
+
txn = ERC20::FakeWallet.new.pay(Eth::Key.new(priv:), to, 555)
|
54
|
+
assert_equal(66, txn.length)
|
55
|
+
assert_match(/^0x[a-f0-9]{64}$/, txn)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_accepts_payments_on_hardhat
|
59
|
+
active = Primitivo.new([])
|
60
|
+
addresses = Primitivo.new(['0xfadef8ba4a5d709a2bf55b7a8798c9b438c640c1'])
|
61
|
+
event = nil
|
62
|
+
daemon =
|
63
|
+
Thread.new do
|
64
|
+
ERC20::FakeWallet.new.accept(addresses, active) do |e|
|
65
|
+
event = e
|
66
|
+
end
|
67
|
+
rescue StandardError => e
|
68
|
+
loog.error(Backtrace.new(e))
|
69
|
+
end
|
70
|
+
wait_for { !active.to_a.empty? }
|
71
|
+
daemon.kill
|
72
|
+
daemon.join(30)
|
73
|
+
refute_nil(event)
|
74
|
+
end
|
75
|
+
end
|
data/test/erc20/test_wallet.rb
CHANGED
@@ -260,25 +260,6 @@ class TestWallet < Minitest::Test
|
|
260
260
|
|
261
261
|
private
|
262
262
|
|
263
|
-
def loog
|
264
|
-
ENV['RAKE'] ? Loog::ERRORS : Loog::VERBOSE
|
265
|
-
end
|
266
|
-
|
267
|
-
def wait_for
|
268
|
-
start = Time.now
|
269
|
-
loop do
|
270
|
-
sleep(0.1)
|
271
|
-
break if yield
|
272
|
-
raise 'timeout' if Time.now - start > 60
|
273
|
-
rescue Errno::ECONNREFUSED
|
274
|
-
retry
|
275
|
-
end
|
276
|
-
end
|
277
|
-
|
278
|
-
def wait_for_port(port)
|
279
|
-
wait_for { Typhoeus::Request.get("http://localhost:#{port}").code == 200 }
|
280
|
-
end
|
281
|
-
|
282
263
|
def env(var)
|
283
264
|
key = ENV.fetch(var, nil)
|
284
265
|
skip("The #{var} environment variable is not set") if key.nil?
|
@@ -371,18 +352,4 @@ class TestWallet < Minitest::Test
|
|
371
352
|
end
|
372
353
|
end
|
373
354
|
end
|
374
|
-
|
375
|
-
class Primitivo
|
376
|
-
def initialize(array)
|
377
|
-
@array = array
|
378
|
-
end
|
379
|
-
|
380
|
-
def to_a
|
381
|
-
@array.to_a
|
382
|
-
end
|
383
|
-
|
384
|
-
def append(item)
|
385
|
-
@array.append(item)
|
386
|
-
end
|
387
|
-
end
|
388
355
|
end
|
data/test/test__helper.rb
CHANGED
@@ -39,3 +39,43 @@ if ENV['RAKE']
|
|
39
39
|
require 'minitest/retry'
|
40
40
|
Minitest::Retry.use!(methods_to_skip: [])
|
41
41
|
end
|
42
|
+
|
43
|
+
# Primitive array.
|
44
|
+
class Primitivo
|
45
|
+
def initialize(array)
|
46
|
+
@array = array
|
47
|
+
end
|
48
|
+
|
49
|
+
def to_a
|
50
|
+
@array.to_a
|
51
|
+
end
|
52
|
+
|
53
|
+
def append(item)
|
54
|
+
@array.append(item)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Test.
|
59
|
+
# Author:: Yegor Bugayenko (yegor256@gmail.com)
|
60
|
+
# Copyright:: Copyright (c) 2025 Yegor Bugayenko
|
61
|
+
# License:: MIT
|
62
|
+
class Minitest::Test
|
63
|
+
def loog
|
64
|
+
ENV['RAKE'] ? Loog::ERRORS : Loog::VERBOSE
|
65
|
+
end
|
66
|
+
|
67
|
+
def wait_for
|
68
|
+
start = Time.now
|
69
|
+
loop do
|
70
|
+
sleep(0.1)
|
71
|
+
break if yield
|
72
|
+
raise 'timeout' if Time.now - start > 60
|
73
|
+
rescue Errno::ECONNREFUSED
|
74
|
+
retry
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def wait_for_port(port)
|
79
|
+
wait_for { Typhoeus::Request.get("http://localhost:#{port}").code == 200 }
|
80
|
+
end
|
81
|
+
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.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yegor Bugayenko
|
@@ -123,8 +123,10 @@ files:
|
|
123
123
|
- hardhat/package.json
|
124
124
|
- lib/erc20.rb
|
125
125
|
- lib/erc20/erc20.rb
|
126
|
+
- lib/erc20/fake_wallet.rb
|
126
127
|
- lib/erc20/wallet.rb
|
127
128
|
- renovate.json
|
129
|
+
- test/erc20/test_fake_wallet.rb
|
128
130
|
- test/erc20/test_wallet.rb
|
129
131
|
- test/test__helper.rb
|
130
132
|
homepage: http://github.com/yegor256/erc20.rb
|