rubic-sdk
Version:
Simplify dApp creation
53 lines • 2.32 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoingeckoApi = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const decorators_1 = require("../../common/utils/decorators");
const backend_blockchains_1 = require("../blockchain/models/backend-blockchains");
/**
* Works with coingecko api to get tokens prices in usd.
*/
class CoingeckoApi {
constructor(httpClient) {
this.httpClient = httpClient;
}
async getTokenPriceFromBackend(blockchain, tokenAddress) {
try {
const backendBlockchain = backend_blockchains_1.TO_BACKEND_BLOCKCHAINS[blockchain];
const result = await this.httpClient.get(`https://api.rubic.exchange/api/v2/tokens/price/${backendBlockchain}/${tokenAddress}`);
return result;
}
catch (error) {
console.debug(error);
return {
network: blockchain,
address: tokenAddress,
usd_price: null
};
}
}
/**
* Gets price of common token or native coin in usd from coingecko.
* @param token Token to get price for.
*/
async getTokenPrice(token) {
const response = await this.getTokenPriceFromBackend(token.blockchain, token.address);
return new bignumber_js_1.default(response?.usd_price || NaN);
}
}
exports.CoingeckoApi = CoingeckoApi;
__decorate([
(0, decorators_1.Cache)({
maxAge: 1000 * 60 * 5
})
], CoingeckoApi.prototype, "getTokenPriceFromBackend", null);
//# sourceMappingURL=coingecko-api.js.map