erc20 0.0.9 → 0.0.10

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: c553c2335f6272040af4bd2d361a30a11f0094e6871190799b90688482ad0eda
4
- data.tar.gz: a5adb387763f6303dfccdcddeefa9a190e51c327f42a299fd25588018f9a9f6b
3
+ metadata.gz: 34581e0a63fe07f3910a76e45848204045119efd01aaa0d2478d8c2ab7b2cac2
4
+ data.tar.gz: 21086a1c737f697d7ac6bc8c3b4f21845c104f007daca5c9091dfdf46bdb53e4
5
5
  SHA512:
6
- metadata.gz: 332235aa28a28b38e0d56caf6f223560679b3b2622491cf6351016b6b8e11221f414bd5790d3b393af644b1d6a9c50be807a80126866108afc6c399729c73231
7
- data.tar.gz: d74969a43d49696bfa5dfa04e26f6f0206772fffed76d94843e28189f2586d7552853361387ae102010ae976f90edcca497b1eee45addfdb101d3734641e091a
6
+ metadata.gz: dda126cea1191e735bed9d3ab733c37a42ab0bef1d4566a84fedd0cb4462d9269f974a35256c9367f054e7b2147f657e8d92e267e669e56d24d2691d332a182e
7
+ data.tar.gz: 5b2787b755d7a7d6c1527dd7c0d5b5bf4e824eeeea5b0c9190ad7d2d3899c768ba85b1c1cd93d51c67539e369a0e978bfef296d550996eb2b83e9685b0cdf386
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.9'
45
+ VERSION = '0.0.10'
46
46
  end
@@ -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.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}"
@@ -0,0 +1,74 @@
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 = []
60
+ event = nil
61
+ daemon =
62
+ Thread.new do
63
+ ERC20::FakeWallet.new.accept(['0xfadef8ba4a5d709a2bf55b7a8798c9b438c640c1'], active) do |e|
64
+ event = e
65
+ end
66
+ rescue StandardError => e
67
+ loog.error(Backtrace.new(e))
68
+ end
69
+ wait_for { !active.empty? }
70
+ daemon.kill
71
+ daemon.join(30)
72
+ refute_nil(event)
73
+ end
74
+ end
@@ -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?
data/test/test__helper.rb CHANGED
@@ -39,3 +39,28 @@ if ENV['RAKE']
39
39
  require 'minitest/retry'
40
40
  Minitest::Retry.use!(methods_to_skip: [])
41
41
  end
42
+
43
+ # Test.
44
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
45
+ # Copyright:: Copyright (c) 2025 Yegor Bugayenko
46
+ # License:: MIT
47
+ class Minitest::Test
48
+ def loog
49
+ ENV['RAKE'] ? Loog::ERRORS : Loog::VERBOSE
50
+ end
51
+
52
+ def wait_for
53
+ start = Time.now
54
+ loop do
55
+ sleep(0.1)
56
+ break if yield
57
+ raise 'timeout' if Time.now - start > 60
58
+ rescue Errno::ECONNREFUSED
59
+ retry
60
+ end
61
+ end
62
+
63
+ def wait_for_port(port)
64
+ wait_for { Typhoeus::Request.get("http://localhost:#{port}").code == 200 }
65
+ end
66
+ 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.9
4
+ version: 0.0.10
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