UNPKG

@ajna-finance/sdk

Version:

A typescript SDK that can be used to create Dapps in Ajna ecosystem.

128 lines 5.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LPToken = void 0; const tslib_1 = require("tslib"); const common_1 = require("../contracts/common"); const pool_1 = require("../contracts/pool"); const position_manager_1 = require("../contracts/position-manager"); const types_1 = require("../types"); const time_1 = require("../utils/time"); class LPToken { /** * @param provider JSON-RPC endpoint. * @param tokenId uniquely identifies this LP token */ constructor(provider, tokenId) { this.provider = provider; this.tokenId = tokenId; this.contractPositionManager = (0, position_manager_1.getPositionManagerContract)(this.provider); } toString() { return 'AJNA LP token ' + this.tokenId; } tokenURI() { return tslib_1.__awaiter(this, void 0, void 0, function* () { return yield (0, position_manager_1.tokenURI)(this.provider, this.tokenId); }); } isIndexInPosition(index_1) { return tslib_1.__awaiter(this, arguments, void 0, function* (index, tokenId = this.tokenId) { return yield (0, position_manager_1.isIndexInPosition)(this.provider, tokenId, index); }); } getPositionIndexesFiltered() { return tslib_1.__awaiter(this, void 0, void 0, function* () { return yield (0, position_manager_1.getPositionIndexesFiltered)(this.provider, this.tokenId); }); } poolKey() { return tslib_1.__awaiter(this, void 0, void 0, function* () { return yield (0, position_manager_1.poolKey)(this.provider, this.tokenId); }); } /** * Moves LP balance from one or more buckets into this position NFT. * @param signer lender * @param pool pool in which lender added liquidity * @param indexes identifies the buckets which lender wants their LP moved into this position NFT * @returns promise to transaction */ memorializePositions(signer, pool, indexes, overrides) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const poolContract = pool.connect(signer); for (const index of indexes) { const allowance = yield (0, pool_1.lpAllowance)(poolContract, index, this.contractPositionManager.address, yield signer.getAddress()); const [lpBalance] = yield (0, pool_1.lenderInfo)(poolContract, yield signer.getAddress(), index); if (allowance.lt(lpBalance)) { throw new types_1.SdkError(`Insufficient LP Balance: ${allowance} < ${lpBalance}`); } } return (0, position_manager_1.memorializePositions)(signer, pool.address, this.tokenId, indexes, overrides); }); } /** * Returns an array of bucket indexes in which current token has liquidity. * @param signer consumer initiating transactions * @returns Array of bucket indexes. */ getPositionIndexes(signer) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const positionIndices = yield (0, position_manager_1.getPositionIndexes)(signer, this.tokenId); return positionIndices.map(x => x.toNumber()); }); } /** * Removes LP balance from position NFT, placing back into pool. * @param signer lender * @param pool pool for which this position NFT is holding LP balance for the lender * @param indexes identifies the buckets in which lender wants LP balance moved out of this position NFT * @returns promise to transaction */ redeemPositions(signer, pool, indexes, overrides) { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (indexes.length === 0) { throw new types_1.SdkError(`No indexes in position for token id: ${this.tokenId}`); } for (const index of indexes) { if (!(yield this.isIndexInPosition(index, this.tokenId))) { throw new types_1.SdkError(`Index ${index} is not in position for token id: ${this.tokenId}`); } } return yield (0, position_manager_1.redeemPositions)(signer, pool.address, this.tokenId, indexes, overrides); }); } /** * Moves memorialized liquidity to a different bucket, allowing LP token holder to adjust position for market price change. * @param signer lender * @param pool pool in which memorialized liquidity should be moved * @param fromIndex existing bucket in which lender's position is memorialized * @param toIndex target bucket into which lender wants their liquidity moved * @param ttlSeconds revert if not processed in this amount of time * @returns promise to transaction */ moveLiquidity(signer, pool, fromIndex, toIndex, ttlSeconds, overrides) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (0, position_manager_1.moveLiquidity)(signer, pool.address, this.tokenId, fromIndex, toIndex, yield (0, time_1.getExpiry)(this.provider, ttlSeconds), overrides); }); } /** * Returns an instance to an existing LP token. * @param tokenId identifies the token * @returns LPToken instance */ static fromTokenId(provider, tokenId) { return new LPToken(provider, tokenId); } /** * Enables signer to bundle transactions together atomically in a single request. * @param signer consumer initiating transactions * @param callData array of transactions to sign and submit * @returns transaction */ multicall(signer, callData) { const contractPoolWithSigner = this.contractPositionManager.connect(signer); return (0, common_1.multicall)(contractPoolWithSigner, callData); } } exports.LPToken = LPToken; //# sourceMappingURL=LPToken.js.map