|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +pragma solidity ^0.8.30; |
| 3 | + |
| 4 | +import "forge-std/Test.sol"; |
| 5 | +import {WETH} from "contracts/mocks/weth.sol"; |
| 6 | +import {BullaFrendLendV2, LoanOfferNotFound} from "contracts/BullaFrendLendV2.sol"; |
| 7 | +import {BullaClaimV2} from "contracts/BullaClaimV2.sol"; |
| 8 | +import {DeployContracts} from "script/DeployContracts.s.sol"; |
| 9 | +import {LoanRequestParams, LoanOffer} from "contracts/interfaces/IBullaFrendLendV2.sol"; |
| 10 | +import {InterestConfig} from "contracts/libraries/CompoundInterestLib.sol"; |
| 11 | +import {LockState} from "contracts/types/Types.sol"; |
| 12 | + |
| 13 | +/// @title Test to validate LoanOfferId behavior - whether first offerId starts from 0 |
| 14 | +/// @notice This test demonstrates the FIXED behavior where first offerId = 0 |
| 15 | +contract TestLoanOfferIdStartsFromZero is Test { |
| 16 | + BullaFrendLendV2 internal bullaFrendLend; |
| 17 | + WETH internal weth; |
| 18 | + |
| 19 | + address creditor = makeAddr("creditor"); |
| 20 | + address debtor = makeAddr("debtor"); |
| 21 | + |
| 22 | + function setUp() public { |
| 23 | + DeployContracts deployer = new DeployContracts(); |
| 24 | + DeployContracts.DeploymentResult memory result = deployer.deployForTest( |
| 25 | + address(this), // deployer |
| 26 | + LockState.Unlocked, // initialLockState |
| 27 | + 10000000000000000, // coreProtocolFee (0.01 ETH) |
| 28 | + 500, // invoiceProtocolFeeBPS (5%) |
| 29 | + 500, // frendLendProtocolFeeBPS (5%) |
| 30 | + address(this) // admin |
| 31 | + ); |
| 32 | + |
| 33 | + BullaClaimV2 bullaClaim = BullaClaimV2(result.bullaClaim); |
| 34 | + bullaFrendLend = new BullaFrendLendV2(address(bullaClaim), address(this), 500); |
| 35 | + |
| 36 | + weth = new WETH(); |
| 37 | + |
| 38 | + // Fund creditor with WETH |
| 39 | + vm.deal(creditor, 10 ether); |
| 40 | + vm.prank(creditor); |
| 41 | + weth.deposit{value: 5 ether}(); |
| 42 | + } |
| 43 | + |
| 44 | + /// @notice Test that validates first loanOfferId = 0 and sequential assignment |
| 45 | + function testFirstLoanOfferIdIsZero() public { |
| 46 | + // Verify initial state |
| 47 | + assertEq(bullaFrendLend.loanOfferCount(), 0, "Initial loanOfferCount should be 0"); |
| 48 | + |
| 49 | + LoanRequestParams memory loanParams = LoanRequestParams({ |
| 50 | + termLength: 30 days, |
| 51 | + interestConfig: InterestConfig({ |
| 52 | + interestRateBps: 500, // 5% |
| 53 | + numberOfPeriodsPerYear: 12 |
| 54 | + }), |
| 55 | + loanAmount: 1 ether, |
| 56 | + creditor: creditor, |
| 57 | + debtor: debtor, |
| 58 | + description: "First loan offer", |
| 59 | + token: address(weth), |
| 60 | + impairmentGracePeriod: 7 days, |
| 61 | + expiresAt: 0, |
| 62 | + callbackContract: address(0), |
| 63 | + callbackSelector: bytes4(0) |
| 64 | + }); |
| 65 | + |
| 66 | + // Approve the contract to spend creditor's WETH (needed for when loan is accepted) |
| 67 | + vm.prank(creditor); |
| 68 | + weth.approve(address(bullaFrendLend), 2 ether); |
| 69 | + |
| 70 | + // Create first loan offer - should get offerId = 0 (FIXED behavior with post-increment) |
| 71 | + vm.prank(creditor); |
| 72 | + uint256 firstOfferId = bullaFrendLend.offerLoan(loanParams); |
| 73 | + assertEq(firstOfferId, 0, "First loan offer should have offerId = 0 (FIXED behavior)"); |
| 74 | + assertEq(bullaFrendLend.loanOfferCount(), 1, "loanOfferCount should be 1 after first offer"); |
| 75 | + |
| 76 | + // Verify offer exists and can be retrieved |
| 77 | + LoanOffer memory retrievedOffer = bullaFrendLend.getLoanOffer(0); |
| 78 | + assertEq(retrievedOffer.params.loanAmount, 1 ether, "First offer should be retrievable"); |
| 79 | + assertEq(retrievedOffer.params.creditor, creditor, "Creditor should match"); |
| 80 | + assertEq(retrievedOffer.requestedByCreditor, true, "Should be requested by creditor"); |
| 81 | + |
| 82 | + // Create second loan offer - should get offerId = 1 |
| 83 | + loanParams.loanAmount = 2 ether; |
| 84 | + loanParams.description = "Second loan offer"; |
| 85 | + |
| 86 | + vm.prank(creditor); |
| 87 | + uint256 secondOfferId = bullaFrendLend.offerLoan(loanParams); |
| 88 | + assertEq(secondOfferId, 1, "Second loan offer should have offerId = 1"); |
| 89 | + assertEq(bullaFrendLend.loanOfferCount(), 2, "loanOfferCount should be 2 after second offer"); |
| 90 | + |
| 91 | + // Verify second offer exists and can be retrieved |
| 92 | + LoanOffer memory secondOffer = bullaFrendLend.getLoanOffer(1); |
| 93 | + assertEq(secondOffer.params.loanAmount, 2 ether, "Second offer should be retrievable"); |
| 94 | + assertEq(secondOffer.params.description, "Second loan offer", "Description should match"); |
| 95 | + } |
| 96 | + |
| 97 | + /// @notice Test boundary checking for getLoanOffer function |
| 98 | + function testGetLoanOfferBoundaryChecking() public { |
| 99 | + // Initially no offers exist, loanOfferCount = 0 |
| 100 | + assertEq(bullaFrendLend.loanOfferCount(), 0); |
| 101 | + |
| 102 | + vm.expectRevert(LoanOfferNotFound.selector); |
| 103 | + bullaFrendLend.getLoanOffer(0); |
| 104 | + |
| 105 | + vm.expectRevert(LoanOfferNotFound.selector); |
| 106 | + bullaFrendLend.getLoanOfferMetadata(0); |
| 107 | + |
| 108 | + // Create one offer |
| 109 | + LoanRequestParams memory loanParams = LoanRequestParams({ |
| 110 | + termLength: 30 days, |
| 111 | + interestConfig: InterestConfig({ |
| 112 | + interestRateBps: 500, // 5% |
| 113 | + numberOfPeriodsPerYear: 12 |
| 114 | + }), |
| 115 | + loanAmount: 1 ether, |
| 116 | + creditor: creditor, |
| 117 | + debtor: debtor, |
| 118 | + description: "Test offer", |
| 119 | + token: address(weth), |
| 120 | + impairmentGracePeriod: 7 days, |
| 121 | + expiresAt: 0, |
| 122 | + callbackContract: address(0), |
| 123 | + callbackSelector: bytes4(0) |
| 124 | + }); |
| 125 | + |
| 126 | + vm.prank(creditor); |
| 127 | + weth.approve(address(bullaFrendLend), 1 ether); |
| 128 | + |
| 129 | + vm.prank(creditor); |
| 130 | + uint256 offerId = bullaFrendLend.offerLoan(loanParams); |
| 131 | + |
| 132 | + // Now offerId = 0 should work (actual offer exists) |
| 133 | + LoanOffer memory validOffer = bullaFrendLend.getLoanOffer(0); |
| 134 | + assertEq(validOffer.params.loanAmount, 1 ether, "Valid offer should be retrievable"); |
| 135 | + assertEq(validOffer.params.creditor, creditor, "Creditor should match"); |
| 136 | + } |
| 137 | +} |
0 commit comments