Class: Glueby::Internal::ContractBuilder

Inherits:
Tapyrus::TxBuilder
  • Object
show all
Defined in:
lib/glueby/internal/contract_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sender_wallet:, fee_estimator: :auto, use_auto_fulfill_inputs: false, use_unfinalized_utxo: false) ⇒ ContractBuilder

Returns a new instance of ContractBuilder.

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/glueby/internal/contract_builder.rb', line 18

def initialize(
  sender_wallet:,
  fee_estimator: :auto,
  use_auto_fulfill_inputs: false,
  use_unfinalized_utxo: false
)
  @sender_wallet = sender_wallet
  set_fee_estimator(fee_estimator)
  @use_auto_fulfill_inputs = use_auto_fulfill_inputs
  @use_unfinalized_utxo = use_unfinalized_utxo
  @p2c_utxos = []
  @prev_txs = []
  @change_script_pubkeys = {}
  @burn_contract = false
  @issues = Hash.new(0)
  super()
end

Instance Attribute Details

#fee_estimatorObject (readonly)

Returns the value of attribute fee_estimator.



4
5
6
# File 'lib/glueby/internal/contract_builder.rb', line 4

def fee_estimator
  @fee_estimator
end

#p2c_utxosObject (readonly)

Returns the value of attribute p2c_utxos.



4
5
6
# File 'lib/glueby/internal/contract_builder.rb', line 4

def p2c_utxos
  @p2c_utxos
end

#prev_txsObject (readonly)

Returns the value of attribute prev_txs.



4
5
6
# File 'lib/glueby/internal/contract_builder.rb', line 4

def prev_txs
  @prev_txs
end

#sender_walletObject (readonly)

Returns the value of attribute sender_wallet.



4
5
6
# File 'lib/glueby/internal/contract_builder.rb', line 4

def sender_wallet
  @sender_wallet
end

#use_auto_fulfill_inputsObject (readonly)

Returns the value of attribute use_auto_fulfill_inputs.



4
5
6
# File 'lib/glueby/internal/contract_builder.rb', line 4

def use_auto_fulfill_inputs
  @use_auto_fulfill_inputs
end

#use_unfinalized_utxoObject (readonly)

Returns the value of attribute use_unfinalized_utxo.



4
5
6
# File 'lib/glueby/internal/contract_builder.rb', line 4

def use_unfinalized_utxo
  @use_unfinalized_utxo
end

Instance Method Details

#add_p2c_utxo_to!(metadata:, amount:, p2c_address: nil, payment_base: nil, only_finalized: use_only_finalized_utxo, fee_estimator: nil) ⇒ Object

Add an UTXO which is sent to the pay-to-contract address which is generated from the metadata



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/glueby/internal/contract_builder.rb', line 178

def add_p2c_utxo_to!(
  metadata:,
  amount:,
  p2c_address: nil,
  payment_base: nil,
  only_finalized: use_only_finalized_utxo,
  fee_estimator: nil
)
  if p2c_address.nil? || payment_base.nil?
    p2c_address, payment_base = sender_wallet
                                  .create_pay_to_contract_address()
  end

  add_utxo_to!(
    address: p2c_address,
    amount: amount,
    only_finalized: only_finalized,
    fee_estimator: fee_estimator
  )
  @p2c_utxos << to_p2c_sign_tx_utxo_hash(@utxos.last)
                  .merge({
                    p2c_address: p2c_address,
                    payment_base: payment_base,
                    metadata: 
                  })
  self
end

#add_utxo(utxo) ⇒ Object

Add utxo to the transaction

Options Hash (utxo):

  • :txid (String)

    The txid

  • :vout (Integer)

    The index of the output in the tx

  • :amount (Integer)

    The value of the output

  • :script_pubkey (String)

    The hex string of the script pubkey

  • :color_id (String)

    The color id hex string of the output



109
110
111
112
# File 'lib/glueby/internal/contract_builder.rb', line 109

def add_utxo(utxo)
  super(to_tapyrusrb_utxo_hash(utxo))
  self
end

#add_utxo_to!(address:, amount:, utxo_provider: nil, only_finalized: use_only_finalized_utxo, fee_estimator: nil) ⇒ Object

Add an UTXO which is sent to the address If the configuration is set to use UTXO provider, the UTXO is provided by the UTXO provider. Otherwise, the UTXO is provided by the wallet. In this case, the address parameter is ignored. This method creates and broadcasts a transaction that sends the amount to the address and add the UTXO to the transaction.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/glueby/internal/contract_builder.rb', line 124

def add_utxo_to!(
  address:,
  amount:,
  utxo_provider: nil,
  only_finalized: use_only_finalized_utxo,
  fee_estimator: nil
)
  tx, index = nil

  Glueby::AR.transaction(isolation: :read_committed) do
    if Glueby.configuration.use_utxo_provider? || utxo_provider
      utxo_provider ||= UtxoProvider.new
      script_pubkey = Tapyrus::Script.parse_from_addr(address)
      tx, index = utxo_provider.get_utxo(script_pubkey, amount)
    else
      fee_estimator ||= @fee_estimator
      txb = Tapyrus::TxBuilder.new
      fee = fee_estimator.fee(Contract::FeeEstimator.dummy_tx(txb.build))
      _sum, utxos = sender_wallet
                     .collect_uncolored_outputs(fee + amount, nil, only_finalized)
      utxos.each { |utxo| txb.add_utxo(to_tapyrusrb_utxo_hash(utxo)) }
      tx = txb.pay(address, amount)
              .change_address(sender_wallet.change_address)
              .fee(fee)
              .build
      sender_wallet.sign_tx(tx)
      index = 0
    end

    # Here needs to use the return tx from Internal::Wallet#broadcast because the txid
    # is changed if you enable FeeProvider.
    tx = sender_wallet.broadcast(tx)
  end

  @prev_txs << tx

  add_utxo({
    script_pubkey: tx.outputs[index].script_pubkey.to_hex,
    txid: tx.txid,
    vout: index,
    amount: tx.outputs[index].value
  })
  self
end

#buildObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/glueby/internal/contract_builder.rb', line 207

def build
  auto_fulfill_inputs_utxos_for_color if use_auto_fulfill_inputs

  tx = Tapyrus::Tx.new
  set_tpc_change_address
  expand_input(tx)
  @outputs.each { |output| tx.outputs << output }
  add_change_for_colored_coin(tx)


  change, provided_utxos = auto_fulfill_inputs_utxos_for_tpc(tx) if use_auto_fulfill_inputs
  add_change_for_tpc(tx, change)

  add_dummy_output(tx)
  sign(tx, provided_utxos)
end

#burn(value, color_id) ⇒ Object

Burn token

Raises:



93
94
95
96
97
98
99
100
# File 'lib/glueby/internal/contract_builder.rb', line 93

def burn(value, color_id)
  raise Glueby::ArgumentError, 'Burn TPC is not supported.' if color_id.default?

  @burn_contract = true
  @outgoings[color_id] ||= 0
  @outgoings[color_id] += value
  self
end

#change_address(address, color_id = Tapyrus::Color::ColorIdentifier.default) ⇒ Object



224
225
226
227
228
229
230
231
232
233
# File 'lib/glueby/internal/contract_builder.rb', line 224

def change_address(address, color_id = Tapyrus::Color::ColorIdentifier.default)
  if color_id.default?
    super(address)
  else
    script_pubkey = Tapyrus::Script.parse_from_addr(address)
    raise ArgumentError, 'invalid address' if !script_pubkey.p2pkh? && !script_pubkey.p2sh?
    @change_script_pubkeys[color_id] = script_pubkey.add_color(color_id)
  end
  self
end

#dummy_feeObject



237
238
239
# File 'lib/glueby/internal/contract_builder.rb', line 237

def dummy_fee
  fee_estimator.fee(Contract::FeeEstimator.dummy_tx(original_build))
end

#nft(out_point, address) ⇒ Object

Issue NFT

Raises:



82
83
84
85
86
87
# File 'lib/glueby/internal/contract_builder.rb', line 82

def nft(out_point, address)
  color_id = Tapyrus::Color::ColorIdentifier.nft(out_point)
  raise Glueby::ArgumentError, 'NFT is already issued.' if @issues[color_id] == 1
  @issues[color_id] = 1
  super
end

#non_reissuable(out_point, address, value) ⇒ Object

Issue non reissuable token



61
62
63
64
65
# File 'lib/glueby/internal/contract_builder.rb', line 61

def non_reissuable(out_point, address, value)
  color_id = Tapyrus::Color::ColorIdentifier.non_reissuable(out_point)
  @issues[color_id] += value
  super
end

#non_reissuable_split(out_point, address, value, split) ⇒ Object

Issue non-reissuable token to the split outputs



72
73
74
75
76
# File 'lib/glueby/internal/contract_builder.rb', line 72

def non_reissuable_split(out_point, address, value, split)
  split_value(value, split) do |value|
    non_reissuable(out_point, address, value)
  end
end

#original_buildObject



206
# File 'lib/glueby/internal/contract_builder.rb', line 206

alias :original_build :build

#reissuable(script_pubkey, address, value) ⇒ Object

Issue reissuable token



40
41
42
43
44
# File 'lib/glueby/internal/contract_builder.rb', line 40

def reissuable(script_pubkey, address, value)
  color_id = Tapyrus::Color::ColorIdentifier.reissuable(script_pubkey)
  @issues[color_id] += value
  super
end

#reissuable_split(script_pubkey, address, value, split) ⇒ Object

Issue reissuable token to the split outputs



51
52
53
54
55
# File 'lib/glueby/internal/contract_builder.rb', line 51

def reissuable_split(script_pubkey, address, value, split)
  split_value(value, split) do |value|
    reissuable(script_pubkey, address, value)
  end
end