|
|
@ -17,15 +17,30 @@ import {
|
|
|
|
SymbolProfile
|
|
|
|
SymbolProfile
|
|
|
|
} from '@prisma/client';
|
|
|
|
} from '@prisma/client';
|
|
|
|
import { format, fromUnixTime, getUnixTime } from 'date-fns';
|
|
|
|
import { format, fromUnixTime, getUnixTime } from 'date-fns';
|
|
|
|
import got from 'got';
|
|
|
|
import got, { Headers } from 'got';
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
@Injectable()
|
|
|
|
export class CoinGeckoService implements DataProviderInterface {
|
|
|
|
export class CoinGeckoService implements DataProviderInterface {
|
|
|
|
private readonly URL = 'https://api.coingecko.com/api/v3';
|
|
|
|
private readonly apiUrl: string;
|
|
|
|
|
|
|
|
private readonly headers: Headers = {};
|
|
|
|
|
|
|
|
|
|
|
|
public constructor(
|
|
|
|
public constructor(
|
|
|
|
private readonly configurationService: ConfigurationService
|
|
|
|
private readonly configurationService: ConfigurationService
|
|
|
|
) {}
|
|
|
|
) {
|
|
|
|
|
|
|
|
const apiKeyDemo = this.configurationService.get('API_KEY_COINGECKO_DEMO');
|
|
|
|
|
|
|
|
const apiKeyPro = this.configurationService.get('API_KEY_COINGECKO_PRO');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.apiUrl = 'https://api.coingecko.com/api/v3';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (apiKeyDemo) {
|
|
|
|
|
|
|
|
this.headers['x-cg-demo-api-key'] = apiKeyDemo;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (apiKeyPro) {
|
|
|
|
|
|
|
|
this.apiUrl = 'https://pro-api.coingecko.com/api/v3';
|
|
|
|
|
|
|
|
this.headers['x-cg-pro-api-key'] = apiKeyPro;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public canHandle(symbol: string) {
|
|
|
|
public canHandle(symbol: string) {
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
@ -49,7 +64,8 @@ export class CoinGeckoService implements DataProviderInterface {
|
|
|
|
abortController.abort();
|
|
|
|
abortController.abort();
|
|
|
|
}, this.configurationService.get('REQUEST_TIMEOUT'));
|
|
|
|
}, this.configurationService.get('REQUEST_TIMEOUT'));
|
|
|
|
|
|
|
|
|
|
|
|
const { name } = await got(`${this.URL}/coins/${aSymbol}`, {
|
|
|
|
const { name } = await got(`${this.apiUrl}/coins/${aSymbol}`, {
|
|
|
|
|
|
|
|
headers: this.headers,
|
|
|
|
// @ts-ignore
|
|
|
|
// @ts-ignore
|
|
|
|
signal: abortController.signal
|
|
|
|
signal: abortController.signal
|
|
|
|
}).json<any>();
|
|
|
|
}).json<any>();
|
|
|
@ -101,11 +117,12 @@ export class CoinGeckoService implements DataProviderInterface {
|
|
|
|
|
|
|
|
|
|
|
|
const { prices } = await got(
|
|
|
|
const { prices } = await got(
|
|
|
|
`${
|
|
|
|
`${
|
|
|
|
this.URL
|
|
|
|
this.apiUrl
|
|
|
|
}/coins/${aSymbol}/market_chart/range?vs_currency=${DEFAULT_CURRENCY.toLowerCase()}&from=${getUnixTime(
|
|
|
|
}/coins/${aSymbol}/market_chart/range?vs_currency=${DEFAULT_CURRENCY.toLowerCase()}&from=${getUnixTime(
|
|
|
|
from
|
|
|
|
from
|
|
|
|
)}&to=${getUnixTime(to)}`,
|
|
|
|
)}&to=${getUnixTime(to)}`,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
headers: this.headers,
|
|
|
|
// @ts-ignore
|
|
|
|
// @ts-ignore
|
|
|
|
signal: abortController.signal
|
|
|
|
signal: abortController.signal
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -163,10 +180,11 @@ export class CoinGeckoService implements DataProviderInterface {
|
|
|
|
}, requestTimeout);
|
|
|
|
}, requestTimeout);
|
|
|
|
|
|
|
|
|
|
|
|
const quotes = await got(
|
|
|
|
const quotes = await got(
|
|
|
|
`${this.URL}/simple/price?ids=${symbols.join(
|
|
|
|
`${this.apiUrl}/simple/price?ids=${symbols.join(
|
|
|
|
','
|
|
|
|
','
|
|
|
|
)}&vs_currencies=${DEFAULT_CURRENCY.toLowerCase()}`,
|
|
|
|
)}&vs_currencies=${DEFAULT_CURRENCY.toLowerCase()}`,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
headers: this.headers,
|
|
|
|
// @ts-ignore
|
|
|
|
// @ts-ignore
|
|
|
|
signal: abortController.signal
|
|
|
|
signal: abortController.signal
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -216,7 +234,8 @@ export class CoinGeckoService implements DataProviderInterface {
|
|
|
|
abortController.abort();
|
|
|
|
abortController.abort();
|
|
|
|
}, this.configurationService.get('REQUEST_TIMEOUT'));
|
|
|
|
}, this.configurationService.get('REQUEST_TIMEOUT'));
|
|
|
|
|
|
|
|
|
|
|
|
const { coins } = await got(`${this.URL}/search?query=${query}`, {
|
|
|
|
const { coins } = await got(`${this.apiUrl}/search?query=${query}`, {
|
|
|
|
|
|
|
|
headers: this.headers,
|
|
|
|
// @ts-ignore
|
|
|
|
// @ts-ignore
|
|
|
|
signal: abortController.signal
|
|
|
|
signal: abortController.signal
|
|
|
|
}).json<any>();
|
|
|
|
}).json<any>();
|
|
|
|