@tracer-protocol/pools-js
Version:
Javascript library for interacting with Tracer's Perpetual Pools
139 lines (116 loc) • 4.47 kB
JavaScript
"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");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Token class for interacting with ERC20 tokens
* The constructor is private so must be instantiated with {@linkcode Token.Create}
*/
class Token {
/**
* @private
*/
constructor() {
// these all need to be overridden in the init function
this.address = '';
this.provider = undefined;
this.multicallProvider = undefined;
this.name = '';
this.symbol = '';
this.decimals = 18;
}
/**
* Replacement constructor pattern to support async initialisations
* @param tokenINfo {@link IToken | IToken interface props}
* @returns a Promise containing an initialised Token class ready to be used
*/
static Create = async tokenInfo => {
const token = new Token(); // initialise the token;
await token.init(tokenInfo);
return token;
};
/**
* Creates an empty Token that can be used as a default
* @returns default constructed token
*/
static CreateDefault = () => {
const token = new Token();
return token;
};
/**
* Private initialisation function called in {@link Token.Create}
* @private
* @param tokenInfo {@link IToken | IToken interface props}
*/
init = async tokenInfo => {
this.provider = tokenInfo.provider;
this.multicallProvider = new _multicall.providers.MulticallProvider(tokenInfo.provider);
this.address = tokenInfo.address;
const contract = _types.ERC20__factory.connect(tokenInfo.address, this.multicallProvider);
this._contract = contract;
const [name, symbol, decimals] = await Promise.all([tokenInfo?.name ? tokenInfo?.name : contract.name(), tokenInfo?.symbol ? tokenInfo?.symbol : contract.symbol(), tokenInfo?.decimals ? tokenInfo?.decimals : contract.decimals()]);
this.name = name;
this.symbol = symbol;
this.decimals = 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 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));
};
/**
* Approve a spender to spend the signers accounts tokens
* @param spender the address of the contract that will spend the 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 Token: provider cannot be undefined");
}
this.provider = provider;
this.multicallProvider = new _multicall.providers.MulticallProvider(provider);
this._contract = this._contract?.connect(this.multicallProvider);
};
}
exports.default = Token;