Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions prices/prices.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ export class PricesController {
@ApiResponse({
description: 'Returns the price of EUR in USD',
})
getEuroPrice(): PriceQueryCurrencies {
getEuroPrice(): Promise<PriceQueryCurrencies> {
return this.pricesService.getEuroPrice();
}

@Get('deps')
@ApiResponse({
description: 'Returns the current price of the DEPS token',
})
async getDepsPrice(): Promise<PriceQueryCurrencies> {
return await this.pricesService.getDepsPrice();
getDepsPrice(): Promise<PriceQueryCurrencies> {
return this.pricesService.getDepsPrice();
}
}
29 changes: 23 additions & 6 deletions prices/prices.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ export class PricesService {

async getDepsPrice(): Promise<PriceQueryCurrencies> {
if (!this.depsPrice) this.depsPrice = await this.fetchFromEcosystemDeps(this.getDeps());
return { usd: Number(this.depsPrice.usd.toFixed(4)), eur: Number(this.depsPrice.eur.toFixed(4)) };
if (!this.euroPrice) this.euroPrice = await this.fetchEuroPrice();

return {
usd: Number(this.depsPrice.usd.toFixed(4)),
eur: Number(this.depsPrice.eur.toFixed(4)),
btc: Number((this.depsPrice.eur * this.euroPrice.btc).toFixed(9)),
};
}

getCollateral(): ApiPriceERC20Mapping {
Expand All @@ -84,8 +90,14 @@ export class PricesService {
return c;
}

getEuroPrice(): PriceQueryCurrencies {
return this.euroPrice;
async getEuroPrice(): Promise<PriceQueryCurrencies> {
if (!this.euroPrice) this.euroPrice = await this.fetchEuroPrice();

return {
usd: Number(this.euroPrice.usd.toFixed(4)),
eur: Number(this.euroPrice.eur.toFixed(4)),
btc: Number(this.euroPrice.btc.toFixed(9)),
};
}

async fetchFromEcosystemDeps(erc: ERC20Info): Promise<PriceQueryCurrencies | null> {
Expand Down Expand Up @@ -135,13 +147,18 @@ export class PricesService {
}

async fetchEuroPrice(): Promise<PriceQueryCurrencies | null> {
const url = `/api/v3/simple/price?ids=usd&vs_currencies=eur`;
const data = await (await COINGECKO_CLIENT(url)).json();
const url = `/api/v3/simple/price?ids=usd&vs_currencies=eur%2Cbtc`;
const data = await(await COINGECKO_CLIENT(url)).json();
if (data.status) {
this.logger.debug(data.status?.error_message || 'Error fetching price from coingecko');
return null;
}
return { eur: 1, usd: 1 / Number(data.usd.eur) };

return {
eur: 1,
usd: 1 / Number(data.usd.eur),
btc: 1 / Number(data.usd.eur / data.usd.btc),
};
}

async fetchFromZchfSources(): Promise<PriceQueryCurrencies | null> {
Expand Down
1 change: 1 addition & 0 deletions prices/prices.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type ERC20Info = {
export type PriceQueryCurrencies = {
usd?: number;
eur?: number;
btc?: number;
};

export type PriceQuery = ERC20Info & {
Expand Down