Skip to content

Commit

Permalink
Merge pull request coinbase#77 from coinbase/failover-info
Browse files Browse the repository at this point in the history
Add FXCalculator reporting
  • Loading branch information
CjS77 authored Oct 27, 2017
2 parents efbd783 + d4b5ea3 commit e64e12f
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@types/ws": "0.0.37",
"bignumber.js": "4.0.2",
"bintrees": "1.0.1",
"ccxt": "^1.9.186",
"ccxt": "1.9.275",
"commander": "2.9.0",
"crypto": "0.0.3",
"gdax": "0.3.1",
Expand Down
4 changes: 4 additions & 0 deletions src/FXService/FXRateCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ export abstract class FXRateCalculator {
* will still be accepted. However, it can also reject the entire Promise if it is unable to calculate rates for any of the given pairs
*/
abstract calculateRatesFor(pairs: CurrencyPair[]): Promise<FXObject[]>;

getLastRequestInfo(): any {
return {};
}
}
6 changes: 6 additions & 0 deletions src/FXService/calculators/FailoverCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ export interface FailoverCalculatorConfig {
export default class FailoverCalculator extends FXRateCalculator {
logger: Logger;
calculators: FXRateCalculator[];
private lastCalculatorUsed: FXRateCalculator = null;

constructor(config: FailoverCalculatorConfig) {
super();
this.calculators = config.calculators;
this.logger = config.logger || ConsoleLoggerFactory();
}

getLastRequestInfo(): any {
return { calculator: this.lastCalculatorUsed };
}

calculateRatesFor(pairs: CurrencyPair[]): Promise<FXObject[]> {
const promises: Promise<FXObject>[] = pairs.map((pair: CurrencyPair) => {
return this.requestRateFor(pair);
Expand All @@ -46,6 +51,7 @@ export default class FailoverCalculator extends FXRateCalculator {

private requestRateFor(pair: CurrencyPair): Promise<FXObject> {
return tryUntil<FXRateCalculator, FXObject>(this.calculators, (calculator: FXRateCalculator) => {
this.lastCalculatorUsed = calculator;
return calculator.calculateRatesFor([pair])
.then((result: FXObject[]) => {
if (result[0] === null || result[0].rate === null) {
Expand Down
4 changes: 2 additions & 2 deletions src/FXService/calculators/RobustCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class RobustCalculator extends FXRateCalculator {
this.report.sources = this.sources.map((source: FXProvider) => source.name);
}

get lastReport(): RobustCalculatorReport {
getLastRequestInfo(): RobustCalculatorReport {
return this.report;
}

Expand Down Expand Up @@ -124,7 +124,7 @@ export class RobustCalculator extends FXRateCalculator {
const update: QueryStatus = { deltas: [], prices: [], rejectReason: [], errors: [], time: new Date(), valid: [], lastPrice: null };
update.lastPrice = this.report.data[_pair] ? this.report.data[_pair].prices : [];
rates.forEach((rate: FXObject | Error, i: number) => {
if (rate instanceof Error || !rate.rate.isFinite()) {
if (rate instanceof Error || !rate || !rate.rate || !rate.rate.isFinite()) {
update.errors[i] = rate instanceof Error ? rate : null;
update.rejectReason[i] = NO_CURRENT_PRICE_ERROR;
this.report.data[_pair] = update;
Expand Down
4 changes: 4 additions & 0 deletions src/FXService/calculators/SimpleRateCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ export default class SimpleRateCalculator extends FXRateCalculator {
// Wait for all promises to resolve before sending results back
return Promise.all(promises);
}

getLastRequestInfo(): any {
return { provider: this.provider };
}
}
5 changes: 4 additions & 1 deletion src/FXService/providers/CryptoProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the *
* License for the specific language governing permissions and limitations under the License. *
**********************************************************************************************************************/
import { CurrencyPair, FXObject, FXProvider, FXProviderConfig, pairAsString } from '../FXProvider';
import { CurrencyPair, EFXRateUnavailable, FXObject, FXProvider, FXProviderConfig, pairAsString } from '../FXProvider';
import { Product, PublicExchangeAPI } from '../../exchanges/PublicExchangeAPI';
import { BigJS } from '../../lib/types';

Expand Down Expand Up @@ -58,6 +58,9 @@ export class CryptoProvider extends FXProvider {
rate: price
};
return result;
}).catch((err: Error) => {
this.log('warn', `${this.name} failed to download the ${pairAsString(pair)} rate`, err.message);
return Promise.reject(new EFXRateUnavailable(err.message, this.name));
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/samples/RobustRateDemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ const fxService: FXService = new FXService({
fxService.on('FXRateUpdate', (rates: FXRates) => {
if (rates['BTC-USD'].rate) { logger.log('info', `BTC price: ${rates['BTC-USD'].rate.toFixed(5)}`); }
if (rates['ETH-BTC'].rate) { logger.log('info', `ETH price: ${rates['ETH-BTC'].rate.toFixed(5)}`); }
logger.log('debug', 'RobustRate report', robustCalculator.lastReport);
logger.log('debug', 'RobustRate report', robustCalculator.getLastRequestInfo());
});
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,9 @@ caseless@~0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc"

ccxt@^1.9.186:
version "1.9.248"
resolved "https://registry.yarnpkg.com/ccxt/-/ccxt-1.9.248.tgz#343dba34f7f8be9bfa2b8611cc7ac384a368d064"
[email protected].275:
version "1.9.275"
resolved "https://registry.yarnpkg.com/ccxt/-/ccxt-1.9.275.tgz#e66abca58c020ffeb9fa95d869b2ab9fd03e2094"
dependencies:
crypto-js "3.1.9-1"
node-fetch "2.0.0-alpha.8"
Expand Down

0 comments on commit e64e12f

Please sign in to comment.