Skip to content

Commit

Permalink
Rename _getRandomSender to _getRandomUser
Browse files Browse the repository at this point in the history
  • Loading branch information
aviggiano committed Jan 11, 2024
1 parent 5b719d3 commit 218bef7
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 117 deletions.
4 changes: 2 additions & 2 deletions test/invariants/Helper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ abstract contract Helper is Deploy, PropertiesConstants {
uint256 internal constant MAX_RATE = 2e18;
uint256 internal constant MAX_TIME_BUCKETS = 24;

function _getRandomSender(address sender) internal pure returns (address) {
return uint160(sender) % 3 == 0 ? USER1 : uint160(sender) % 3 == 1 ? USER2 : USER3;
function _getRandomUser(address user) internal pure returns (address) {
return uint160(user) % 3 == 0 ? USER1 : uint160(user) % 3 == 1 ? USER2 : USER3;
}

function _getRandomYieldCurve(uint256 seed) internal pure returns (YieldCurve memory) {
Expand Down
232 changes: 117 additions & 115 deletions test/invariants/TargetFunctions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ abstract contract TargetFunctions is Deploy, Helper, Properties, BaseTargetFunct
) public getSender {
__before();

lender = _getRandomSender(lender);
lender = _getRandomUser(lender);
amount = between(amount, 0, MAX_AMOUNT_USDC * 1e12 / 100);
dueDate = between(dueDate, block.timestamp, block.timestamp + MAX_DURATION);

Expand All @@ -121,13 +121,13 @@ abstract contract TargetFunctions is Deploy, Helper, Properties, BaseTargetFunct

__after();

if (lender == sender) {
eq(_after.sender.borrowAmount, _before.sender.borrowAmount, BORROW_03);
} else {
gt(_after.sender.borrowAmount, _before.sender.borrowAmount, BORROW_01);
}

if (amount > size.config().minimumCredit) {
if (lender == sender) {
eq(_after.sender.borrowAmount, _before.sender.borrowAmount, BORROW_03);
} else {
gt(_after.sender.borrowAmount, _before.sender.borrowAmount, BORROW_01);
}

if (virtualCollateralLoanIds.length > 0) {
gte(_after.activeLoans, _before.activeLoans + 1, BORROW_02);
} else {
Expand All @@ -136,42 +136,42 @@ abstract contract TargetFunctions is Deploy, Helper, Properties, BaseTargetFunct
}
}

// function borrowAsLimitOrder(uint256 maxAmount, uint256 yieldCurveSeed) public getSender {
// __before();
function borrowAsLimitOrder(uint256 maxAmount, uint256 yieldCurveSeed) public getSender {
__before();

// maxAmount = between(maxAmount, 0, MAX_AMOUNT_USDC * 1e12);
// YieldCurve memory curveRelativeTime = _getRandomYieldCurve(yieldCurveSeed);
maxAmount = between(maxAmount, 0, MAX_AMOUNT_USDC * 1e12);
YieldCurve memory curveRelativeTime = _getRandomYieldCurve(yieldCurveSeed);

// hevm.prank(sender);
// size.borrowAsLimitOrder(BorrowAsLimitOrderParams({maxAmount: maxAmount, curveRelativeTime: curveRelativeTime}));
hevm.prank(sender);
size.borrowAsLimitOrder(BorrowAsLimitOrderParams({maxAmount: maxAmount, curveRelativeTime: curveRelativeTime}));

// __after();
// }
__after();
}

// function lendAsMarketOrder(address borrower, uint256 dueDate, uint256 amount, bool exactAmountIn)
// public
// getSender
// {
// __before();
function lendAsMarketOrder(address borrower, uint256 dueDate, uint256 amount, bool exactAmountIn)
public
getSender
{
__before();

// borrower = _getRandomSender(borrower);
// dueDate = between(dueDate, block.timestamp, block.timestamp + MAX_DURATION);
// amount = between(amount, 0, _before.sender.borrowAmount / 10);
borrower = _getRandomUser(borrower);
dueDate = between(dueDate, block.timestamp, block.timestamp + MAX_DURATION);
amount = between(amount, 0, _before.sender.borrowAmount / 10);

// hevm.prank(sender);
// size.lendAsMarketOrder(
// LendAsMarketOrderParams({borrower: borrower, dueDate: dueDate, amount: amount, exactAmountIn: exactAmountIn})
// );
hevm.prank(sender);
size.lendAsMarketOrder(
LendAsMarketOrderParams({borrower: borrower, dueDate: dueDate, amount: amount, exactAmountIn: exactAmountIn})
);

// __after();
__after();

// if (sender == borrower) {
// eq(_after.sender.borrowAmount, _before.sender.borrowAmount, BORROW_03);
// } else {
// lt(_after.sender.borrowAmount, _before.sender.borrowAmount, BORROW_01);
// }
// eq(_after.activeLoans, _before.activeLoans + 1, BORROW_02);
// }
if (sender == borrower) {
eq(_after.sender.borrowAmount, _before.sender.borrowAmount, BORROW_03);
} else {
lt(_after.sender.borrowAmount, _before.sender.borrowAmount, BORROW_01);
}
eq(_after.activeLoans, _before.activeLoans + 1, BORROW_02);
}

function lendAsLimitOrder(uint256 maxAmount, uint256 maxDueDate, uint256 yieldCurveSeed) public getSender {
__before();
Expand All @@ -188,126 +188,128 @@ abstract contract TargetFunctions is Deploy, Helper, Properties, BaseTargetFunct
__after();
}

// function borrowerExit(uint256 loanId, address borrowerToExitTo) public getSender {
// __before(loanId);
function borrowerExit(uint256 loanId, address borrowerToExitTo) public getSender {
__before(loanId);

// precondition(_before.activeLoans > 0);
precondition(_before.activeLoans > 0);

// loanId = between(loanId, 0, _before.activeLoans - 1);
// borrowerToExitTo = _getRandomSender(borrowerToExitTo);
loanId = between(loanId, 0, _before.activeLoans - 1);
borrowerToExitTo = _getRandomUser(borrowerToExitTo);

// hevm.prank(sender);
// size.borrowerExit(BorrowerExitParams({loanId: loanId, borrowerToExitTo: borrowerToExitTo}));
hevm.prank(sender);
size.borrowerExit(BorrowerExitParams({loanId: loanId, borrowerToExitTo: borrowerToExitTo}));

// __after(loanId);
__after(loanId);

// if (borrowerToExitTo == sender) {
// eq(_after.sender.debtAmount, _before.sender.debtAmount, BORROWER_EXIT_01);
// } else {
// lt(_after.sender.debtAmount, _before.sender.debtAmount, BORROWER_EXIT_01);
// }
// }
if (borrowerToExitTo == sender) {
eq(_after.sender.debtAmount, _before.sender.debtAmount, BORROWER_EXIT_01);
} else {
lt(_after.sender.debtAmount, _before.sender.debtAmount, BORROWER_EXIT_01);
}
}

// function repay(uint256 loanId, uint256 amount) public getSender {
// __before(loanId);
function repay(uint256 loanId, uint256 amount) public getSender {
__before(loanId);

// precondition(_before.activeLoans > 0);
precondition(_before.activeLoans > 0);

// loanId = between(loanId, 0, _before.activeLoans - 1);
loanId = between(loanId, 0, _before.activeLoans - 1);

// hevm.prank(sender);
// size.repay(RepayParams({loanId: loanId, amount: amount}));
hevm.prank(sender);
size.repay(RepayParams({loanId: loanId, amount: amount}));

// __after(loanId);
__after(loanId);

// lt(_after.sender.borrowAmount, _before.sender.borrowAmount, REPAY_01);
// gt(_after.protocolBorrowAmount, _before.protocolBorrowAmount, REPAY_01);
// lt(_after.sender.debtAmount, _before.sender.debtAmount, REPAY_02);
// }
lt(_after.sender.borrowAmount, _before.sender.borrowAmount, REPAY_01);
gt(_after.protocolBorrowAmount, _before.protocolBorrowAmount, REPAY_01);
lt(_after.sender.debtAmount, _before.sender.debtAmount, REPAY_02);
}

// function claim(uint256 loanId) public getSender {
// __before(loanId);
function claim(uint256 loanId) public getSender {
__before(loanId);

// precondition(_before.activeLoans > 0);
precondition(_before.activeLoans > 0);

// loanId = between(loanId, 0, _before.activeLoans - 1);
loanId = between(loanId, 0, _before.activeLoans - 1);

// hevm.prank(sender);
// size.claim(ClaimParams({loanId: loanId}));
hevm.prank(sender);
size.claim(ClaimParams({loanId: loanId}));

// __after(loanId);
__after(loanId);

// gte(_after.sender.borrowAmount, _before.sender.borrowAmount, BORROW_01);
// t(size.isFOL(loanId), CLAIM_02);
// }
gte(_after.sender.borrowAmount, _before.sender.borrowAmount, BORROW_01);
t(size.isFOL(loanId), CLAIM_02);
}

// function liquidateLoan(uint256 loanId) public getSender {
// __before(loanId);
function liquidateLoan(uint256 loanId) public getSender {
__before(loanId);

// precondition(_before.activeLoans > 0);
precondition(_before.activeLoans > 0);

// loanId = between(loanId, 0, _before.activeLoans - 1);
loanId = between(loanId, 0, _before.activeLoans - 1);

// hevm.prank(sender);
// size.liquidateLoan(LiquidateLoanParams({loanId: loanId}));
hevm.prank(sender);
size.liquidateLoan(LiquidateLoanParams({loanId: loanId, minimumCollateralRatio: 1e18}));

// __after(loanId);
__after(loanId);

// gt(_after.sender.collateralAmount, _before.sender.collateralAmount, LIQUIDATE_01);
// lt(_after.sender.borrowAmount, _before.sender.borrowAmount, LIQUIDATE_02);
// lt(_after.borrower.debtAmount, _before.borrower.debtAmount, LIQUIDATE_02);
// t(_before.isSenderLiquidatable, LIQUIDATE_03);
// }
gt(_after.sender.collateralAmount, _before.sender.collateralAmount, LIQUIDATE_01);
lt(_after.sender.borrowAmount, _before.sender.borrowAmount, LIQUIDATE_02);
lt(_after.borrower.debtAmount, _before.borrower.debtAmount, LIQUIDATE_02);
t(_before.isSenderLiquidatable, LIQUIDATE_03);
}

// function selfLiquidateLoan(uint256 loanId) public getSender {
// __before(loanId);
function selfLiquidateLoan(uint256 loanId) public getSender {
__before(loanId);

// precondition(_before.activeLoans > 0);
precondition(_before.activeLoans > 0);

// loanId = between(loanId, 0, _before.activeLoans - 1);
loanId = between(loanId, 0, _before.activeLoans - 1);

// hevm.prank(sender);
// size.selfLiquidateLoan(SelfLiquidateLoanParams({loanId: loanId}));
hevm.prank(sender);
size.selfLiquidateLoan(SelfLiquidateLoanParams({loanId: loanId}));

// __after(loanId);
__after(loanId);

// lt(_after.sender.collateralAmount, _before.sender.collateralAmount, LIQUIDATE_01);
// lt(_after.sender.debtAmount, _before.sender.debtAmount, LIQUIDATE_02);
// }
lt(_after.sender.collateralAmount, _before.sender.collateralAmount, LIQUIDATE_01);
lt(_after.sender.debtAmount, _before.sender.debtAmount, LIQUIDATE_02);
}

// function liquidateLoanWithReplacement(uint256 loanId, address borrower) public getSender {
// __before(loanId);
function liquidateLoanWithReplacement(uint256 loanId, address borrower) public getSender {
__before(loanId);

// precondition(_before.activeLoans > 0);
precondition(_before.activeLoans > 0);

// loanId = between(loanId, 0, _before.activeLoans - 1);
// borrower = _getRandomSender(borrower);
loanId = between(loanId, 0, _before.activeLoans - 1);
borrower = _getRandomUser(borrower);

// hevm.prank(sender);
// size.liquidateLoanWithReplacement(LiquidateLoanWithReplacementParams({loanId: loanId, borrower: borrower}));
hevm.prank(sender);
size.liquidateLoanWithReplacement(
LiquidateLoanWithReplacementParams({loanId: loanId, borrower: borrower, minimumCollateralRatio: 1e18})
);

// __after(loanId);
__after(loanId);

// lt(_after.borrower.debtAmount, _before.borrower.debtAmount, LIQUIDATE_02);
// }
lt(_after.borrower.debtAmount, _before.borrower.debtAmount, LIQUIDATE_02);
}

// function moveToVariablePool(uint256 loanId) public getSender {
// __before(loanId);
function moveToVariablePool(uint256 loanId) public getSender {
__before(loanId);

// precondition(_before.activeLoans > 0);
precondition(_before.activeLoans > 0);

// loanId = between(loanId, 0, _before.activeLoans - 1);
loanId = between(loanId, 0, _before.activeLoans - 1);

// hevm.prank(sender);
// size.moveToVariablePool(MoveToVariablePoolParams({loanId: loanId}));
hevm.prank(sender);
size.moveToVariablePool(MoveToVariablePoolParams({loanId: loanId}));

// __after(loanId);
// }
__after(loanId);
}

// function setPrice(uint256 price) public {
// uint256 oldPrice = priceFeed.getPrice();
// price = between(price, oldPrice * 1e18 / 1.2e18, priceFeed.getPrice() * 1.2e18 / 1e18);
function setPrice(uint256 price) public {
uint256 oldPrice = priceFeed.getPrice();
price = between(price, oldPrice * 1e18 / 1.2e18, priceFeed.getPrice() * 1.2e18 / 1e18);

// priceFeed.setPrice(price);
// }
priceFeed.setPrice(price);
}
}

0 comments on commit 218bef7

Please sign in to comment.