Class: ERC20::FakeWallet

Inherits:
Object
  • Object
show all
Defined in:
lib/erc20/fake_wallet.rb

Overview

A fake wallet that behaves like a ERC20::Wallet.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2025 Yegor Bugayenko

License

MIT

Constant Summary collapse

TXN_HASH =

Transaction hash always returned:

'0x172de9cda30537eae68ab4a96163ebbb8f8a85293b8737dd2e5deb4714b14623'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFakeWallet

Ctor.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/erc20/fake_wallet.rb', line 25

def initialize
  @host = 'example.com'
  @port = 443
  @ssl = true
  @chain = 1
  @contract = ERC20::Wallet::USDT
  @ws_path = '/'
  @http_path = '/'
  @history = []
  @balances = {}
  @eth_balances = {}
end

Instance Attribute Details

#chainObject (readonly)

Fakes:



19
20
21
# File 'lib/erc20/fake_wallet.rb', line 19

def chain
  @chain
end

#contractObject (readonly)

Fakes:



19
20
21
# File 'lib/erc20/fake_wallet.rb', line 19

def contract
  @contract
end

#historyObject (readonly)

Full history of all method calls:



22
23
24
# File 'lib/erc20/fake_wallet.rb', line 22

def history
  @history
end

#hostObject (readonly)

Fakes:



19
20
21
# File 'lib/erc20/fake_wallet.rb', line 19

def host
  @host
end

#http_pathObject (readonly)

Fakes:



19
20
21
# File 'lib/erc20/fake_wallet.rb', line 19

def http_path
  @http_path
end

#portObject (readonly)

Fakes:



19
20
21
# File 'lib/erc20/fake_wallet.rb', line 19

def port
  @port
end

#sslObject (readonly)

Fakes:



19
20
21
# File 'lib/erc20/fake_wallet.rb', line 19

def ssl
  @ssl
end

#ws_pathObject (readonly)

Fakes:



19
20
21
# File 'lib/erc20/fake_wallet.rb', line 19

def ws_path
  @ws_path
end

Instance Method Details

#accept(addresses, active = [], raw: false, delay: 1) ⇒ Object

Wait and accept.

Parameters:

  • addresses (Array<String>)

    Addresses to monitor

  • active (Array) (defaults to: [])

    List of addresses that we are actually listening to

  • raw (Boolean) (defaults to: false)

    TRUE if you need to get JSON events as they arrive from Websockets

  • delay (Integer) (defaults to: 1)

    How many seconds to wait between eth_subscribe calls



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/erc20/fake_wallet.rb', line 122

def accept(addresses, active = [], raw: false, delay: 1)
  @history << { method: :accept, addresses:, active:, raw:, delay: }
  addresses.to_a.each { |a| active.append(a) }
  loop do
    sleep(delay)
    a = addresses.to_a.sample
    next if a.nil?
    event =
      if raw
        {}
      else
        {
          amount: 424_242,
          from: '0xd5ff1bfcde7a03da61ad229d962c74f1ea2f16a5',
          to: a,
          txn: TXN_HASH
        }
      end
    yield event
  end
end

#balance(address) ⇒ Integer

Get ERC20 balance of a public address.

Parameters:

  • address (String)

    Public key, in hex, starting from ‘0x’

Returns:

  • (Integer)

    Balance, in tokens



56
57
58
59
60
# File 'lib/erc20/fake_wallet.rb', line 56

def balance(address)
  b = @balances[address] || 42_000_000
  @history << { method: :balance, address:, result: b }
  b
end

#eth_balance(address) ⇒ Integer

Get ETH balance of a public address.

Parameters:

  • address (String)

    Public key, in hex, starting from ‘0x’

Returns:

  • (Integer)

    Balance, in tokens



66
67
68
69
70
# File 'lib/erc20/fake_wallet.rb', line 66

def eth_balance(address)
  b = @eth_balances[address] || 77_000_000_000_000_000
  @history << { method: :eth_balance, address:, result: b }
  b
end

#eth_pay(priv, address, amount, gas_price: nil) ⇒ String

Send a single ETH payment from a private address to a public one.

Parameters:

  • priv (String)

    Private key, in hex

  • address (String)

    Public key, in hex

  • amount (Integer)

    The amount of ETHs to send

Returns:

  • (String)

    Transaction hash



110
111
112
113
114
# File 'lib/erc20/fake_wallet.rb', line 110

def eth_pay(priv, address, amount, gas_price: nil)
  hex = TXN_HASH
  @history << { method: :eth_pay, priv:, address:, amount:, gas_price:, result: hex }
  hex
end

#gas_estimate(from, to, amount) ⇒ Integer

How much gas units is required in order to send ERC20 transaction.

Parameters:

  • from (String)

    The departing address, in hex

  • to (String)

    Arriving address, in hex

  • amount (Integer)

    How many ERC20 tokens to send

Returns:

  • (Integer)

    How many gas units required



78
79
80
81
82
# File 'lib/erc20/fake_wallet.rb', line 78

def gas_estimate(from, to, amount)
  gas = 66_000
  @history << { method: :gas_estimate, from:, to:, amount:, result: gas }
  gas
end

#gas_priceInteger

What is the price of gas unit in gwei?

Returns:

  • (Integer)

    Price of gas unit, in gwei (0.000000001 ETH)



86
87
88
89
90
# File 'lib/erc20/fake_wallet.rb', line 86

def gas_price
  gwei = 55_555
  @history << { method: :gas_price, result: gwei }
  gwei
end

#pay(priv, address, amount, gas_limit: nil, gas_price: nil) ⇒ String

Send a single ERC20 payment from a private address to a public one.

Parameters:

  • _priv (String)

    Private key, in hex

  • _address (String)

    Public key, in hex

  • _amount (Integer)

    The amount of ERC20 tokens to send

Returns:

  • (String)

    Transaction hash



98
99
100
101
102
# File 'lib/erc20/fake_wallet.rb', line 98

def pay(priv, address, amount, gas_limit: nil, gas_price: nil)
  hex = TXN_HASH
  @history << { method: :pay, priv:, address:, amount:, gas_limit:, gas_price:, result: hex }
  hex
end

#set_balance(address, tokens) ⇒ Object

Set balance, to be returned by the balance().

Parameters:

  • address (String)

    Public key, in hex, starting from ‘0x’

  • tokens (Integer)

    How many tokens to put there



41
42
43
# File 'lib/erc20/fake_wallet.rb', line 41

def set_balance(address, tokens)
  @balances[address] = tokens
end

#set_eth_balance(address, wei) ⇒ Object

Set balance, to be returned by the balance().

Parameters:

  • address (String)

    Public key, in hex, starting from ‘0x’

  • wei (Integer)

    How many wei to put there



48
49
50
# File 'lib/erc20/fake_wallet.rb', line 48

def set_eth_balance(address, wei)
  @eth_balances[address] = wei
end