tornado-oracles/lib/tokenPriceOracle.js

50 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2024-05-12 17:49:53 +00:00
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenPriceOracle = void 0;
const ethers_1 = require("ethers");
const contracts_1 = require("./contracts");
const constants_1 = require("./constants");
const multicall_1 = require("./multicall");
const providers_1 = require("./providers");
class TokenPriceOracle {
constructor(netId, rpcUrl, config) {
const { offchainOracleContract } = config;
this.provider = (0, providers_1.getProvider)(netId, rpcUrl, config);
this.multicall = contracts_1.Multicall__factory.connect(constants_1.multiCallAddress, this.provider);
if (offchainOracleContract) {
this.oracle = contracts_1.OffchainOracle__factory.connect(offchainOracleContract, this.provider);
}
}
fetchPrices(tokens) {
return __awaiter(this, void 0, void 0, function* () {
// setup mock price for testnets
if (!this.oracle) {
return new Promise((resolve) => resolve(tokens.reduce((acc, _, index) => {
acc[tokens[index].symbol] = (0, ethers_1.parseEther)('0.0001').toString();
return acc;
}, {})));
}
const prices = (yield (0, multicall_1.multicall)(this.multicall, tokens.map(({ tokenAddress }) => ({
contract: this.oracle,
name: 'getRateToEth',
params: [tokenAddress, true],
}))));
return prices.reduce((acc, price, index) => {
const tokenPriceInwei = (price * BigInt(10 ** tokens[index].decimals)) / BigInt(10 ** 18);
acc[tokens[index].symbol.toLowerCase()] = tokenPriceInwei.toString();
return acc;
}, {});
});
}
}
exports.TokenPriceOracle = TokenPriceOracle;
//# sourceMappingURL=tokenPriceOracle.js.map