Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add eip-2981 extension (wip) #31

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 67 additions & 17 deletions src/LSSVMRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ contract LSSVMRouter {
}

struct RobustPairNFTsFoTokenAndTokenforNFTsTrade {
Copy link

@harmvandendorpel harmvandendorpel Aug 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing 'r' on _ position: RobustPairNFTsFo_TokenAndTokenforNFTsTrade

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no point in changing it now, will make all contracts interacting with previous versions incompatible.

RobustPairSwapSpecific[] tokenToNFTTrades;
RobustPairSwapSpecific[] tokenToNFTTrades;
RobustPairSwapSpecificForToken[] nftToTokenTrades;
uint256 inputAmount;
address payable tokenRecipient;
Expand Down Expand Up @@ -86,6 +86,7 @@ contract LSSVMRouter {
)
external
payable
virtual
checkDeadline(deadline)
returns (uint256 remainingValue)
{
Expand All @@ -109,6 +110,7 @@ contract LSSVMRouter {
)
external
payable
virtual
checkDeadline(deadline)
returns (uint256 remainingValue)
{
Expand Down Expand Up @@ -137,7 +139,13 @@ contract LSSVMRouter {
address payable ethRecipient,
address nftRecipient,
uint256 deadline
) external payable checkDeadline(deadline) returns (uint256 outputAmount) {
)
external
payable
virtual
checkDeadline(deadline)
returns (uint256 outputAmount)
{
// Swap NFTs for ETH
// minOutput of swap set to 0 since we're doing an aggregate slippage check
outputAmount = _swapNFTsForToken(
Expand Down Expand Up @@ -177,7 +185,13 @@ contract LSSVMRouter {
address payable ethRecipient,
address nftRecipient,
uint256 deadline
) external payable checkDeadline(deadline) returns (uint256 outputAmount) {
)
external
payable
virtual
checkDeadline(deadline)
returns (uint256 outputAmount)
{
// Swap NFTs for ETH
// minOutput of swap set to 0 since we're doing an aggregate slippage check
outputAmount = _swapNFTsForToken(
Expand Down Expand Up @@ -225,7 +239,12 @@ contract LSSVMRouter {
uint256 inputAmount,
address nftRecipient,
uint256 deadline
) external checkDeadline(deadline) returns (uint256 remainingValue) {
)
external
virtual
checkDeadline(deadline)
returns (uint256 remainingValue)
{
return _swapERC20ForAnyNFTs(swapList, inputAmount, nftRecipient);
}

Expand All @@ -242,7 +261,12 @@ contract LSSVMRouter {
uint256 inputAmount,
address nftRecipient,
uint256 deadline
) external checkDeadline(deadline) returns (uint256 remainingValue) {
)
external
virtual
checkDeadline(deadline)
returns (uint256 remainingValue)
{
return _swapERC20ForSpecificNFTs(swapList, inputAmount, nftRecipient);
}

Expand All @@ -259,7 +283,7 @@ contract LSSVMRouter {
uint256 minOutput,
address tokenRecipient,
uint256 deadline
) external checkDeadline(deadline) returns (uint256 outputAmount) {
) external virtual checkDeadline(deadline) returns (uint256 outputAmount) {
return _swapNFTsForToken(swapList, minOutput, payable(tokenRecipient));
}

Expand All @@ -279,7 +303,7 @@ contract LSSVMRouter {
uint256 minOutput,
address nftRecipient,
uint256 deadline
) external checkDeadline(deadline) returns (uint256 outputAmount) {
) external virtual checkDeadline(deadline) returns (uint256 outputAmount) {
// Swap NFTs for ERC20
// minOutput of swap set to 0 since we're doing an aggregate slippage check
// output tokens are sent to msg.sender
Expand Down Expand Up @@ -320,7 +344,7 @@ contract LSSVMRouter {
uint256 minOutput,
address nftRecipient,
uint256 deadline
) external checkDeadline(deadline) returns (uint256 outputAmount) {
) external virtual checkDeadline(deadline) returns (uint256 outputAmount) {
// Swap NFTs for ERC20
// minOutput of swap set to 0 since we're doing an aggregate slippage check
// output tokens are sent to msg.sender
Expand Down Expand Up @@ -368,6 +392,7 @@ contract LSSVMRouter {
)
external
payable
virtual
checkDeadline(deadline)
returns (uint256 remainingValue)
{
Expand Down Expand Up @@ -425,7 +450,13 @@ contract LSSVMRouter {
address payable ethRecipient,
address nftRecipient,
uint256 deadline
) public payable checkDeadline(deadline) returns (uint256 remainingValue) {
)
public
payable
virtual
checkDeadline(deadline)
returns (uint256 remainingValue)
{
remainingValue = msg.value;
uint256 pairCost;
CurveErrorCodes.Error error;
Expand Down Expand Up @@ -482,7 +513,12 @@ contract LSSVMRouter {
uint256 inputAmount,
address nftRecipient,
uint256 deadline
) external checkDeadline(deadline) returns (uint256 remainingValue) {
)
external
virtual
checkDeadline(deadline)
returns (uint256 remainingValue)
{
remainingValue = inputAmount;
uint256 pairCost;
CurveErrorCodes.Error error;
Expand Down Expand Up @@ -529,7 +565,7 @@ contract LSSVMRouter {
uint256 inputAmount,
address nftRecipient,
uint256 deadline
) public checkDeadline(deadline) returns (uint256 remainingValue) {
) public virtual checkDeadline(deadline) returns (uint256 remainingValue) {
remainingValue = inputAmount;
uint256 pairCost;
CurveErrorCodes.Error error;
Expand Down Expand Up @@ -576,7 +612,7 @@ contract LSSVMRouter {
RobustPairSwapSpecificForToken[] calldata swapList,
address payable tokenRecipient,
uint256 deadline
) public checkDeadline(deadline) returns (uint256 outputAmount) {
) public virtual checkDeadline(deadline) returns (uint256 outputAmount) {
// Try doing each swap
uint256 numSwaps = swapList.length;
for (uint256 i; i < numSwaps; ) {
Expand Down Expand Up @@ -627,7 +663,12 @@ contract LSSVMRouter {
*/
function robustSwapETHForSpecificNFTsAndNFTsToToken(
RobustPairNFTsFoTokenAndTokenforNFTsTrade calldata params
) external payable returns (uint256 remainingValue, uint256 outputAmount) {
)
external
payable
virtual
returns (uint256 remainingValue, uint256 outputAmount)
{
{
remainingValue = msg.value;
uint256 pairCost;
Expand Down Expand Up @@ -734,7 +775,12 @@ contract LSSVMRouter {
*/
function robustSwapERC20ForSpecificNFTsAndNFTsToToken(
RobustPairNFTsFoTokenAndTokenforNFTsTrade calldata params
) external payable returns (uint256 remainingValue, uint256 outputAmount) {
)
external
payable
virtual
returns (uint256 remainingValue, uint256 outputAmount)
{
{
remainingValue = params.inputAmount;
uint256 pairCost;
Expand All @@ -744,17 +790,21 @@ contract LSSVMRouter {
uint256 numSwaps = params.tokenToNFTTrades.length;
for (uint256 i; i < numSwaps; ) {
// Calculate actual cost per swap
(error, , , pairCost, ) = params.tokenToNFTTrades[i]
(error, , , pairCost, ) = params
.tokenToNFTTrades[i]
.swapInfo
.pair
.getBuyNFTQuote(params.tokenToNFTTrades[i].swapInfo.nftIds.length);
.getBuyNFTQuote(
params.tokenToNFTTrades[i].swapInfo.nftIds.length
);

// If within our maxCost and no error, proceed
if (
pairCost <= params.tokenToNFTTrades[i].maxCost &&
error == CurveErrorCodes.Error.OK
) {
remainingValue -= params.tokenToNFTTrades[i]
remainingValue -= params
.tokenToNFTTrades[i]
.swapInfo
.pair
.swapTokenForSpecificNFTs(
Expand Down
8 changes: 8 additions & 0 deletions src/LSSVMRouterWithRoyalty.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {LSSVMRouter} from "./LSSVMRouter.sol";

contract LSSVMRouterWithRoyalty is LSSVMRouter, EIP2981Extension {
// TODO: Add EIP2981 complaint versions of all sudoswap swap functions.
}
49 changes: 49 additions & 0 deletions src/extensions/EIP2981.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";

///
/// @dev Interface for the NFT Royalty Standard
///
interface IERC2981 is IERC165 {
/// ERC165 bytes to add to interface array - set in parent contract
/// implementing this standard
///
/// bytes4(keccak256("royaltyInfo(uint256,uint256)")) == 0x2a55205a
/// bytes4 private constant _INTERFACE_ID_ERC2981 = 0x2a55205a;
/// _registerInterface(_INTERFACE_ID_ERC2981);

/// @notice Called with the sale price to determine how much royalty
// is owed and to whom.
/// @param _tokenId - the NFT asset queried for royalty information
/// @param _salePrice - the sale price of the NFT asset specified by _tokenId
/// @return receiver - address of who should be sent the royalty payment
/// @return royaltyAmount - the royalty payment amount for _salePrice
function royaltyInfo(uint256 _tokenId, uint256 _salePrice)
external
view
returns (address receiver, uint256 royaltyAmount);
}

contract EIP2981Extension {
bytes4 private constant _INTERFACE_ID_ERC2981 = 0x2a55205a;

function _checkRoyalties(address _contract) internal view returns (bool) {
bool success = IERC165(_contract).supportsInterface(
_INTERFACE_ID_ERC2981
);
return success;
}

function _getRoyaltyInfo(
IERC2981 _token,
uint256 _tokenId,
uint256 _salePrice
) internal view returns (address _royaltyReceiver, uint256 _royaltyAmount) {
(_royaltyReceiver, _royaltyAmount) = IERC2981(_token).royaltyInfo(
_tokenId,
_salePrice
);
}
}