Skip to content

Commit

Permalink
update extractData methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cfaur09 committed Aug 8, 2024
1 parent a2c6175 commit 776666d
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/endpoints/tokens/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,10 +786,11 @@ export class TokenService {
token.marketCap = token.price * NumberUtils.denominateString(token.circulatingSupply, token.decimals);
}
} else if (token.assets?.priceSource?.url) {
const tokenData = await this.fetchTokenDataFromUrl(token.assets.priceSource.url);
const pathToPrice = "0.usdPrice";
const tokenData = await this.fetchTokenDataFromUrl(token.assets.priceSource.url, pathToPrice);

if (tokenData) {
token.price = tokenData.usdPrice;
token.price = tokenData;

const supply = await this.esdtService.getTokenSupply(token.identifier);
token.supply = supply.totalSupply;
Expand All @@ -811,13 +812,27 @@ export class TokenService {
return tokens;
}

private async fetchTokenDataFromUrl(url: string): Promise<any> {
private extractData(data: any, path: string): any {
const keys = path.split('.');
let result: any = data;

for (const key of keys) {
if (result === undefined || result === null) {
return undefined;
}
result = !isNaN(Number(key)) ? result[Number(key)] : result[key];
}
return result;
}

private async fetchTokenDataFromUrl(url: string, path: string): Promise<any> {
try {
const response = await fetch(url);
const data = await response.json();
const extractedValue = this.extractData(data, path);

if (data && Array.isArray(data) && data.length > 0) {
return data[0];
if (extractedValue) {
return extractedValue;
} else {
this.logger.error(`No valid data found at URL: ${url}`);
}
Expand Down

0 comments on commit 776666d

Please sign in to comment.