UNPKG

@ajna-finance/sdk

Version:

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

195 lines 9.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Bucket = void 0; const tslib_1 = require("tslib"); const ethers_1 = require("ethers"); const constants_1 = require("../constants"); const common_1 = require("../contracts/common"); const pool_1 = require("../contracts/pool"); const pool_info_utils_1 = require("../contracts/pool-info-utils"); const numeric_1 = require("../utils/numeric"); const pricing_1 = require("../utils/pricing"); const time_1 = require("../utils/time"); const lender_helper_1 = require("../contracts/lender-helper"); /** * Models a price bucket in a pool. */ class Bucket { /** * @param provider JSON-RPC endpoint. * @param pool Pool to which this bucket belongs. * @param index Price bucket index. */ constructor(provider, pool, index) { /** * Calculate how much quote token could currently be exchanged for LP. * @param lpBalance amount of LP to redeem for quote token * @returns The current amount of quote tokens that can be exchanged for the given LP, WAD units. */ this.lpToQuoteTokens = (lpTokens) => tslib_1.__awaiter(this, void 0, void 0, function* () { return yield (0, pool_info_utils_1.lpToQuoteTokens)(this.contractUtils, this.poolContract.address, lpTokens, this.index); }); /** * Calculate how much collateral could be exchanged for LP. * @param lpBalance amount of LP to redeem for collateral * @returns The exact amount of collateral that can be exchanged for the given LP, WAD units. */ this.lpToCollateral = (lpTokens) => tslib_1.__awaiter(this, void 0, void 0, function* () { return yield (0, pool_info_utils_1.lpToCollateral)(this.contractUtils, this.poolContract.address, lpTokens, this.index); }); this.provider = provider; this.pool = pool; this.poolContract = pool.contract; this.contractUtils = (0, pool_info_utils_1.getPoolInfoUtilsContract)(this.provider); this.index = index; this.price = (0, pricing_1.indexToPrice)(index); this.bucketName = `${pool.name} bucket ${this.index} (${(0, numeric_1.fromWad)(this.price)})`; } toString() { return this.bucketName; } /** * 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 promise to transaction */ multicall(signer, callData) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const contractPoolWithSigner = this.poolContract.connect(signer); return (0, common_1.multicall)(contractPoolWithSigner, callData); }); } /** * Retrieve current state of the bucket or by index if provided. * @returns {@link BucketStatus} */ getStatus(index) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const [, deposit, collateral, bucketLP, , exchangeRate] = yield (0, pool_info_utils_1.bucketInfo)(this.contractUtils, this.poolContract.address, index || this.index); return { deposit, collateral, bucketLP, exchangeRate, }; }); } /** * Deposits quote token into the bucket. * @param signer lender * @param amount amount to deposit * @param ttlSeconds revert if not processed in this amount of block time * @returns promise to transaction */ addQuoteToken(signer, amount, ttlSeconds) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (0, lender_helper_1.addQuoteToken)(signer, this.pool.poolAddress, amount, this.index, yield (0, time_1.getExpiry)(this.provider, ttlSeconds)); }); } /** * Moves quote token from current bucket to another bucket. * @param signer lender * @param toIndex price bucket to which quote token should be deposited * @param maxAmountToMove optionally limits amount to move * @param ttlSeconds revert if not processed in this amount of time * @returns promise to transaction */ moveQuoteToken(signer_1, toIndex_1) { return tslib_1.__awaiter(this, arguments, void 0, function* (signer, toIndex, maxAmountToMove = ethers_1.constants.MaxUint256, ttlSeconds) { return (0, lender_helper_1.moveQuoteToken)(signer, this.pool.poolAddress, maxAmountToMove, this.index, toIndex, yield (0, time_1.getExpiry)(this.provider, ttlSeconds)); }); } /** * Removes quote token from the bucket. * @param signer lender * @param maxAmount optionally limits amount to remove * @returns promise to transaction */ removeQuoteToken(signer_1) { return tslib_1.__awaiter(this, arguments, void 0, function* (signer, maxAmount = ethers_1.constants.MaxUint256) { const contractPoolWithSigner = this.poolContract.connect(signer); return (0, pool_1.removeQuoteToken)(contractPoolWithSigner, maxAmount, this.index); }); } /** * Shows a lender's position in a single bucket. * @returns {@link Position} */ getPosition(lenderAddress) { return tslib_1.__awaiter(this, void 0, void 0, function* () { // pool contract multicall to find pending debt and LPB let data = yield this.pool.ethcallProvider.all([ this.pool.contractMulti.debtInfo(), this.pool.contractMulti.lenderInfo(this.index, lenderAddress), ]); const lpBalance = ethers_1.BigNumber.from(data[1][0]); // info contract multicall to get htp and calculate token amounts for LPB const pricesInfoCall = this.pool.contractUtilsMulti.poolPricesInfo(this.poolContract.address); const lpToQuoteCall = this.pool.contractUtilsMulti.lpToQuoteTokens(this.poolContract.address, lpBalance, this.index); const lpToCollateralCall = this.pool.contractUtilsMulti.lpToCollateral(this.poolContract.address, lpBalance, this.index); data = yield this.pool.ethcallProvider.all([pricesInfoCall, lpToQuoteCall, lpToCollateralCall]); const htpIndex = +data[0][3]; const lupIndex = +data[0][5]; const depositRedeemable = ethers_1.BigNumber.from(data[1]); const collateralRedeemable = ethers_1.BigNumber.from(data[2]); let depositWithdrawable; if (this.index > lupIndex) { // if withdrawing below the LUP (higher index), the withdrawal cannot affect the LUP depositWithdrawable = depositRedeemable; } else { let liquidityBetweenLupAndHtp = (0, numeric_1.toWad)(0); // if pool is collateralized (LUP above HTP), find debt between current LUP and HTP if (lupIndex < htpIndex) { data = yield this.pool.ethcallProvider.all([ this.pool.contractMulti.depositUpToIndex(lupIndex), this.pool.contractMulti.depositUpToIndex(htpIndex), ]); liquidityBetweenLupAndHtp = ethers_1.BigNumber.from(data[1]).sub(ethers_1.BigNumber.from(data[0])); } depositWithdrawable = liquidityBetweenLupAndHtp.lt(depositRedeemable) ? liquidityBetweenLupAndHtp : depositRedeemable; } return { lpBalance, depositRedeemable, collateralRedeemable, depositWithdrawable, }; }); } estimateDepositFeeRate() { return tslib_1.__awaiter(this, void 0, void 0, function* () { // current annualized rate divided by 365*3 (8 hours of interest) return yield this.pool.utils.contract.depositFeeRate(this.pool.poolAddress); }); } /** * Retrieves a lender's LP balance in a bucket. * @param lenderAddress lender * @param index fenwick index of the desired bucket * @returns LP balance */ lpBalance(lenderAddress) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const [lpBalance] = yield (0, pool_1.lenderInfo)(this.poolContract, lenderAddress, this.index); return lpBalance; }); } /** * Kick a loan with a lender's liquidity based on a LUP calculated as if they withdraw liquidity. * @param signer lender * @param limitIndex bucket in which lender has an LP balance * @returns promise to transaction */ lenderKick(signer_1) { return tslib_1.__awaiter(this, arguments, void 0, function* (signer, limitIndex = constants_1.MAX_FENWICK_INDEX) { const contractPoolWithSigner = this.poolContract.connect(signer); return (0, pool_1.lenderKick)(contractPoolWithSigner, this.index, limitIndex); }); } } exports.Bucket = Bucket; //# sourceMappingURL=Bucket.js.map