From 9909aa2792a3796a74c4ea19e41254851823fc95 Mon Sep 17 00:00:00 2001 From: Jonas Hals Date: Wed, 23 Jun 2021 12:27:07 +0200 Subject: [PATCH] Add Tiingo Token-Allocation --- composite/token-allocation/README.md | 1 + composite/token-allocation/src/config.ts | 3 + .../src/data-providers/tiingo.ts | 65 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 composite/token-allocation/src/data-providers/tiingo.ts diff --git a/composite/token-allocation/README.md b/composite/token-allocation/README.md index 99ac5502ab..cab2b33bbc 100644 --- a/composite/token-allocation/README.md +++ b/composite/token-allocation/README.md @@ -22,6 +22,7 @@ The data providers supported and their properties are as follows: | cryptocompare | :white_check_mark: | :white_check_mark: | :white_check_mark: | | | kaiko | :white_check_mark: | :white_check_mark: | :x: | Crypto Quotes are not supported | | nomics | :white_check_mark: | :white_check_mark: | :white_check_mark: | | +| tiingo | :white_check_mark: | :white_check_mark: | :x: | | ## Input Params diff --git a/composite/token-allocation/src/config.ts b/composite/token-allocation/src/config.ts index e1fb433ec2..d9bdd1191c 100644 --- a/composite/token-allocation/src/config.ts +++ b/composite/token-allocation/src/config.ts @@ -7,6 +7,7 @@ import * as coingecko from './data-providers/coingecko' import * as coinapi from './data-providers/coinapi' import * as amberdata from './data-providers/amberdata' import * as kaiko from './data-providers/kaiko' +import * as tiingo from './data-providers/tiingo' import { Config, PriceAdapter } from './types' enum DataProvider { @@ -19,6 +20,7 @@ enum DataProvider { Coingecko = 'coingecko', Coinapi = 'coinapi', Kaiko = 'kaiko', + Tiingo = 'tiingo', } const providers: Record = { @@ -30,6 +32,7 @@ const providers: Record = { [DataProvider.Coingecko]: coingecko, [DataProvider.Coinapi]: coinapi, [DataProvider.Kaiko]: kaiko, + [DataProvider.Tiingo]: tiingo, } export const DEFAULT_TOKEN_DECIMALS = 18 diff --git a/composite/token-allocation/src/data-providers/tiingo.ts b/composite/token-allocation/src/data-providers/tiingo.ts new file mode 100644 index 0000000000..ab6a88169f --- /dev/null +++ b/composite/token-allocation/src/data-providers/tiingo.ts @@ -0,0 +1,65 @@ +import { Requester } from '@chainlink/external-adapter' +import { util } from '@chainlink/ea-bootstrap' + +export interface ResponseSchema { + ticker: string + baseCurrency: string + quoteCurrency: string + priceData: { + date: string + open: number + high: number + low: number + close: number + volume: number + volumeNotional: number + fxOpen: number + fxHigh: number + fxLow: number + fxClose: number + fxVolumeNotional: number + fxRate: number + tradesDone: number + }[] +} + +// When an invalid symbol is given the response body is empty +const customError = (data: ResponseSchema[]) => !data.length + +const getPriceData = async (base: string, quote: string) => { + const options = { + url: 'https://api.tiingo.com/tiingo/crypto/prices', + params: { + token: util.getRequiredEnv('API_KEY'), + baseCurrency: base, + convertCurrency: quote, + consolidateBaseCurrency: true, + resampleFreq: '24hour', + }, + } + + const response = await Requester.request(options, customError) + return Requester.validateResultNumber(response.data as ResponseSchema[], [ + 0, + 'priceData', + 0, + 'fxClose', + ]) +} + +export const getPrices = async ( + baseSymbols: string[], + quote: string, +): Promise> => { + const entries = await Promise.all( + baseSymbols.map(async (symbol) => { + const data = await getPriceData(symbol, quote) + return [symbol, data] + }), + ) + + return Object.fromEntries(entries) +} +export const getMarketCaps = () => { + throw Error('not implemented') +}