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: Implement TransferEventEmitter (v1.0.0) #770

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions src/Periphery/TransferEventEmitter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pragma solidity ^0.8.17;

contract TransferEventEmitter {
event TokensTransferred(
address token,
address from,
address to,
uint256 amount
);

function emitTransferEvent(
address token,
address from,
address to,
uint256 amount
) external {
emit TokensTransferred(token, from, to, amount);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding access control to emitTransferEvent.

The emitTransferEvent function is external and can be called by any address. Consider adding access control to restrict who can emit transfer events.

You can use OpenZeppelin's Ownable contract to restrict access to the owner:

import "@openzeppelin/contracts/access/Ownable.sol";

contract TransferEventEmitter is Ownable {
    // ...
    function emitTransferEvent(
        address token,
        address from,
        address to,
        uint256 amount
    ) external onlyOwner {
        emit TokensTransferred(token, from, to, amount);
    }
}

}
117 changes: 115 additions & 2 deletions test/solidity/Facets/GenericSwapFacetV3.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { LibAllowList } from "lifi/Libraries/LibAllowList.sol";
import { FeeCollector } from "lifi/Periphery/FeeCollector.sol";
import { ERC20 } from "solmate/tokens/ERC20.sol";
import { ContractCallNotAllowed, CumulativeSlippageTooHigh, NativeAssetTransferFailed } from "lifi/Errors/GenericErrors.sol";

import { TransferEventEmitter } from "lifi/Periphery/TransferEventEmitter.sol";
import { UniswapV2Router02 } from "../utils/Interfaces.sol";
import { TestHelpers, MockUniswapDEX, NonETHReceiver } from "../utils/TestHelpers.sol";
import { ERC20, SafeTransferLib } from "solmate/utils/SafeTransferLib.sol";
Expand Down Expand Up @@ -62,6 +62,13 @@ contract GenericSwapFacetV3Test is DSTest, DiamondTest, TestHelpers {
uint256 toAmount
);

event TokensTransferred(
address token,
address from,
address to,
uint256 amount
);

// These values are for Mainnet
address internal constant USDC_ADDRESS =
0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
Expand All @@ -81,7 +88,6 @@ contract GenericSwapFacetV3Test is DSTest, DiamondTest, TestHelpers {
0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
address internal constant FEE_COLLECTOR =
0xbD6C7B0d2f68c2b7805d88388319cfB6EcB50eA9;

// -----

LiFiDiamond internal diamond;
Expand All @@ -93,6 +99,7 @@ contract GenericSwapFacetV3Test is DSTest, DiamondTest, TestHelpers {
ERC20 internal weth;
UniswapV2Router02 internal uniswap;
FeeCollector internal feeCollector;
TransferEventEmitter internal eventEmitter;

function fork() internal {
string memory rpcUrl = vm.envString("ETH_NODE_URI_MAINNET");
Expand All @@ -112,6 +119,7 @@ contract GenericSwapFacetV3Test is DSTest, DiamondTest, TestHelpers {
weth = ERC20(WETH_ADDRESS);
uniswap = UniswapV2Router02(UNISWAP_V2_ROUTER);
feeCollector = FeeCollector(FEE_COLLECTOR);
eventEmitter = new TransferEventEmitter();

// add genericSwapFacet (v1) to diamond (for gas usage comparison)
bytes4[] memory functionSelectors = new bytes4[](4);
Expand Down Expand Up @@ -196,6 +204,10 @@ contract GenericSwapFacetV3Test is DSTest, DiamondTest, TestHelpers {
genericSwapFacetV3.setFunctionApprovalBySignature(
feeCollector.collectNativeFees.selector
);
genericSwapFacetV3.addDex(address(eventEmitter));
genericSwapFacetV3.setFunctionApprovalBySignature(
eventEmitter.emitTransferEvent.selector
);

vm.label(address(genericSwapFacet), "LiFiDiamond");
vm.label(WETH_ADDRESS, "WETH_TOKEN");
Expand Down Expand Up @@ -1532,6 +1544,61 @@ contract GenericSwapFacetV3Test is DSTest, DiamondTest, TestHelpers {
);
}

function _produceSwapDataERC20FeeAndEmitTransferEvent()
private
view
returns (
LibSwap.SwapData[] memory swapData,
uint256 amountIn,
uint256 minAmountOut
)
{
amountIn = 100 * 10 ** dai.decimals();

uint integratorFee = 5 * 10 ** dai.decimals();
uint lifiFee = 0;
address integratorAddress = address(0xb33f); // some random address

// Swap1: Collect ERC20 fee (DAI)
// prepare swapData
swapData = new LibSwap.SwapData[](2);
swapData[0] = LibSwap.SwapData(
FEE_COLLECTOR,
FEE_COLLECTOR,
DAI_ADDRESS,
DAI_ADDRESS,
amountIn,
abi.encodeWithSelector(
feeCollector.collectTokenFees.selector,
DAI_ADDRESS,
integratorFee,
lifiFee,
integratorAddress
),
true
);

uint256 amountOutFeeCollection = amountIn - integratorFee - lifiFee;
minAmountOut = amountOutFeeCollection;

// Swap2: Emit Transfer Event
swapData[1] = LibSwap.SwapData(
address(eventEmitter),
address(eventEmitter),
DAI_ADDRESS,
DAI_ADDRESS,
amountOutFeeCollection,
abi.encodeWithSelector(
eventEmitter.emitTransferEvent.selector,
DAI_ADDRESS,
DAI_HOLDER,
SOME_WALLET,
amountOutFeeCollection
),
false
);
}

function test_CanCollectERC20FeesAndSwapToERC20_V1() public {
vm.startPrank(DAI_HOLDER);
dai.approve(address(genericSwapFacet), 100 * 10 ** dai.decimals());
Expand Down Expand Up @@ -1572,6 +1639,52 @@ contract GenericSwapFacetV3Test is DSTest, DiamondTest, TestHelpers {
vm.stopPrank();
}

function test_CanCollectERC20FeesAndTransferERC20_V1() public {
vm.startPrank(DAI_HOLDER);
dai.approve(address(genericSwapFacet), 100 * 10 ** dai.decimals());
uint startingBalance = dai.balanceOf(SOME_WALLET);
// get swapData
(
LibSwap.SwapData[] memory swapData,
uint256 amountIn,
uint256 minAmountOut
) = _produceSwapDataERC20FeeAndEmitTransferEvent();

vm.expectEmit(true, true, true, true, address(eventEmitter));
emit TokensTransferred(
DAI_ADDRESS,
DAI_HOLDER,
SOME_WALLET,
minAmountOut
);

vm.expectEmit(true, true, true, true, address(diamond));
emit LiFiGenericSwapCompleted(
0x0000000000000000000000000000000000000000000000000000000000000000, // transactionId,
"integrator", // integrator,
"referrer", // referrer,
SOME_WALLET, // receiver,
DAI_ADDRESS, // fromAssetId,
DAI_ADDRESS, // toAssetId,
amountIn, // fromAmount,
minAmountOut // toAmount (with liquidity in that selected block)
);

genericSwapFacet.swapTokensGeneric(
"",
"integrator",
"referrer",
payable(SOME_WALLET),
minAmountOut,
swapData
);

uint endingBalance = dai.balanceOf(SOME_WALLET);
assertEq(endingBalance, startingBalance + minAmountOut);

vm.stopPrank();
}

function test_CanCollectERC20FeesAndSwapToERC20_V2() public {
// ACTIVATE THIS CODE TO TEST GAS USAGE EXCL. MAX APPROVAL
vm.startPrank(address(genericSwapFacet));
Expand Down
Loading