diff --git a/lib/consts/Database.ts b/lib/consts/Database.ts index bc143ee..58e779d 100644 --- a/lib/consts/Database.ts +++ b/lib/consts/Database.ts @@ -17,6 +17,7 @@ export type SequelizeAttributes = { export type PairFactory = { base: string; quote: string; + rate?: number; }; export type PairAttributes = PairFactory & { diff --git a/lib/db/models/Pair.ts b/lib/db/models/Pair.ts index a0bfd11..8b4504f 100644 --- a/lib/db/models/Pair.ts +++ b/lib/db/models/Pair.ts @@ -7,6 +7,7 @@ export default (sequelize: Sequelize.Sequelize, dataTypes: Sequelize.DataTypes) id: { type: dataTypes.STRING, primaryKey: true }, base: { type: dataTypes.STRING, allowNull: false }, quote: { type: dataTypes.STRING, allowNull: false }, + rate: { type: dataTypes.FLOAT, allowNull: true }, }; const options: Sequelize.DefineOptions = { diff --git a/lib/rates/RateProvider.ts b/lib/rates/RateProvider.ts index 0adc94d..ae60eea 100644 --- a/lib/rates/RateProvider.ts +++ b/lib/rates/RateProvider.ts @@ -23,6 +23,14 @@ class RateProvider { */ public init = async (pairs: PairInstance[]) => { pairs.forEach((pair) => { + // If a pair has a hardcoded rate the CryptoCompare rate doesn't have to be queried + if (pair.rate) { + this.logger.debug(`Setting hardcoded rate for pair ${pair.id}: ${pair.rate}`); + + this.rates.set(pair.id, pair.rate); + return; + } + const baseAssets = this.baseAssetsMap.get(pair.quote); if (baseAssets) { diff --git a/lib/service/Service.ts b/lib/service/Service.ts index 874dd06..a207238 100644 --- a/lib/service/Service.ts +++ b/lib/service/Service.ts @@ -13,6 +13,9 @@ import { splitPairId, stringify, generateId, mapToObject } from '../Utils'; type PairConfig = { base: string; quote: string; + + // If there is a hardcoded rate the CryptoCompare API will not be queried + rate?: number; }; type Pair = { @@ -61,13 +64,23 @@ class Service extends EventEmitter { type PairArray = PairConfig[] | PairInstance[]; + const isUndefinedOrNull = (value: any) => value === undefined || value === null; + const comparePairArrays = (array: PairArray, compare: PairArray, callback: Function) => { array.forEach((pair) => { let inCompare = false; - compare.forEach((comaprePair) => { - if (pair.base === comaprePair.base && pair.quote === comaprePair.quote) { - inCompare = true; + compare.forEach((comparePair) => { + if (pair.base === comparePair.base && + pair.quote === comparePair.quote) { + + // If the rate is equal in config and database or not defined in the config + // and null in the database the database entry doesn't have to be updated + if (pair.rate === comparePair.rate || + (isUndefinedOrNull(pair.rate)) && isUndefinedOrNull(comparePair.rate)) { + + inCompare = true; + } } }); @@ -79,16 +92,16 @@ class Service extends EventEmitter { const promises: Promise[] = []; - comparePairArrays(pairs, dbPairs, (pair: PairFactory) => { - promises.push(this.pairRepository.addPair(pair)); - this.logger.debug(`Adding pair to database: ${stringify(pair)}`); - }); - comparePairArrays(dbPairs, pairs, (pair: PairFactory) => { promises.push(this.pairRepository.removePair(pair)); this.logger.debug(`Removing pair from database: ${stringify(pair)}`); }); + comparePairArrays(pairs, dbPairs, (pair: PairFactory) => { + promises.push(this.pairRepository.addPair(pair)); + this.logger.debug(`Adding pair to database: ${stringify(pair)}`); + }); + await Promise.all(promises); if (promises.length !== 0) {