@tracer-protocol/pools-js
Version:
Javascript library for interacting with Tracer's Perpetual Pools
142 lines (119 loc) • 5.09 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _types = require("@tracer-protocol/pool-state-helper/types");
var _bignumber = _interopRequireDefault(require("bignumber.js"));
var _multicall = require("@0xsequence/multicall");
var _ethers = require("ethers");
var _utils = require("../utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* PoolStateHelper class for interacting with on chain PoolStateHelper contracts
* The constructor is private so must be instantiated with {@linkcode PoolStateHelper.Create}
*/
class PoolStateHelper {
/**
* @private
*/
constructor() {
this.address = '';
this.poolAddress = '';
this.committerAddress = '';
this.fullCommitPeriod = 1;
this.provider = undefined;
this.multicallProvider = undefined;
}
/**
* Replacement constructor pattern to support async initialisations
* @param tokenINfo {@link IPoolStateHelper | IPoolStateHelper interface props}
* @returns a Promise containing an initialised PoolStateHelper class ready to be used
*/
static Create = async config => {
const poolStateHelper = new PoolStateHelper(); // initialise the poolStateHelper;
await poolStateHelper.init(config);
return poolStateHelper;
};
/**
* Creates an empty PoolStateHelper that can be used as a default
* @returns default constructed poolStateHelper
*/
static CreateDefault = () => {
const poolStateHelper = new PoolStateHelper();
return poolStateHelper;
};
/**
* Private initialisation function called in {@link PoolStateHelper.Create}
* @private
* @param config {@link IPoolStateHelper | IPoolStateHelper interface props}
*/
init = async config => {
this.provider = config.provider;
this.multicallProvider = new _multicall.providers.MulticallProvider(config.provider);
this.address = config.address;
this.poolAddress = config.poolAddress;
this.committerAddress = config.committerAddress;
const contract = _types.PoolStateHelper__factory.connect(config.address, config.provider);
this._contract = contract;
const fullCommitPeriod = await contract.fullCommitPeriod(config.poolAddress);
this.fullCommitPeriod = fullCommitPeriod.toNumber();
};
/**
* 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 PoolStateHelper: provider cannot be undefined");
}
this.provider = provider;
this.multicallProvider = new _multicall.providers.MulticallProvider(provider);
this._contract = this._contract?.connect(this.multicallProvider);
};
getExpectedPoolState = async ({
periods
}) => {
if (!this._contract) {
throw Error("Failed to fetch expected pool state: Contract not defined");
}
if (!this.poolAddress) {
throw Error("Failed to fetch expected pool state: poolAddress not set");
}
const expectedState = await this._contract.getExpectedState(this.poolAddress, periods);
return {
longSupply: (0, _utils.ethersBNtoBN)(expectedState.longSupply),
longBalance: (0, _utils.ethersBNtoBN)(expectedState.longBalance),
pendingLongTokenBurn: (0, _utils.ethersBNtoBN)(expectedState.remainingPendingLongBurnTokens),
shortSupply: (0, _utils.ethersBNtoBN)(expectedState.shortSupply),
shortBalance: (0, _utils.ethersBNtoBN)(expectedState.shortBalance),
pendingShortTokenBurn: (0, _utils.ethersBNtoBN)(expectedState.remainingPendingShortBurnTokens),
// oracle wrapper formats price to decimal places
// pool state helper leaves oracle price in wad, format here to be consistent with
oraclePrice: new _bignumber.default(_ethers.ethers.utils.formatEther(expectedState.oraclePrice))
};
};
getCommitQueue = async ({
periods
}) => {
if (!this._contract) {
throw Error("Failed to commit queue: Contract not defined");
}
if (!this.poolAddress) {
throw Error("Failed to commit queue: poolAddress not set");
}
if (!this.committerAddress) {
throw Error("Failed to commit queue: committerAddress not set");
}
const commitQueue = await this._contract.getCommitQueue(this.committerAddress, periods);
return commitQueue.map(totalPoolCommitment => ({
longMintSettlement: (0, _utils.ethersBNtoBN)(totalPoolCommitment.longMintSettlement),
longBurnPoolTokens: (0, _utils.ethersBNtoBN)(totalPoolCommitment.longBurnPoolTokens),
shortMintSettlement: (0, _utils.ethersBNtoBN)(totalPoolCommitment.shortMintSettlement),
shortBurnPoolTokens: (0, _utils.ethersBNtoBN)(totalPoolCommitment.shortBurnPoolTokens),
shortBurnLongMintPoolTokens: (0, _utils.ethersBNtoBN)(totalPoolCommitment.shortBurnLongMintPoolTokens),
longBurnShortMintPoolTokens: (0, _utils.ethersBNtoBN)(totalPoolCommitment.longBurnShortMintPoolTokens)
}));
};
}
exports.default = PoolStateHelper;