Class: IOSTSdk::Main

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/iost_sdk.rb

Constant Summary collapse

DEFAULTS =
{
  gas_limit: 2_000_000,
  gas_ratio: 1,
  delay: 0,
  expiration: 90,
  approval_limit_amount: :unlimited
}.freeze
TXN_POLL_CAP =
90
TXN_STATUS =
{
  pending: 'pending',
  success: 'success',
  failed: 'failed'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:) ⇒ Main

Returns a new instance of Main.

Parameters:

  • endpoint (String)

    a URL of the JSON RPC endpoint of IOST



37
38
39
40
41
42
43
44
# File 'lib/iost_sdk.rb', line 37

def initialize(endpoint:)
  @endpoint = endpoint
  @client = IOSTSdk::Http::Client.new(base_url: @endpoint)

  DEFAULTS.each do |k, v|
    instance_variable_set("@#{k}".to_sym, v)
  end
end

Instance Attribute Details

#approval_limit_amountObject

Returns the value of attribute approval_limit_amount.



16
17
18
# File 'lib/iost_sdk.rb', line 16

def approval_limit_amount
  @approval_limit_amount
end

#clientObject (readonly)

Returns the value of attribute client.



15
16
17
# File 'lib/iost_sdk.rb', line 15

def client
  @client
end

#delayObject

Returns the value of attribute delay.



16
17
18
# File 'lib/iost_sdk.rb', line 16

def delay
  @delay
end

#expirationObject

Returns the value of attribute expiration.



16
17
18
# File 'lib/iost_sdk.rb', line 16

def expiration
  @expiration
end

#gas_limitObject

Returns the value of attribute gas_limit.



16
17
18
# File 'lib/iost_sdk.rb', line 16

def gas_limit
  @gas_limit
end

#gas_ratioObject

Returns the value of attribute gas_ratio.



16
17
18
# File 'lib/iost_sdk.rb', line 16

def gas_ratio
  @gas_ratio
end

#transactionObject

Returns the value of attribute transaction.



16
17
18
# File 'lib/iost_sdk.rb', line 16

def transaction
  @transaction
end

Instance Method Details

#call_abi(contract_id:, abi_name:, abi_args:) ⇒ Object

Create an instance of IOSTSdk::Models::Transaction with an action to call the ABI.

Parameters:

  • contract_id (String)

    a Contract’s ID

  • abi_name (String)

    the name of an ABI to call

  • abi_args (any)

    args to the ABI

Returns:

  • a new instance of Transaction



91
92
93
94
95
96
97
98
# File 'lib/iost_sdk.rb', line 91

def call_abi(contract_id:, abi_name:, abi_args:)
  transaction = init_transaction
  transaction.add_action(contract_id: contract_id, action_name: abi_name, action_data: abi_args)
  transaction.set_time_params(expiration: expiration, delay: delay)

  @transaction = transaction
  self
end

#new_account(name:, creator:, owner_key:, active_key:, initial_ram:, initial_gas_pledge:) ⇒ Object

Create an instance IOSTSdk::Models::Transaction to create a new account

Parameters:

  • name (String)

    the name of the account to be created

  • creator (String)

    the name of the account that’s creating a new account

  • owner_key (IOSTSdk::Crypto::KeyPair)

    the owner key of the new account

  • active_key (IOSTSdk::Crypto::KeyPair)

    the active key of the new account

  • initial_ram (Integer)

    the initial RAM of the new account

  • initial_gas_pledge (Integer)

    the initial gas pledge of the new account



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/iost_sdk.rb', line 128

def (name:, creator:, owner_key:, active_key:, initial_ram:, initial_gas_pledge:)
  transaction = init_transaction
  transaction.add_action(
    contract_id: 'auth.iost',
    action_name: :signUp,
    action_data: [name, owner_key.id, active_key.id]
  )

  if initial_ram > 10
    transaction.add_action(
      contract_id: 'ram.iost',
      action_name: :buy,
      action_data: [creator, name, initial_ram]
    )
  end

  if initial_gas_pledge > 0
    transaction.add_action(
      contract_id: 'ram.iost',
      action_name: :buy,
      action_data: [creator, name, initial_gas_pledge.to_s]
    )
  end

  transaction.set_time_params(expiration: expiration, delay: delay)
  @transaction = transaction
  self
end

#sign_and_send(account_name:, key_pair:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/iost_sdk.rb', line 48

def sign_and_send(account_name:, key_pair:)
  if @transaction && @transaction.is_valid?
    resp = @client.send_tx(
      transaction: @transaction,
      account_name: ,
      key_pair: key_pair
    )

    if resp['pre_tx_receipt']['status_code'] != TXN_STATUS[:success].upcase
      {
        status: TXN_STATUS[:failed],
        txn_hash: resp['hash']
      }
    else
      txn_hash = resp['hash']
      num_tries = 0
      txn_status = TXN_STATUS[:pending]
      txn_receipt = nil
      # poll transaction receipt by hash
      while ![TXN_STATUS[:success], TXN_STATUS[:failed]].include?(txn_status) && num_tries <= TXN_POLL_CAP do
        begin
          txn_receipt = @client.get_tx_receipt_by_tx_hash(hash_value: txn_hash)
        rescue
        end
        txn_status = txn_receipt.status_code.downcase if txn_receipt && txn_receipt.status_code
        num_tries += 1
        sleep(1)
      end

      {
        status: txn_status,
        txn_hash: txn_receipt.tx_hash
      }
    end
  end
end

#transfer(token:, from:, to:, amount:, memo:) ⇒ Object

Create an instance IOSTSdk::Models::Transaction with an action to transfer tokens

Parameters:

  • token (String)

    name of the token in the transfer

  • from (String)

    sender

  • to (String)

    recipient

  • amount (Integer)

    amount to transfer

  • memo (String)

    memo/notes for the transfer

Returns:

  • a new instance of Transaction



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/iost_sdk.rb', line 108

def transfer(token:, from:, to:, amount:, memo:)
  call_abi(
    contract_id: 'token.iost',
    abi_name: :transfer,
    abi_args: [token, from, to, amount.to_s, memo]
  )
  @transaction.add_approve(token: :iost, amount: amount)
  @transaction.set_time_params(expiration: expiration, delay: delay)

  self
end