Class: OpenAssets::Protocol::TransactionOutput

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/openassets/protocol/transaction_output.rb

Overview

Represents a transaction output and its asset ID and asset quantity.

Constant Summary

Constants included from Util

Util::OA_NAMESPACE, Util::OA_VERSION_BYTE, Util::OA_VERSION_BYTE_TESTNET

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#address_to_asset_id, #address_to_oa_address, #coin_to_satoshi, #decode_leb128, #encode_leb128, #generate_asset_id, #oa_address_to_address, #oa_address_to_asset_id, #pubkey_hash_to_asset_id, #satoshi_to_coin, #script_to_address, #to_bytes, #validate_address

Constructor Details

#initialize(value, script, asset_id = nil, asset_quantity = 0, output_type = OutputType::UNCOLORED, metadata = '') ⇒ TransactionOutput

Returns a new instance of TransactionOutput.

Parameters:

  • value (Integer) —

    The satoshi value of the output.

  • script (Bitcoin::Script) —

    The script controlling redemption of the output.

  • asset_id (String) (defaults to: nil) —

    The asset ID of the output.

  • asset_quantity (Integer) (defaults to: 0) —

    The asset quantity of the output.

  • output_type (OpenAssets::Transaction::OutPutType) (defaults to: OutputType::UNCOLORED) —

    The type of the output.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/openassets/protocol/transaction_output.rb', line 26

def initialize(value, script, asset_id = nil, asset_quantity = 0, output_type = OutputType::UNCOLORED,  = '')
  raise ArgumentError, "invalid output_type : #{output_type}" unless OutputType.all.include?(output_type)
  raise ArgumentError, "invalid asset_quantity. asset_quantity should be unsignd integer. " unless asset_quantity.between?(0, MarkerOutput::MAX_ASSET_QUANTITY)
  @value = value
  @script = script
  @asset_id = asset_id
  @asset_quantity = asset_quantity
  @output_type = output_type
  @metadata = 
  load_asset_definition_url
end

Instance Attribute Details

#account ⇒ Object

Returns the value of attribute account.



15
16
17
# File 'lib/openassets/protocol/transaction_output.rb', line 15

def 
  @account
end

#asset_definition ⇒ Object

Returns the value of attribute asset_definition.



18
19
20
# File 'lib/openassets/protocol/transaction_output.rb', line 18

def asset_definition
  @asset_definition
end

#asset_definition_url ⇒ Object

Returns the value of attribute asset_definition_url.



17
18
19
# File 'lib/openassets/protocol/transaction_output.rb', line 17

def asset_definition_url
  @asset_definition_url
end

#asset_id ⇒ Object

Returns the value of attribute asset_id.



11
12
13
# File 'lib/openassets/protocol/transaction_output.rb', line 11

def asset_id
  @asset_id
end

#asset_quantity ⇒ Object

Returns the value of attribute asset_quantity.



12
13
14
# File 'lib/openassets/protocol/transaction_output.rb', line 12

def asset_quantity
  @asset_quantity
end

#metadata ⇒ Object

Returns the value of attribute metadata.



16
17
18
# File 'lib/openassets/protocol/transaction_output.rb', line 16

def 
  @metadata
end

#output_type ⇒ Object

Returns the value of attribute output_type.



13
14
15
# File 'lib/openassets/protocol/transaction_output.rb', line 13

def output_type
  @output_type
end

#script ⇒ Object

Returns the value of attribute script.



10
11
12
# File 'lib/openassets/protocol/transaction_output.rb', line 10

def script
  @script
end

#value ⇒ Object

Returns the value of attribute value.



9
10
11
# File 'lib/openassets/protocol/transaction_output.rb', line 9

def value
  @value
end

Instance Method Details

#asset_amount ⇒ Object

calculate asset amount. asset amount is the value obtained by converting the asset quantity to the unit of divisibility that are defined in the Asset definition file.



40
41
42
43
# File 'lib/openassets/protocol/transaction_output.rb', line 40

def asset_amount
  d = divisibility
  d > 0 ? (@asset_quantity.to_f / (10 ** d)).to_f : @asset_quantity
end

#divisibility ⇒ Object

get divisibility defined by asset definition file.



46
47
48
49
# File 'lib/openassets/protocol/transaction_output.rb', line 46

def divisibility
  return 0 if !valid_asset_definition? || @asset_definition.divisibility.nil?
  @asset_definition.divisibility
end

#to_hash ⇒ Object

convert to hash object.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/openassets/protocol/transaction_output.rb', line 52

def to_hash
  address = script_to_address(@script)
  {
      'address' =>  address,
      'oa_address' => address.nil? ? nil : address_to_oa_address(address),
      'script' => @script.to_payload.bth,
      'amount' => satoshi_to_coin(@value),
      'asset_id' => @asset_id,
      'asset_quantity' => @asset_quantity.to_s,
      'asset_amount' => asset_amount.to_s,
      'account' => @account,
      'asset_definition_url' => @asset_definition_url
  }
end