Skip to content

Commit

Permalink
feat: tests for ratio lender
Browse files Browse the repository at this point in the history
dangerousfood committed May 5, 2024
1 parent c81527f commit 1906173
Showing 1 changed file with 166 additions and 0 deletions.
166 changes: 166 additions & 0 deletions test/TestV1RatioLenderEnforcer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// SPDX-License-Identifier: Apache-2.0
// █████╗ ███████╗████████╗ █████╗ ██████╗ ██╗ █████╗ ██╗ ██╗ ██╗
// ██╔══██╗██╔════╝╚══██╔══╝██╔══██╗██╔══██╗██║██╔══██╗ ██║ ██║███║
// ███████║███████╗ ██║ ███████║██████╔╝██║███████║ ██║ ██║╚██║
// ██╔══██║╚════██║ ██║ ██╔══██║██╔══██╗██║██╔══██║ ╚██╗ ██╔╝ ██║
// ██║ ██║███████║ ██║ ██║ ██║██║ ██║██║██║ ██║ ╚████╔╝ ██║
// ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝╚═╝ ╚═╝ ╚═══╝ ╚═╝
//
// Astaria v1 Lending
// Built on Starport https://github.com/astariaXYZ/starport
// Designed with love by Astaria Labs, Inc

pragma solidity ^0.8.17;

import "test/AstariaV1Test.sol";

import {Starport} from "starport-core/Starport.sol";
import {StarportLib, AdditionalTransfer} from "starport-core/lib/StarportLib.sol";

import {AstariaV1RatioLenderEnforcer} from "v1-core/enforcers/AstariaV1RatioLenderEnforcer.sol";
import {AstariaV1Lib} from "v1-core/lib/AstariaV1Lib.sol";

import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";

import {console2} from "forge-std/console2.sol";

contract TestV1RatioLenderEnforcer is AstariaV1Test, AstariaV1RatioLenderEnforcer {
using FixedPointMathLib for uint256;

function setUp() public virtual override {
super.setUp();

lenderEnforcer = new AstariaV1RatioLenderEnforcer();
}

function getDefaultV1RatioLenderDetails(Starport.Loan memory loan)
public
pure
returns (AstariaV1RatioLenderEnforcer.Details memory details)
{
details = AstariaV1RatioLenderEnforcer.Details({
matchIdentifier: true,
minCollateralAmount: loan.collateral[0].amount,
collateralToDebtRatio: loan.debt[0].amount.divWadUp(loan.collateral[0].amount),
loan: loanCopy(loan)
});
}

function testV1RatioLenderEnforcerAmount() public {
Starport.Loan memory loan = generateDefaultLoanTerms();
AstariaV1RatioLenderEnforcer.Details memory details = getDefaultV1RatioLenderDetails(loan);

// Test general passing case
lenderEnforcer.validate(new AdditionalTransfer[](0), loan, abi.encode(details));

// Test Debt Bundle
loan.debt = new SpentItem[](2);
vm.expectRevert(DebtBundlesNotSupported.selector);
lenderEnforcer.validate(new AdditionalTransfer[](0), loan, abi.encode(details));

// Test Collateral Bundle
loan = generateDefaultLoanTerms();
loan.collateral = new SpentItem[](2);
vm.expectRevert(CollateralBundlesNotSupported.selector);
lenderEnforcer.validate(new AdditionalTransfer[](0), loan, abi.encode(details));

// Test below min collateral amount
loan = generateDefaultLoanTerms();
loan.collateral[0].amount--;
vm.expectRevert(BelowMinCollateralAmount.selector);
lenderEnforcer.validate(new AdditionalTransfer[](0), loan, abi.encode(details));

// Test
}

// function testV1LenderEnforcerMinDebtExceedsMax() public {
// Starport.Loan memory loan = generateDefaultLoanTerms();
// AstariaV1LenderEnforcer.Details memory details = AstariaV1LenderEnforcer.Details({
// matchIdentifier: true,
// minDebtAmount: loan.debt[0].amount + 1,
// loan: loanCopy(loan)
// });

// // Test excessive amount
// vm.expectRevert(MinDebtAmountExceedsMax.selector);
// lenderEnforcer.validate(new AdditionalTransfer[](0), loan, abi.encode(details));
// }

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

// AstariaV1LenderEnforcer.Details memory details = AstariaV1LenderEnforcer.Details({
// matchIdentifier: true,
// minDebtAmount: loan.debt[0].amount,
// loan: loanCopy(loan)
// });

// // Test malleable rate
// AstariaV1Lib.setBasePricingRate(
// loan.terms.pricingData, AstariaV1Lib.getBasePricingRate(details.loan.terms.pricingData) + 1
// );
// lenderEnforcer.validate(new AdditionalTransfer[](0), loan, abi.encode(details));

// // Test insufficient rate
// AstariaV1Lib.setBasePricingRate(
// loan.terms.pricingData, AstariaV1Lib.getBasePricingRate(details.loan.terms.pricingData) - 1
// );
// vm.expectRevert(LoanRateLessThanCaveatRate.selector);
// lenderEnforcer.validate(new AdditionalTransfer[](0), loan, abi.encode(details));
// }

// // Test matchIdentifier
// function testV1LenderEnforcerMatchIdentifier() public {
// Starport.Loan memory loan = generateDefaultLoanTerms();

// AstariaV1LenderEnforcer.Details memory details = AstariaV1LenderEnforcer.Details({
// matchIdentifier: false,
// minDebtAmount: loan.debt[0].amount,
// loan: loanCopy(loan)
// });
// loan.collateral[0].identifier += 1;

// lenderEnforcer.validate(new AdditionalTransfer[](0), loan, abi.encode(details));
// details.matchIdentifier = true;

// vm.expectRevert(LenderEnforcer.InvalidLoanTerms.selector);
// lenderEnforcer.validate(new AdditionalTransfer[](0), loan, abi.encode(details));
// }

// function testV1LenderEnforcerAdditionalTransfers() external {
// // Test invalid additional transfer from lender
// AdditionalTransfer[] memory additionalTransfers = new AdditionalTransfer[](1);
// additionalTransfers[0] = AdditionalTransfer({
// token: address(0),
// amount: 0,
// to: address(0),
// from: lender.addr,
// identifier: 0,
// itemType: ItemType.ERC20
// });

// Starport.Loan memory loan = generateDefaultLoanTerms();
// AstariaV1LenderEnforcer.Details memory details =
// AstariaV1LenderEnforcer.Details({matchIdentifier: false, minDebtAmount: loan.debt[0].amount, loan: loan});

// vm.expectRevert(LenderEnforcer.InvalidAdditionalTransfer.selector);
// lenderEnforcer.validate(additionalTransfers, loan, abi.encode(details));

// // Test valid additional transfer from other party
// additionalTransfers[0].from = borrower.addr;
// lenderEnforcer.validate(additionalTransfers, loan, abi.encode(details));
// }

// function testV1LenderEnforcerDebtBundlesNotSupported() external {
// Starport.Loan memory loan = generateDefaultLoanTerms();
// SpentItem[] memory debt = new SpentItem[](2);
// debt[0] = loan.debt[0];
// debt[1] = _getERC721SpentItem(TestERC721(loan.debt[0].token), loan.debt[0].identifier + 1);
// loan.debt = debt;

// AstariaV1LenderEnforcer.Details({matchIdentifier: false, minDebtAmount: loan.debt[0].amount, loan: loan});

// vm.expectRevert(DebtBundlesNotSupported.selector);
// lenderEnforcer.validate(new AdditionalTransfer[](0), loan, abi.encode(LenderEnforcer.Details({loan: loan})));
// }
}

0 comments on commit 1906173

Please sign in to comment.