Class: Glueby::UtxoProvider

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUtxoProvider

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

.configObject (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

#addressObject (readonly)

Returns the value of attribute address.



31
32
33
# File 'lib/glueby/utxo_provider.rb', line 31

def address
  @address
end

#fee_estimatorObject (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_manageObject (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

#walletObject (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

Parameters:

  • config (Hash)
  • opts (Hash)

    a customizable set of options

Options Hash (config):

  • :default_value (Integer)


19
20
21
# File 'lib/glueby/utxo_provider.rb', line 19

def configure(config)
  @config = config
end

Instance Method Details

#change_addressObject



89
90
91
# File 'lib/glueby/utxo_provider.rb', line 89

def change_address
  wallet.change_address
end

#current_utxo_pool_sizeObject



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_valueObject



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.

Parameters:

  • tx (Tapyrus::Tx)

    The tx that will be filled the inputs

  • target_amount (Integer)

    The tapyrus amount the tx is expected to be added in this method

  • current_amount (Integer) (defaults to: 0)

    The tapyrus amount the tx already has in its inputs

  • fee_estimator (Glueby::Contract::FeeEstimator) (defaults to: Contract::FeeEstimator::Fixed.new)

Returns:

  • (Tapyrus::Tx)

    tx The tx that is added inputs

  • (Integer)

    fee The final fee after the inputs are filled

  • (Integer)

    current_amount The final amount of the tx inputs

  • (Array<Hash>)

    provided_utxos The utxos that are added to the tx inputs



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

Parameters:

  • script_pubkey (Tapyrus::Script)

    The script to be provided

  • value (Integer) (defaults to: DEFAULT_VALUE)

    The tpc amount to be provided

Returns:

  • (Array<(Tapyrus::Tx, Integer)>)

    The tx that has a UTXO to be provided in its outputs. The output index in the tx to indicate the place of a provided UTXO.

Raises:



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_amountObject



111
112
113
# File 'lib/glueby/utxo_provider.rb', line 111

def tpc_amount
  wallet.balance(false)
end

#utxo_pool_sizeObject



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_poolObject



125
126
127
# File 'lib/glueby/utxo_provider.rb', line 125

def value_to_fill_utxo_pool
  default_value * utxo_pool_size
end