Skip to content

Commit

Permalink
Merge pull request #7 from AstariaXYZ/feat/caveatTesting-new
Browse files Browse the repository at this point in the history
Feat/testing
  • Loading branch information
SantiagoGregory authored Sep 15, 2023
2 parents 83b8961 + 08cb51c commit 402dc07
Show file tree
Hide file tree
Showing 7 changed files with 445 additions and 54 deletions.
33 changes: 12 additions & 21 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
name: test

on: workflow_dispatch
on:
workflow_dispatch:
pull_request:

env:
FOUNDRY_PROFILE: ci

jobs:
check:
strategy:
fail-fast: true

name: Foundry project
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Checkout repository
uses: actions/checkout@v3
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
- uses: actions/setup-node@v3
with:
version: nightly

- name: Run Forge build
run: |
forge --version
forge build --sizes
id: build

- name: Run Forge tests
run: |
forge test -vvv
id: test
node-version: 16
- run: yarn
- uses: onbjerg/foundry-toolchain@v1
- run: forge test --ffi -vvv
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ artifacts/
cache_hardhat/
typechain-types/

.idea/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
Expand Down
151 changes: 151 additions & 0 deletions test/StarPortTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ import {Consideration} from "seaport-core/src/lib/Consideration.sol";
import {UniqueOriginator} from "src/originators/UniqueOriginator.sol";
import {MerkleOriginator} from "src/originators/MerkleOriginator.sol";
import {SimpleInterestPricing} from "src/pricing/SimpleInterestPricing.sol";
import {CompoundInterestPricing} from "src/pricing/CompoundInterestPricing.sol";
import {BasePricing} from "src/pricing/BasePricing.sol";
import {AstariaV1Pricing} from "src/pricing/AstariaV1Pricing.sol";
import {FixedTermHook} from "src/hooks/FixedTermHook.sol";
import {AstariaV1SettlementHook} from "src/hooks/AstariaV1SettlementHook.sol";
import {DutchAuctionHandler} from "src/handlers/DutchAuctionHandler.sol";
import {EnglishAuctionHandler} from "src/handlers/EnglishAuctionHandler.sol";
import {AstariaV1SettlementHandler} from "src/handlers/AstariaV1SettlementHandler.sol";
import {Merkle} from "seaport/lib/murky/src/Merkle.sol";

import {BaseOrderTest} from "seaport/test/foundry/utils/BaseOrderTest.sol";
Expand All @@ -67,6 +71,18 @@ interface IWETH9 {
}

contract StarPortTest is BaseOrderTest {
SettlementHook fixedTermHook;
SettlementHook astariaSettlementHook;

SettlementHandler dutchAuctionHandler;
SettlementHandler englishAuctionHandler;
SettlementHandler astariaSettlementHandler;

Pricing simpleInterestPricing;
Pricing astariaPricing;

ConsiderationInterface public constant seaport = ConsiderationInterface(0x2e234DAe75C793f67A35089C9d99245E1C58470b);

Pricing pricing;
SettlementHandler handler;
SettlementHook hook;
Expand Down Expand Up @@ -189,6 +205,22 @@ contract StarPortTest is BaseOrderTest {
conduitController.updateChannel(refinancerConduit, address(LM), true);
erc20s[0].approve(address(refinancerConduit), 100000);
vm.stopPrank();

/////////

fixedTermHook = new FixedTermHook();
astariaSettlementHook = new AstariaV1SettlementHook(LM);

dutchAuctionHandler = new DutchAuctionHandler(LM);
englishAuctionHandler = new EnglishAuctionHandler({
LM_: LM,
consideration_: seaport,
EAZone_: 0x110b2B128A9eD1be5Ef3232D8e4E41640dF5c2Cd
});
astariaSettlementHandler = new AstariaV1SettlementHandler(LM);

simpleInterestPricing = new SimpleInterestPricing(LM);
astariaPricing = new AstariaV1Pricing(LM);
}

function onERC721Received(
Expand Down Expand Up @@ -698,4 +730,123 @@ contract StarPortTest is BaseOrderTest {
assertEq(balanceAfter - balanceBefore, debt[0].amount);
vm.stopPrank();
}

function _repayLoan(address borrower, uint256 amount, LoanManager.Loan memory loan) internal {
vm.startPrank(borrower);
erc20s[0].approve(address(consideration), amount);
vm.stopPrank();
_executeRepayLoan(loan);
}

function _createLoan721Collateral20Debt(address lender, uint256 borrowAmount, LoanManager.Terms memory terms) internal returns (LoanManager.Loan memory loan) {
uint256 initial721Balance = erc721s[0].balanceOf(borrower.addr);
assertTrue(initial721Balance > 0, "Test must have at least one erc721 token");
uint256 initial20Balance = erc20s[0].balanceOf(borrower.addr);

loan = _createLoan({
lender: lender,
terms: terms,
collateralItem:
ConsiderationItem({
token: address(erc721s[0]),
startAmount: 1,
endAmount: 1,
identifierOrCriteria: 1,
itemType: ItemType.ERC721,
recipient: payable(address(custodian))
}),
debtItem:
SpentItem({
itemType: ItemType.ERC20,
token: address(erc20s[0]),
amount: borrowAmount,
identifier: 0
})
});

assertTrue(erc721s[0].balanceOf(borrower.addr) < initial721Balance, "Borrower ERC721 was not sent out");
assertTrue(erc20s[0].balanceOf(borrower.addr) > initial20Balance, "Borrower did not receive ERC20");

}

// TODO update or overload to take interest rate
function _createLoan20Collateral20Debt(address lender, uint256 collateralAmount, uint256 borrowAmount, LoanManager.Terms memory terms) internal returns (LoanManager.Loan memory loan) {
uint256 initial20Balance1 = erc20s[1].balanceOf(borrower.addr);
assertTrue(initial20Balance1 > 0, "Borrower must have at least one erc20 token");

uint256 initial20Balance0 = erc20s[0].balanceOf(borrower.addr);

loan = _createLoan({
lender: lender,
terms: terms,
collateralItem:
ConsiderationItem({
token: address(erc20s[1]),
startAmount: collateralAmount,
endAmount: collateralAmount,
identifierOrCriteria: 0,
itemType: ItemType.ERC20,
recipient: payable(address(custodian))
}),
debtItem:
SpentItem({
itemType: ItemType.ERC20,
token: address(erc20s[0]),
amount: borrowAmount,
identifier: 0
})
});

assertEq(initial20Balance1 - collateralAmount, erc20s[1].balanceOf(borrower.addr), "Borrower ERC20 was not sent out");
assertEq(initial20Balance0 + borrowAmount, erc20s[0].balanceOf(borrower.addr), "Borrower did not receive ERC20");
}

// TODO fix
function _createLoan20Collateral721Debt(address lender, LoanManager.Terms memory terms) internal returns (LoanManager.Loan memory loan) {
return _createLoan({
lender: lender,
terms: terms,
collateralItem:
ConsiderationItem({
token: address(erc20s[0]),
startAmount: 20,
endAmount: 20,
identifierOrCriteria: 0,
itemType: ItemType.ERC20,
recipient: payable(address(custodian))
}),
debtItem:
SpentItem({
itemType: ItemType.ERC721,
token: address(erc721s[0]),
amount: 1,
identifier: 0
})
});
}

function _createLoan(address lender, LoanManager.Terms memory terms, ConsiderationItem memory collateralItem, SpentItem memory debtItem) internal returns (LoanManager.Loan memory loan) {
selectedCollateral.push(collateralItem);
debt.push(debtItem);

UniqueOriginator.Details memory loanDetails = UniqueOriginator.Details({
conduit: address(lenderConduit),
custodian: address(custodian),
issuer: lender,
deadline: block.timestamp + 100,
terms: terms,
collateral: ConsiderationItemLib.toSpentItemArray(selectedCollateral),
debt: debt
});

loan = newLoan(
NewLoanData({
custodian: address(custodian),
caveats: new LoanManager.Caveat[](0), // TODO check
details: abi.encode(loanDetails)
}),
Originator(UO),
selectedCollateral
);
}
}
94 changes: 94 additions & 0 deletions test/TestExoticLoans.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import "./StarPortTest.sol";
import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
import {LibString} from "solady/src/utils/LibString.sol";

import "forge-std/console.sol";

contract TestExoticLoans is StarPortTest {
function testSwap() public {
SwapHook swapHook = new SwapHook();
SwapHandler swapHandler = new SwapHandler(LM);
SwapPricing swapPricing = new SwapPricing(LM);

bytes memory swapPricingData = "";
bytes memory swapHandlerData = "";
bytes memory swapHookData = "";

LoanManager.Terms memory terms = LoanManager.Terms({
hook: address(swapHook),
handler: address(swapHandler),
pricing: address(swapPricing),
pricingData: swapPricingData,
handlerData: swapHandlerData,
hookData: swapHookData
});

// uint256 initialErc201balance = erc20s[1].balanceOf(borrower.addr);
// uint256 initialErc202balance = erc20s[0].balanceOf(borrower.addr);

LoanManager.Loan memory loan = _createLoan20Collateral20Debt({
lender: lender.addr,
collateralAmount: 20, // erc20s[1]
borrowAmount: 100, // erc20s[0]
terms: terms
});

// assertEq(erc20s[1].balanceOf(borrower.addr), initialErc201balance);
// assertEq(erc20s[0].balanceOf(borrower.addr), initialErc202balance);
// skip(10 days);
//
// _repayLoan({
// borrower: borrower.addr,
// amount: 375,
// loan: loan
// });
}
}

contract SwapHook is SettlementHook {
function isActive(LoanManager.Loan calldata loan) external view override returns (bool) {
return true;
}
}

contract SwapHandler is SettlementHandler {

constructor(LoanManager LM_) SettlementHandler(LM_) {}

function execute(
LoanManager.Loan calldata loan
) external override returns (bytes4) {
return bytes4(0);
}

function validate(
LoanManager.Loan calldata loan
) external view override returns (bool) {
return true;
}

function getSettlement(
LoanManager.Loan memory loan
) external override returns (ReceivedItem[] memory consideration, address restricted) {
return (new ReceivedItem[](0), address(0));
}
}

contract SwapPricing is Pricing {

constructor(LoanManager LM_) Pricing(LM_) {}

function getPaymentConsideration(
LoanManager.Loan memory loan
) public view override returns (ReceivedItem[] memory, ReceivedItem[] memory) {
return (new ReceivedItem[](0), new ReceivedItem[](0));
}

function isValidRefinance(
LoanManager.Loan memory loan,
bytes memory newPricingData,
address caller
) external view override returns (ReceivedItem[] memory, ReceivedItem[] memory, ReceivedItem[] memory) {
return (new ReceivedItem[](0), new ReceivedItem[](0), new ReceivedItem[](0));
}
}
Loading

0 comments on commit 402dc07

Please sign in to comment.