Skip to content

Commit

Permalink
fix: wrap minimal amount of weth in forwarders
Browse files Browse the repository at this point in the history
Signed-off-by: bennett <[email protected]>
  • Loading branch information
bmzig committed Nov 13, 2024
1 parent 51b9c4f commit 1f8eac2
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions contracts/chain-adapters/ForwarderBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ abstract contract ForwarderBase is UUPSUpgradeable, ForwarderInterface {
error RelayTokensFailed(address baseToken);
// Error which is triggered when there is no adapter set in the `chainAdapters` mapping.
error UninitializedChainAdapter();
// Error which is triggered when the contract attempts to wrap a native token for an amount greater than
// its balance.
error InsufficientNativeTokenBalance();

/*
* @dev Cross domain admin permissioning is implemented specifically for each L2 that this contract is deployed on, so this base contract
Expand Down Expand Up @@ -136,7 +139,11 @@ abstract contract ForwarderBase is UUPSUpgradeable, ForwarderInterface {
) external payable override onlyAdmin {
address adapter = chainAdapters[destinationChainId];
if (adapter == address(0)) revert UninitializedChainAdapter();
if (baseToken == address(WRAPPED_NATIVE_TOKEN)) _wrapNativeToken();
if (baseToken == address(WRAPPED_NATIVE_TOKEN)) {
// Only wrap the minimum required amount of the native token.
uint256 nativeTokenBalance = WRAPPED_NATIVE_TOKEN.balanceOf(address(this));
if (nativeTokenBalance < amount) _wrapNativeToken(amount - nativeTokenBalance);
}

(bool success, ) = adapter.delegatecall(
abi.encodeCall(AdapterInterface.relayTokens, (baseToken, destinationChainToken, amount, target))
Expand All @@ -160,10 +167,11 @@ abstract contract ForwarderBase is UUPSUpgradeable, ForwarderInterface {
}

/*
* @notice Wraps the contract's entire balance of the native token.
* @notice Wraps the specified amount of the network's native token.
*/
function _wrapNativeToken() internal {
if (address(this).balance > 0) WRAPPED_NATIVE_TOKEN.deposit{ value: address(this).balance }();
function _wrapNativeToken(uint256 wrapAmount) internal {
if (address(this).balance < wrapAmount) revert InsufficientNativeTokenBalance();
WRAPPED_NATIVE_TOKEN.deposit{ value: wrapAmount }();
}

// Reserve storage slots for future versions of this base contract to add state variables without
Expand Down

0 comments on commit 1f8eac2

Please sign in to comment.