Class: Runar::SDK::MockProvider
- Defined in:
- lib/runar/sdk/provider.rb
Overview
In-memory provider for unit tests and local development.
Pre-populate with UTXOs and transactions, then inspect broadcasted transactions after the fact.
provider = Runar::SDK::MockProvider.new
provider.add_utxo('myAddress', Runar::SDK::Utxo.new(txid: 'abc...', ...))
provider.get_utxos('myAddress') # => [<Utxo>]
Constant Summary collapse
- DEFAULT_FEE_RATE =
100
Instance Method Summary collapse
-
#add_contract_utxo(script_hash, utxo) ⇒ Object
Register a UTXO under a script hash for stateful contract lookup.
-
#add_transaction(tx) ⇒ Object
Register a transaction for later retrieval.
-
#add_utxo(address, utxo) ⇒ Object
Register a UTXO under an address.
- #broadcast(raw_tx) ⇒ Object
-
#get_broadcasted_txs ⇒ Object
Return a copy of all raw transaction hexes that have been broadcasted.
- #get_contract_utxo(script_hash) ⇒ Object
- #get_fee_rate ⇒ Object
- #get_network ⇒ Object
- #get_raw_transaction(txid) ⇒ Object
-
#get_transaction(txid) ⇒ Object
– Provider interface ————————————————-.
- #get_utxos(address) ⇒ Object
-
#initialize(network: 'testnet') ⇒ MockProvider
constructor
A new instance of MockProvider.
-
#set_fee_rate(rate) ⇒ Object
Override the fee rate (default: 100 sat/KB).
Constructor Details
#initialize(network: 'testnet') ⇒ MockProvider
Returns a new instance of MockProvider.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/runar/sdk/provider.rb', line 66 def initialize(network: 'testnet') @transactions = {} @utxos = {} @contract_utxos = {} @broadcasted_txs = [] @raw_transactions = {} @broadcast_count = 0 @network = network @fee_rate = DEFAULT_FEE_RATE end |
Instance Method Details
#add_contract_utxo(script_hash, utxo) ⇒ Object
Register a UTXO under a script hash for stateful contract lookup.
91 92 93 |
# File 'lib/runar/sdk/provider.rb', line 91 def add_contract_utxo(script_hash, utxo) @contract_utxos[script_hash] = utxo end |
#add_transaction(tx) ⇒ Object
Register a transaction for later retrieval.
80 81 82 |
# File 'lib/runar/sdk/provider.rb', line 80 def add_transaction(tx) @transactions[tx.txid] = tx end |
#add_utxo(address, utxo) ⇒ Object
Register a UTXO under an address.
85 86 87 88 |
# File 'lib/runar/sdk/provider.rb', line 85 def add_utxo(address, utxo) @utxos[address] ||= [] @utxos[address] << utxo end |
#broadcast(raw_tx) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/runar/sdk/provider.rb', line 125 def broadcast(raw_tx) @broadcasted_txs << raw_tx @broadcast_count += 1 fake_txid = mock_hash64("mock-broadcast-#{@broadcast_count}-#{raw_tx[0, 16]}") # Auto-store raw hex so get_raw_transaction works without an explicit add_transaction call. @raw_transactions[fake_txid] = raw_tx fake_txid end |
#get_broadcasted_txs ⇒ Object
Return a copy of all raw transaction hexes that have been broadcasted.
101 102 103 |
# File 'lib/runar/sdk/provider.rb', line 101 def get_broadcasted_txs @broadcasted_txs.dup end |
#get_contract_utxo(script_hash) ⇒ Object
138 139 140 |
# File 'lib/runar/sdk/provider.rb', line 138 def get_contract_utxo(script_hash) @contract_utxos[script_hash] end |
#get_fee_rate ⇒ Object
146 147 148 |
# File 'lib/runar/sdk/provider.rb', line 146 def get_fee_rate @fee_rate end |
#get_network ⇒ Object
142 143 144 |
# File 'lib/runar/sdk/provider.rb', line 142 def get_network @network end |
#get_raw_transaction(txid) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/runar/sdk/provider.rb', line 114 def get_raw_transaction(txid) # Return auto-stored raw hex from a previous broadcast first. return @raw_transactions[txid] if @raw_transactions.key?(txid) tx = @transactions[txid] raise "MockProvider: transaction #{txid} not found" unless tx raise "MockProvider: transaction #{txid} has no raw hex" if tx.raw.to_s.empty? tx.raw end |
#get_transaction(txid) ⇒ Object
– Provider interface ————————————————-
107 108 109 110 111 112 |
# File 'lib/runar/sdk/provider.rb', line 107 def get_transaction(txid) tx = @transactions[txid] raise "MockProvider: transaction #{txid} not found" unless tx tx end |
#get_utxos(address) ⇒ Object
134 135 136 |
# File 'lib/runar/sdk/provider.rb', line 134 def get_utxos(address) Array(@utxos[address]).dup end |
#set_fee_rate(rate) ⇒ Object
Override the fee rate (default: 100 sat/KB).
96 97 98 |
# File 'lib/runar/sdk/provider.rb', line 96 def set_fee_rate(rate) @fee_rate = rate end |