lib/glueby/contract/timestamp/tx_builder/simple.rb in glueby-1.1.2 vs lib/glueby/contract/timestamp/tx_builder/simple.rb in glueby-1.2.0.beta.1
- old
+ new
@@ -2,25 +2,22 @@
module Contract
class Timestamp
module TxBuilder
# The simple Timestamp method
class Simple
- include Glueby::Contract::TxBuilder
-
- attr_reader :funding_tx
-
def initialize(wallet, fee_estimator)
@wallet = wallet
@fee_estimator = fee_estimator
- @txb = Tapyrus::TxBuilder.new
- @prev_txs = []
+ @txb = Internal::ContractBuilder.new(
+ sender_wallet: @wallet.internal_wallet,
+ fee_estimator: @fee_estimator
+ )
end
def build
- @txb.fee(fee).change_address(@wallet.internal_wallet.change_address)
- sign_tx
+ @txb.build
end
def set_data(prefix, data)
@prefix = prefix
@data = data
@@ -33,67 +30,32 @@
self
end
def set_inputs(utxo_provider)
if utxo_provider
- script_pubkey = Tapyrus::Script.parse_from_addr(@wallet.internal_wallet.receive_address)
- @funding_tx, index = utxo_provider.get_utxo(script_pubkey, fee)
-
- utxo = {
- script_pubkey: @funding_tx.outputs[index].script_pubkey.to_hex,
- txid: @funding_tx.txid,
- vout: index,
- amount: funding_tx.outputs[index].value
- }
-
- @txb.add_utxo(to_tapyrusrb_utxo_hash(utxo))
- @prev_txs << to_sign_tx_utxo_hash(utxo)
+ @txb.add_utxo_to!(
+ address: @wallet.internal_wallet.receive_address,
+ amount: input_amount,
+ utxo_provider: utxo_provider
+ )
else
+ fee = input_amount
+ return self if fee == 0
+
_, outputs = @wallet.internal_wallet.collect_uncolored_outputs(fee)
- outputs.each do |utxo|
- @txb.add_utxo(to_tapyrusrb_utxo_hash(utxo))
- end
+ outputs.each { |utxo| @txb.add_utxo(utxo) }
end
self
end
- private
-
- def fee
- @fee ||= @fee_estimator.fee(FeeEstimator.dummy_tx(@txb.build))
+ def funding_tx
+ @txb.prev_txs.first
end
- def sign_tx
- # General signing process skips signing to p2c inputs because no key of the p2c address in the wallet.
- @wallet.internal_wallet.sign_tx(@txb.build, @prev_txs)
- end
+ private
- # @param utxo
- # @option utxo [String] :txid The txid
- # @option utxo [Integer] :vout The index of the output in the tx
- # @option utxo [Integer] :amount The value of the output
- # @option utxo [String] :script_pubkey The hex string of the script pubkey
- def to_tapyrusrb_utxo_hash(utxo)
- {
- script_pubkey: Tapyrus::Script.parse_from_payload(utxo[:script_pubkey].htb),
- txid: utxo[:txid],
- index: utxo[:vout],
- value: utxo[:amount]
- }
- end
-
- # @param utxo
- # @option utxo [String] :txid The txid
- # @option utxo [Integer] :vout The index of the output in the tx
- # @option utxo [Integer] :amount The value of the output
- # @option utxo [String] :script_pubkey The hex string of the script pubkey
- def to_sign_tx_utxo_hash(utxo)
- {
- scriptPubKey: utxo[:script_pubkey],
- txid: utxo[:txid],
- vout: utxo[:vout],
- amount: utxo[:amount]
- }
+ def input_amount
+ @txb.dummy_fee
end
end
end
end
end
\ No newline at end of file