Skip to content

Commit

Permalink
Refactor with idiomatic using SafeTransferLib
Browse files Browse the repository at this point in the history
Replaced explicit `SafeTransferLib` function calls with more idiomatic method invocations by adding `using SafeTransferLib for address`. This change enhances code readability and maintains functional integrity.
  • Loading branch information
shuhuiluo committed Sep 10, 2024
1 parent bb06174 commit 56fe758
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions contracts/src/utils/libraries/CurrencyTransfer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {CustomRevert} from "contracts/src/utils/libraries/CustomRevert.sol";
// contracts

library CurrencyTransfer {
using SafeTransferLib for address;

/// @dev The address interpreted as native token of the chain.
address internal constant NATIVE_TOKEN =
address(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
Expand Down Expand Up @@ -99,15 +101,15 @@ library CurrencyTransfer {
}

if (from == address(this)) {
SafeTransferLib.safeTransfer(token, to, amount);
token.safeTransfer(to, amount);
} else {
SafeTransferLib.safeTransferFrom(token, from, to, amount);
token.safeTransferFrom(from, to, amount);
}
}

/// @dev Transfers `amount` of native token to `to`.
function safeTransferNativeToken(address to, uint256 value) internal {
SafeTransferLib.safeTransferETH(to, value);
to.safeTransferETH(value);
}

/// @dev Transfers `amount` of native token to `to`. (With native token wrapping)
Expand All @@ -116,10 +118,10 @@ library CurrencyTransfer {
uint256 value,
address _nativeTokenWrapper
) internal {
bool success = SafeTransferLib.trySafeTransferETH(to, value, gasleft());
bool success = to.trySafeTransferETH(value, gasleft());
if (!success) {
IWETH(_nativeTokenWrapper).deposit{value: value}();
SafeTransferLib.safeTransfer(_nativeTokenWrapper, to, value);
_nativeTokenWrapper.safeTransfer(to, value);
}
}
}

0 comments on commit 56fe758

Please sign in to comment.