Class: Glueby::UtxoProvider
- Inherits:
-
Object
- Object
- Glueby::UtxoProvider
- Defined in:
- lib/glueby/utxo_provider.rb,
lib/glueby/utxo_provider/tasks.rb
Defined Under Namespace
Classes: Tasks
Constant Summary collapse
- WALLET_ID =
'UTXO_PROVIDER_WALLET'
- DEFAULT_VALUE =
1_000
- DEFAULT_UTXO_POOL_SIZE =
20
- MAX_UTXO_POOL_SIZE =
2_000
Class Attribute Summary collapse
-
.config ⇒ Object
readonly
Returns the value of attribute config.
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#fee_estimator ⇒ Object
readonly
Returns the value of attribute fee_estimator.
-
#fee_estimator_for_manage ⇒ Object
readonly
Returns the value of attribute fee_estimator_for_manage.
-
#wallet ⇒ Object
readonly
Returns the value of attribute wallet.
Class Method Summary collapse
Instance Method Summary collapse
- #change_address ⇒ Object
- #current_utxo_pool_size ⇒ Object
- #default_value ⇒ Object
-
#fill_inputs(tx, target_amount:, current_amount: 0, fee_estimator: Contract::FeeEstimator::Fixed.new) ⇒ Tapyrus::Tx, ...
(also: #fill_uncolored_inputs)
Fill inputs in the tx up to target_amount of TPC from UTXO pool This method should be called before adding change output and script_sig in outputs.
-
#get_utxo(script_pubkey, value = DEFAULT_VALUE) ⇒ Array<(Tapyrus::Tx, Integer)>
Provide a UTXO.
-
#initialize ⇒ UtxoProvider
constructor
A new instance of UtxoProvider.
- #tpc_amount ⇒ Object
- #utxo_pool_size ⇒ Object
- #value_to_fill_utxo_pool ⇒ Object
Constructor Details
#initialize ⇒ UtxoProvider
Returns a new instance of UtxoProvider.
24 25 26 27 28 29 |
# File 'lib/glueby/utxo_provider.rb', line 24 def initialize @wallet = load_wallet validate_config! @fee_estimator = (UtxoProvider.config && UtxoProvider.config[:fee_estimator]) || Glueby::Contract::FeeEstimator::Fixed.new @fee_estimator_for_manage = UtxoProvider.config && UtxoProvider.config[:fee_estimator_for_manage] || @fee_estimator end |
Class Attribute Details
.config ⇒ Object (readonly)
Returns the value of attribute config.
11 12 13 |
# File 'lib/glueby/utxo_provider.rb', line 11 def config @config end |
Instance Attribute Details
#address ⇒ Object (readonly)
Returns the value of attribute address.
31 32 33 |
# File 'lib/glueby/utxo_provider.rb', line 31 def address @address end |
#fee_estimator ⇒ Object (readonly)
Returns the value of attribute fee_estimator.
31 32 33 |
# File 'lib/glueby/utxo_provider.rb', line 31 def fee_estimator @fee_estimator end |
#fee_estimator_for_manage ⇒ Object (readonly)
Returns the value of attribute fee_estimator_for_manage.
31 32 33 |
# File 'lib/glueby/utxo_provider.rb', line 31 def fee_estimator_for_manage @fee_estimator_for_manage end |
#wallet ⇒ Object (readonly)
Returns the value of attribute wallet.
31 32 33 |
# File 'lib/glueby/utxo_provider.rb', line 31 def wallet @wallet end |
Class Method Details
.configure(config) ⇒ Object
19 20 21 |
# File 'lib/glueby/utxo_provider.rb', line 19 def configure(config) @config = config end |
Instance Method Details
#change_address ⇒ Object
89 90 91 |
# File 'lib/glueby/utxo_provider.rb', line 89 def change_address wallet.change_address end |
#current_utxo_pool_size ⇒ Object
115 116 117 118 119 |
# File 'lib/glueby/utxo_provider.rb', line 115 def current_utxo_pool_size wallet .list_unspent(false) .count { |o| !o[:color_id] && o[:amount] == default_value } end |
#default_value ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/glueby/utxo_provider.rb', line 93 def default_value @default_value ||= ( Glueby::AR::SystemInformation.utxo_provider_default_value || (UtxoProvider.config && UtxoProvider.config[:default_value]) || DEFAULT_VALUE ) end |
#fill_inputs(tx, target_amount:, current_amount: 0, fee_estimator: Contract::FeeEstimator::Fixed.new) ⇒ Tapyrus::Tx, ... Also known as: fill_uncolored_inputs
Fill inputs in the tx up to target_amount of TPC from UTXO pool This method should be called before adding change output and script_sig in outputs. FeeEstimator.dummy_tx returns fee amount to the TX that will be added one TPC input, a change TPC output and script sigs in outputs.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/glueby/utxo_provider.rb', line 76 def fill_inputs(tx, target_amount: , current_amount: 0, fee_estimator: Contract::FeeEstimator::Fixed.new) wallet.fill_uncolored_inputs( tx, target_amount: target_amount, current_amount: current_amount, fee_estimator: fee_estimator ) do |utxo| # It must use only UTXOs that has defalut_value amount. utxo[:amount] == default_value end end |
#get_utxo(script_pubkey, value = DEFAULT_VALUE) ⇒ Array<(Tapyrus::Tx, Integer)>
Provide a UTXO
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/glueby/utxo_provider.rb', line 40 def get_utxo(script_pubkey, value = DEFAULT_VALUE) txb = Tapyrus::TxBuilder.new txb.pay(script_pubkey.addresses.first, value) fee = fee_estimator.fee(Contract::FeeEstimator.dummy_tx(txb.build)) # The outputs need to be shuffled so that no utxos are spent twice as possible. sum, outputs = collect_uncolored_outputs(wallet, fee + value) outputs.each do |utxo| txb.add_utxo({ script_pubkey: Tapyrus::Script.parse_from_payload(utxo[:script_pubkey].htb), txid: utxo[:txid], index: utxo[:vout], value: utxo[:amount] }) end txb.fee(fee).change_address(wallet.change_address) tx = txb.build signed_tx = wallet.sign_tx(tx) [signed_tx, 0] end |
#tpc_amount ⇒ Object
111 112 113 |
# File 'lib/glueby/utxo_provider.rb', line 111 def tpc_amount wallet.balance(false) end |
#utxo_pool_size ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/glueby/utxo_provider.rb', line 102 def utxo_pool_size @utxo_pool_size ||= ( Glueby::AR::SystemInformation.utxo_provider_pool_size || (UtxoProvider.config && UtxoProvider.config[:utxo_pool_size]) || DEFAULT_UTXO_POOL_SIZE ) end |
#value_to_fill_utxo_pool ⇒ Object
125 126 127 |
# File 'lib/glueby/utxo_provider.rb', line 125 def value_to_fill_utxo_pool default_value * utxo_pool_size end |