class IOSTSdk::Main

Constants

DEFAULTS
SERVER_TIME_DIFF_THRESHOLD
TXN_POLL_CAP
TXN_STATUS

Attributes

approval_limit_amount[RW]
client[R]
delay[RW]
expiration[RW]
gas_limit[RW]
gas_ratio[RW]
transaction[RW]

Public Class Methods

new(endpoint:) click to toggle source

@param endpoint [String] a URL of the JSON RPC endpoint of IOST

# File lib/iost_sdk.rb, line 39
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

Public Instance Methods

call_abi(contract_id:, abi_name:, abi_args:) click to toggle source

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

@param contract_id [String] a Contract's ID @param abi_name [String] the name of an ABI to call @param abi_args [any] args to the ABI @return a new instance of Transaction

# File lib/iost_sdk.rb, line 95
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,
    server_time_diff: server_time_diff
  )
  @transaction = transaction
  self
end
new_account(name:, creator:, owner_key:, active_key:, initial_ram:, initial_gas_pledge:) click to toggle source

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

@param name [String] the name of the account to be created @param creator [String] the name of the account that's creating a new account @param owner_key [IOSTSdk::Crypto::KeyPair] the owner key of the new account @param active_key [IOSTSdk::Crypto::KeyPair] the active key of the new account @param initial_ram [Integer] the initial RAM of the new account @param initial_gas_pledge [Integer] the initial gas pledge of the new account

# File lib/iost_sdk.rb, line 134
def new_account(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,
    server_time_diff: server_time_diff
  )

  @transaction = transaction

  self
end
sign_and_send(account_name:, key_pair:) click to toggle source
# File lib/iost_sdk.rb, line 50
def sign_and_send(account_name:, key_pair:)
  if @transaction && @transaction.is_valid?
    resp = @client.send_tx(
      transaction: @transaction,
      account_name: account_name,
      key_pair: key_pair
    )

    if !resp['pre_tx_receipt'] || (resp['pre_tx_receipt'] && resp['pre_tx_receipt']['status_code'] != TXN_STATUS[:success].upcase)
      {
        status: TXN_STATUS[:failed],
        txn_hash: resp['hash'],
        message: resp['pre_tx_receipt'] ? resp['pre_tx_receipt']['error'] : ''
      }
    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,
        message: ''
      }
    end
  end
end
transfer(token:, from:, to:, amount:, memo:) click to toggle source

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

@param token [String] name of the token in the transfer @param from [String] sender @param to [String] recipient @param amount [Integer] amount to transfer @param memo [String] memo/notes for the transfer @return a new instance of Transaction

# File lib/iost_sdk.rb, line 115
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)

  self
end

Private Instance Methods

init_transaction() click to toggle source
# File lib/iost_sdk.rb, line 170
def init_transaction
  time_now = Time.now.utc.to_i * 1_000_000
  transaction = IOSTSdk::Models::Query::Transaction.new.populate(
    model_data: {
      'time' => time_now,
      'expiration' => time_now + delay * 1_000_000,
      'gas_ratio' => gas_ratio,
      'gas_limit' => gas_limit,
      'delay' => delay,
      'chain_id' => 0,
      'signers' => [],
      'actions' => [],
      'amount_limit' => [],
      'signatures' => []
    }
  )
  transaction
end
server_time_diff() click to toggle source
# File lib/iost_sdk.rb, line 189
def server_time_diff
  request_start_time = Time.now.utc.to_i * 1_000_000_000
  node_server_time = @client.get_node_info.server_time.to_i
  request_end_time = Time.now.utc.to_i * 1_000_000_000

  if request_end_time - request_start_time < SERVER_TIME_DIFF_THRESHOLD
    node_server_time - request_end_time
  else
    0
  end
end