Skip to content

Commit

Permalink
Switch to try/catch for unwrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
wirew0lf committed Sep 5, 2024
1 parent b96a14e commit 7af6da6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
23 changes: 7 additions & 16 deletions AxelarHandler/src/AxelarHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -382,18 +382,15 @@ contract AxelarHandler is AxelarExecutableUpgradeable, Ownable2StepUpgradeable,
_sendToken(token, amount, destination);
} else if (command == Commands.SendNative) {
address destination = abi.decode(data, (address));
if (_wETHSymbolHash != DISABLED_SYMBOL && keccak256(abi.encodePacked(tokenSymbol)) == _wETHSymbolHash) {
_sendNative(token, amount, destination);
} else {
_sendToken(token, amount, destination);
}

_sendNative(token, amount, destination);
} else if (command == Commands.Swap) {
(address destination, bool unwrapOut, bytes memory swap) = abi.decode(data, (address, bool, bytes));

try SkipSwapRouter.swap(swapRouter, destination, tokenIn, amount, swap) returns (
IERC20 tokenOut, uint256 amountOut
) {
if (unwrapOut && address(tokenOut) == _getTokenAddress(wETHSymbol)) {
if(unwrapOut) {
_sendNative(address(tokenOut), amountOut, destination);
} else {
_sendToken(address(tokenOut), amountOut, destination);
Expand All @@ -408,7 +405,7 @@ contract AxelarHandler is AxelarExecutableUpgradeable, Ownable2StepUpgradeable,
try SkipSwapRouter.multiSwap(swapRouter, destination, tokenIn, amount, swaps) returns (
IERC20 tokenOut, uint256 amountOut
) {
if (unwrapOut && address(tokenOut) == _getTokenAddress(wETHSymbol)) {
if(unwrapOut) {
_sendNative(address(tokenOut), amountOut, destination);
} else {
_sendToken(address(tokenOut), amountOut, destination);
Expand All @@ -426,15 +423,9 @@ contract AxelarHandler is AxelarExecutableUpgradeable, Ownable2StepUpgradeable,
}

function _sendNative(address token, uint256 amount, address destination) internal {
// Unwrap native token.
IWETH weth = IWETH(token);
weth.withdraw(amount);

// Send it unwrapped to the destination
(bool success,) = destination.call{value: amount}("");

if (!success) {
revert NativePaymentFailed();
try SkipSwapRouter.sendNative(token, amount, destination) {}
catch {
_sendToken(token, amount, destination);
}
}

Expand Down
15 changes: 15 additions & 0 deletions AxelarHandler/src/libraries/SkipSwapRouter.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: UNLICENSED
import {IERC20} from "lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
import {IWETH} from "../interfaces/IWETH.sol";
import {ISwapRouter02} from "../interfaces/ISwapRouter02.sol";
import {BytesLib, Path} from "./Path.sol";

Expand All @@ -13,6 +14,7 @@ library SkipSwapRouter {
using Path for bytes;

error InsufficientOutputAmount();
error NativePaymentFailed();

enum SwapCommands {
ExactInputSingle,
Expand Down Expand Up @@ -116,6 +118,19 @@ library SkipSwapRouter {
}
}

function sendNative(address token, uint256 amount, address destination) external {
// Unwrap native token.
IWETH weth = IWETH(token);
weth.withdraw(amount);

// Send it unwrapped to the destination
(bool success,) = destination.call{value: amount}("");

if (!success) {
revert NativePaymentFailed();
}
}

function _fixPath(address tokenA, address tokenB, bytes memory path) internal pure returns (bytes memory) {
(address decodedA,,) = path.decodeFirstPool();
if (decodedA != tokenA) {
Expand Down

0 comments on commit 7af6da6

Please sign in to comment.