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

Implement fees collection of Uniswap #16

Merged
merged 4 commits into from
Jan 30, 2024
Merged
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
24 changes: 22 additions & 2 deletions contracts/L2TokenReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Own
import {ISwapRouter} from "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import {TransferHelper} from "@uniswap/v3-periphery/contracts/libraries/TransferHelper.sol";

import {IL2TokenReceiver, IERC165} from "./interfaces/IL2TokenReceiver.sol";
import {IL2TokenReceiver, IERC165, IERC721Receiver} from "./interfaces/IL2TokenReceiver.sol";
import {INonfungiblePositionManager} from "./interfaces/uniswap-v3/INonfungiblePositionManager.sol";
FedokDL marked this conversation as resolved.
Show resolved Hide resolved

contract L2TokenReceiver is IL2TokenReceiver, OwnableUpgradeable, UUPSUpgradeable {
Expand Down Expand Up @@ -35,7 +35,10 @@ contract L2TokenReceiver is IL2TokenReceiver, OwnableUpgradeable, UUPSUpgradeabl
}

function supportsInterface(bytes4 interfaceId_) external pure returns (bool) {
return interfaceId_ == type(IL2TokenReceiver).interfaceId || interfaceId_ == type(IERC165).interfaceId;
return
interfaceId_ == type(IL2TokenReceiver).interfaceId ||
interfaceId_ == type(IERC721Receiver).interfaceId ||
interfaceId_ == type(IERC165).interfaceId;
}

function editParams(SwapParams memory newParams_) external onlyOwner {
Expand Down Expand Up @@ -116,6 +119,23 @@ contract L2TokenReceiver is IL2TokenReceiver, OwnableUpgradeable, UUPSUpgradeabl
emit LiquidityIncreased(tokenId_, amount0_, amount1_, liquidity_, amountMin0_, amountMin1_);
}

function collectFees(uint256 tokenId_) external returns (uint256 amount0_, uint256 amount1_) {
INonfungiblePositionManager.CollectParams memory params_ = INonfungiblePositionManager.CollectParams({
tokenId: tokenId_,
recipient: address(this),
amount0Max: type(uint128).max,
amount1Max: type(uint128).max
});

(amount0_, amount1_) = INonfungiblePositionManager(nonfungiblePositionManager).collect(params_);

emit FeesCollected(tokenId_, amount0_, amount1_);
}

function onERC721Received(address, address, uint256, bytes calldata) external pure returns (bytes4) {
return this.onERC721Received.selector;
}

function _editParams(SwapParams memory newParams_) private {
require(newParams_.tokenIn != address(0), "L2TR: invalid tokenIn");
require(newParams_.tokenOut != address(0), "L2TR: invalid tokenOut");
Expand Down
11 changes: 10 additions & 1 deletion contracts/interfaces/IL2TokenReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
pragma solidity ^0.8.20;

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

/**
* This is Swap contract that swaps tokens using Uniswap V3.
*/
interface IL2TokenReceiver is IERC165 {
interface IL2TokenReceiver is IERC165, IERC721Receiver {
/**
* The structure that stores the swap params.
* @param tokenIn The address of the token to swap from.
Expand Down Expand Up @@ -55,6 +56,14 @@ interface IL2TokenReceiver is IERC165 {
uint256 amount1Min
);

/**
* The event that is emitted when the fees are collected.
* @param tokenId The ID of the position.
* @param amount0 The amount of token0 collected.
* @param amount1 The amount of token1 collected.
*/
event FeesCollected(uint256 indexed tokenId, uint256 amount0, uint256 amount1);

/**
* The function to edit the swap params.
* @param params_ The new swap params.
Expand Down
13 changes: 12 additions & 1 deletion contracts/interfaces/uniswap-v3/INonfungiblePositionManager.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface INonfungiblePositionManager {
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";

interface INonfungiblePositionManager is IERC721 {
struct IncreaseLiquidityParams {
uint256 tokenId;
uint256 amount0Desired;
Expand All @@ -11,10 +13,19 @@ interface INonfungiblePositionManager {
uint256 deadline;
}

struct CollectParams {
uint256 tokenId;
address recipient;
uint128 amount0Max;
uint128 amount1Max;
}

function increaseLiquidity(
IncreaseLiquidityParams calldata params
) external payable returns (uint128 liquidity, uint256 amount0, uint256 amount1);

function collect(CollectParams calldata params) external payable returns (uint256 amount0, uint256 amount1);

function positions(
uint256 tokenId
)
Expand Down
5 changes: 5 additions & 0 deletions test/L2TokenReceiver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ describe('L2TokenReceiver', () => {
it('should support IERC165', async () => {
expect(await l2TokenReceiver.supportsInterface('0x01ffc9a7')).to.be.true;
});
it('should support IERC721Receiver', async () => {
expect(await l2TokenReceiver.supportsInterface('0x150b7a02')).to.be.true;
});
});

describe('#editParams', () => {
Expand Down Expand Up @@ -217,3 +220,5 @@ describe('L2TokenReceiver', () => {
});
});
});

// npx hardhat test "test/L2TokenReceiver.test.ts"
2 changes: 2 additions & 0 deletions test/fork/L1Sender.fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,5 @@ describe('L1Sender Fork', () => {
});
});
});

// npx hardhat test "test/fork/L1Sender.fork.test.ts"
27 changes: 26 additions & 1 deletion test/fork/L2TokenReceiver.fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,31 @@ describe('L2TokenReceiver Fork', () => {
await expect(tx).to.changeTokenBalance(outputToken, l2TokenReceiver, -txResult[2]);
});
});

describe('#collectFees', () => {
const poolId = 376582;

beforeEach('setup', async () => {
const poolOwner = await nonfungiblePositionManager.ownerOf(poolId);
const poolOwnerSigner = await ethers.getImpersonatedSigner(poolOwner);

await OWNER.sendTransaction({ to: poolOwner, value: wei(10) });

await nonfungiblePositionManager
.connect(poolOwnerSigner)
['safeTransferFrom(address,address,uint256)'](poolOwner, l2TokenReceiver, poolId);
});

it('should collect fees', async () => {
const outputTokenBalance = await outputToken.balanceOf(l2TokenReceiver);
const inputTokenBalance = await inputToken.balanceOf(l2TokenReceiver);

await l2TokenReceiver.collectFees(poolId);

expect(await outputToken.balanceOf(l2TokenReceiver)).to.greaterThan(outputTokenBalance);
expect(await inputToken.balanceOf(l2TokenReceiver)).to.greaterThan(inputTokenBalance);
});
});
});

// npx hardhat test "test/fork/Swap.fork.test.ts"
// npx hardhat test "test/fork/L2TokenReceiver.fork.test.ts"
Loading