Class: AppChain::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/appchain/transaction.rb

Defined Under Namespace

Classes: VersionError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(valid_until_block:, chain_id:, nonce: nil, version: 1, to: nil, data: nil, value: "0", quota: 1_000_000) ⇒ void

Parameters:

  • nonce (String) (defaults to: nil)

    default is SecureRandom.hex; if you provide with nil or empty string, it will be assigned a random string.

  • valid_until_block (Integer)
  • chain_id (Integer | String)

    hex string if version == 1

  • version (Integer) (defaults to: 1)
  • to (String) (defaults to: nil)
  • data (String) (defaults to: nil)
  • value: (String | Integer) (defaults to: "0")

    hex string or decimal integer

  • quota (Integer) (defaults to: 1_000_000)

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/appchain/transaction.rb', line 21

def initialize(valid_until_block:, chain_id:, nonce: nil, version: 1, to: nil, data: nil, value: "0", quota: 1_000_000) # rubocop:disable Metrics/ParameterLists
  raise VersionError, "transaction version error, expected 0 or 1, got #{version}" unless [0, 1].include?(version)

  @to = to
  @nonce = nonce.blank? ? SecureRandom.hex : nonce
  @quota = quota
  @valid_until_block = valid_until_block
  @data = data
  @chain_id = if chain_id.is_a?(String)
                chain_id.delete("-")
              else
                chain_id
              end
  @version = version
  @value = if value.is_a?(Integer)
             Utils.to_hex(value)
           else
             value
           end
end

Instance Attribute Details

#chain_idObject

Returns the value of attribute chain_id.



9
10
11
# File 'lib/appchain/transaction.rb', line 9

def chain_id
  @chain_id
end

#dataObject

Returns the value of attribute data.



9
10
11
# File 'lib/appchain/transaction.rb', line 9

def data
  @data
end

#nonceObject

Returns the value of attribute nonce.



9
10
11
# File 'lib/appchain/transaction.rb', line 9

def nonce
  @nonce
end

#quotaObject

Returns the value of attribute quota.



9
10
11
# File 'lib/appchain/transaction.rb', line 9

def quota
  @quota
end

#toObject

Returns the value of attribute to.



9
10
11
# File 'lib/appchain/transaction.rb', line 9

def to
  @to
end

#valid_until_blockObject

Returns the value of attribute valid_until_block.



9
10
11
# File 'lib/appchain/transaction.rb', line 9

def valid_until_block
  @valid_until_block
end

#valueObject

Returns the value of attribute value.



9
10
11
# File 'lib/appchain/transaction.rb', line 9

def value
  @value
end

#versionObject

Returns the value of attribute version.



9
10
11
# File 'lib/appchain/transaction.rb', line 9

def version
  @version
end

Class Method Details

.from_hash(hash) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/appchain/transaction.rb', line 42

def self.from_hash(hash)
  h = hash.map { |k, v| { k.to_sym => v } }.reduce({}, :merge)

  new(
    nonce: h[:nonce],
    valid_until_block: h[:valid_until_block],
    chain_id: h[:chain_id],
    to: h[:to],
    data: h[:data],
    version: h[:version] || 1,
    value: h[:value] || "0",
    quota: h[:quota] || 1_000_000
  )
end