Class: Tapyrus::TxBuilder
- Inherits:
-
Object
- Object
- Tapyrus::TxBuilder
- Defined in:
- lib/tapyrus/tx_builder.rb
Overview
Transaction Builder class.
TxBuilder makes it easy to build transactions without having to deal with TxOut/TxIn/Script directly.
Instance Attribute Summary collapse
-
#outputs ⇒ Object
readonly
Returns the value of attribute outputs.
-
#utxos ⇒ Object
readonly
Returns the value of attribute utxos.
Instance Method Summary collapse
-
#add_utxo(utxo) ⇒ Object
Add utxo for transaction input.
-
#build ⇒ Object
Build transaction.
-
#change_address(address) ⇒ Object
Set address for change.
-
#data(*contents) ⇒ Object
Create data output.
-
#fee(fee) ⇒ Object
Set transaction fee.
-
#initialize ⇒ TxBuilder
constructor
A new instance of TxBuilder.
-
#nft(out_point, address) ⇒ Object
Issue NFT.
-
#non_reissuable(out_point, address, value) ⇒ Object
Issue non reissuable token.
-
#pay(address, value, color_id = Tapyrus::Color::ColorIdentifier.default) ⇒ Object
Create payment output.
-
#reissuable(script_pubkey, address, value) ⇒ Object
Issue reissuable token.
Constructor Details
#initialize ⇒ TxBuilder
Returns a new instance of TxBuilder.
36 37 38 39 40 41 |
# File 'lib/tapyrus/tx_builder.rb', line 36 def initialize @utxos = [] @incomings = {} @outgoings = {} @outputs = [] end |
Instance Attribute Details
#outputs ⇒ Object (readonly)
Returns the value of attribute outputs.
34 35 36 |
# File 'lib/tapyrus/tx_builder.rb', line 34 def outputs @outputs end |
#utxos ⇒ Object (readonly)
Returns the value of attribute utxos.
34 35 36 |
# File 'lib/tapyrus/tx_builder.rb', line 34 def utxos @utxos end |
Instance Method Details
#add_utxo(utxo) ⇒ Object
Add utxo for transaction input
45 46 47 48 49 50 51 |
# File 'lib/tapyrus/tx_builder.rb', line 45 def add_utxo(utxo) @utxos << utxo color_id = utxo[:color_id] || Tapyrus::Color::ColorIdentifier.default @incomings[color_id] ||= 0 @incomings[color_id] += utxo[:value] self end |
#build ⇒ Object
Build transaction
128 129 130 131 132 133 134 |
# File 'lib/tapyrus/tx_builder.rb', line 128 def build tx = Tapyrus::Tx.new (tx) @outputs.each { |output| tx.outputs << output } add_change(tx) if @change_script_pubkey tx end |
#change_address(address) ⇒ Object
Set address for change. If set, #build method add output for change which has the specified address If not set, transaction built by #build method has no output for change.
120 121 122 123 124 125 |
# File 'lib/tapyrus/tx_builder.rb', line 120 def change_address(address) script_pubkey = Tapyrus::Script.parse_from_addr(address) raise ArgumentError, "invalid address" if !script_pubkey.p2pkh? && !script_pubkey.p2sh? @change_script_pubkey = script_pubkey self end |
#data(*contents) ⇒ Object
Create data output
102 103 104 105 106 107 |
# File 'lib/tapyrus/tx_builder.rb', line 102 def data(*contents) payload = contents.join script = Tapyrus::Script.new << Tapyrus::Script::OP_RETURN << payload @outputs << Tapyrus::TxOut.new(script_pubkey: script) self end |
#fee(fee) ⇒ Object
Set transaction fee.
111 112 113 114 |
# File 'lib/tapyrus/tx_builder.rb', line 111 def fee(fee) @fee = fee self end |
#nft(out_point, address) ⇒ Object
Issue NFT
75 76 77 78 |
# File 'lib/tapyrus/tx_builder.rb', line 75 def nft(out_point, address) color_id = Tapyrus::Color::ColorIdentifier.nft(out_point) pay(address, 1, color_id) end |
#non_reissuable(out_point, address, value) ⇒ Object
Issue non reissuable token
66 67 68 69 |
# File 'lib/tapyrus/tx_builder.rb', line 66 def non_reissuable(out_point, address, value) color_id = Tapyrus::Color::ColorIdentifier.non_reissuable(out_point) pay(address, value, color_id) end |
#pay(address, value, color_id = Tapyrus::Color::ColorIdentifier.default) ⇒ Object
Create payment output.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/tapyrus/tx_builder.rb', line 84 def pay(address, value, color_id = Tapyrus::Color::ColorIdentifier.default) script_pubkey = Tapyrus::Script.parse_from_addr(address) unless color_id.default? raise ArgumentError, "invalid address" if !script_pubkey.p2pkh? && !script_pubkey.p2sh? script_pubkey = script_pubkey.add_color(color_id) end output = Tapyrus::TxOut.new(script_pubkey: script_pubkey, value: value) raise ArgumentError, "The transaction amount is too small" if color_id.default? && output.dust? @outgoings[color_id] ||= 0 @outgoings[color_id] += value @outputs << output self end |
#reissuable(script_pubkey, address, value) ⇒ Object
Issue reissuable token
57 58 59 60 |
# File 'lib/tapyrus/tx_builder.rb', line 57 def reissuable(script_pubkey, address, value) color_id = Tapyrus::Color::ColorIdentifier.reissuable(script_pubkey) pay(address, value, color_id) end |