carbon-js-sdk 0.3.39 → 0.3.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { Token } from "../codec";
1
+ import { Token, TokenPrice } from "../codec";
2
2
  import { NetworkConfigProvider } from "../constant";
3
3
  import { FeeQuote } from "../hydrogen/feeQuote";
4
4
  import { BlockchainUtils, TypeUtils } from "../util";
@@ -57,6 +57,8 @@ declare class TokenClient {
57
57
  reloadDenomGeckoMap(): Promise<void>;
58
58
  reloadUSDValues(denoms?: string[]): Promise<TypeUtils.SimpleMap<BigNumber>>;
59
59
  getUSDValuesFromCoinGecko(geckoIds: string[]): Promise<any>;
60
+ processTokenPrices(tokenPrices: TokenPrice[]): TypeUtils.SimpleMap<BigNumber>;
61
+ getUSDValuesFromPricingModule(): Promise<TypeUtils.SimpleMap<BigNumber>>;
60
62
  getDenomToGeckoIdMap(): Promise<TypeUtils.SimpleMap<string>>;
61
63
  setCommonAssetConfig(): void;
62
64
  }
@@ -415,25 +415,30 @@ class TokenClient {
415
415
  this.usdValues.iusd = number_1.BN_ONE;
416
416
  //Get corresponding geckoId for denoms and removes any duplicated geckoIds (espeically for different wrapped tokens as they correspond to the same geckoId(same price))
417
417
  const geckoIds = denoms.reduce((coinIds, denom) => {
418
- var _a;
419
- const geckoId = (_a = this.geckoTokenNames[denom]) !== null && _a !== void 0 ? _a : denom;
418
+ // To ensure that ibc denoms are not added to the gecko ids list, the default is removed.
419
+ const geckoId = this.geckoTokenNames[denom];
420
420
  if (geckoId && !coinIds.includes(geckoId)) {
421
421
  coinIds.push(geckoId);
422
422
  }
423
423
  return coinIds;
424
424
  }, []);
425
425
  const geckoIdToUsdPriceMap = yield this.getUSDValuesFromCoinGecko(geckoIds);
426
+ const carbonTokenPrices = yield this.getUSDValuesFromPricingModule();
426
427
  const uscStablecoin = this.getNativeStablecoin();
427
428
  //store price based on denoms
428
429
  for (const denom of denoms) {
430
+ const carbonTokenPrice = carbonTokenPrices[denom];
431
+ // if token price in pricing module exists for denom, return that as usd price first
432
+ // else check coingecko
433
+ if (carbonTokenPrice) {
434
+ this.usdValues[denom] = carbonTokenPrice;
435
+ continue;
436
+ }
429
437
  const coinId = (_a = this.geckoTokenNames[denom]) !== null && _a !== void 0 ? _a : denom;
430
438
  const price = util_1.NumberUtils.bnOrZero((_b = geckoIdToUsdPriceMap === null || geckoIdToUsdPriceMap === void 0 ? void 0 : geckoIdToUsdPriceMap[coinId]) === null || _b === void 0 ? void 0 : _b.usd);
431
439
  if (price.gt(0)) {
432
- if (denom === (uscStablecoin === null || uscStablecoin === void 0 ? void 0 : uscStablecoin.denom)) {
433
- this.usdValues[denom] = constant_1.uscUsdValue;
434
- continue;
435
- }
436
- this.usdValues[denom] = price;
440
+ // if denom is usc, then return uscUsdValue, else return coingecko usd price
441
+ this.usdValues[denom] = denom === (uscStablecoin === null || uscStablecoin === void 0 ? void 0 : uscStablecoin.denom) ? constant_1.uscUsdValue : price;
437
442
  }
438
443
  }
439
444
  return this.usdValues;
@@ -441,10 +446,45 @@ class TokenClient {
441
446
  }
442
447
  getUSDValuesFromCoinGecko(geckoIds) {
443
448
  return __awaiter(this, void 0, void 0, function* () {
444
- const response = yield util_1.FetchUtils.fetch(`https://coingecko-proxy.dem.exchange/price?ids=${geckoIds.join(",")}&vs_currencies=usd`);
449
+ const response = yield util_1.FetchUtils.fetch(`https://coingecko-proxy.dem.exchange/api/v3/simple/price?ids=${geckoIds.join(",")}&vs_currencies=usd`);
445
450
  return yield response.json();
446
451
  });
447
452
  }
453
+ processTokenPrices(tokenPrices) {
454
+ return tokenPrices.reduce((prevPrices, price) => {
455
+ const newPrev = prevPrices;
456
+ newPrev[price.denom] = number_1.bnOrZero(price.twap).shiftedBy(-constant_1.decTypeDecimals);
457
+ return newPrev;
458
+ }, {});
459
+ }
460
+ getUSDValuesFromPricingModule() {
461
+ var _a, _b, _c, _d;
462
+ return __awaiter(this, void 0, void 0, function* () {
463
+ const initTokenPrices = yield this.query.pricing.TokenPriceAll({
464
+ pagination: {
465
+ limit: new long_1.default(10000),
466
+ offset: new long_1.default(0),
467
+ key: new Uint8Array(),
468
+ countTotal: true,
469
+ reverse: false,
470
+ },
471
+ });
472
+ if (((_a = initTokenPrices.pagination) === null || _a === void 0 ? void 0 : _a.total) && ((_b = initTokenPrices.pagination) === null || _b === void 0 ? void 0 : _b.total.lt(10000))) {
473
+ const tokenPricesMap = this.processTokenPrices(initTokenPrices.tokenPrices);
474
+ return tokenPricesMap;
475
+ }
476
+ const fullTokenPrices = yield this.query.pricing.TokenPriceAll({
477
+ pagination: {
478
+ limit: (_d = (_c = initTokenPrices.pagination) === null || _c === void 0 ? void 0 : _c.total) !== null && _d !== void 0 ? _d : new long_1.default(0),
479
+ offset: new long_1.default(0),
480
+ key: new Uint8Array(),
481
+ countTotal: true,
482
+ reverse: false,
483
+ },
484
+ });
485
+ return this.processTokenPrices(fullTokenPrices.tokenPrices);
486
+ });
487
+ }
448
488
  getDenomToGeckoIdMap() {
449
489
  var _a;
450
490
  return __awaiter(this, void 0, void 0, function* () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "carbon-js-sdk",
3
- "version": "0.3.39",
3
+ "version": "0.3.40",
4
4
  "description": "TypeScript SDK for Carbon blockchain",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",