Class: Nem::Transaction::Transfer

Inherits:
Base
  • Object
show all
Defined in:
lib/nem/transaction/transfer.rb

Overview

Constant Summary collapse

TYPE =

257 (transfer transaction)

0x0101

Constants inherited from Base

Base::NETWORK

Instance Attribute Summary collapse

Attributes inherited from Base

#deadline, #fee, #signature, #signer, #timestamp, #type

Instance Method Summary collapse

Methods inherited from Base

#network

Constructor Details

#initialize(recipient, amount, message = '', mosaics: [], timestamp: nil, deadline: nil, network: nil) ⇒ Transfer

Returns a new instance of Transfer.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/nem/transaction/transfer.rb', line 16

def initialize(recipient, amount, message = '', mosaics: [], timestamp: nil, deadline: nil, network: nil)
  @amount = amount
  @recipient = recipient
  @message = message.is_a?(Nem::Model::Message) ?
    message :
    Nem::Model::Message.new(message.to_s)
  @mosaics = mosaics

  @network = network || Nem.default_network
  @type = TYPE
  @fee = Nem::Fee::Transfer.new(self)
  @timestamp = timestamp || Time.now
  @deadline = deadline || Time.now + Nem.default_deadline
end

Instance Attribute Details

#amountInteger

Returns the current value of amount.

Returns:

  • (Integer)

    the current value of amount



11
12
13
# File 'lib/nem/transaction/transfer.rb', line 11

def amount
  @amount
end

#messageNem::Model::Message

Returns the current value of message.

Returns:



11
12
13
# File 'lib/nem/transaction/transfer.rb', line 11

def message
  @message
end

#mosaicsArray <Nem::Model::MosaicId>

Returns the current value of mosaics.

Returns:



11
12
13
# File 'lib/nem/transaction/transfer.rb', line 11

def mosaics
  @mosaics
end

#recipientString

Returns the current value of recipient.

Returns:

  • (String)

    the current value of recipient



11
12
13
# File 'lib/nem/transaction/transfer.rb', line 11

def recipient
  @recipient
end

Instance Method Details

#has_message?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/nem/transaction/transfer.rb', line 32

def has_message?
  message.bytesize > 0
end

#has_mosaics?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/nem/transaction/transfer.rb', line 37

def has_mosaics?
  mosaics.size > 0
end

#to_hashHash

attributes must be CAMEL CASE for NIS params Mosaics need to be sorted by fqn. if not it will occur FAILURE_SIGNATURE_NOT_VERIFIABLE

Returns:

  • (Hash)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/nem/transaction/transfer.rb', line 49

def to_hash
  tmp = {
    amount: (amount * 1_000_000).to_i,
    recipient: recipient,
    message: message.to_hash
  }
  unless mosaics.empty?
    tmp[:mosaics] = mosaics
      .sort_by { |mo| mo.fqn }
      .map do |moa|
      {
        mosaicId: {
          namespaceId: moa.mosaic_id.namespace_id,
          name: moa.mosaic_id.name,
        },
        quantity: moa.amount
      }
    end
  end
  tmp
end

#versionObject



41
42
43
# File 'lib/nem/transaction/transfer.rb', line 41

def version
  network(hex: true) + (has_mosaics? ? 2 : 1)
end