UNPKG

@tracer-protocol/pools-js

Version:

Javascript library for interacting with Tracer's Perpetual Pools

164 lines (140 loc) 6.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultCommitter = 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 _helpers = require("../utils/helpers"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const defaultCommitter = { pendingLong: { mint: new _bignumber.default(0), burn: new _bignumber.default(0) }, pendingShort: { mint: new _bignumber.default(0), burn: new _bignumber.default(0) }, settlementTokenDecimals: 18, mintingFee: new _bignumber.default(0), burningFee: new _bignumber.default(0) }; /** * Interface for interacting with the PoolComitter. * Can be used standalone, but is always initialised within a * {@linkcode Pool} * The constructor is private so must be instantiated with {@linkcode Committer.Create} */ exports.defaultCommitter = defaultCommitter; class Committer { // decimal percentages constructor() { // these all need to be ovverridden in the init function this.address = ''; this.provider = undefined; this.multicallProvider = undefined; this.pendingLong = { ...defaultCommitter.pendingLong }; this.pendingShort = { ...defaultCommitter.pendingShort }; this.settlementTokenDecimals = defaultCommitter.settlementTokenDecimals; this.mintingFee = defaultCommitter.mintingFee; this.burningFee = defaultCommitter.burningFee; } /** * Replacement constructor pattern to support async initialisations * @param committerInfo {@link IPoolCommitter | IPoolCommitter interface props} * @returns a Promise containing an initialised Committer class ready to be used */ static Create = async committerInfo => { const committer = new Committer(); // initialise the token; await committer.init(committerInfo); return committer; }; /** * Creates an empty committer that can be used as a default */ static CreateDefault = () => { const committer = new Committer(); return committer; }; /** * Private initialisation function called in {@link Committer.Create} * @param committerInfo {@link IPoolCommitter | IPoolCommitter interface props} */ init = async commitInfo => { this.provider = commitInfo.provider; this.multicallProvider = new _multicall.providers.MulticallProvider(commitInfo.provider); this.address = commitInfo.address; this.settlementTokenDecimals = commitInfo.settlementTokenDecimals; const contract = _types.PoolCommitter__factory.connect(commitInfo.address, this.multicallProvider); this._contract = contract; try { const [mintingFee, burningFee, { pendingLong, pendingShort }] = await Promise.all([this._contract.getMintingFee(), this._contract.getBurningFee(), this.fetchAllShadowPools()]); this.mintingFee = new _bignumber.default(_ethers.ethers.utils.formatEther(mintingFee)); this.burningFee = new _bignumber.default(_ethers.ethers.utils.formatEther(burningFee)); this.pendingShort = pendingShort; this.pendingLong = pendingLong; } catch (error) { throw Error("Failed to initialise committer: " + error); } }; /** * Submits a commit * @param type 1 of 4 commit types. These values are (0, 1, 2, 3) => (shortMint, shortBurn, longMint, longBurn) * @param amount either a number or BigNumber representing the amount of tokens * @param payForClaim * @param fromAggregateBalances is true when the user wants to pay from balances yet to be claimed, * false if they want to use the balances within their wallet * to be committed if burning, or the amount of quote token to use to mint new tokens */ commit = (type, amount, payForClaim, fromAggregateBalances) => { if (!this._contract) throw Error("Failed to commit: this._contract undefined"); return this._contract.commit((0, _helpers.encodeCommitParams)(payForClaim, fromAggregateBalances, type, _ethers.ethers.utils.parseUnits(amount.toString(), this.settlementTokenDecimals))); }; /** * Updates all shadow pool balances. * Calls {@linkcode Committer.fetchShadowPool} for each of the commit types. * As such this will also set the internal state of the Class * @returns all refetched shadow pool balances */ fetchAllShadowPools = async () => { if (!this._contract) throw Error("Failed to update pending amounts: this._contract undefined"); // current update interval const updateInterval = await this._contract.updateIntervalId(); const [[pendingLongMintSettlement, // eslint-disable-next-line _, // pendingLongBurnTokens pendingShortMintSettlement], pendingLongBurnTokens, pendingShortBurnTokens] = await Promise.all([this._contract.totalPoolCommitments(updateInterval), this._contract.pendingLongBurnPoolTokens(), this._contract.pendingShortBurnPoolTokens()]).catch(error => { throw Error("Failed to update pending amounts: " + error?.message); }); const decimals = this.settlementTokenDecimals; return { pendingLong: { mint: new _bignumber.default(_ethers.ethers.utils.formatUnits(pendingLongMintSettlement ?? 0, decimals)), burn: new _bignumber.default(_ethers.ethers.utils.formatUnits(pendingLongBurnTokens ?? 0, decimals)) }, pendingShort: { mint: new _bignumber.default(_ethers.ethers.utils.formatUnits(pendingShortMintSettlement ?? 0, decimals)), burn: new _bignumber.default(_ethers.ethers.utils.formatUnits(pendingShortBurnTokens ?? 0, 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 Committer: provider cannot be undefined"); } this.provider = provider; this.multicallProvider = new _multicall.providers.MulticallProvider(provider); this._contract = this._contract?.connect(this.multicallProvider); }; } exports.default = Committer;