Skip to content

Commit 786defa

Browse files
committed
feat: add future leverage for add and remove collateral
1 parent 9c4ff3b commit 786defa

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/lendMarkets/LendMarketTemplate.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,20 @@ export class LendMarketTemplate {
17471747
return await this._addCollateral(collateral, address, false) as string;
17481748
}
17491749

1750+
public async addCollateralFutureLeverage(collateral: number | string, userAddress = ''): Promise<string> {
1751+
userAddress = _getAddress.call(this.llamalend, userAddress);
1752+
const [userCollateral, {collateral: currentCollateral}] = await Promise.all([
1753+
_getUserCollateral(this.llamalend.constants.NETWORK_NAME, this.addresses.controller, userAddress),
1754+
this.userState(userAddress),
1755+
]);
1756+
1757+
const total_deposit_from_user = userCollateral.total_deposit_from_user_precise;
1758+
const collateralBN = BN(collateral);
1759+
const currentCollateralBN = BN(currentCollateral);
1760+
1761+
return currentCollateralBN.plus(collateralBN).div(BN(total_deposit_from_user).plus(collateralBN)).toString();
1762+
}
1763+
17501764
// ---------------- REMOVE COLLATERAL ----------------
17511765

17521766
public async maxRemovable(): Promise<string> {
@@ -1813,6 +1827,20 @@ export class LendMarketTemplate {
18131827
return await this._removeCollateral(collateral, false) as string;
18141828
}
18151829

1830+
public async removeCollateralFutureLeverage(collateral: number | string, userAddress = ''): Promise<string> {
1831+
userAddress = _getAddress.call(this.llamalend, userAddress);
1832+
const [userCollateral, {collateral: currentCollateral}] = await Promise.all([
1833+
_getUserCollateral(this.llamalend.constants.NETWORK_NAME, this.addresses.controller, userAddress),
1834+
this.userState(userAddress),
1835+
]);
1836+
1837+
const total_deposit_from_user = userCollateral.total_deposit_from_user_precise;
1838+
const collateralBN = BN(collateral);
1839+
const currentCollateralBN = BN(currentCollateral);
1840+
1841+
return currentCollateralBN.minus(collateralBN).div(BN(total_deposit_from_user).minus(collateralBN)).toString();
1842+
}
1843+
18161844
// ---------------- REPAY ----------------
18171845

18181846
private async _repayBands(debt: number | string, address: string): Promise<[bigint, bigint]> {

src/mintMarkets/MintMarketTemplate.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,21 @@ export class MintMarketTemplate {
10011001
return await this._addCollateral(collateral, address, false) as string;
10021002
}
10031003

1004+
public async addCollateralFutureLeverage(collateral: number | string, userAddress = ''): Promise<string> {
1005+
this._checkLeverageForStats();
1006+
userAddress = _getAddress.call(this.llamalend, userAddress);
1007+
const [userCollateral, {collateral: currentCollateral}] = await Promise.all([
1008+
_getUserCollateralCrvUsdFull(this.llamalend.constants.NETWORK_NAME, this.controller, userAddress),
1009+
this.userState(userAddress),
1010+
]);
1011+
1012+
const total_deposit_from_user = userCollateral.total_deposit_from_user_precise ?? userCollateral.total_deposit_precise;
1013+
const collateralBN = BN(collateral);
1014+
const currentCollateralBN = BN(currentCollateral);
1015+
1016+
return currentCollateralBN.plus(collateralBN).div(BN(total_deposit_from_user).plus(collateralBN)).toString();
1017+
}
1018+
10041019
// ---------------- REMOVE COLLATERAL ----------------
10051020

10061021
public async maxRemovable(): Promise<string> {
@@ -1069,6 +1084,21 @@ export class MintMarketTemplate {
10691084
return await this._removeCollateral(collateral, false) as string;
10701085
}
10711086

1087+
public async removeCollateralFutureLeverage(collateral: number | string, userAddress = ''): Promise<string> {
1088+
this._checkLeverageForStats();
1089+
userAddress = _getAddress.call(this.llamalend, userAddress);
1090+
const [userCollateral, {collateral: currentCollateral}] = await Promise.all([
1091+
_getUserCollateralCrvUsdFull(this.llamalend.constants.NETWORK_NAME, this.controller, userAddress),
1092+
this.userState(userAddress),
1093+
]);
1094+
1095+
const total_deposit_from_user = userCollateral.total_deposit_from_user_precise ?? userCollateral.total_deposit_precise;
1096+
const collateralBN = BN(collateral);
1097+
const currentCollateralBN = BN(currentCollateral);
1098+
1099+
return currentCollateralBN.minus(collateralBN).div(BN(total_deposit_from_user).minus(collateralBN)).toString();
1100+
}
1101+
10721102
// ---------------- REPAY ----------------
10731103

10741104
private async _repayBands(debt: number | string, address: string): Promise<[bigint, bigint]> {

0 commit comments

Comments
 (0)