Skip to content

Commit

Permalink
update borrower enforcer to use blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
0xgregthedev committed Nov 20, 2023
1 parent 5a0aca3 commit 46f5ffd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 36 deletions.
20 changes: 10 additions & 10 deletions src/enforcers/AstariaV1BorrowerEnforcer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ contract AstariaV1BorrowerEnforcer is BorrowerEnforcer {
error DebtBundlesNotSupported();

struct V1BorrowerDetails {
uint256 startTime;
uint256 endTime;
uint256 startBlock;
uint256 endBlock;
uint256 startRate;
uint256 maxAmount;
uint256 minAmount;
Expand Down Expand Up @@ -80,22 +80,22 @@ contract AstariaV1BorrowerEnforcer is BorrowerEnforcer {
function _locateCurrentRate(V1BorrowerDetails memory v1Details) internal view returns (uint256 currentRate) {
uint256 endRate = AstariaV1Lib.getBasePricingRate(v1Details.details.loan.terms.pricingData);

// if endRate == startRate, or startTime == endTime, or block.timestamp > endTime
// if endRate == startRate, or startBlock == endBlock, or block.number > endBlock
if (
endRate == v1Details.startRate || v1Details.startTime == v1Details.endTime
|| block.timestamp > v1Details.endTime
endRate == v1Details.startRate || v1Details.startBlock == v1Details.endBlock
|| block.number > v1Details.endBlock
) {
return endRate;
}

// Will revert if startTime > endTime
uint256 duration = v1Details.endTime - v1Details.startTime;
// Will revert if startBlock > endBlock
uint256 duration = v1Details.endBlock - v1Details.startBlock;
uint256 elapsed;
uint256 remaining;
unchecked {
// block.timestamp <= endTime && startTime < endTime, can't overflow
elapsed = block.timestamp - v1Details.startTime;
// block.timestamp <= endTime, can't underflow
// block.number <= endBlock && startBlock < endBlock, can't overflow
elapsed = block.number - v1Details.startBlock;
// block.number <= endBlock, can't underflow
remaining = duration - elapsed;
}

Expand Down
4 changes: 2 additions & 2 deletions test/AstariaV1Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ contract AstariaV1Test is StarportTest {
loan.issuer = address(0);
AstariaV1BorrowerEnforcer.V1BorrowerDetails memory v1BorrowerDetails = AstariaV1BorrowerEnforcer
.V1BorrowerDetails({
startTime: block.timestamp,
endTime: block.timestamp,
startBlock: block.number,
endBlock: block.number,
startRate: AstariaV1Lib.getBasePricingRate(loan.terms.pricingData),
minAmount: loan.debt[0].amount,
maxAmount: loan.debt[0].amount,
Expand Down
47 changes: 23 additions & 24 deletions test/TestV1BorrowerEnforcer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,27 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer {
Starport.Loan memory loan = generateDefaultLoanTerms();

AstariaV1BorrowerEnforcer.V1BorrowerDetails memory details = AstariaV1BorrowerEnforcer.V1BorrowerDetails({
startTime: block.timestamp,
endTime: block.timestamp + 10 minutes,
startBlock: block.number,
endBlock: block.number + 10,
startRate: endRate / 2,
minAmount: loan.debt[0].amount,
maxAmount: loan.debt[0].amount,
details: BorrowerEnforcer.Details(loan)
});
vm.warp(block.timestamp + 10 minutes);

vm.roll(block.number + 10);
borrowerEnforcer.validate(new AdditionalTransfer[](0), loan, abi.encode(details));

// Test after endTime
vm.warp(block.timestamp + 10 minutes);
// Test after endBlock
vm.roll(block.number + 15);
borrowerEnforcer.validate(new AdditionalTransfer[](0), loan, abi.encode(details));
}

function testV1BorrowerEnforcerStart() public {
Starport.Loan memory loan = generateDefaultLoanTerms();

AstariaV1BorrowerEnforcer.V1BorrowerDetails memory details = AstariaV1BorrowerEnforcer.V1BorrowerDetails({
startTime: block.timestamp,
endTime: block.timestamp + 10 minutes,
startBlock: block.number,
endBlock: block.number + 10,
startRate: endRate / 2,
minAmount: loan.debt[0].amount,
maxAmount: loan.debt[0].amount,
Expand All @@ -61,21 +60,21 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer {
function testRevertLocateCurrentRateAndAmount() public {
Starport.Loan memory loan = generateDefaultLoanTerms();
AstariaV1BorrowerEnforcer.V1BorrowerDetails memory details = AstariaV1BorrowerEnforcer.V1BorrowerDetails({
startTime: block.timestamp + 10 minutes,
endTime: block.timestamp,
startBlock: block.number + 10,
endBlock: block.number,
startRate: endRate / 2,
minAmount: loan.debt[0].amount,
maxAmount: loan.debt[0].amount,
details: BorrowerEnforcer.Details(loan)
});

// Revert if startTime > endTime
// Revert if startBlock > endBlock
vm.expectRevert(stdError.arithmeticError);
_locateCurrentRate(details);

details.endTime = block.timestamp + 20 minutes;
details.endBlock = block.number + 20;

//revert if startTime > current time
//revert if startBlock > current block
vm.expectRevert(stdError.arithmeticError);
_locateCurrentRate(details);
}
Expand All @@ -84,8 +83,8 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer {
Starport.Loan memory loan = generateDefaultLoanTerms();

AstariaV1BorrowerEnforcer.V1BorrowerDetails memory details = AstariaV1BorrowerEnforcer.V1BorrowerDetails({
startTime: block.timestamp,
endTime: block.timestamp + 10 minutes,
startBlock: block.number,
endBlock: block.number + 10,
startRate: endRate / 2,
minAmount: loan.debt[0].amount,
maxAmount: loan.debt[0].amount,
Expand All @@ -94,7 +93,7 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer {
uint256 rate = endRate / 2 + endRate / 4;
loan.terms.pricingData = abi.encode(BasePricing.Details({carryRate: 0, rate: rate, decimals: 18}));

vm.warp(block.timestamp + 5 minutes);
vm.roll(block.number + 5);

uint256 actualRate = AstariaV1BorrowerEnforcer(address(borrowerEnforcer)).locateCurrentRate(abi.encode(details));
assertEq(actualRate, rate, "actualRate != expectedRate");
Expand All @@ -105,8 +104,8 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer {
Starport.Loan memory loan = generateDefaultLoanTerms();

AstariaV1BorrowerEnforcer.V1BorrowerDetails memory details = AstariaV1BorrowerEnforcer.V1BorrowerDetails({
startTime: block.timestamp,
endTime: block.timestamp + 10 minutes,
startBlock: block.number,
endBlock: block.number + 10,
startRate: endRate / 2,
minAmount: loan.debt[0].amount,
maxAmount: loan.debt[0].amount * 2,
Expand All @@ -129,8 +128,8 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer {
Starport.Loan memory loan = generateDefaultLoanTerms();

AstariaV1BorrowerEnforcer.V1BorrowerDetails memory details = AstariaV1BorrowerEnforcer.V1BorrowerDetails({
startTime: block.timestamp,
endTime: block.timestamp + 10 minutes,
startBlock: block.number,
endBlock: block.number + 10,
startRate: endRate / 2,
minAmount: loan.debt[0].amount,
maxAmount: loan.debt[0].amount,
Expand All @@ -140,7 +139,7 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer {
abi.encode(BasePricing.Details({carryRate: 0, rate: endRate * 3 / 4 + 1, decimals: 18}));

vm.expectRevert(LoanRateExceedsCurrentRate.selector);
vm.warp(block.timestamp + 5 minutes);
vm.roll(block.number + 5);

borrowerEnforcer.validate(new AdditionalTransfer[](0), loan, abi.encode(details));
}
Expand All @@ -149,16 +148,16 @@ contract TestV1BorrowerEnforcer is AstariaV1Test, AstariaV1BorrowerEnforcer {
Starport.Loan memory loan = generateDefaultLoanTerms();

AstariaV1BorrowerEnforcer.V1BorrowerDetails memory details = AstariaV1BorrowerEnforcer.V1BorrowerDetails({
startTime: block.timestamp,
endTime: block.timestamp + 10 minutes,
startBlock: block.number,
endBlock: block.number + 10,
startRate: endRate / 2,
minAmount: loan.debt[0].amount,
maxAmount: loan.debt[0].amount,
details: BorrowerEnforcer.Details(loanCopy(loan))
});
loan.terms.pricingData = abi.encode(BasePricing.Details({carryRate: 0, rate: details.startRate, decimals: 18}));

vm.warp(block.timestamp + 5 minutes);
vm.roll(block.number + 5);

borrowerEnforcer.validate(new AdditionalTransfer[](0), loan, abi.encode(details));
}
Expand Down

0 comments on commit 46f5ffd

Please sign in to comment.