@ajna-finance/sdk
Version:
A typescript SDK that can be used to create Dapps in Ajna ecosystem.
62 lines • 2.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FungibleBucket = void 0;
const tslib_1 = require("tslib");
const ethers_1 = require("ethers");
const Bucket_1 = require("./Bucket");
const types_1 = require("../types");
const numeric_1 = require("../utils/numeric");
/**
* Models a price bucket in a pool with ERC-20 collateral.
*/
class FungibleBucket extends Bucket_1.Bucket {
/**
* @param provider JSON-RPC endpoint.
* @param pool Pool to which this bucket belongs.
* @param index Price bucket index.
*/
constructor(provider, pool, index) {
super(provider, pool, index);
}
/**
* Withdraw all available liquidity from the given bucket using multicall transaction (first quote token, then - collateral if LP is left).
* @param signer address to redeem LP
* @returns promise to transaction
*/
withdrawLiquidity(signer) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
// get bucket details
const bucketStatus = yield this.getStatus();
// determine lender's LP balance
const signerAddress = yield signer.getAddress();
const [lpBalance] = yield this.poolContract.lenderInfo(this.index, signerAddress);
// multiply by exchange rate to estimate amount of quote token they can withdraw
const estimatedDepositWithdrawal = (0, numeric_1.wmul)(lpBalance, bucketStatus.exchangeRate);
// if lender has nothing to redeem, exit
if (lpBalance.eq(ethers_1.constants.Zero)) {
throw new types_1.SdkError(`${signerAddress} has no LP in bucket ${this.index}`);
}
// if there is any quote token in the bucket, redeem LP for deposit first
const callData = [];
if (lpBalance && bucketStatus.deposit.gt(0)) {
callData.push({
methodName: 'removeQuoteToken',
args: [ethers_1.constants.MaxUint256, this.index],
});
}
// CAUTION: This estimate may cause revert because we cannot predict exchange rate for an
// arbitrary future block where the TX will be processed.
const withdrawCollateral = (bucketStatus.deposit.eq(0) || estimatedDepositWithdrawal.gt(bucketStatus.deposit)) &&
bucketStatus.collateral.gt(0);
if (withdrawCollateral) {
callData.push({
methodName: 'removeCollateral',
args: [ethers_1.constants.MaxUint256, this.index],
});
}
return this.multicall(signer, callData);
});
}
}
exports.FungibleBucket = FungibleBucket;
//# sourceMappingURL=FungibleBucket.js.map