Class: ERC20::FakeWallet
- Inherits:
-
Object
- Object
- ERC20::FakeWallet
- Defined in:
- lib/erc20/fake_wallet.rb
Overview
A fake wallet that behaves like a ERC20::Wallet.
- Author
-
Yegor Bugayenko (yegor256@gmail.com)
- Copyright
-
Copyright © 2025 Yegor Bugayenko
- License
-
MIT
Constant Summary collapse
- TXN_HASH =
'0x172de9cda30537eae68ab4a96163ebbb8f8a85293b8737dd2e5deb4714b14623'
Instance Attribute Summary collapse
-
#chain ⇒ Object
readonly
Returns the value of attribute chain.
-
#contract ⇒ Object
readonly
Returns the value of attribute contract.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#http_path ⇒ Object
readonly
Returns the value of attribute http_path.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#ssl ⇒ Object
readonly
Returns the value of attribute ssl.
-
#ws_path ⇒ Object
readonly
Returns the value of attribute ws_path.
Instance Method Summary collapse
-
#accept(addresses, active = [], raw: false, delay: 1) ⇒ Object
Wait and accept.
-
#balance(address) ⇒ Integer
Get ERC20 balance of a public address.
-
#eth_balance(address) ⇒ Integer
Get ETH balance of a public address.
-
#eth_pay(priv, address, amount, price: nil) ⇒ String
Send a single ETH payment from a private address to a public one.
-
#gas_estimate(from, to, amount) ⇒ Integer
How many gas units are required to send an ERC20 transaction.
-
#gas_price ⇒ Integer
What is the price of gas unit in gwei?.
-
#initialize ⇒ FakeWallet
constructor
Ctor.
-
#pay(priv, address, amount, limit: nil, price: nil) ⇒ String
Send a single ERC20 payment from a private address to a public one.
-
#set_balance(address, tokens) ⇒ Object
Set balance, to be returned by the balance().
-
#set_eth_balance(address, wei) ⇒ Object
Set balance, to be returned by the balance().
-
#sum_of(_txn) ⇒ Integer
Get ERC20 amount (in tokens) that was sent in the given transaction.
Constructor Details
#initialize ⇒ FakeWallet
Ctor.
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/erc20/fake_wallet.rb', line 20 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
#chain ⇒ Object (readonly)
Returns the value of attribute chain.
17 18 19 |
# File 'lib/erc20/fake_wallet.rb', line 17 def chain @chain end |
#contract ⇒ Object (readonly)
Returns the value of attribute contract.
17 18 19 |
# File 'lib/erc20/fake_wallet.rb', line 17 def contract @contract end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
17 18 19 |
# File 'lib/erc20/fake_wallet.rb', line 17 def history @history end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
17 18 19 |
# File 'lib/erc20/fake_wallet.rb', line 17 def host @host end |
#http_path ⇒ Object (readonly)
Returns the value of attribute http_path.
17 18 19 |
# File 'lib/erc20/fake_wallet.rb', line 17 def http_path @http_path end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
17 18 19 |
# File 'lib/erc20/fake_wallet.rb', line 17 def port @port end |
#ssl ⇒ Object (readonly)
Returns the value of attribute ssl.
17 18 19 |
# File 'lib/erc20/fake_wallet.rb', line 17 def ssl @ssl end |
#ws_path ⇒ Object (readonly)
Returns the value of attribute ws_path.
17 18 19 |
# File 'lib/erc20/fake_wallet.rb', line 17 def ws_path @ws_path end |
Instance Method Details
#accept(addresses, active = [], raw: false, delay: 1) ⇒ Object
Wait and accept.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/erc20/fake_wallet.rb', line 128 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? yield( if raw {} else { amount: 424_242, from: '0xd5ff1bfcde7a03da61ad229d962c74f1ea2f16a5', to: a, txn: TXN_HASH } end ) end end |
#balance(address) ⇒ Integer
Get ERC20 balance of a public address.
51 52 53 54 55 |
# File 'lib/erc20/fake_wallet.rb', line 51 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.
61 62 63 64 65 |
# File 'lib/erc20/fake_wallet.rb', line 61 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, price: nil) ⇒ String
Send a single ETH payment from a private address to a public one.
116 117 118 119 120 |
# File 'lib/erc20/fake_wallet.rb', line 116 def eth_pay(priv, address, amount, price: nil) hex = TXN_HASH @history << { method: :eth_pay, priv:, address:, amount:, price:, result: hex } hex end |
#gas_estimate(from, to, amount) ⇒ Integer
How many gas units are required to send an ERC20 transaction.
81 82 83 84 85 |
# File 'lib/erc20/fake_wallet.rb', line 81 def gas_estimate(from, to, amount) gas = 66_000 @history << { method: :gas_estimate, from:, to:, amount:, result: gas } gas end |
#gas_price ⇒ Integer
What is the price of gas unit in gwei?
89 90 91 92 93 |
# File 'lib/erc20/fake_wallet.rb', line 89 def gas_price gwei = 55_555 @history << { method: :gas_price, result: gwei } gwei end |
#pay(priv, address, amount, limit: nil, price: nil) ⇒ String
Send a single ERC20 payment from a private address to a public one.
103 104 105 106 107 |
# File 'lib/erc20/fake_wallet.rb', line 103 def pay(priv, address, amount, limit: nil, price: nil) hex = TXN_HASH @history << { method: :pay, priv:, address:, amount:, limit:, price:, result: hex } hex end |
#set_balance(address, tokens) ⇒ Object
Set balance, to be returned by the balance().
36 37 38 |
# File 'lib/erc20/fake_wallet.rb', line 36 def set_balance(address, tokens) @balances[address] = tokens end |
#set_eth_balance(address, wei) ⇒ Object
Set balance, to be returned by the balance().
43 44 45 |
# File 'lib/erc20/fake_wallet.rb', line 43 def set_eth_balance(address, wei) @eth_balances[address] = wei end |
#sum_of(_txn) ⇒ Integer
Get ERC20 amount (in tokens) that was sent in the given transaction.
71 72 73 |
# File 'lib/erc20/fake_wallet.rb', line 71 def sum_of(_txn) 42_000_000 end |