Class: Tapyrus::TxBuilder

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

Examples:


txb = Tapyrus::TxBuilder.new
utxo1 = {
  script_pubkey: Tapyrus::Script.parse_from_addr('mgCuyNQ1pUbKqL57tJQZX3hhUCaZcuX3RQ'),
  txid: 'e1fb3255ead43dccd3ae0ac2c4f81b32260ca52749936a739669918bbb895411',
  index: 0,
  value: 3_000
}
color_id = Tapyrus::Color::ColorIdentifier.nft(...)
utxo2 = {
  script_pubkey: Tapyrus::Script.parse_from_addr('mu9QMUcB9UCHbQjZJLAuyysQhM9tmFQbPx'),
  color_id: color_id,
  txid: 'e1fb3255ead43dccd3ae0ac2c4f81b32260ca52749936a739669918bbb895411',
  index: 1,
  value: 3_000
}

tx = txb
  .add_utxo(utxo1)
  .add_utxo(utxo2)
  .data("0102030405060a0b0c")
  .reissuable(utxo1[:script_pubkey],'n4jKJN5UMLsAejL1M5CTzQ8npeWoLBLCAH', 10_000)
  .pay('n4jKJN5UMLsAejL1M5CTzQ8npeWoLBLCAH', 1_000)
  .build

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTxBuilder

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

#outputsObject (readonly)

Returns the value of attribute outputs.



34
35
36
# File 'lib/tapyrus/tx_builder.rb', line 34

def outputs
  @outputs
end

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

Parameters:

  • utxo (Hash)

    a hash whose fields are ‘txid`, `index`, `script_pubkey`, `value`, and `color_id` (color_id is optional)



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

#buildObject

Build transaction



128
129
130
131
132
133
134
# File 'lib/tapyrus/tx_builder.rb', line 128

def build
  tx = Tapyrus::Tx.new
  expand_input(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.

Parameters:

  • address (String)

    p2pkh or p2sh address.

Raises:

  • (ArgumentError)


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

Parameters:

  • contents ([String])

    array of hex string



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.

Parameters:

  • fee (Integer)

    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

Parameters:



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

Parameters:



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.

Parameters:

Raises:

  • (ArgumentError)


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

Parameters:

  • script_pubkey (Tapyrus::Script)

    the script pubkey in the issue input.

  • address (String)

    p2pkh or p2sh address.

  • value (Integer)

    issued amount.



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