Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dashboard] Change third party service to get HMT price #3152

Merged
merged 1 commit into from
Mar 5, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const DEFAULT_CORS_ALLOWED_ORIGIN = 'http://localhost:3001';
const DEFAULT_CORS_ALLOWED_HEADERS = 'Content-Type, Accept';

const DEFAULT_HMT_PRICE_SOURCE =
'https://api.coingecko.com/api/v3/simple/price?ids=human-protocol&vs_currencies=usd';
'https://api.coinlore.net/api/ticker/?id=53887';
const DEFAULT_HMT_PRICE_FROM = 'human-protocol';
const DEFAULT_HMT_PRICE_TO = 'usd';
const DEFAULT_HCAPTCHA_STATS_SOURCE =
Expand Down Expand Up @@ -53,8 +53,10 @@ export class EnvironmentConfigService {
DEFAULT_HMT_PRICE_SOURCE,
);
}
get hmtPriceSourceApiKey(): string {
return this.configService.getOrThrow<string>('HMT_PRICE_SOURCE_API_KEY');
get hmtPriceSourceApiKey(): string | undefined {
return this.configService.get<string | undefined>(
'HMT_PRICE_SOURCE_API_KEY',
);
}
get hmtPriceFromKey(): string {
return this.configService.get<string>(
Expand Down
42 changes: 33 additions & 9 deletions packages/apps/dashboard/server/src/modules/stats/stats.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,22 +321,46 @@ export class StatsService implements OnModuleInit {
return cachedHmtPrice;
}

const headers = this.envConfigService.hmtPriceSourceApiKey
? { 'x-cg-demo-api-key': this.envConfigService.hmtPriceSourceApiKey }
: {};

const { data } = await lastValueFrom(
this.httpService.get(this.envConfigService.hmtPriceSource, {
headers: {
'x-cg-demo-api-key': this.envConfigService.hmtPriceSourceApiKey,
},
}),
this.httpService.get(this.envConfigService.hmtPriceSource, { headers }),
);
const hmtPrice =
data[this.envConfigService.hmtPriceFromKey][
this.envConfigService.hmtPriceToKey
];

let hmtPrice: number;

if (this.envConfigService.hmtPriceSource.includes('coingecko')) {
if (
!data ||
!data[this.envConfigService.hmtPriceFromKey] ||
!data[this.envConfigService.hmtPriceFromKey][
this.envConfigService.hmtPriceToKey
]
) {
throw new Error('Failed to fetch HMT price from CoinGecko API');
}
hmtPrice = parseFloat(
data[this.envConfigService.hmtPriceFromKey][
this.envConfigService.hmtPriceToKey
],
);
} else if (this.envConfigService.hmtPriceSource.includes('coinlore')) {
if (!data || !data[0] || !data[0].price_usd) {
throw new Error('Failed to fetch HMT price from Coinlore API');
}
hmtPrice = parseFloat(data[0].price_usd);
} else {
throw new Error('Unsupported HMT price source');
}

await this.cacheManager.set(
this.redisConfigService.hmtPriceCacheKey,
hmtPrice,
this.redisConfigService.cacheHmtPriceTTL,
);

return hmtPrice;
}

Expand Down