@d8x/perpetuals-sdk
Version:
Node TypeScript SDK for D8X Perpetual Futures
131 lines • 6.62 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const d8XMath_1 = require("./d8XMath");
const perpetualDataHandler_1 = __importDefault(require("./perpetualDataHandler"));
const writeAccessHandler_1 = __importDefault(require("./writeAccessHandler"));
/**
* Functions to provide liquidity. This class requires a private key and executes
* smart-contract interactions that require gas-payments.
* @extends WriteAccessHandler
*/
class LiquidityProviderTool extends writeAccessHandler_1.default {
/**
* Constructor
* @param {NodeSDKConfig} config Configuration object, see PerpetualDataHandler.
* readSDKConfig.
* @example
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
* async function main() {
* console.log(LiquidityProviderTool);
* // load configuration for Polygon zkEVM (testnet)
* const config = PerpetualDataHandler.readSDKConfig("cardona");
* // LiquidityProviderTool (authentication required, PK is an environment variable with a private key)
* const pk: string = <string>process.env.PK;
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
* // Create a proxy instance to access the blockchain
* await lqudtProviderTool.createProxyInstance();
* }
* main();
*
* @param {string | Signer} signer Private key or ethers Signer of the account
*/
constructor(config, signer) {
super(config, signer);
}
/**
* Add liquidity to the PnL participant fund. The address gets pool shares in return.
* @param {string} poolSymbolName Name of pool symbol (e.g. MATIC)
* @param {number} amountCC Amount in pool-collateral currency
* @example
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
* async function main() {
* console.log(LiquidityProviderTool);
* // setup (authentication required, PK is an environment variable with a private key)
* const config = PerpetualDataHandler.readSDKConfig("cardona");
* const pk: string = <string>process.env.PK;
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
* await lqudtProviderTool.createProxyInstance();
* // add liquidity
* await lqudtProviderTool.setAllowance("MATIC");
* let respAddLiquidity = await lqudtProviderTool.addLiquidity("MATIC", 0.1);
* console.log(respAddLiquidity);
* }
* main();
*
* @return Transaction object
*/
async addLiquidity(poolSymbolName, amountCC, overrides) {
if (this.proxyContract == null || this.signer == null) {
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
}
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
let decimals = this.getMarginTokenDecimalsFromSymbol(poolSymbolName);
let tx = await this.proxyContract.addLiquidity(poolId, (0, d8XMath_1.floatToDecN)(amountCC, decimals), overrides || { gasLimit: this.gasLimit });
return tx;
}
/**
* Initiates a liquidity withdrawal from the pool
* It triggers a time-delayed unlocking of the given number of pool shares.
* The amount of pool shares to be unlocked is fixed by this call, but not their value in pool currency.
* @param {string} poolSymbolName Name of pool symbol (e.g. MATIC).
* @param {string} amountPoolShares Amount in pool-shares, removes everything if > available amount.
* @example
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
* async function main() {
* console.log(LiquidityProviderTool);
* // setup (authentication required, PK is an environment variable with a private key)
* const config = PerpetualDataHandler.readSDKConfig("cardona");
* const pk: string = <string>process.env.PK;
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
* await lqudtProviderTool.createProxyInstance();
* // initiate withdrawal
* let respRemoveLiquidity = await lqudtProviderTool.initiateLiquidityWithdrawal("MATIC", 0.1);
* console.log(respRemoveLiquidity);
* }
* main();
*
* @return Transaction object.
*/
async initiateLiquidityWithdrawal(poolSymbolName, amountPoolShares, overrides) {
if (this.proxyContract == null || this.signer == null) {
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
}
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
let tx = await this.proxyContract.withdrawLiquidity(poolId, (0, d8XMath_1.floatToDec18)(amountPoolShares), overrides || { gasLimit: this.gasLimit });
return tx;
}
/**
* Withdraws as much liquidity as there is available after a call to initiateLiquidityWithdrawal.
* The address loses pool shares in return.
* @param poolSymbolName
* @example
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
* async function main() {
* console.log(LiquidityProviderTool);
* // setup (authentication required, PK is an environment variable with a private key)
* const config = PerpetualDataHandler.readSDKConfig("cardona");
* const pk: string = <string>process.env.PK;
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
* await lqudtProviderTool.createProxyInstance();
* // remove liquidity
* let respRemoveLiquidity = await lqudtProviderTool.executeLiquidityWithdrawal("MATIC", 0.1);
* console.log(respRemoveLiquidity);
* }
* main();
*
* @returns Transaction object.
*/
async executeLiquidityWithdrawal(poolSymbolName, overrides) {
if (this.proxyContract == null || this.signer == null) {
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
}
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
let tx = await this.proxyContract.executeLiquidityWithdrawal(poolId, this.traderAddr, overrides || { gasLimit: this.gasLimit });
return tx;
}
}
exports.default = LiquidityProviderTool;
//# sourceMappingURL=liquidityProviderTool.js.map