Skip to content

Add swap fee #8

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

Merged
merged 19 commits into from
Jun 19, 2025
Merged
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
Prev Previous commit
Next Next commit
Refactor data encoding in ForwarderLogic and related tests for fee ha…
…ndling
  • Loading branch information
0x0Louis committed May 11, 2025
commit 10503ba4214d1dafbcac6ec08067ef6918d87551
6 changes: 3 additions & 3 deletions src/ForwarderLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ contract ForwarderLogic is IForwarderLogic {
if (msg.sender != _router) revert ForwarderLogic__OnlyRouter();
if (_blacklist[from] || (from != to && _blacklist[to])) revert ForwarderLogic__Blacklisted();

uint256 feePercent = uint256(uint16(bytes2(data[0:2])));
address approval = address(uint160(bytes20(data[2:22])));
address router = address(uint160(bytes20(data[22:42])));
address approval = address(uint160(bytes20(data[0:20])));
address router = address(uint160(bytes20(data[20:40])));
uint256 feePercent = uint256(uint16(bytes2(data[40:42])));
bytes memory routerData = data[42:];

RouterLib.transfer(_router, tokenIn, from, address(this), amountIn);
Expand Down
10 changes: 5 additions & 5 deletions test/ForwarderLogic.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ contract ForwarderLogicTest is Test, PackedRouteHelper {
MockERC20(tokenIn).mint(from, amountIn);

bytes memory data = abi.encodePacked(
uint16(0),
address(this),
address(this),
uint16(0),
abi.encodeCall(this.swap, (tokenIn, tokenOut, amountIn, amountOut, address(forwarderLogic)))
);

Expand Down Expand Up @@ -136,9 +136,9 @@ contract ForwarderLogicTest is Test, PackedRouteHelper {
uint256 feeAmountIn = (amountIn * feePercent) / 10_000;

bytes memory data = abi.encodePacked(
uint16(feePercent),
address(this),
address(this),
uint16(feePercent),
abi.encodeCall(this.swap, (tokenIn, tokenOut, amountIn - feeAmountIn, amountOut, address(forwarderLogic)))
);

Expand Down Expand Up @@ -171,14 +171,14 @@ contract ForwarderLogicTest is Test, PackedRouteHelper {

vm.expectRevert(IForwarderLogic.ForwarderLogic__UntrustedRouter.selector);
forwarderLogic.swapExactIn(
token0, address(0), 0, 0, address(1), address(0), abi.encodePacked(uint16(0), address(1), address(1), "")
token0, address(0), 0, 0, address(1), address(0), abi.encodePacked(address(1), address(1), uint16(0), "")
);

forwarderLogic.updateTrustedRouter(address(0), true);

vm.expectRevert(IForwarderLogic.ForwarderLogic__NoCode.selector);
forwarderLogic.swapExactIn(
token0, address(0), 0, 0, address(1), address(0), abi.encodePacked(uint16(0), address(this), address(0), "")
token0, address(0), 0, 0, address(1), address(0), abi.encodePacked(address(this), address(0), uint16(0), "")
);

revertData = bytes("Error");
Expand All @@ -191,7 +191,7 @@ contract ForwarderLogicTest is Test, PackedRouteHelper {
0,
address(1),
address(0),
abi.encodePacked(uint16(0), address(this), address(this), "")
abi.encodePacked(address(this), address(this), uint16(0), "")
);
}

Expand Down
12 changes: 6 additions & 6 deletions test/ForwarderLogicIntegration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ contract ForwarderLogicIntegrationTest is Test {
AVAX_AMOUNT / 2,
alice,
block.timestamp,
abi.encodePacked(uint16(0), ODOS, ODOS, ODOS_USDC_AVAX)
abi.encodePacked(ODOS, ODOS, uint16(0), ODOS_USDC_AVAX)
);

assertEq(IERC20(USDC).balanceOf(address(this)), 0, "test_ODOS::1");
Expand All @@ -114,7 +114,7 @@ contract ForwarderLogicIntegrationTest is Test {
USDC_AMOUNT / 2,
alice,
block.timestamp,
abi.encodePacked(uint16(0), ODOS, ODOS, ODOS_AVAX_USDC)
abi.encodePacked(ODOS, ODOS, uint16(0), ODOS_AVAX_USDC)
);

assertEq(IERC20(WAVAX).balanceOf(address(this)), 0, "test_ODOS::3");
Expand All @@ -136,7 +136,7 @@ contract ForwarderLogicIntegrationTest is Test {
AVAX_AMOUNT / 2,
alice,
block.timestamp,
abi.encodePacked(uint16(0), OKX_APPROVAL, OKX, OKX_USDC_AVAX)
abi.encodePacked(OKX_APPROVAL, OKX, uint16(0), OKX_USDC_AVAX)
);

assertEq(IERC20(USDC).balanceOf(address(this)), 0, "test_OKX::1");
Expand All @@ -150,7 +150,7 @@ contract ForwarderLogicIntegrationTest is Test {
USDC_AMOUNT / 2,
alice,
block.timestamp,
abi.encodePacked(uint16(0), OKX_APPROVAL, OKX, OKX_AVAX_USDC)
abi.encodePacked(OKX_APPROVAL, OKX, uint16(0), OKX_AVAX_USDC)
);

assertEq(IERC20(WAVAX).balanceOf(address(this)), 0, "test_OKX::3");
Expand All @@ -172,7 +172,7 @@ contract ForwarderLogicIntegrationTest is Test {
AVAX_AMOUNT / 2,
alice,
block.timestamp,
abi.encodePacked(uint16(0), JAR, JAR, JAR_USDC_AVAX)
abi.encodePacked(JAR, JAR, uint16(0), JAR_USDC_AVAX)
);

assertEq(IERC20(USDC).balanceOf(address(this)), 0, "test_JAR::1");
Expand All @@ -186,7 +186,7 @@ contract ForwarderLogicIntegrationTest is Test {
USDC_AMOUNT / 2,
alice,
block.timestamp,
abi.encodePacked(uint16(0), JAR, JAR, JAR_AVAX_USDC)
abi.encodePacked(JAR, JAR, uint16(0), JAR_AVAX_USDC)
);

assertEq(IERC20(WAVAX).balanceOf(address(this)), 0, "test_JAR::3");
Expand Down
Loading