UNPKG

crypto-janitor

Version:

The Crypto Janitor provides a simple interface for fetching and cleaning crypto account data.

120 lines 4.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable max-len */ const base_1 = require("./base"); /** * @description A Generic CSV Implementation of BaseConnection */ class CsvConnection extends base_1.default { // eslint-disable-next-line valid-jsdoc /** * @description Create CSV Connection instance * @param {string} name - Name of connection * @param {string} fileName - CVS file with transactions * @param {(fileName: string) => Array<any>} loadFileContents - Method used to load csv contents from filename */ constructor(name, fileName, loadFileContents) { super(name, "csv", { requireSymbols: false }); this.fileName = fileName; this.loadFileContents = loadFileContents; this.transactions = []; this.rawTransactions = []; } /** * JSON object representing connection * @override BaseConnection.toJSON * @return {any} */ toJSON() { const newJSON = super.toJSON(); const updatedParams = newJSON.params; updatedParams.fileName = this.fileName; newJSON.params = updatedParams; return newJSON; } /** * @description Format csv transactions * @return {void} */ _formatTransactions() { throw Error(`NotImplementedError: ${this.name}._formatTransactions() has not been implemented.`); } /** * @description Format csv orders * @return {void} */ _formatOrders() { throw Error(`NotImplementedError: ${this.name}._formatOrders() has not been implemented.`); } /** * @description Download csv for storage bucket * @override BaseConnection.initialize * @param {boolean} forceReload - (Optional) Additional paramaters * @return {Promise<void>} */ async initialize(forceReload = false) { if (!this.initialized || forceReload) { this.rawTransactions = await this.loadFileContents(this.fileName); this.initialized = true; } } /** * @description Filter csv connection transactions for withdrawals * @override BaseConnection.getWithdrawals * @param {string} key - Withdrawal key (default="send") * @return {Promise<Array<any>>} Array of withdrawal objects */ async getWithdrawals(key = "send") { if (!this.initialized) { await this.initialize(); } return this.transactions.filter((x) => x.type === key); } /** * @description Filter csv connection transactions for deposits * @override BaseConnection.getDeposits * @param {string} key - Withdrawal key (default="receive") * @return {Promise<Array<any>>} Array of deposit objects */ async getDeposits(key = "receive") { if (!this.initialized) { await this.initialize(); } return this.transactions.filter((x) => x.type === key); } /** * @description Filter csv connection transactions for buys and sells * @override BaseConnection.getOrders * @param {Array<string>} keys - Order keys (default=["buy", "sell", "swap"]) * @return {Promise<Array<any>>} Array of order objects */ async getOrders(keys = ["buy", "sell", "swap"]) { if (!this.initialized) { await this.initialize(); } const test = this.transactions.filter((x) => keys.includes(x.type)); return test; // return this.transactions.filter((x: any) => keys.includes(x.type)); } /** * @description Get csv connection transactions (withdrawals, deposits, and orders) * @override BaseConnection.getTransactions * @return {Promise<Array<any>>} Array of withdrawal objects */ async getTransactions() { if (!this.initialized) { await this.initialize(); } return this.transactions; } /** * @description Get all csv connection transactions (withdrawals, deposits, and interest) * @override BaseConnection.getAllTransactions * @return {Promise<Array<any>>} Array of transaction objects */ async getAllTransactions() { return await this.getTransactions(); } } exports.default = CsvConnection; //# sourceMappingURL=csv.js.map