Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Contents:

...

Table of Contents
stylenone

Auth: true

params:

Code Block
languagejson
params: {
        currencyId: {
          type: 'string',
          optional: true,
        },
        type: {
          type: 'string',
          optional: true,
        },
        from: {
          type: 'string',
          optional: false,
        },
        to: {
          type: 'string',
          optional: false,
        },
      },

Responce:

A string is returned in Base64 format with the following encoded fields

For trading (columns):

  1. Timestamp - standart date format

...

  1. Side - Selects one of the values "buy", "sell" values according to the following rule

Code Block
languagejs
ctx.meta.user.id === trade?.buyUserId ? 'buy' : 'sell';

...

  1. Fee - The value and accuracy are taken by the following formula

Code Block
languagejs
const fee = side === trade?.makerOrderSide
              ? side === 'buy'
                ? trade.maker_fee.toFixed(currenciesMap[base_currency])
                : trade.maker_fee.toFixed(currenciesMap[quote_currency])
              : side === 'buy'
                ? trade.taker_fee.toFixed(currenciesMap[base_currency])
                : trade.taker_fee.toFixed(currenciesMap[quote_currency])

...

  1. Role - The value is taken by the following formula

Code Block
languagejs
const role = trade?.makerOrderSide === side ? 'Maker' : 'Taker';

...

For Trasactions (columns): in work

Logic:

Steps:

  1. We check whether the specified date from which to take data does not exceed 3 months from today

  2. We check what type of operations is being sought, trading or transactions. type === 'trading' OR else

  3. Depending on the required data, we form requests for trades or for operations to receive them based on a time interval.

  4. Form an array of objects with keys that correspond to the column names and values for them.

  5. Passing an array through a converter from an array of objects to a csv string

  6. We convert the CSV string to base64 and return it to the front end to create a file and send it to the client

The frontend can generate text for a file in this way:

Code Block
languagejs
const decodedBuffer = Buffer.from(base64Data, 'base64');
const text = decodedBuffer.toString('utf-8')

// base64Data - data from GQL
// text - text fromat for CSV file

Trading

A query is formed to the Trade table and, based on the data, an array of objects with data in the following form is formed:

Code Block
languagejs
const createHistoryForOneTrade = async (trade, currenciesMap) => {
            const side =  ctx.meta.user.id === trade?.buyUserId ? 'buy' : 'sell';
            const [base_currency, quote_currency] = trade.market.split('-')
            const fee = side === trade?.makerOrderSide
              ? side === 'buy'
                ? trade.maker_fee.toFixed(currenciesMap[base_currency])
                : trade.maker_fee.toFixed(currenciesMap[quote_currency])
              : side === 'buy'
                ? trade.taker_fee.toFixed(currenciesMap[base_currency])
                : trade.taker_fee.toFixed(currenciesMap[quote_currency])
            const role = trade?.makerOrderSide === side ? 'Maker' : 'Taker';

            return {
              Timestamp: trade.createdAt,
              Market_pair: trade.market,
              Side: side,
              Price: trade.price,
              Amount: trade.amount,
              Total: trade.volume,
              Fee: fee,
              Role: role,
            }
          }
const tradeHistory = data.map(trade => {
            return createHistoryForOneTrade(trade, currenciesMap)
          })

currenciesMap - designed to take currency parameters and not make unnecessary queries to the database

Code Block
languagejs
const allCurrencies = await ctx.call('DB_Currencies.find', {})
const currenciesMap = {}

allCurrencies.forEach(currency => {
  currenciesMap[currency.id] = currency.precision
})

Converting to CSV

To convert to CSV format, there is a function that takes an array of objects of the same type. At the beginning, it forms the name of the columns taking the keys of the object, after which it searches through all objects taking the key values and writing them to a string in the format for csv

Code Block
languagejs
convertToCSV(objArray){
  let array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
  let str = '';

  const header = Object.keys(objArray[0])

  str += header.join(',') + '\r\n';

  for (let i = 0; i < array.length; i++) {
    let line = '';
    for (let index in array[i]) {
      if (line !== '') line += ','

      line += array[i][index];
    }

    str += line + '\r\n';
  }

  return str;
},