Skip to content

Commit

Permalink
fix: quota min value fixed to proper formula
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmikko authored and lekhovitsky committed Dec 6, 2023
1 parent 75a6ac7 commit e16559a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
2 changes: 1 addition & 1 deletion contracts/libraries/CollateralLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ library CollateralLogic {
unchecked {
valueUSD = convertToUSDFn(priceOracle, balance - 1, token); // U:[CLL-1]
}
weightedValueUSD = Math.min(valueUSD, quotaUSD) * liquidationThreshold / PERCENTAGE_FACTOR; // U:[CLL-1]
weightedValueUSD = Math.min(valueUSD * liquidationThreshold / PERCENTAGE_FACTOR, quotaUSD); // U:[CLL-1]
nonZeroBalance = true; // U:[CLL-1]
}
}
Expand Down
2 changes: 0 additions & 2 deletions contracts/test/unit/core/PriceOracleV3.unit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ contract PriceOracleV3UnitTest is Test, IPriceOracleV3Events {
// ----------------------- //

/// @notice U:[PO-1]: `_getPrice` works as expected
/// forge-config: default.fuzz.runs = 5000
function test_U_PO_01_getPrice_works_as_expected(
int256 answer,
uint256 updatedAt,
Expand Down Expand Up @@ -77,7 +76,6 @@ contract PriceOracleV3UnitTest is Test, IPriceOracleV3Events {
}

/// @notice U:[PO-2]: `_getPriceFeedParams` works as expected
/// forge-config: default.fuzz.runs = 5000
function test_U_PO_02_getPriceFeedParams_works_as_expected(address token, PriceFeedParams memory expectedParams)
public
{
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/unit/credit/CreditManagerV3.unit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ contract CreditManagerV3UnitTest is TestHelper, ICreditManagerV3Events, BalanceH
stEthBalance: 0,
usdcBalance: 0,
expectedTotalValueUSD: 2 * vars.get("LINK_QUOTA_IN_USD"),
expectedTwvUSD: vars.get("LINK_QUOTA_IN_USD") * vars.get("LINK_LT") / PERCENTAGE_FACTOR,
expectedTwvUSD: vars.get("LINK_QUOTA_IN_USD"),
expectedEnabledTokensMask: LINK_TOKEN_MASK
}),
CollateralCalcTestCase({
Expand Down
52 changes: 26 additions & 26 deletions contracts/test/unit/libraries/CollateralLogic.unit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ contract CollateralLogicUnitTest is TestHelper, CollateralLogicHelper {
quotaUSD: 40_00,
//
expectedValueUSD: (5_000 - 1) * 2,
expectedWeightedValueUSD: 40_00 * 80_00 / PERCENTAGE_FACTOR,
expectedWeightedValueUSD: 40_00,
expectedNonZeroBalance: true,
priceOracleCalled: true
})
Expand Down Expand Up @@ -383,28 +383,28 @@ contract CollateralLogicUnitTest is TestHelper, CollateralLogicHelper {
quotas: arrayOf(Q({t: Tokens.USDT, quota: 5_000})),
target: type(uint256).max,
expectedTotalValueUSD: (10_000 - 1) * prices[Tokens.USDT],
expectedTwvUSD: 5_000 * prices[Tokens.DAI] * lts[Tokens.USDT] / PERCENTAGE_FACTOR,
expectedTwvUSD: (5_000 - 1) * prices[Tokens.DAI],
expectedOrder: arrayOf(Tokens.USDT)
}),
CalcQuotedTokenCollateralTestCase({
name: "Two token calc, no target, one twv<quota, another twv > quota",
balances: arrayOf(B({t: Tokens.USDT, balance: 10_000}), B({t: Tokens.LINK, balance: 1_000})),
balances: arrayOf(B({t: Tokens.USDT, balance: 70_000}), B({t: Tokens.LINK, balance: 1_000})),
quotas: arrayOf(Q({t: Tokens.USDT, quota: 5_000}), Q({t: Tokens.LINK, quota: 20_000})),
target: type(uint256).max,
expectedTotalValueUSD: (10_000 - 1) * prices[Tokens.USDT] + (1_000 - 1) * prices[Tokens.LINK],
expectedTwvUSD: 5_000 * prices[Tokens.DAI] * lts[Tokens.USDT] / PERCENTAGE_FACTOR
expectedTotalValueUSD: (70_000 - 1) * prices[Tokens.USDT] + (1_000 - 1) * prices[Tokens.LINK],
expectedTwvUSD: 5_000 * prices[Tokens.DAI]
+ (1_000 - 1) * prices[Tokens.LINK] * lts[Tokens.LINK] / PERCENTAGE_FACTOR,
expectedOrder: arrayOf(Tokens.USDT, Tokens.LINK)
}),
CalcQuotedTokenCollateralTestCase({
name: "Stops when target reached",
balances: arrayOf(B({t: Tokens.USDT, balance: 10_000})),
balances: arrayOf(B({t: Tokens.USDT, balance: 20_000})),
quotas: arrayOf(
Q({t: Tokens.USDT, quota: 5_000}), Q({t: Tokens.WETH, quota: 50}), Q({t: Tokens.LINK, quota: 50_000})
),
target: 5_000 * prices[Tokens.DAI] * lts[Tokens.USDT] / PERCENTAGE_FACTOR,
expectedTotalValueUSD: (10_000 - 1) * prices[Tokens.USDT],
expectedTwvUSD: 5_000 * prices[Tokens.DAI] * lts[Tokens.USDT] / PERCENTAGE_FACTOR,
target: 5_000 * prices[Tokens.DAI],
expectedTotalValueUSD: (20_000 - 1) * prices[Tokens.USDT],
expectedTwvUSD: 5_000 * prices[Tokens.DAI],
expectedOrder: arrayOf(Tokens.USDT)
})
];
Expand Down Expand Up @@ -500,53 +500,53 @@ contract CollateralLogicUnitTest is TestHelper, CollateralLogicHelper {
expectedOrder: arrayOf(Tokens.USDT)
}),
CalcCollateralTestCase({
name: "One quoted token calc, no target, no hints, value < quota",
balances: arrayOf(B({t: Tokens.USDT, balance: 10_000})),
name: "One quoted token calc, no target, no hints, value > quota",
balances: arrayOf(B({t: Tokens.USDT, balance: 20_000})),
quotas: arrayOf(Q({t: Tokens.USDT, quota: 5_000})),
enabledTokensMask: getTokenMask(arrayOf(Tokens.USDT)),
quotedTokensMask: getTokenMask(arrayOf(Tokens.USDT)),
lazy: false,
minHealthFactor: PERCENTAGE_FACTOR,
collateralHints: new uint256[](0),
totalDebtUSD: 0,
expectedTotalValueUSD: (10_000 - 1) * prices[Tokens.USDT],
expectedTwvUSD: 5_000 * prices[Tokens.DAI] * lts[Tokens.USDT] / PERCENTAGE_FACTOR,
expectedTotalValueUSD: (20_000 - 1) * prices[Tokens.USDT],
expectedTwvUSD: 5_000 * prices[Tokens.DAI],
expectedTokensToDisable: 0,
expectedOrder: arrayOf(Tokens.USDT)
}),
CalcCollateralTestCase({
name: "It removes non-quoted tokens with 0 and 1 balances",
balances: arrayOf(B({t: Tokens.USDT, balance: 10_000}), B({t: Tokens.LINK, balance: 1})),
balances: arrayOf(B({t: Tokens.USDT, balance: 20_000}), B({t: Tokens.LINK, balance: 1})),
quotas: arrayOf(Q({t: Tokens.USDT, quota: 5_000})),
enabledTokensMask: getTokenMask(arrayOf(Tokens.USDT, Tokens.LINK, Tokens.DAI)),
quotedTokensMask: getTokenMask(arrayOf(Tokens.USDT)),
lazy: false,
minHealthFactor: PERCENTAGE_FACTOR,
collateralHints: new uint256[](0),
totalDebtUSD: 0,
expectedTotalValueUSD: (10_000 - 1) * prices[Tokens.USDT],
expectedTwvUSD: 5_000 * prices[Tokens.DAI] * lts[Tokens.USDT] / PERCENTAGE_FACTOR,
expectedTotalValueUSD: (20_000 - 1) * prices[Tokens.USDT],
expectedTwvUSD: 5_000 * prices[Tokens.DAI],
expectedTokensToDisable: getTokenMask(arrayOf(Tokens.LINK, Tokens.DAI)),
expectedOrder: arrayOf(Tokens.USDT, Tokens.DAI, Tokens.LINK)
}),
CalcCollateralTestCase({
name: "It stops if target reached during quoted token collateral computation",
balances: arrayOf(B({t: Tokens.USDT, balance: 10_000}), B({t: Tokens.LINK, balance: 1})),
balances: arrayOf(B({t: Tokens.USDT, balance: 20_000}), B({t: Tokens.LINK, balance: 1})),
quotas: arrayOf(Q({t: Tokens.USDT, quota: 5_000}), Q({t: Tokens.WETH, quota: 5_000})),
enabledTokensMask: getTokenMask(arrayOf(Tokens.USDT, Tokens.WETH, Tokens.LINK, Tokens.DAI)),
quotedTokensMask: getTokenMask(arrayOf(Tokens.USDT, Tokens.WETH)),
lazy: true,
minHealthFactor: 2 * PERCENTAGE_FACTOR,
collateralHints: new uint256[](0),
totalDebtUSD: (5_000 * prices[Tokens.DAI] * lts[Tokens.USDT] / PERCENTAGE_FACTOR) / 2,
expectedTotalValueUSD: (10_000 - 1) * prices[Tokens.USDT],
expectedTwvUSD: 5_000 * prices[Tokens.DAI] * lts[Tokens.USDT] / PERCENTAGE_FACTOR,
totalDebtUSD: 5_000 * prices[Tokens.DAI] / 2,
expectedTotalValueUSD: (20_000 - 1) * prices[Tokens.USDT],
expectedTwvUSD: 5_000 * prices[Tokens.DAI],
expectedTokensToDisable: 0,
expectedOrder: arrayOf(Tokens.USDT)
}),
CalcCollateralTestCase({
name: "It stops if target reached during non-quoted token collateral computation, and updates target properly after quoted calc",
balances: arrayOf(B({t: Tokens.USDT, balance: 10_000}), B({t: Tokens.DAI, balance: 8_000})),
balances: arrayOf(B({t: Tokens.USDT, balance: 20_000}), B({t: Tokens.DAI, balance: 8_000})),
quotas: arrayOf(Q({t: Tokens.USDT, quota: 5_000}), Q({t: Tokens.WETH, quota: 5_000})),
enabledTokensMask: getTokenMask(arrayOf(Tokens.USDT, Tokens.WETH, Tokens.LINK, Tokens.DAI)),
quotedTokensMask: getTokenMask(arrayOf(Tokens.USDT, Tokens.WETH)),
Expand All @@ -555,15 +555,15 @@ contract CollateralLogicUnitTest is TestHelper, CollateralLogicHelper {
collateralHints: new uint256[](0),
totalDebtUSD: 5_000 * prices[Tokens.DAI] * lts[Tokens.USDT] / PERCENTAGE_FACTOR
+ (8_000 - 1) * prices[Tokens.DAI] * lts[Tokens.DAI] / PERCENTAGE_FACTOR,
expectedTotalValueUSD: (10_000 - 1) * prices[Tokens.USDT] + (8_000 - 1) * prices[Tokens.DAI],
expectedTwvUSD: 5_000 * prices[Tokens.DAI] * lts[Tokens.USDT] / PERCENTAGE_FACTOR
expectedTotalValueUSD: (20_000 - 1) * prices[Tokens.USDT] + (8_000 - 1) * prices[Tokens.DAI],
expectedTwvUSD: 5_000 * prices[Tokens.DAI]
+ (8_000 - 1) * prices[Tokens.DAI] * lts[Tokens.DAI] / PERCENTAGE_FACTOR,
expectedTokensToDisable: 0,
expectedOrder: arrayOf(Tokens.USDT, Tokens.WETH, Tokens.DAI)
}),
CalcCollateralTestCase({
name: "Collateral hints work for non-quoted tokens",
balances: arrayOf(B({t: Tokens.USDT, balance: 10_000}), B({t: Tokens.DAI, balance: 8_000})),
balances: arrayOf(B({t: Tokens.USDT, balance: 20_000}), B({t: Tokens.DAI, balance: 8_000})),
quotas: arrayOf(Q({t: Tokens.USDT, quota: 5_000})),
enabledTokensMask: getTokenMask(arrayOf(Tokens.USDT, Tokens.WETH, Tokens.LINK, Tokens.DAI)),
quotedTokensMask: getTokenMask(arrayOf(Tokens.USDT)),
Expand All @@ -572,8 +572,8 @@ contract CollateralLogicUnitTest is TestHelper, CollateralLogicHelper {
collateralHints: getHints(arrayOf(Tokens.WETH, Tokens.LINK)),
totalDebtUSD: 5_000 * prices[Tokens.DAI] * lts[Tokens.USDT] / PERCENTAGE_FACTOR
+ (8_000 - 1) * prices[Tokens.DAI] * lts[Tokens.DAI] / PERCENTAGE_FACTOR,
expectedTotalValueUSD: (10_000 - 1) * prices[Tokens.USDT] + (8_000 - 1) * prices[Tokens.DAI],
expectedTwvUSD: 5_000 * prices[Tokens.DAI] * lts[Tokens.USDT] / PERCENTAGE_FACTOR
expectedTotalValueUSD: (20_000 - 1) * prices[Tokens.USDT] + (8_000 - 1) * prices[Tokens.DAI],
expectedTwvUSD: 5_000 * prices[Tokens.DAI]
+ (8_000 - 1) * prices[Tokens.DAI] * lts[Tokens.DAI] / PERCENTAGE_FACTOR,
expectedTokensToDisable: getTokenMask(arrayOf(Tokens.WETH, Tokens.LINK)),
expectedOrder: arrayOf(Tokens.USDT, Tokens.WETH, Tokens.LINK, Tokens.DAI)
Expand Down

0 comments on commit e16559a

Please sign in to comment.