Class: IOSTSdk::Models::Query::Transaction

Inherits:
Object
  • Object
show all
Includes:
IOSTSdk::Models
Defined in:
lib/iost_sdk/models/query/transaction.rb

Constant Summary collapse

GAS_LIMIT_RANGE =
[6000, 4_000_000].freeze
GAS_RATIO_RANGE =
[1, 100].freeze
NUMERIC_REGEX =
/^\d+(\.\d+)?$/.freeze

Constants included from IOSTSdk::Models

MODEL_REGISTRY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IOSTSdk::Models

included, #parse, #raw_data

Instance Attribute Details

#chain_idObject

Returns the value of attribute chain_id.



20
21
22
# File 'lib/iost_sdk/models/query/transaction.rb', line 20

def chain_id
  @chain_id
end

Class Method Details

.attr_namesObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/iost_sdk/models/query/transaction.rb', line 22

def self.attr_names
  [
    'time',
    'expiration',
    'gas_ratio',
    'gas_limit',
    'delay',
    'chain_id',
    'reserved',
    'signers',
    'actions',
    'amount_limit',
    'signatures'
  ]
end

Instance Method Details

#add_action(contract_id:, action_name:, action_data:) ⇒ Object

Add an action to the transaction

Parameters:

  • contract_id (String)

    a Contract’s ID

  • action_name (String)

    the name of an action

  • action_data (any)

    args to the action



55
56
57
58
59
60
61
62
63
# File 'lib/iost_sdk/models/query/transaction.rb', line 55

def add_action(contract_id:, action_name:, action_data:)
  @actions << IOSTSdk::Models::Action.new.populate(
    model_data: {
      'contract' => contract_id,
      'action_name' => action_name.to_s,
      'data' => JSON.generate(action_data)
    }
  )
end

#add_approve(token:, amount:) ⇒ Object

Add an amount limit to the transaction

Parameters:

  • token (String)

    name of the token

  • amount (Integer|String)

    amount of the token or ‘unlimited’

Raises:



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/iost_sdk/models/query/transaction.rb', line 69

def add_approve(token:, amount:)
  raise IOSTSdk::Errors::InvalidTransactionError.new('approve token should not be *') if token == '*'
  raise IOSTSdk::Errors::InvalidTransactionError.new('approve amount should be numeric') unless amount.is_a?(Numeric)

  @amount_limit << IOSTSdk::Models::AmountLimit.new.populate(
    model_data: {
      'token' => token.to_s,
      'value' => amount.to_s
    }
  )
end

#is_valid?Boolean

Verify if the transaction object is valid

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
# File 'lib/iost_sdk/models/query/transaction.rb', line 82

def is_valid?
  [
    # check gas limit
    gas_limit.is_a?(Numeric) && gas_limit.between?(GAS_LIMIT_RANGE.first, GAS_LIMIT_RANGE.last),
    # check gas ratio
    gas_ratio.is_a?(Numeric) && gas_ratio.between?(GAS_RATIO_RANGE.first, GAS_RATIO_RANGE.last),
    # check approve token and amount
    amount_limit.all? { |al| al.token != '*' || /^\d+(\.\d+)?$/ =~ al.value }
  ].all?
end

#set_time_params(expiration:, delay:) ⇒ Object

set the time implicitly, and set expiration and delay explicitly

Parameters:

  • expiration (Integer)

    number of seconds, since creation, the transaction will expire in

  • delay (Integer)

    the delay



42
43
44
45
46
47
48
# File 'lib/iost_sdk/models/query/transaction.rb', line 42

def set_time_params(expiration:, delay:)
  time_now = (Time.now.utc.to_f * 1000).to_i * 1_000_000

  @time = time_now
  @expiration = @time + expiration * 1_000_000_000
  @delay = delay
end