Method: IOSTSdk::Main#sign_and_send

Defined in:
lib/iost_sdk.rb

#sign_and_send(account_name:, key_pair:) ⇒ Object



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
84
85
86
87
# 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: ,
      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