Skip to content

Commit

Permalink
separate fee logic to solver fee bps and flat execution fee
Browse files Browse the repository at this point in the history
  • Loading branch information
thal0x committed Nov 8, 2024
1 parent 59d2bf5 commit 3ddda67
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 17 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
{}
{
"solidity.formatter": "forge",
"solidity.linter": ""
}
14 changes: 9 additions & 5 deletions AxelarHandler/src/GoFastHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@ contract GoFastHandler {
address tokenIn,
uint256 swapAmountIn,
bytes memory swapCalldata,
uint256 feeAmount,
uint256 executionFeeAmount,
uint256 solverFeeBPS,
bytes32 sender,
bytes32 recipient,
uint32 destinationDomain,
uint64 timeoutTimestamp,
bytes calldata destinationCalldata
) public payable returns (bytes32) {
require(feeAmount != 0, "fast transfer fee cannot be zero");

require(executionFeeAmount != 0, "execution fee cannot be zero");
require(solverFeeBPS != 0, "solver fee cannot be zero");
uint256 swapAmountOut = _swap(tokenIn, swapAmountIn, swapCalldata);

require(swapAmountOut >= feeAmount, "amount received from swap is less than fast transfer fee");
uint256 solverFeeAmount = (swapAmountOut * solverFeeBPS) / 10000;
uint256 totalFee = executionFeeAmount + solverFeeAmount;

require(swapAmountOut >= totalFee, "amount received from swap is less than fee");

// this is the amount that the recipient will receive on the destination chain
uint256 swapAmountOutAfterFee = swapAmountOut - feeAmount;
uint256 swapAmountOutAfterFee = swapAmountOut - totalFee;

return fastTransferGateway.submitOrder(
sender,
Expand Down
117 changes: 106 additions & 11 deletions AxelarHandler/test/GoFastHandler.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ contract GoFastHandlerTest is Test {
address tokenIn = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1; // WETH
uint256 amountIn = 1 ether;
uint256 fastTransferFee = 1_000_000; // 1 USDC
uint256 solverFeeBPS = 10; // 0.1%
uint32 destinationDomain = 10;
uint64 timeoutTimestamp = uint64(block.timestamp + 100);
bytes32 sender = keccak256("sender");
Expand All @@ -55,7 +56,7 @@ contract GoFastHandlerTest is Test {
sender,
recipient,
2702776834,
2701776834,
2699074058,
destinationDomain,
timeoutTimestamp,
""
Expand All @@ -67,7 +68,16 @@ contract GoFastHandlerTest is Test {
IERC20(tokenIn).approve(address(handler), amountIn);

bytes32 orderId = handler.swapAndSubmitOrder(
tokenIn, amountIn, swapCalldata, fastTransferFee, sender, recipient, destinationDomain, timeoutTimestamp, ""
tokenIn,
amountIn,
swapCalldata,
fastTransferFee,
solverFeeBPS,
sender,
recipient,
destinationDomain,
timeoutTimestamp,
""
);
vm.stopPrank();

Expand All @@ -78,6 +88,7 @@ contract GoFastHandlerTest is Test {
address tokenIn = 0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9; // USDT
uint256 amountIn = 100_000_000;
uint256 fastTransferFee = 1_000_000; // 1 USDC
uint256 solverFeeBPS = 10; // 0.1%
uint32 destinationDomain = 10;
uint64 timeoutTimestamp = uint64(block.timestamp + 100);
bytes32 sender = keccak256("sender");
Expand All @@ -96,7 +107,7 @@ contract GoFastHandlerTest is Test {
sender,
recipient,
99963678,
98963678,
98863715,
destinationDomain,
timeoutTimestamp,
""
Expand All @@ -108,7 +119,16 @@ contract GoFastHandlerTest is Test {
IERC20(tokenIn).approve(address(handler), amountIn);

bytes32 orderId = handler.swapAndSubmitOrder(
tokenIn, amountIn, swapCalldata, fastTransferFee, sender, recipient, destinationDomain, timeoutTimestamp, ""
tokenIn,
amountIn,
swapCalldata,
fastTransferFee,
solverFeeBPS,
sender,
recipient,
destinationDomain,
timeoutTimestamp,
""
);
vm.stopPrank();

Expand All @@ -119,6 +139,7 @@ contract GoFastHandlerTest is Test {
address tokenIn = address(0); // ETH
uint256 amountIn = 1 ether;
uint256 fastTransferFee = 1_000_000; // 1 USDC
uint256 solverFeeBPS = 10; // 0.1%
uint32 destinationDomain = 10;
uint64 timeoutTimestamp = uint64(block.timestamp + 100);
bytes32 sender = keccak256("sender");
Expand All @@ -139,7 +160,7 @@ contract GoFastHandlerTest is Test {
sender,
recipient,
2702776834,
2701776834,
2699074058,
destinationDomain,
timeoutTimestamp,
""
Expand All @@ -149,7 +170,16 @@ contract GoFastHandlerTest is Test {

vm.startPrank(alice);
bytes32 orderId = handler.swapAndSubmitOrder{value: amountIn}(
tokenIn, amountIn, swapCalldata, fastTransferFee, sender, recipient, destinationDomain, timeoutTimestamp, ""
tokenIn,
amountIn,
swapCalldata,
fastTransferFee,
solverFeeBPS,
sender,
recipient,
destinationDomain,
timeoutTimestamp,
""
);
vm.stopPrank();

Expand All @@ -161,6 +191,7 @@ contract GoFastHandlerTest is Test {
uint256 amountIn = 1 ether;
uint256 amountOutMinimum = 1_000_000_000_000; // 1,000,000 USDC
uint256 fastTransferFee = 1_000_000; // 1 USDC
uint256 solverFeeBPS = 10; // 0.1%
uint32 destinationDomain = 10;
uint64 timeoutTimestamp = uint64(block.timestamp + 100);
bytes32 sender = keccak256("sender");
Expand All @@ -178,7 +209,16 @@ contract GoFastHandlerTest is Test {

vm.expectRevert("Too little received");
handler.swapAndSubmitOrder(
tokenIn, amountIn, swapCalldata, fastTransferFee, sender, recipient, destinationDomain, timeoutTimestamp, ""
tokenIn,
amountIn,
swapCalldata,
fastTransferFee,
solverFeeBPS,
sender,
recipient,
destinationDomain,
timeoutTimestamp,
""
);
vm.stopPrank();
}
Expand All @@ -187,6 +227,7 @@ contract GoFastHandlerTest is Test {
address tokenIn = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1; // WETH
uint256 amountIn = 1 ether;
uint256 fastTransferFee = 3000_000_000; // 3000 USDC
uint256 solverFeeBPS = 10; // 0.1%
uint32 destinationDomain = 10;
uint64 timeoutTimestamp = uint64(block.timestamp + 100);
bytes32 sender = keccak256("sender");
Expand All @@ -201,9 +242,18 @@ contract GoFastHandlerTest is Test {
vm.startPrank(alice);
IERC20(tokenIn).approve(address(handler), amountIn);

vm.expectRevert("amount received from swap is less than fast transfer fee");
vm.expectRevert("amount received from swap is less than fee");
handler.swapAndSubmitOrder(
tokenIn, amountIn, swapCalldata, fastTransferFee, sender, recipient, destinationDomain, timeoutTimestamp, ""
tokenIn,
amountIn,
swapCalldata,
fastTransferFee,
solverFeeBPS,
sender,
recipient,
destinationDomain,
timeoutTimestamp,
""
);
vm.stopPrank();
}
Expand All @@ -212,6 +262,7 @@ contract GoFastHandlerTest is Test {
address tokenIn = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1; // WETH
uint256 amountIn = 1 ether;
uint256 fastTransferFee = 0;
uint256 solverFeeBPS = 10; // 0.1%
uint32 destinationDomain = 10;
uint64 timeoutTimestamp = uint64(block.timestamp + 100);
bytes32 sender = keccak256("sender");
Expand All @@ -226,9 +277,53 @@ contract GoFastHandlerTest is Test {
vm.startPrank(alice);
IERC20(tokenIn).approve(address(handler), amountIn);

vm.expectRevert("fast transfer fee cannot be zero");
vm.expectRevert("execution fee cannot be zero");
handler.swapAndSubmitOrder(
tokenIn, amountIn, swapCalldata, fastTransferFee, sender, recipient, destinationDomain, timeoutTimestamp, ""
tokenIn,
amountIn,
swapCalldata,
fastTransferFee,
solverFeeBPS,
sender,
recipient,
destinationDomain,
timeoutTimestamp,
""
);
vm.stopPrank();
}

function testSwapAndSubmitOrderRevertsIfSolverFeeIsZero() public {
address tokenIn = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1; // WETH
uint256 amountIn = 1 ether;
uint256 fastTransferFee = 1_000_000; // 1 USDC
uint256 solverFeeBPS = 0;
uint32 destinationDomain = 10;
uint64 timeoutTimestamp = uint64(block.timestamp + 100);
bytes32 sender = keccak256("sender");
bytes32 recipient = keccak256("recipient");

bytes memory swapCalldata = _encodeSwapExactInputCalldata(tokenIn, usdc, 500, address(handler), amountIn, 0, 0);

deal(tokenIn, alice, amountIn);

vm.mockCall(fastTransferGateway, abi.encodeWithSelector(IFastTransferGateway.token.selector), abi.encode(usdc));

vm.startPrank(alice);
IERC20(tokenIn).approve(address(handler), amountIn);

vm.expectRevert("solver fee cannot be zero");
handler.swapAndSubmitOrder(
tokenIn,
amountIn,
swapCalldata,
fastTransferFee,
solverFeeBPS,
sender,
recipient,
destinationDomain,
timeoutTimestamp,
""
);
vm.stopPrank();
}
Expand Down

0 comments on commit 3ddda67

Please sign in to comment.