Add native relayer payment for exactIn swaps
This commit is contained in:
parent
330122eece
commit
162dd6ce9e
|
@ -96,30 +96,31 @@ contract CrossChainSwapV2 {
|
||||||
"tokenOut must be wrapped native asset"
|
"tokenOut must be wrapped native asset"
|
||||||
);
|
);
|
||||||
|
|
||||||
// pay relayer before attempting to do the swap
|
|
||||||
// reflect payment in second swap amount
|
|
||||||
IERC20 feeToken = IERC20(FEE_TOKEN_ADDRESS);
|
|
||||||
feeToken.safeTransfer(msg.sender, payload.relayerFee);
|
|
||||||
uint256 swapAmountLessFees = payload.swapAmount - payload.relayerFee;
|
|
||||||
|
|
||||||
// approve the router to spend tokens
|
// approve the router to spend tokens
|
||||||
TransferHelper.safeApprove(
|
TransferHelper.safeApprove(
|
||||||
uniPath[0],
|
uniPath[0],
|
||||||
address(SWAP_ROUTER),
|
address(SWAP_ROUTER),
|
||||||
swapAmountLessFees
|
payload.swapAmount
|
||||||
);
|
);
|
||||||
|
|
||||||
// try to execute the swap
|
// try to execute the swap
|
||||||
try SWAP_ROUTER.swapExactTokensForTokens(
|
try SWAP_ROUTER.swapExactTokensForTokens(
|
||||||
swapAmountLessFees,
|
payload.swapAmount,
|
||||||
payload.estimatedAmount,
|
payload.estimatedAmount,
|
||||||
uniPath,
|
uniPath,
|
||||||
address(this),
|
address(this),
|
||||||
payload.deadline
|
payload.deadline
|
||||||
) returns (uint256[] memory amounts) {
|
) returns (uint256[] memory amounts) {
|
||||||
|
// calculate how much to pay the relayer in the native token
|
||||||
|
uint256 nativeRelayerFee = amounts[1] * payload.relayerFee / payload.swapAmount;
|
||||||
|
uint256 nativeAmountOut = amounts[1] - nativeRelayerFee;
|
||||||
|
|
||||||
// unwrap native and send to recipient
|
// unwrap native and send to recipient
|
||||||
IWETH(WRAPPED_NATIVE).withdraw(amounts[1]);
|
IWETH(WRAPPED_NATIVE).withdraw(amounts[1]);
|
||||||
payable(payload.recipientAddress).transfer(amounts[1]);
|
payable(payload.recipientAddress).transfer(nativeAmountOut);
|
||||||
|
|
||||||
|
/// pay the relayer in the native token
|
||||||
|
payable(msg.sender).transfer(nativeRelayerFee);
|
||||||
|
|
||||||
// used in UI to tell user they're getting
|
// used in UI to tell user they're getting
|
||||||
// their desired token
|
// their desired token
|
||||||
|
@ -127,15 +128,19 @@ contract CrossChainSwapV2 {
|
||||||
payload.recipientAddress,
|
payload.recipientAddress,
|
||||||
uniPath[1],
|
uniPath[1],
|
||||||
msg.sender,
|
msg.sender,
|
||||||
amounts[1],
|
nativeAmountOut,
|
||||||
1
|
1
|
||||||
);
|
);
|
||||||
return amounts;
|
return amounts;
|
||||||
} catch {
|
} catch {
|
||||||
|
// pay relayer in the feeToken since the swap failed
|
||||||
|
IERC20 feeToken = IERC20(FEE_TOKEN_ADDRESS);
|
||||||
|
feeToken.safeTransfer(msg.sender, payload.relayerFee);
|
||||||
|
|
||||||
// swap failed - return feeToken to recipient
|
// swap failed - return feeToken to recipient
|
||||||
IERC20(FEE_TOKEN_ADDRESS).safeTransfer(
|
feeToken.safeTransfer(
|
||||||
payload.recipientAddress,
|
payload.recipientAddress,
|
||||||
swapAmountLessFees
|
payload.swapAmount - payload.relayerFee
|
||||||
);
|
);
|
||||||
|
|
||||||
// used in UI to tell user they're getting
|
// used in UI to tell user they're getting
|
||||||
|
@ -144,7 +149,7 @@ contract CrossChainSwapV2 {
|
||||||
payload.recipientAddress,
|
payload.recipientAddress,
|
||||||
uniPath[0],
|
uniPath[0],
|
||||||
msg.sender,
|
msg.sender,
|
||||||
swapAmountLessFees,
|
payload.swapAmount - payload.relayerFee,
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -176,8 +181,7 @@ contract CrossChainSwapV2 {
|
||||||
"tokenOut must be wrapped native asset"
|
"tokenOut must be wrapped native asset"
|
||||||
);
|
);
|
||||||
|
|
||||||
// pay relayer before attempting to do the swap
|
// pay the relayer in feeToken so that user gets desired exact amount out
|
||||||
// reflect payment in second swap amount
|
|
||||||
IERC20 feeToken = IERC20(FEE_TOKEN_ADDRESS);
|
IERC20 feeToken = IERC20(FEE_TOKEN_ADDRESS);
|
||||||
feeToken.safeTransfer(msg.sender, payload.relayerFee);
|
feeToken.safeTransfer(msg.sender, payload.relayerFee);
|
||||||
uint256 maxAmountInLessFees = payload.swapAmount - payload.relayerFee;
|
uint256 maxAmountInLessFees = payload.swapAmount - payload.relayerFee;
|
||||||
|
|
|
@ -95,17 +95,11 @@ contract CrossChainSwapV3 {
|
||||||
"tokenOut must be wrapped Native"
|
"tokenOut must be wrapped Native"
|
||||||
);
|
);
|
||||||
|
|
||||||
// pay relayer before attempting to do the swap
|
|
||||||
// reflect payment in second swap amount
|
|
||||||
IERC20 feeToken = IERC20(FEE_TOKEN_ADDRESS);
|
|
||||||
feeToken.safeTransfer(msg.sender, payload.relayerFee);
|
|
||||||
uint256 swapAmountLessFees = payload.swapAmount - payload.relayerFee;
|
|
||||||
|
|
||||||
// approve the router to spend tokens
|
// approve the router to spend tokens
|
||||||
TransferHelper.safeApprove(
|
TransferHelper.safeApprove(
|
||||||
payload.path[0],
|
payload.path[0],
|
||||||
address(SWAP_ROUTER),
|
address(SWAP_ROUTER),
|
||||||
swapAmountLessFees
|
payload.swapAmount
|
||||||
);
|
);
|
||||||
|
|
||||||
// set swap options with user params
|
// set swap options with user params
|
||||||
|
@ -116,16 +110,23 @@ contract CrossChainSwapV3 {
|
||||||
fee: payload.poolFee,
|
fee: payload.poolFee,
|
||||||
recipient: address(this),
|
recipient: address(this),
|
||||||
deadline: payload.deadline,
|
deadline: payload.deadline,
|
||||||
amountIn: swapAmountLessFees,
|
amountIn: payload.swapAmount,
|
||||||
amountOutMinimum: payload.estimatedAmount,
|
amountOutMinimum: payload.estimatedAmount,
|
||||||
sqrtPriceLimitX96: 0
|
sqrtPriceLimitX96: 0
|
||||||
});
|
});
|
||||||
|
|
||||||
// try to execute the swap
|
// try to execute the swap
|
||||||
try SWAP_ROUTER.exactInputSingle(params) returns (uint256 amountOut) {
|
try SWAP_ROUTER.exactInputSingle(params) returns (uint256 amountOut) {
|
||||||
|
// calculate how much to pay the relayer in the native token
|
||||||
|
uint256 nativeRelayerFee = amountOut * payload.relayerFee / payload.swapAmount;
|
||||||
|
uint256 nativeAmountOut = amountOut - nativeRelayerFee;
|
||||||
|
|
||||||
// unwrap native and send to recipient
|
// unwrap native and send to recipient
|
||||||
IWETH(WRAPPED_NATIVE).withdraw(amountOut);
|
IWETH(WRAPPED_NATIVE).withdraw(amountOut);
|
||||||
payable(payload.recipientAddress).transfer(amountOut);
|
payable(payload.recipientAddress).transfer(nativeAmountOut);
|
||||||
|
|
||||||
|
/// pay the relayer in the native token
|
||||||
|
payable(msg.sender).transfer(nativeRelayerFee);
|
||||||
|
|
||||||
// used in UI to tell user they're getting
|
// used in UI to tell user they're getting
|
||||||
// their desired token
|
// their desired token
|
||||||
|
@ -133,15 +134,19 @@ contract CrossChainSwapV3 {
|
||||||
payload.recipientAddress,
|
payload.recipientAddress,
|
||||||
payload.path[1],
|
payload.path[1],
|
||||||
msg.sender,
|
msg.sender,
|
||||||
amountOut,
|
nativeAmountOut,
|
||||||
1
|
1
|
||||||
);
|
);
|
||||||
return amountOut;
|
return amountOut;
|
||||||
} catch {
|
} catch {
|
||||||
// swap failed - return feeToken to recipient
|
// pay relayer in the feeToken since the swap failed
|
||||||
IERC20(FEE_TOKEN_ADDRESS).safeTransfer(
|
IERC20 feeToken = IERC20(FEE_TOKEN_ADDRESS);
|
||||||
|
feeToken.safeTransfer(msg.sender, payload.relayerFee);
|
||||||
|
|
||||||
|
// swap failed - return feeToken (less relayer fees) to recipient
|
||||||
|
feeToken.safeTransfer(
|
||||||
payload.recipientAddress,
|
payload.recipientAddress,
|
||||||
swapAmountLessFees
|
payload.swapAmount - payload.relayerFee
|
||||||
);
|
);
|
||||||
|
|
||||||
// used in UI to tell user they're getting
|
// used in UI to tell user they're getting
|
||||||
|
@ -150,7 +155,7 @@ contract CrossChainSwapV3 {
|
||||||
payload.recipientAddress,
|
payload.recipientAddress,
|
||||||
payload.path[0],
|
payload.path[0],
|
||||||
msg.sender,
|
msg.sender,
|
||||||
swapAmountLessFees,
|
payload.swapAmount - payload.relayerFee,
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -177,8 +182,7 @@ contract CrossChainSwapV3 {
|
||||||
"tokenOut must be wrapped native asset"
|
"tokenOut must be wrapped native asset"
|
||||||
);
|
);
|
||||||
|
|
||||||
// pay relayer before attempting to do the swap
|
// pay the relayer in feeToken so that user gets desired exact amount out
|
||||||
// reflect payment in second swap amount
|
|
||||||
IERC20 feeToken = IERC20(FEE_TOKEN_ADDRESS);
|
IERC20 feeToken = IERC20(FEE_TOKEN_ADDRESS);
|
||||||
feeToken.safeTransfer(msg.sender, payload.relayerFee);
|
feeToken.safeTransfer(msg.sender, payload.relayerFee);
|
||||||
uint256 maxAmountInLessFees = payload.swapAmount - payload.relayerFee;
|
uint256 maxAmountInLessFees = payload.swapAmount - payload.relayerFee;
|
||||||
|
|
Loading…
Reference in New Issue