Querying endpoints

Public endpoints

Following example shows how make a simple query to a public endpoint without authentication:

const fetch = require('node-fetch');

async function public () {
  const params = {
    year: "2021",
    month: "03",
    day: "01",
  };
  const url = `https://api.rhino.fi/v1/trading/r/24HoursVolume/${params.year}/${params.month}/${params.day}`;

  const rTradingVolume = await fetch(url, {
    method: "GET",
    headers: {
      "Accept": "application/json",
      "Content-Type": "application/json",
    },
  });
  console.info('trading volume history', await rTradingVolume.json())
}

Private Endpoints

Using same rhino.fi client we can call authorized endpoints (see Example 1), same result can be achieved manually signing a signature POST (see Example 1) and for get GET we need an Authorization header (see Example 3):

const HDWalletProvider = require('truffle-hdwallet-provider')
const Web3 = require('web3')
const RhinofiClientFactory = require('@rhino.fi/client-js');
const fetch = require('node-fetch');

async function private () {
  // ---SETUP---
  const providerUrl = '// Infura or similar provider url //';
  const ethPrivKey = '// Your private key //';
  const starkPrivateKey = '// Your stark private key //';

  const provider = new HDWalletProvider(ethPrivKey, providerUrl);
  const web3 = new Web3(provider);

  const rhinofiConfig = {
    api: 'https://api.rhino.fi',
    wallet: {
      type: 'tradingKey',
      meta: {
        starkPrivateKey
      }
    }
  }
  const rhinofi = await RhinofiClientFactory(web3, rhinofiConfig);

  // ---EXAMPLE 1 [Rhinofi CLIENT]---
  const balances = await rhinofi.getBalance()
  console.info('balances', balances)

  // ---EXAMPLE 2 [POST]---
  const { nonce, signature } = await rhinofi.sign.nonceSignature()

  const body = {
    nonce: nonce.toString(),
    signature
  }

  const balanceUrl = 'https://api.rhino.fi/v1/trading/r/getBalance';

  const rBalance = await fetch(balanceUrl, {
    method: "POST",
    body: JSON.stringify(body),
    headers: {
      "Accept": "application/json",
      "Content-Type": "application/json",
    },
  });
  console.info('balances', await rBalance.json())

  // ---EXAMPLE 3 [GET]---
  const { nonce, signature } = await rhinofi.sign.nonceSignature()

  const makeEcRecoverHeader = data => {
    const bufferStarkAuthData = Buffer.from(JSON.stringify(data))
    return 'EcRecover ' + bufferStarkAuthData.toString('base64')
  }

  const authHeaders = makeEcRecoverHeader({ nonce, signature })
  const user24HourVolumeUrl = 'https://api.rhino.fi/v1/trading/r/User24HoursVolume';

  const rUserVolume = await fetch(user24HourVolumeUrl, {
    method: "GET",
    headers: {
      "Accept": "application/json",
      "Content-Type": "application/json",
      "Authorization": authHeaders
    },
  });
  console.info('User 24 hour volume', await rUserVolume.json())
}

Last updated