Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

Commit

Permalink
feat: optional hardcoded rates
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Jan 20, 2019
1 parent a816011 commit 9b3388b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/consts/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type SequelizeAttributes<T extends { [key: string]: any }> = {
export type PairFactory = {
base: string;
quote: string;
rate?: number;
};

export type PairAttributes = PairFactory & {
Expand Down
1 change: 1 addition & 0 deletions lib/db/models/Pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<db.PairInstance> = {
Expand Down
8 changes: 8 additions & 0 deletions lib/rates/RateProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 21 additions & 8 deletions lib/service/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
}
}
});

Expand All @@ -79,16 +92,16 @@ class Service extends EventEmitter {

const promises: Promise<any>[] = [];

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) {
Expand Down

0 comments on commit 9b3388b

Please sign in to comment.