@ajna-finance/sdk
Version:
A typescript SDK that can be used to create Dapps in Ajna ecosystem.
123 lines • 5.99 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Liquidation = void 0;
const tslib_1 = require("tslib");
const ethers_1 = require("ethers");
const constants_1 = require("../constants");
const pool_1 = require("../contracts/pool");
const pool_info_utils_1 = require("../contracts/pool-info-utils");
const time_1 = require("../utils/time");
/**
* Models an auction used to liquidate an undercollateralized borrower.
*/
class Liquidation {
/**
* @param provider JSON-RPC endpoint.
* @param pool Identifies pool to which this bucket belongs.
* @param borrowerAddress Identifies the loan being liquidated.
*/
constructor(provider, pool, borrowerAddress) {
this.provider = provider;
this.poolContract = pool;
this.utilsContract = (0, pool_info_utils_1.getPoolInfoUtilsContract)(this.provider);
this.borrowerAddress = borrowerAddress;
}
/**
* Retrieve current state of the auction.
* @returns {@link AuctionStatus}
*/
getStatus() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const [kickTimestamp, collateral, debtToCover, isCollateralized, price, neutralPrice] = yield (0, pool_info_utils_1.auctionStatus)(this.utilsContract, this.poolContract.address, this.borrowerAddress);
return Liquidation._prepareAuctionStatus(yield (0, time_1.getBlockTime)(this.provider), kickTimestamp, collateral, debtToCover, isCollateralized, price, neutralPrice);
});
}
static _prepareAuctionStatus(currentTimestamp, kickTimestamp, collateral, debtToCover, isCollateralized, price, neutralPrice) {
const kickTimestampNumber = kickTimestamp.toNumber();
const kickTime = new Date(kickTimestampNumber * 1000);
const elapsedTime = currentTimestamp - kickTimestampNumber;
const zero = ethers_1.constants.Zero;
const isTakeable = collateral.gt(zero);
const isSettleable = kickTimestampNumber > 0 && (elapsedTime >= constants_1.HOUR_TO_SECONDS * 72 || collateral.eq(0));
return {
kickTime,
collateral,
debtToCover,
isTakeable,
isCollateralized,
price,
neutralPrice,
isSettleable,
};
}
/**
* Performs arb take operation during debt liquidation auction.
* @param signer taker
* @param bucketIndex identifies the price bucket
* @returns promise to transaction
*/
arbTake(signer, bucketIndex) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const contractPoolWithSigner = this.poolContract.connect(signer);
return (0, pool_1.bucketTake)(contractPoolWithSigner, this.borrowerAddress, false, bucketIndex);
});
}
/**
* Performs deposit take operation during debt liquidation auction.
* @param signer taker
* @param bucketIndex identifies the price bucket
* @returns promise to transaction
*/
depositTake(signer, bucketIndex) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const contractPoolWithSigner = this.poolContract.connect(signer);
return (0, pool_1.bucketTake)(contractPoolWithSigner, this.borrowerAddress, true, bucketIndex);
});
}
// TODO: update this to support both pool types -> erc721 pool will have problems with the default value
// ALTERNATIVE: create a new `take method for each pool type
/**
* called by actors to purchase collateral from the auction in exchange for quote token
* @param signer taker
* @param maxAmount max amount of collateral that will be taken from the auction
* @returns promise to transaction
*/
take(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.take)(contractPoolWithSigner, this.borrowerAddress, maxAmount, yield signer.getAddress());
});
}
/**
* Called by actors to purchase collateral from the auction in exchange for quote token with otption to invoke callback function.
* @param signer taker
* @param maxAmount max amount of collateral that will be taken from the auction
* @param callee identifies where collateral should be sent and where quote token should be obtained
* @param callData if provided, take will assume the callee implements IERC*Taker. Take will send collateral to
* callee before passing this data to IERC*Taker.atomicSwapCallback. If not provided,
* the callback function will not be invoked.
* @returns promise to transaction
*/
// TODO: needs to be tested properly
takeWithCall(signer, maxAmount, callee, callData) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const contractPoolWithSigner = this.poolContract.connect(signer);
return (0, pool_1.take)(contractPoolWithSigner, this.borrowerAddress, maxAmount, callee, callData);
});
}
/**
* Called by actors to settle an amount of debt in a completed liquidation.
* @param signer settler
* @param maxDepth measured from HPB, maximum number of buckets deep to settle debt,
* used to prevent unbounded iteration clearing large liquidations
* @returns promise to transaction
*/
settle(signer_1) {
return tslib_1.__awaiter(this, arguments, void 0, function* (signer, maxDepth = constants_1.MAX_SETTLE_BUCKETS) {
const contractPoolWithSigner = this.poolContract.connect(signer);
return (0, pool_1.settle)(contractPoolWithSigner, this.borrowerAddress, maxDepth);
});
}
}
exports.Liquidation = Liquidation;
//# sourceMappingURL=Liquidation.js.map