Keys for testing
Base Url

Fio Request Example

The following is an example of how to send, get, approve or reject fio request. Use the FIO Testnet Monitor to register your Testnet private/public keys and fund your Testnet account if you haven’t registered yet.

Import

Importing using commonJS syntax is supported by Node.js out of the box:

    const { FIOSDK } = require('@fioprotocol/fiosdk');
    const { fetch } = require('node-fetch');

Step 1. Initializing the SDK.

    
    const fetchJson = async (uri, opts = {}) => {
      return fetch(uri, opts)
    }
   
    const baseUrl = 'http://testnet.fioprotocol.io/v1/';
    
    const payeePrivateKey = '';
    const payeePublicKey = '';
    const payeeFioAddress = '';
    const payerFioAddress = '';
    const requestAmount = 1; // set amount
    const memo = ''; // optional

    const fioSdkPayee = new FIOSDK(
      payeePrivateKey,
      payeePublicKey,
      baseUrl,
      fetchJson
    )

    const { public_address: payerPublicKey } = await fioSdkPayee.getPublicAddress(payerFioAddress, "FIO", "FIO")
  • privateKey/publicKey - The wallet user’s private/public keys
  • baseURL - The base URL to a FIO Protocol blockchain API node
  • fetchjson - A reference to fetchJson, used for http post/get calls

Step 2. Get fee for send request.

Use /get_fee to look up the payer fee for /new_funds_request. You need to set payeeFioAddress for this call

    const { fee: sendRequestFee } = await fioSdkPayee.getFeeForNewFundsRequest(payeeFioAddress)

Step 3. Send new funds request.

Call requestFunds with needed params. payeeTokenPublicAddress could be public address of any coin (BTC, ETH, …), and you should provide proper chainCode/tokenCode for it.

    const requestFundsResult = await fioSdkPayee.genericAction('requestFunds', {
        payerFioAddress,
        payeeFioAddress,
        payeeTokenPublicAddress: fioSdkPayee.publicKey,
        payerFioPublicKey: payerPublicKey,
        amount: requestAmount,
        chainCode: 'FIO',
        tokenCode: 'FIO',
        memo,
        maxFee: sendRequestFee,
    })
    const requestId = requestFundsResult.fio_request_id

Step 4. Check request sent.

You could check that request created for payee.

    const { requests: sentRequests } = await fioSdkPayee.getSentFioRequests()
    const sentRequest = sentRequests.find(pr => parseInt(pr.fio_request_id) === parseInt(requestId))
    if (sentRequest && sentRequest.fio_request_id) {
      console.log(sentRequest);
    }

Final code

The following summarizes the steps to send and get sent FIO Request:

    const { FIOSDK } = require('@fioprotocol/fiosdk');
    const { fetch } = require('node-fetch');
    
    const fetchJson = async (uri, opts = {}) => {
      return fetch(uri, opts)
    }
   
    const baseUrl = 'http://testnet.fioprotocol.io/v1/';
    
    async function main() {
      const payeePrivateKey = '';
      const payeePublicKey = '';
      const payeeFioAddress = '';
      const payerFioAddress = '';
      const requestAmount = 1; // set amount
      const memo = ''; // optional

      const fioSdkPayee = new FIOSDK(
        payeePrivateKey,
        payeePublicKey,
        baseUrl,
        fetchJson
      )

      const { public_address: payerPublicKey } = await fioSdkPayee.getPublicAddress(payerFioAddress, "FIO", "FIO")
      const { fee: sendRequestFee } = await fioSdkPayee.getFeeForNewFundsRequest(payeeFioAddress)
    
      const requestFundsResult = await fioSdkPayee.genericAction('requestFunds', {
        payerFioAddress,
        payeeFioAddress,
        payeeTokenPublicAddress: fioSdkPayee.publicKey,
        payerFioPublicKey: payerPublicKey,
        amount: requestAmount,
        chainCode: 'FIO',
        tokenCode: 'FIO',
        memo,
        maxFee: sendRequestFee,
      })
      const requestId = requestFundsResult.fio_request_id
    
      let sentRequest
      while (!sentRequest) {
        try {
          const { requests } = await fioSdkPayee.getSentFioRequests()
          sentRequest = requests.find(pr => parseInt(pr.fio_request_id) === parseInt(requestId))
          if (sentRequest && sentRequest.fio_request_id) {
            console.log(sentRequest);
          }
        } catch (e) {
          console.log(e);
        }
      }
    }
    
    main()
id Payee FIO Address Payer FIO Address Memo Amount