@evolutionland/evolution-js
Version:
evolution evolution-js evolutionland evolution-js-sdk evolution-land metaverse
70 lines (69 loc) • 3.77 kB
JavaScript
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());
});
};
/* eslint-disable @typescript-eslint/no-empty-function */
import { Contract } from "@ethersproject/contracts";
import { getNetwork } from "@ethersproject/networks";
import { getDefaultProvider } from "@ethersproject/providers";
import { TokenAmount } from "./entities/fractions/tokenAmount";
import { Pair } from "./entities/pair";
import IPancakePair from "./abis/IUniswapV2Pair.json";
import invariant from "tiny-invariant";
import ERC20 from "./abis/ERC20.json";
import { ChainId } from "./constants";
import { Token } from "./entities/token";
let TOKEN_DECIMALS_CACHE = {
[ChainId.MAINNET]: {
"0xE0B7927c4aF23765Cb51314A0E0521A9645F0E2A": 9, // DGD
},
};
/**
* Contains methods for constructing instances of pairs and tokens from on-chain data.
*/
export class Fetcher {
/**
* Cannot be constructed.
*/
constructor() { }
/**
* Fetch information for a given token on the given chain, using the given ethers provider.
* @param chainId chain of the token
* @param address address of the token on the chain
* @param provider provider used to fetch the token
* @param symbol optional symbol of the token
* @param name optional name of the token
*/
static fetchTokenData(chainId, address, provider = getDefaultProvider(getNetwork(chainId)), symbol, name) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const parsedDecimals = typeof ((_a = TOKEN_DECIMALS_CACHE === null || TOKEN_DECIMALS_CACHE === void 0 ? void 0 : TOKEN_DECIMALS_CACHE[chainId]) === null || _a === void 0 ? void 0 : _a[address]) === "number"
? TOKEN_DECIMALS_CACHE[chainId][address]
: yield new Contract(address, ERC20, provider).decimals().then((decimals) => {
TOKEN_DECIMALS_CACHE = Object.assign(Object.assign({}, TOKEN_DECIMALS_CACHE), { [chainId]: Object.assign(Object.assign({}, TOKEN_DECIMALS_CACHE === null || TOKEN_DECIMALS_CACHE === void 0 ? void 0 : TOKEN_DECIMALS_CACHE[chainId]), { [address]: decimals }) });
return decimals;
});
return new Token(chainId, address, parsedDecimals, symbol, name);
});
}
/**
* Fetches information about a pair and constructs a pair from the given two tokens.
* @param tokenA first token
* @param tokenB second token
* @param provider the provider to use to fetch the data
*/
static fetchPairData(tokenA, tokenB, provider = getDefaultProvider(getNetwork(tokenA.chainId))) {
return __awaiter(this, void 0, void 0, function* () {
invariant(tokenA.chainId === tokenB.chainId, "CHAIN_ID");
const address = Pair.getAddress(tokenA, tokenB);
const [reserves0, reserves1] = yield new Contract(address, IPancakePair.abi, provider).getReserves();
const balances = tokenA.sortsBefore(tokenB) ? [reserves0, reserves1] : [reserves1, reserves0];
return new Pair(new TokenAmount(tokenA, balances[0]), new TokenAmount(tokenB, balances[1]));
});
}
}