From 09bcb6bcf84cc1c094304b32b31e458079e4d848 Mon Sep 17 00:00:00 2001 From: Zahrun <10415894+Zahrun@users.noreply.github.com> Date: Tue, 13 Dec 2022 12:22:18 +0100 Subject: [PATCH] feat: support liquid trades import --- src/parsers/trades/index.ts | 2 + src/parsers/trades/liquid/index.ts | 88 ++++++++++++++++++++++++++++++ src/types/locations.ts | 4 ++ 3 files changed, 94 insertions(+) create mode 100644 src/parsers/trades/liquid/index.ts diff --git a/src/parsers/trades/index.ts b/src/parsers/trades/index.ts index 30b720d..387b6ad 100644 --- a/src/parsers/trades/index.ts +++ b/src/parsers/trades/index.ts @@ -4,6 +4,7 @@ import binanceParser from './binance'; import bittrexParser from './bittrex'; import geminiParser from './gemini'; import krakenParser from './kraken'; +import liquidParser from './liquid'; import poloniexParser from './poloniex'; import revolutParser from './revolut'; @@ -12,6 +13,7 @@ const parserMapping: {[key in EXCHANGES]: any} = { [EXCHANGES.Bittrex]: bittrexParser, [EXCHANGES.Gemini]: geminiParser, [EXCHANGES.Kraken]: krakenParser, + [EXCHANGES.Liquid]: liquidParser, [EXCHANGES.Poloniex]: poloniexParser, [EXCHANGES.Revolut]: revolutParser, } diff --git a/src/parsers/trades/liquid/index.ts b/src/parsers/trades/liquid/index.ts new file mode 100644 index 0000000..5694cff --- /dev/null +++ b/src/parsers/trades/liquid/index.ts @@ -0,0 +1,88 @@ +import { getCSVData } from '../../'; +import { EXCHANGES, IImport, IPartialTrade, ITrade } from '../../../types'; +import { createDateAsUTC, createID } from '../../utils'; + +interface ILiquid { + exchange: string; + currency_type: string; + direction: string; + transaction_id: string; + transaction_type: string; + gross_amount: string; + currency: string; + execution_id: string; + generated_for_type: string; + generated_for_id: string; + transaction_hash: string; + from_address: string; + to_address: string; + state: string; + created_at_jst: string; + updated_at_jst: string; + created_at_utc: string; + updated_at: string; + notes: string; + used_id: string; + account_id: string; +} + +export default async function processData(importDetails: IImport): Promise { + const data: ILiquid[] = await getCSVData(importDetails.data) as ILiquid[]; + const internalFormat: ITrade[] = []; + if (data.length < 1) { + return internalFormat; + } + let splitTrade = data[0]; + let lineContinuity = 0; + for (const trade of data) { + const tradeToAdd: IPartialTrade = { + date : createDateAsUTC(new Date(trade.updated_at)).getTime(), + exchange : EXCHANGES.Liquid, + exchangeID : trade.execution_id, + }; + switch (trade.transaction_type) { + case 'rebate_trade_fee': + case 'trade_fee': + case 'quick_exchange': + case 'trade': { + switch (lineContinuity) { + case 0: { + splitTrade = trade; + lineContinuity = 1; + continue; + } + case 1: { + lineContinuity = 0; + if (trade.direction === "pay" && splitTrade.direction === "receive") { + tradeToAdd.boughtCurrency = splitTrade.currency; + tradeToAdd.soldCurrency = trade.currency; + tradeToAdd.amountSold = Math.abs(parseFloat(trade.gross_amount)); + tradeToAdd.rate = Math.abs(parseFloat(trade.gross_amount) / parseFloat(splitTrade.gross_amount)); + } else if (trade.direction === "receive" && splitTrade.direction === "pay") { + tradeToAdd.soldCurrency = splitTrade.currency; + tradeToAdd.boughtCurrency = trade.currency; + tradeToAdd.amountSold = Math.abs(parseFloat(splitTrade.gross_amount)); + tradeToAdd.rate = Math.abs(parseFloat(splitTrade.gross_amount) / parseFloat(trade.gross_amount)); + } else { + console.error(`Error parsing ${trade.exchange} trade splitTrade.direction=${splitTrade.direction} and trade.direction=${trade.direction}`); + break; + } + tradeToAdd.ID = createID(tradeToAdd); + internalFormat.push(tradeToAdd as ITrade); + continue; + } + default: { + console.error(`Error parsing ${trade.exchange} trade lineContinuity=${lineContinuity}`); + break; + } + } + break; + } + default: { + console.log(`Ignored ${trade.exchange} trade of type ${trade.transaction_type}`); + continue; + } + } + } + return internalFormat; +} diff --git a/src/types/locations.ts b/src/types/locations.ts index c0053d4..3826dc7 100644 --- a/src/types/locations.ts +++ b/src/types/locations.ts @@ -3,6 +3,8 @@ export type Location = EXCHANGES | string; export enum EXCHANGES { Bittrex = 'BITTREX', Gemini= 'GEMINI', + Kraken = 'KRAKEN', + Liquid = 'LIQUID', Poloniex = 'POLONIEX', Kraken = 'KRAKEN', Binance = 'BINANCE', @@ -16,6 +18,8 @@ export enum IncomeImportTypes { export enum ExchangesTradeHeaders { BITTREX = '07230399aaa8d1f15e88e38bd43a01c5ef1af6c1f9131668d346e196ff090d80', GEMINI = '996edee25db7f3d1dd16c83c164c6cff8c6d0f5d6b3aafe6d1700f2a830f6c9e', + KRAKEN = '85bf27e799cc0a30fe5b201cd6a4724e4a52feb433f41a1e8b046924e3bf8dc5', + LIQUID = '12c125c9080e41e087ff0955826bba94568d637214b166a296447e53ae469abe', POLONIEX = 'd7484d726e014edaa059c0137ac91183a7eaa9ee5d52713aa48bb4104b01afb0', KRAKEN = '85bf27e799cc0a30fe5b201cd6a4724e4a52feb433f41a1e8b046924e3bf8dc5', BINANCE = '4d0d5df894fe488872e513f6148dfa14ff29272e759b7fb3c86d264687a7cf99',