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, Util::OA_VERSION_BYTE_TESTNET
Instance Attribute Summary collapse
-
#amount ⇒ Object
The minimum allowed output value.
-
#efr ⇒ Object
The estimated transaction fee rate (satoshis/KB).
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
-
#burn_asset(unspents, asset_id, fee) ⇒ Object
Create a transaction for burn asset.
-
#initialize(amount = 600, efr = 10000) ⇒ TransactionBuilder
constructor
A new instance of TransactionBuilder.
-
#issue_asset(issue_spec, metadata, fees) ⇒ Object
Creates a transaction for issuing an asset.
-
#transfer_asset(asset_id, asset_transfer_spec, btc_change_script, fees) ⇒ Object
Creates a transaction for sending an asset.
-
#transfer_assets(transfer_specs, btc_transfer_spec, fees) ⇒ Object
Creates a transaction for sending assets to many.
-
#transfer_btc(btc_transfer_spec, fees) ⇒ Object
Creates a transaction for sending bitcoins.
-
#transfer_btcs(btc_transfer_specs, fees) ⇒ Object
Creates a transaction for sending bitcoins to many.
Methods included from Util
#address_to_oa_address, #coin_to_satoshi, #decode_leb128, #encode_leb128, #generate_asset_id, #hash_to_asset_id, #oa_address_to_address, #pubkey_hash_to_asset_id, #read_leb128, #read_var_integer, #satoshi_to_coin, #script_to_asset_id, #to_bytes, #valid_asset_id?, #validate_address
Constructor Details
#initialize(amount = 600, efr = 10000) ⇒ TransactionBuilder
Returns a new instance of TransactionBuilder.
13 14 15 16 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 13 def initialize(amount = 600, efr = 10000) @amount = amount @efr = efr 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 |
#efr ⇒ Object
The estimated transaction fee rate (satoshis/KB).
11 12 13 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 11 def efr @efr 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] unspent_outputs @param asset_id The ID of the asset to collect. @param asset_quantity The asset quantity to collect. @return[Array, int] A list of outputs, and the total asset quantity collected.
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 136 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).
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 118 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
#burn_asset(unspents, asset_id, fee) ⇒ Object
Create a transaction for burn asset
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 87 def burn_asset(unspents, asset_id, fee) tx = Bitcoin::Tx.new targets = unspents.select{|o|o.output.asset_id == asset_id} if fee == :auto # Calculate fees and otsuri (assume that one vout exists) fee = calc_fee(targets.size, 1) end raise TransactionBuildError.new('There is no asset.') if targets.length == 0 total_amount = targets.inject(0){|sum, o|o.output.value + sum} otsuri = total_amount - fee if otsuri < @amount uncolored_outputs, uncolored_amount = TransactionBuilder.collect_uncolored_outputs(unspents, @amount - otsuri) targets = targets + uncolored_outputs otsuri += uncolored_amount end targets.each{|o| tx_in = Bitcoin::TxIn.new(out_point: o.out_point, script_sig: o.output.script) tx.in << tx_in } tx.out << create_uncolored_output(targets[0].output.address, otsuri) tx end |
#issue_asset(issue_spec, metadata, fees) ⇒ Object
Creates a transaction for issuing an asset. @return An unsigned transaction for issuing asset.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 23 def issue_asset(issue_spec, , fees) if fees == :auto # Calculate fees (assume that one vin and four vouts are wrote) fees = calc_fee(1, 4) end inputs, total_amount = TransactionBuilder.collect_uncolored_outputs(issue_spec.unspent_outputs, 2 * @amount + fees) tx = Bitcoin::Tx.new inputs.each { |spendable| tx.in << Bitcoin::TxIn.new(out_point: spendable.out_point, script_sig: spendable.output.script) } 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.out << create_colored_output(issue_address) } tx.out << create_marker_output(asset_quantities, ) tx.out << create_uncolored_output(from_address, total_amount - @amount - fees) tx end |
#transfer_asset(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.
55 56 57 58 59 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 55 def transfer_asset(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_assets(transfer_specs, btc_transfer_spec, fees) ⇒ Object
Creates a transaction for sending assets to many. @param[Array] asset_transfer_spec The parameters of the asset being transferred. @param btc_transfer_spec The script where to send bitcoin change, if any. @param fees The fees to include in the transaction. @return The resulting unsigned transaction.
66 67 68 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 66 def transfer_assets(transfer_specs, btc_transfer_spec, fees) transfer(transfer_specs, [btc_transfer_spec], fees) end |
#transfer_btc(btc_transfer_spec, fees) ⇒ Object
74 75 76 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 74 def transfer_btc(btc_transfer_spec, fees) transfer_btcs([btc_transfer_spec], fees) end |
#transfer_btcs(btc_transfer_specs, fees) ⇒ Object
Creates a transaction for sending bitcoins to many. @param[Array] btc_transfer_specs The parameters of the bitcoins being transferred. @param fees The fees to include in the transaction. @return The resulting unsigned transaction.
82 83 84 |
# File 'lib/openassets/transaction/transaction_builder.rb', line 82 def transfer_btcs(btc_transfer_specs, fees) transfer([], btc_transfer_specs, fees) end |