Skip to content

Commit

Permalink
Refactor collateral delta calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
EridianAlpha committed Jun 3, 2024
1 parent f4a838f commit 0d0a200
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/AaveFunctions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,12 @@ contract AaveFunctions is TokenSwaps {
uint256 reinvestedCollateralTotal,
uint256 suppliedCollateralTotal
) internal pure returns (uint256 delta, bool isPositive) {
int256 result =
int256(totalCollateralBase - (reinvestedCollateralTotal * 1e2) - (suppliedCollateralTotal * 1e2));
if (result < 0) {
delta = uint256(-result) / 1e2;
uint256 reinvestedAndSuppliedCollateralBase = (reinvestedCollateralTotal + suppliedCollateralTotal) * 1e2;
if (totalCollateralBase < reinvestedAndSuppliedCollateralBase) {
delta = (reinvestedAndSuppliedCollateralBase - totalCollateralBase) / 1e2;
isPositive = false;
} else {
delta = uint256(result) / 1e2;
delta = (totalCollateralBase - reinvestedAndSuppliedCollateralBase) / 1e2;
isPositive = true;
}
return (delta, isPositive);
Expand Down
37 changes: 36 additions & 1 deletion test/unit/AavePMAaveTest.t.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import {AavePMTestSetup} from "test/unit/AavePMTestSetupTest.t.sol";
import {console} from "forge-std/Test.sol";

import {AavePMTestSetup} from "test/unit/AavePMTestSetupTest.t.sol";
import {IAavePM} from "src/interfaces/IAavePM.sol";

// ================================================================
Expand Down Expand Up @@ -33,4 +34,38 @@ contract AavePMAaveTests is AavePMTestSetup {
vm.expectRevert(IAavePM.AaveFunctions__FlashLoanInitiatorUnauthorized.selector);
aavePM.executeOperation(asset, amount, premium, attacker1, params);
}

function test_Exposed_GetTotalCollateralDelta() public {
// Send ETH from manager1 to the contract
vm.startPrank(manager1);
sendEth(address(this), SEND_VALUE);
vm.stopPrank();

// TODO: These are all magic numbers, but I'm not sure where else to put them while keeping the tests understandable.

// Collateral $1000, reinvested collateral debt $0, supplied collateral $500
(uint256 delta1, bool isPositive1) = _getTotalCollateralDelta(1000 * 1e8, 0 * 1e6, 500 * 1e6);
assertEq(delta1, 500 * 1e6);
assertTrue(isPositive1);

// Collateral $1000, reinvested collateral $0, supplied collateral $1500
(uint256 delta2, bool isPositive2) = _getTotalCollateralDelta(1000 * 1e8, 0, 1000 * 1e6);
assertEq(delta2, 0);
assertTrue(isPositive2);

// Collateral $1000, reinvested collateral $0, supplied collateral $1500
(uint256 delta3, bool isPositive3) = _getTotalCollateralDelta(1000 * 1e8, 0, 1500 * 1e6);
assertEq(delta3, 500 * 1e6);
assertFalse(isPositive3);

// Collateral $1000, reinvested collateral $200, supplied collateral $300
(uint256 delta4, bool isPositive4) = _getTotalCollateralDelta(1000 * 1e8, 200 * 1e6, 300 * 1e6);
assertEq(delta4, 500 * 1e6);
assertTrue(isPositive4);

// Collateral $1000, reinvested collateral $200, supplied collateral $1000
(uint256 delta5, bool isPositive5) = _getTotalCollateralDelta(1000 * 1e8, 200 * 1e6, 1000 * 1e6);
assertEq(delta5, 200 * 1e6);
assertFalse(isPositive5);
}
}

0 comments on commit 0d0a200

Please sign in to comment.