Skip to content

Commit

Permalink
add in the acquireTokens method along with stargate interface /w tests
Browse files Browse the repository at this point in the history
- move from absolute paths to relative for starport-core in src
  • Loading branch information
androolloyd authored and dangerousfood committed Dec 3, 2023
1 parent 8ac5598 commit 0f189db
Show file tree
Hide file tree
Showing 22 changed files with 107 additions and 63 deletions.
12 changes: 11 additions & 1 deletion src/Starport.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
import {SafeTransferLib} from "solady/src/utils/SafeTransferLib.sol";
import {SignatureCheckerLib} from "solady/src/utils/SignatureCheckerLib.sol";

interface Stargate {
function getOwner(address) external returns (address);
}

contract Starport is PausableNonReentrant {
using FixedPointMathLib for uint256;
using {StarportLib.getId} for Starport.Loan;
Expand Down Expand Up @@ -85,6 +89,7 @@ contract Starport is PausableNonReentrant {
bytes32 private constant _INVALID_LOAN = 0x045f33d100000000000000000000000000000000000000000000000000000000;
bytes32 private constant _LOAN_EXISTS = 0x14ec57fc00000000000000000000000000000000000000000000000000000000;

Stargate public immutable SG;
address public immutable defaultCustodian;
bytes32 public immutable DEFAULT_CUSTODIAN_CODE_HASH;

Expand Down Expand Up @@ -154,7 +159,8 @@ contract Starport is PausableNonReentrant {
/* CONSTRUCTOR */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

constructor(address seaport_) {
constructor(address seaport_, Stargate stargate_) {
SG = stargate_;
address custodian = address(new Custodian(this, seaport_));

bytes32 defaultCustodianCodeHash;
Expand Down Expand Up @@ -628,6 +634,10 @@ contract Starport is PausableNonReentrant {
}
}

function acquireTokens(SpentItem[] memory items) external {
StarportLib.transferSpentItems(items, SG.getOwner(msg.sender), msg.sender, false);
}

/**
* @dev Issues a LM token if needed, only owner can call
* @param loan The loan to issue
Expand Down
6 changes: 3 additions & 3 deletions src/enforcers/BorrowerEnforcer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

pragma solidity ^0.8.17;

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

import {ConsiderationInterface} from "seaport-types/src/interfaces/ConsiderationInterface.sol";

Expand Down
6 changes: 3 additions & 3 deletions src/enforcers/BorrowerEnforcerBNPL.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

pragma solidity ^0.8.17;

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

import {ConsiderationInterface} from "seaport-types/src/interfaces/ConsiderationInterface.sol";

Expand Down
4 changes: 2 additions & 2 deletions src/enforcers/CaveatEnforcer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

pragma solidity ^0.8.17;

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

abstract contract CaveatEnforcer {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
Expand Down
6 changes: 3 additions & 3 deletions src/enforcers/LenderEnforcer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

pragma solidity ^0.8.17;

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

contract LenderEnforcer is CaveatEnforcer {
error LenderOnlyEnforcer();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/RefStarportLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

pragma solidity ^0.8.17;

import {Starport} from "starport-core/Starport.sol";
import {Starport} from "../Starport.sol";

import {ItemType, ReceivedItem, SpentItem} from "seaport-types/src/lib/ConsiderationStructs.sol";

Expand Down
2 changes: 1 addition & 1 deletion src/lib/StarportLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

pragma solidity ^0.8.17;

import {Starport} from "starport-core/Starport.sol";
import {Starport} from "../Starport.sol";

import {ItemType, ReceivedItem, SpentItem} from "seaport-types/src/lib/ConsiderationStructs.sol";

Expand Down
2 changes: 1 addition & 1 deletion src/lib/Validation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

pragma solidity ^0.8.17;

import {Starport} from "starport-core/Starport.sol";
import {Starport} from "../Starport.sol";

abstract contract Validation {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
Expand Down
4 changes: 2 additions & 2 deletions src/originators/Originator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

pragma solidity ^0.8.17;

import {Starport} from "starport-core/Starport.sol";
import {CaveatEnforcer} from "starport-core/enforcers/CaveatEnforcer.sol";
import {Starport} from "../Starport.sol";
import {CaveatEnforcer} from "../enforcers/CaveatEnforcer.sol";

import {SpentItem} from "seaport-types/src/lib/ConsiderationStructs.sol";

Expand Down
8 changes: 4 additions & 4 deletions src/originators/StrategistOriginator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@

pragma solidity ^0.8.17;

import {Starport} from "starport-core/Starport.sol";
import {CaveatEnforcer} from "starport-core/enforcers/CaveatEnforcer.sol";
import {Originator} from "starport-core/originators/Originator.sol";
import {AdditionalTransfer} from "starport-core/lib/StarportLib.sol";
import {Starport} from "../Starport.sol";
import {CaveatEnforcer} from "../enforcers/CaveatEnforcer.sol";
import {Originator} from "../originators/Originator.sol";
import {AdditionalTransfer} from "../lib/StarportLib.sol";

import {ConduitControllerInterface} from "seaport-types/src/interfaces/ConduitControllerInterface.sol";
import {ConduitInterface} from "seaport-types/src/interfaces/ConduitInterface.sol";
Expand Down
10 changes: 5 additions & 5 deletions src/pricing/BasePricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

pragma solidity ^0.8.17;

import {Starport} from "starport-core/Starport.sol";
import {Pricing} from "starport-core/pricing/Pricing.sol";
import {Status} from "starport-core/status/Status.sol";
import {Validation} from "starport-core/lib/Validation.sol";
import {StarportLib} from "starport-core/lib/StarportLib.sol";
import {Starport} from "../Starport.sol";
import {Pricing} from "../pricing/Pricing.sol";
import {Status} from "../status/Status.sol";
import {Validation} from "../lib/Validation.sol";
import {StarportLib} from "../lib/StarportLib.sol";

import {ReceivedItem} from "seaport-types/src/lib/ConsiderationStructs.sol";
import {SpentItem} from "seaport-types/src/lib/ConsiderationStructs.sol";
Expand Down
6 changes: 3 additions & 3 deletions src/pricing/Pricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

pragma solidity ^0.8.17;

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

import {SpentItem} from "seaport-types/src/lib/ConsiderationStructs.sol";

Expand Down
10 changes: 5 additions & 5 deletions src/pricing/SimpleInterestPricing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

pragma solidity ^0.8.17;

import {Starport} from "starport-core/Starport.sol";
import {ReceivedItem, BasePricing} from "starport-core/pricing/BasePricing.sol";
import {Pricing} from "starport-core/pricing/Pricing.sol";
import {Validation} from "starport-core/lib/Validation.sol";
import {StarportLib, AdditionalTransfer} from "starport-core/lib/StarportLib.sol";
import {Starport} from "../Starport.sol";
import {ReceivedItem, BasePricing} from "../pricing/BasePricing.sol";
import {Pricing} from "../pricing/Pricing.sol";
import {Validation} from "../lib/Validation.sol";
import {StarportLib, AdditionalTransfer} from "../lib/StarportLib.sol";

import {SpentItem, ReceivedItem} from "seaport-types/src/lib/ConsiderationStructs.sol";
import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
Expand Down
8 changes: 4 additions & 4 deletions src/settlement/DutchAuctionSettlement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ import {
OrderParameters
} from "seaport-types/src/lib/ConsiderationStructs.sol";

import {Pricing} from "starport-core/pricing/Pricing.sol";
import {Pricing} from "../pricing/Pricing.sol";
import {AmountDeriver} from "seaport-core/src/lib/AmountDeriver.sol";
import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
import {Starport, Settlement} from "starport-core/settlement/Settlement.sol";
import {Starport, Settlement} from "../settlement/Settlement.sol";

import {BasePricing} from "starport-core/pricing/BasePricing.sol";
import {Validation} from "starport-core/lib/Validation.sol";
import {BasePricing} from "../pricing/BasePricing.sol";
import {Validation} from "../lib/Validation.sol";

abstract contract DutchAuctionSettlement is Settlement, AmountDeriver {
using FixedPointMathLib for uint256;
Expand Down
10 changes: 5 additions & 5 deletions src/settlement/FixedTermDutchAuctionSettlement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

pragma solidity ^0.8.17;

import {Starport} from "starport-core/Starport.sol";
import {DutchAuctionSettlement} from "starport-core/settlement/DutchAuctionSettlement.sol";
import {FixedTermStatus} from "starport-core/status/FixedTermStatus.sol";
import {Settlement} from "starport-core/settlement/Settlement.sol";
import {StarportLib} from "starport-core/lib/StarportLib.sol";
import {Starport} from "../Starport.sol";
import {DutchAuctionSettlement} from "../settlement/DutchAuctionSettlement.sol";
import {FixedTermStatus} from "../status/FixedTermStatus.sol";
import {Settlement} from "../settlement/Settlement.sol";
import {StarportLib} from "../lib/StarportLib.sol";

import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
import {SpentItem} from "seaport-types/src/lib/ConsiderationStructs.sol";
Expand Down
4 changes: 2 additions & 2 deletions src/settlement/Settlement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

pragma solidity ^0.8.17;

import {Starport} from "starport-core/Starport.sol";
import {Validation} from "starport-core/lib/Validation.sol";
import {Starport} from "../Starport.sol";
import {Validation} from "../lib/Validation.sol";

import {ReceivedItem} from "seaport-types/src/lib/ConsiderationStructs.sol";

Expand Down
6 changes: 3 additions & 3 deletions src/status/FixedTermStatus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

pragma solidity ^0.8.17;

import {Starport} from "starport-core/Starport.sol";
import {Status} from "starport-core/status/Status.sol";
import {Validation} from "starport-core/lib/Validation.sol";
import {Starport} from "../Starport.sol";
import {Status} from "../status/Status.sol";
import {Validation} from "../lib/Validation.sol";

contract FixedTermStatus is Status {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
Expand Down
4 changes: 2 additions & 2 deletions src/status/Status.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

pragma solidity ^0.8.17;

import {Starport} from "starport-core/Starport.sol";
import {Validation} from "starport-core/lib/Validation.sol";
import {Starport} from "../Starport.sol";
import {Validation} from "../lib/Validation.sol";

abstract contract Status is Validation {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
Expand Down
17 changes: 10 additions & 7 deletions test/StarportTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pragma solidity ^0.8.17;

import "forge-std/Test.sol";
import {Vm} from "forge-std/Vm.sol";
import {Starport} from "starport-core/Starport.sol";
import {Starport, Stargate} from "starport-core/Starport.sol";
import {Pricing} from "starport-core/pricing/Pricing.sol";
import {StrategistOriginator} from "starport-core/originators/StrategistOriginator.sol";
import {
Expand Down Expand Up @@ -56,7 +56,7 @@ import {ERC20} from "solady/src/tokens/ERC20.sol";
import {ERC721} from "solady/src/tokens/ERC721.sol";
import {ERC1155} from "solady/src/tokens/ERC1155.sol";
import {ContractOffererInterface} from "seaport-types/src/interfaces/ContractOffererInterface.sol";
import {TokenReceiverInterface} from "starport-core/interfaces/TokenReceiverInterface.sol";
import {TokenReceiverInterface} from "./interfaces/TokenReceiverInterface.sol";
import {Actions} from "starport-core/lib/StarportLib.sol";

import {CaveatEnforcer} from "starport-core/enforcers/CaveatEnforcer.sol";
Expand Down Expand Up @@ -96,7 +96,7 @@ contract MockIssuer is TokenReceiverInterface {
}
}

contract StarportTest is BaseOrderTest {
contract StarportTest is BaseOrderTest, Stargate {
using Cast for *;
using FixedPointMathLib for uint256;

Expand Down Expand Up @@ -142,6 +142,11 @@ contract StarportTest is BaseOrderTest {

LenderEnforcer lenderEnforcer;

//mock the test harness as stargate
function getOwner(address) external view returns (address) {
return borrower.addr;
}

function _deployAndConfigureConsideration() public {
conduitController = new ConduitController();

Expand Down Expand Up @@ -179,7 +184,7 @@ contract StarportTest is BaseOrderTest {
refinancer = makeAndAllocateAccount("refinancer");
fulfiller = makeAndAllocateAccount("fulfiller");

SP = new Starport(address(consideration));
SP = new Starport(address(consideration), Stargate(address(this)));
custodian = Custodian(payable(SP.defaultCustodian()));
SO = new StrategistOriginator(SP, strategist.addr, 1e16, address(this));
pricing = new SimpleInterestPricing(SP);
Expand Down Expand Up @@ -553,9 +558,7 @@ contract StarportTest is BaseOrderTest {
pure
returns (ConsiderationItem[] memory)
{
ConsiderationItem[] memory considerationItems = new ConsiderationItem[](
_receivedItems.length
);
ConsiderationItem[] memory considerationItems = new ConsiderationItem[](_receivedItems.length);
for (uint256 i = 0; i < _receivedItems.length; ++i) {
considerationItems[i] = ConsiderationItem(
_receivedItems[i].itemType,
Expand Down
8 changes: 2 additions & 6 deletions test/integration-testing/TestNewLoan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,7 @@ contract TestNewLoan is StarportTest {
(ReceivedItem[] memory settlementConsideration, address authorized) =
Settlement(activeLoan.terms.settlement).getSettlementConsideration(activeLoan);
settlementConsideration = StarportLib.removeZeroAmountItems(settlementConsideration);
ConsiderationItem[] memory consider = new ConsiderationItem[](
settlementConsideration.length
);
ConsiderationItem[] memory consider = new ConsiderationItem[](settlementConsideration.length);
uint256 i = 0;
for (; i < settlementConsideration.length;) {
consider[i].token = settlementConsideration[i].token;
Expand All @@ -401,9 +399,7 @@ contract TestNewLoan is StarportTest {
++i;
}
}
OfferItem[] memory repayOffering = new OfferItem[](
activeLoan.collateral.length
);
OfferItem[] memory repayOffering = new OfferItem[](activeLoan.collateral.length);
i = 0;
for (; i < activeLoan.collateral.length;) {
repayOffering[i] = OfferItem({
Expand Down
File renamed without changes.
35 changes: 35 additions & 0 deletions test/unit-testing/TestStarport.sol
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,41 @@ contract TestStarport is StarportTest, DeepEq {
loan.toStorage(activeLoan);
}

function testStargateGetOwner() public {
assertEq(address(borrower.addr), SP.SG().getOwner(address(this)));
}

function testAcquireTokensSuccess() public {
vm.startPrank(borrower.addr);
erc20s[1].approve(address(SP), 100);
erc721s[0].approve(address(SP), 2);
erc1155s[0].setApprovalForAll(address(SP), true);
vm.stopPrank();
uint256 balanceBefore = erc20s[1].balanceOf(address(this));
SpentItem[] memory items = new SpentItem[](3);
items[0] = SpentItem({token: address(erc721s[0]), amount: 1, identifier: 2, itemType: ItemType.ERC721});
items[1] = SpentItem({token: address(erc20s[1]), amount: 100, identifier: 0, itemType: ItemType.ERC20});
items[2] = SpentItem({token: address(erc1155s[0]), amount: 1, identifier: 1, itemType: ItemType.ERC1155});
SP.acquireTokens(items);
assert(erc721s[0].ownerOf(2) == address(this));
assert(erc20s[1].balanceOf(address(this)) == balanceBefore + 100);
assert(erc1155s[0].balanceOf(address(this), 1) == 1);
}

function testAcquireTokensFail() public {
uint256 balanceBefore = erc20s[0].balanceOf(address(this));
vm.mockCall(address(this), abi.encodeWithSelector(Stargate.getOwner.selector, address(this)), abi.encode(0));
SpentItem[] memory items = new SpentItem[](3);
items[0] = SpentItem({token: address(erc721s[0]), amount: 1, identifier: 2, itemType: ItemType.ERC721});
items[1] = SpentItem({token: address(erc20s[1]), amount: 100, identifier: 0, itemType: ItemType.ERC20});
items[2] = SpentItem({token: address(erc1155s[0]), amount: 1, identifier: 1, itemType: ItemType.ERC1155});
vm.expectRevert();
SP.acquireTokens(items);
assert(erc721s[0].ownerOf(2) == address(borrower.addr));
assert(erc20s[1].balanceOf(address(this)) == balanceBefore);
assert(erc1155s[0].balanceOf(address(this), 1) == 0);
}

function testIncrementCaveatNonce() public {
vm.roll(5);
uint256 newNonce = SP.caveatNonces(address(this)) + uint256(blockhash(block.number - 1) << 0x80);
Expand Down

0 comments on commit 0f189db

Please sign in to comment.