Skip to content

Commit

Permalink
closes AST-2785
Browse files Browse the repository at this point in the history
  • Loading branch information
dangerousfood committed Jan 10, 2024
1 parent 9a050d8 commit 82c9c64
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 48 deletions.
33 changes: 17 additions & 16 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ TestNewLoan:testNewLoanAs1271ProxyAccountSender() (gas: 874115)
TestNewLoan:testNewLoanAs1271ProxyAccountThirdPartyFiller() (gas: 885129)
TestNewLoan:testNewLoanERC721CollateralDefaultTerms2() (gas: 429545)
TestNewLoan:testNewLoanRefinance() (gas: 590031)
TestNewLoan:testNewLoanViaOriginatorBorrowerApprovalAndLenderApproval() (gas: 325574)
TestNewLoan:testNewLoanViaOriginatorLenderApproval() (gas: 384348)
TestNewLoan:testNewLoanViaOriginatorBorrowerApprovalAndLenderApproval() (gas: 326073)
TestNewLoan:testNewLoanViaOriginatorLenderApproval() (gas: 384859)
TestNewLoan:testSettleLoan() (gas: 642171)
TestPausableNonReentrant:testNotOwner() (gas: 21254)
TestPausableNonReentrant:testPauseAndUnpause() (gas: 22621)
Expand Down Expand Up @@ -138,17 +138,18 @@ TestStarport:testStargateGetOwner() (gas: 8808)
TestStarport:testTokenNoCodeCollateral() (gas: 150695)
TestStarport:testTokenNoCodeDebt() (gas: 180946)
TestStarport:testUnpause() (gas: 17341)
TestStrategistOriginator:testEncodeWithAccountCounter() (gas: 12307)
TestStrategistOriginator:testGetStrategistData() (gas: 1513389)
TestStrategistOriginator:testIncrementCounterAsStrategist() (gas: 38723)
TestStrategistOriginator:testIncrementCounterNotAuthorized() (gas: 13401)
TestStrategistOriginator:testInvalidCollateral() (gas: 210503)
TestStrategistOriginator:testInvalidDeadline() (gas: 216182)
TestStrategistOriginator:testInvalidDebt() (gas: 212163)
TestStrategistOriginator:testInvalidDebtAmountAskingMoreThanOffered() (gas: 212502)
TestStrategistOriginator:testInvalidDebtAmountOfferingZero() (gas: 212823)
TestStrategistOriginator:testInvalidDebtAmountRequestingZero() (gas: 212755)
TestStrategistOriginator:testInvalidDebtLength() (gas: 211430)
TestStrategistOriginator:testInvalidOffer() (gas: 426690)
TestStrategistOriginator:testInvalidSigner() (gas: 214609)
TestStrategistOriginator:testSetStrategist() (gas: 17774)
TestStrategistOriginator:testEncodeWithAccountCounter() (gas: 12330)
TestStrategistOriginator:testGetStrategistData() (gas: 1809233)
TestStrategistOriginator:testIncrementCounterAsStrategist() (gas: 38767)
TestStrategistOriginator:testIncrementCounterNotAuthorized() (gas: 13423)
TestStrategistOriginator:testInvalidCollateral() (gas: 211094)
TestStrategistOriginator:testInvalidDeadline() (gas: 216915)
TestStrategistOriginator:testInvalidDebt() (gas: 212802)
TestStrategistOriginator:testInvalidDebtAmountAskingMoreThanOffered() (gas: 213176)
TestStrategistOriginator:testInvalidDebtAmountOfferingZero() (gas: 213486)
TestStrategistOriginator:testInvalidDebtAmountRequestingZero() (gas: 213441)
TestStrategistOriginator:testInvalidDebtLength() (gas: 212096)
TestStrategistOriginator:testInvalidOffer() (gas: 427919)
TestStrategistOriginator:testInvalidSigner() (gas: 215234)
TestStrategistOriginator:testSetStrategist() (gas: 17884)
TestStrategistOriginator:testWithdraw() (gas: 168001)
File renamed without changes.
39 changes: 39 additions & 0 deletions src/lib/StarportLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,21 @@ library StarportLib {
}
}

function transferSpentItemsSelf(SpentItem[] memory transfers, address from, address to) internal {
if (transfers.length > 0) {
uint256 i = 0;
for (; i < transfers.length;) {
SpentItem memory transfer = transfers[i];
_transferItem(transfer.itemType, transfer.token, transfer.identifier, transfer.amount, from, to);
unchecked {
++i;
}
}
} else {
revert InvalidTransferLength();
}
}

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* PRIVATE INTERNAL FUNCTIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
Expand Down Expand Up @@ -348,4 +363,28 @@ library StarportLib {
revert InvalidItemType();
}
}

function _transferItem(
ItemType itemType,
address token,
uint256 identifier,
uint256 amount,
address from,
address to
) internal {
if (token.code.length == 0) {
revert InvalidItemTokenNoCode();
}
if (itemType == ItemType.ERC20) {
SafeTransferLib.safeTransfer(token, to, amount);
} else if (itemType == ItemType.ERC721) {
// erc721 transfer
ERC721(token).transferFrom(from, to, identifier);
} else if (itemType == ItemType.ERC1155) {
// erc1155 transfer
ERC1155(token).safeTransferFrom(from, to, identifier, amount, new bytes(0));
} else {
revert InvalidItemType();
}
}
}
40 changes: 38 additions & 2 deletions src/originators/StrategistOriginator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ pragma solidity ^0.8.17;
import {Starport} from "../Starport.sol";
import {CaveatEnforcer} from "../enforcers/CaveatEnforcer.sol";
import {Originator} from "../originators/Originator.sol";
import {AdditionalTransfer} from "../lib/StarportLib.sol";
import {AdditionalTransfer, StarportLib} from "../lib/StarportLib.sol";

import {ConduitControllerInterface} from "seaport-types/src/interfaces/ConduitControllerInterface.sol";
import {ConduitInterface} from "seaport-types/src/interfaces/ConduitInterface.sol";
import {ItemType, ReceivedItem, SpentItem} from "seaport-types/src/lib/ConsiderationStructs.sol";
import {ECDSA} from "solady/src/utils/ECDSA.sol";
import {Ownable} from "solady/src/auth/Ownable.sol";
import {SignatureCheckerLib} from "solady/src/utils/SignatureCheckerLib.sol";
import {TokenReceiverInterface} from "../interfaces/TokenReceiverInterface.sol";

// Validator abstract contract that lays out the necessary structure and functions for the validator
contract StrategistOriginator is Ownable, Originator {
contract StrategistOriginator is Ownable, Originator, TokenReceiverInterface {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
Expand Down Expand Up @@ -269,4 +270,39 @@ contract StrategistOriginator is Ownable, Originator {
revert InvalidSigner();
}
}

function withdraw(SpentItem[] memory transfers, address recipient) external onlyOwner {
StarportLib.transferSpentItemsSelf(transfers, address(this), recipient);
}

// PUBLIC FUNCTIONS
function onERC721Received(address operator, address from, uint256 tokenId, bytes calldata data)
public
pure
virtual
override
returns (bytes4)
{
return TokenReceiverInterface.onERC721Received.selector;
}

function onERC1155Received(address, address, uint256, uint256, bytes calldata)
external
pure
virtual
override
returns (bytes4)
{
return TokenReceiverInterface.onERC1155Received.selector;
}

function onERC1155BatchReceived(address, address, uint256[] calldata, uint256[] calldata, bytes calldata)
external
pure
virtual
override
returns (bytes4)
{
return TokenReceiverInterface.onERC1155BatchReceived.selector;
}
}
2 changes: 1 addition & 1 deletion test/StarportTest.sol
Original file line number Diff line number Diff line change
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 "./interfaces/TokenReceiverInterface.sol";
import {TokenReceiverInterface} from "starport-core/interfaces/TokenReceiverInterface.sol";
import {Actions} from "starport-core/lib/StarportLib.sol";

import {CaveatEnforcer} from "starport-core/enforcers/CaveatEnforcer.sol";
Expand Down
30 changes: 1 addition & 29 deletions test/unit-testing/TestStarport.sol
Original file line number Diff line number Diff line change
Expand Up @@ -175,39 +175,11 @@ contract MockExoticPricing is Pricing {
}
}

contract MockOriginator is StrategistOriginator, TokenReceiverInterface {
contract MockOriginator is StrategistOriginator {
constructor(Starport SP_, address strategist_, uint256 fee_)
StrategistOriginator(SP_, strategist_, fee_, msg.sender)
{}

// PUBLIC FUNCTIONS
function onERC721Received(address operator, address from, uint256 tokenId, bytes calldata data)
public
pure
virtual
returns (bytes4)
{
return TokenReceiverInterface.onERC721Received.selector;
}

function onERC1155Received(address, address, uint256, uint256, bytes calldata)
external
pure
virtual
returns (bytes4)
{
return TokenReceiverInterface.onERC1155Received.selector;
}

function onERC1155BatchReceived(address, address, uint256[] calldata, uint256[] calldata, bytes calldata)
external
pure
virtual
returns (bytes4)
{
return TokenReceiverInterface.onERC1155BatchReceived.selector;
}

function terms(bytes calldata) public view returns (Starport.Terms memory) {
return Starport.Terms({
status: address(0),
Expand Down
34 changes: 34 additions & 0 deletions test/unit-testing/TestStrategistOriginator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,38 @@ contract TestStrategistOriginator is StarportTest, DeepEq {
})
);
}

function testWithdraw() public {
vm.startPrank(borrower.addr);
erc721s[0].transferFrom(borrower.addr, address(SO), 1);
erc20s[1].transfer(address(SO), 10_000);
erc1155s[0].safeTransferFrom(borrower.addr, address(SO), 1, 1, "");
vm.stopPrank();

SpentItem[] memory spentItems = new SpentItem[](3);

spentItems[0] = SpentItem({itemType: ItemType.ERC721, token: address(erc721s[0]), identifier: 1, amount: 1});
spentItems[1] = SpentItem({itemType: ItemType.ERC20, token: address(erc20s[1]), identifier: 0, amount: 10_000});
spentItems[2] = SpentItem({itemType: ItemType.ERC1155, token: address(erc1155s[0]), identifier: 1, amount: 1});

uint256 balanceBefore = erc20s[1].balanceOf(strategist.addr);
address owner = SO.owner();
vm.startPrank(owner);
SO.withdraw(spentItems, strategist.addr);
vm.stopPrank();

assertEq(
erc721s[0].ownerOf(1), strategist.addr, "erc721s not transferred properly on StrategistOriginator withdraw"
);
assertEq(
erc20s[1].balanceOf(strategist.addr) - balanceBefore,
10_000,
"erc20s not transferred properly on StrategistOriginator withdraw"
);
assertEq(
erc1155s[0].balanceOf(strategist.addr, 1),
1,
"erc1155s not transferred properly on StrategistOriginator withdraw"
);
}
}

0 comments on commit 82c9c64

Please sign in to comment.