UNPKG

@tracer-protocol/pools-js

Version:

Javascript library for interacting with Tracer's Perpetual Pools

159 lines (133 loc) 5.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _types = require("@tracer-protocol/perpetual-pools-contracts/types"); var _bignumber = _interopRequireDefault(require("bignumber.js")); var _multicall = require("@0xsequence/multicall"); var _ethers = require("ethers"); var _ = require(".."); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class PoolToken { /** * @private */ constructor() { // these all need to be ovverridden in the init function this.address = ''; this.provider = undefined; this.multicallProvider = undefined; this.name = ''; this.symbol = ''; this.decimals = 18; this.pool = ''; // not ideal but it can default to a long token this.side = _.SideEnum.long; this.supply = new _bignumber.default(0); } /** * Replacement constructor pattern to support async initialisations * @param tokenINfo {@link IPoolToken | IPoolToken interface props} * @returns a Promise containing an initialised PoolToken class ready to be used */ static Create = async tokenInfo => { const token = new PoolToken(); await token.init(tokenInfo); return token; }; /** * Creates an empty PoolToken that can be used as a default * @returns default constructed pool token */ static CreateDefault = () => { const token = new PoolToken(); return token; }; /** * Private initialisation function called in {@link PoolToken.Create} * @param tokenInfo {@link IPoolToken | IPoolToken interface props} */ init = async tokenInfo => { this.provider = tokenInfo.provider; this.multicallProvider = new _multicall.providers.MulticallProvider(tokenInfo.provider); this.address = tokenInfo.address; this.pool = tokenInfo.pool; this.side = tokenInfo.side; const contract = _types.PoolToken__factory.connect(tokenInfo.address, tokenInfo.provider); this._contract = contract; const [name, symbol, decimals, supply] = await Promise.all([tokenInfo?.name ? tokenInfo?.name : contract.name(), tokenInfo?.symbol ? tokenInfo?.symbol : contract.symbol(), tokenInfo?.decimals ? tokenInfo?.decimals : contract.decimals(), contract.totalSupply()]); this.name = name; this.symbol = symbol; this.decimals = decimals; this.supply = new _bignumber.default(_ethers.ethers.utils.formatUnits(supply, decimals)); }; /** * Fetch an accounts balance for this token * @param account Account to check balance of * @returns The accounts balance formatted in a BigNumber */ fetchBalance = async account => { if (!this._contract) { throw Error("Failed to fetch balance: this._contract undefined"); } const balanceOf = await this._contract.balanceOf(account).catch(error => { throw Error("Failed to fetch balance: " + error?.message); }); return new _bignumber.default(_ethers.ethers.utils.formatUnits(balanceOf, this.decimals)); }; /** * Fetch an accounts allowance for a given spender * @param account Account to check allowance * @param spender Spender of the accounts tokens * @returns the allowance the account has given to the spender address to spend this pool token */ fetchAllowance = async (account, spender) => { if (!this._contract) { throw Error("Failed to fetch allowance: this._contract undefined"); } const balanceOf = await this._contract.allowance(account, spender).catch(error => { throw Error("Failed to fetch allowance: " + error?.message); }); return new _bignumber.default(_ethers.ethers.utils.formatUnits(balanceOf, this.decimals)); }; /** * Fetch and set the total token supply * @returns most up to date token supply */ fetchSupply = async () => { if (!this._contract) { throw Error("Failed to fetch token supply: this._contract undefined"); } const totalSupply = await this._contract.totalSupply().catch(error => { throw Error("Failed to fetch token supply: " + error?.message); }); const formattedSupply = new _bignumber.default(_ethers.ethers.utils.formatUnits(totalSupply, this.decimals)); this.supply = formattedSupply; return formattedSupply; }; /** * Approve a spender to spend the signers accounts pool tokens * @param spender the address of the contract that will spend the pool tokens * @param amount the amount the signer is allowing the spender to spend * @returns an ethers transaction */ approve = async (spender, amount) => { if (!this._contract) { throw Error("Failed to approve token: this._contract undefined"); } return this._contract.approve(spender, _ethers.ethers.utils.formatUnits(amount.toString(), this.decimals)); }; /** * Replaces the provider and connects the contract instance * @param provider The new provider to connect to */ connect = provider => { if (!provider) { throw Error("Failed to connect PoolToken: provider cannot be undefined"); } this.provider = provider; this.multicallProvider = new _multicall.providers.MulticallProvider(provider); this._contract = this._contract?.connect(this.multicallProvider); }; } exports.default = PoolToken;