Class: OpenAssets::Transaction::TransactionBuilder
- Inherits:
-
Object
- Object
- OpenAssets::Transaction::TransactionBuilder
- Includes:
- Util
- Defined in:
- lib/openassets/transaction/transaction_builder.rb
Constant Summary
Constants included from Util
Util::OA_NAMESPACE, Util::OA_VERSION_BYTE
Instance Attribute Summary collapse
-
#amount ⇒ Object
The minimum allowed output value.
Class Method Summary collapse
-
.collect_colored_outputs(unspent_outputs, asset_id, asset_quantity) ⇒ Object
Returns a list of colored outputs for the specified quantity.
-
.collect_uncolored_outputs(unspent_outputs, amount) ⇒ Array
collect uncolored outputs in unspent outputs(contains colored output).
Instance Method Summary collapse
-
#initialize(amount = 600) ⇒ TransactionBuilder
constructor
A new instance of TransactionBuilder.
-
#issue_asset(issue_spec, metadata, fees) ⇒ Object
Creates a transaction for issuing an asset.
-
#transfer_assets(asset_id, asset_transfer_spec, btc_change_script, fees) ⇒ Object
Creates a transaction for sending an asset.
-
#transfer_btc(btc_transfer_spec, fees) ⇒ Object
Creates a transaction for sending bitcoins.
Methods included from Util
#address_to_oa_address, #decode_leb128, #encode_leb128, #generate_asset_id, #oa_address_to_address, #pubkey_hash_to_asset_id, #satoshi_to_coin, #script_to_address, #to_bytes, #validate_address
Constructor Details
#initialize(amount = 600) ⇒ TransactionBuilder
Returns a new instance of TransactionBuilder.
10 11 12 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 10 def initialize(amount = 600) @amount = amount end |
Instance Attribute Details
#amount ⇒ Object
The minimum allowed output value.
8 9 10 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 8 def amount @amount end |
Class Method Details
.collect_colored_outputs(unspent_outputs, asset_id, asset_quantity) ⇒ Object
Returns a list of colored outputs for the specified quantity. @param[Array[OpenAssets::Transaction::SpendableOutput]] unspent_outputs @param asset_id The ID of the asset to collect. @param asset_quantity The asset quantity to collect. @return[Array[OpenAssets::Transaction::SpendableOutput], int] A list of outputs, and the total asset quantity collected.
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 84 def self.collect_colored_outputs(unspent_outputs, asset_id, asset_quantity) total_amount = 0 result = [] unspent_outputs.each do |output| if output.output.asset_id == asset_id result << output total_amount += output.output.asset_quantity end return result, total_amount if total_amount >= asset_quantity end raise InsufficientAssetQuantityError end |
.collect_uncolored_outputs(unspent_outputs, amount) ⇒ Array
collect uncolored outputs in unspent outputs(contains colored output).
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 66 def self.collect_uncolored_outputs(unspent_outputs, amount) total_amount = 0 results = [] unspent_outputs.each do |output| if output.output.asset_id.nil? results << output total_amount += output.output.value end return results, total_amount if total_amount >= amount end raise InsufficientFundsError end |
Instance Method Details
#issue_asset(issue_spec, metadata, fees) ⇒ Object
Creates a transaction for issuing an asset. @return An unsigned transaction for issuing asset.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 19 def issue_asset(issue_spec, , fees) inputs, total_amount = TransactionBuilder.collect_uncolored_outputs(issue_spec.unspent_outputs, 2 * @amount + fees) tx = Bitcoin::Protocol::Tx.new inputs.each { |spendable| script_sig = spendable.output.script.to_binary tx_in = Bitcoin::Protocol::TxIn.from_hex_hash(spendable.out_point.hash, spendable.out_point.index) tx_in.script_sig = script_sig tx.add_in(tx_in) } issue_address = oa_address_to_address(issue_spec.to_script) from_address = oa_address_to_address(issue_spec.change_script) validate_address([issue_address, from_address]) asset_quantities =[] issue_spec.split_output_amount.each{|amount| asset_quantities << amount tx.add_out(create_colored_output(issue_address)) } tx.add_out(create_marker_output(asset_quantities, )) tx.add_out(create_uncolored_output(from_address, total_amount - @amount - fees)) tx end |
#transfer_assets(asset_id, asset_transfer_spec, btc_change_script, fees) ⇒ Object
Creates a transaction for sending an asset. @param asset_id The ID of the asset being sent. @param asset_transfer_spec The parameters of the asset being transferred. @param btc_change_script The script where to send bitcoin change, if any. @param fees The fees to include in the transaction. @return The resulting unsigned transaction.
48 49 50 51 52 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 48 def transfer_assets(asset_id, asset_transfer_spec, btc_change_script, fees) btc_transfer_spec = OpenAssets::Transaction::TransferParameters.new( asset_transfer_spec.unspent_outputs, nil, oa_address_to_address(btc_change_script), 0) transfer([[asset_id, asset_transfer_spec]], btc_transfer_spec, fees) end |
#transfer_btc(btc_transfer_spec, fees) ⇒ Object
58 59 60 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 58 def transfer_btc(btc_transfer_spec, fees) transfer([], btc_transfer_spec, fees) end |