Skip to content

Commit

Permalink
small contract fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kelemeno committed Feb 16, 2024
1 parent b05c529 commit e94be5a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
10 changes: 3 additions & 7 deletions l1-contracts/contracts/bridge/L1SharedBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Initializable, Owna
address _prevMsgSender,
address _l1Token,
uint256 _amount
) external payable onlyBridgehubOrEra(_chainId) {
) external virtual payable onlyBridgehubOrEra(_chainId) {
if (_l1Token == ETH_TOKEN_ADDRESS) {
require(msg.value == _amount, "L1SharedBridge: msg.value not equal to amount");
} else {
Expand Down Expand Up @@ -181,10 +181,6 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Initializable, Owna
require(msg.value == 0, "ShB m.v > 0 for BH d.it 2");
amount = _depositAmount;

/// This breaks the _depositeFunds function, it returns 0, as we are withdrawing funds from ourselves, so our balance doesn't increase
/// This should not happen, this bridge only calls the Bridgehub if Eth is the baseToken or for wrapped base token deposits
require(_prevMsgSender != address(this), "ShB calling itself");

uint256 withdrawAmount = _depositFunds(_prevMsgSender, _l1Token, _depositAmount);
require(withdrawAmount == _depositAmount, "5T"); // The token has non-standard transfer logic
}
Expand Down Expand Up @@ -404,7 +400,7 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Initializable, Owna
uint16 _l2TxNumberInBatch,
bytes calldata _message,
bytes32[] calldata _merkleProof
) external override onlyLegacyBridge returns (address l1Receiver, address l1Token, uint256 amount) {
) external override onlyLegacyBridge returns (address, address, uint256) { // l1Receiver, l1Token, amount
return
_finalizeWithdrawal(
ERA_CHAIN_ID,
Expand Down Expand Up @@ -540,7 +536,7 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Initializable, Owna
// Check that the message length is correct.
// It should be equal to the length of the function signature + address + address + uint256 = 4 + 20 + 20 + 32 =
// 76 (bytes).
require(_l2ToL1message.length == 76, "kk");
require(_l2ToL1message.length == 76, "ShB wrong msg len 2");
(l1Receiver, offset) = UnsafeBytes.readAddress(_l2ToL1message, offset);
(l1Token, offset) = UnsafeBytes.readAddress(_l2ToL1message, offset);
(amount, offset) = UnsafeBytes.readUint256(_l2ToL1message, offset);
Expand Down
50 changes: 50 additions & 0 deletions l1-contracts/contracts/dev-contracts/test/L1SharedBridgeTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.20;

import "../../bridge/L1SharedBridge.sol";
import {IMailbox} from "../../state-transition/chain-interfaces/IMailbox.sol";
import "../../bridge/interfaces/IL1SharedBridge.sol";

/// @author Matter Labs
contract L1SharedBridgeTest is L1SharedBridge {

address private immutable eraDiamondProxy;

constructor(address _diamondProxyAddress, address payable _l1WethAddress, IBridgehub _bridgehub, IL1ERC20Bridge _legacyBridge) L1SharedBridge(_l1WethAddress, _bridgehub, _legacyBridge){
eraDiamondProxy = _diamondProxyAddress;
}

/// @notice Checks that the message sender is the bridgehub or Era
modifier onlyBridgehubOrTestEra(uint256 _chainId) {
require(
(msg.sender == address(bridgehub)) || (_chainId == ERA_CHAIN_ID && msg.sender == eraDiamondProxy),
"L1SharedBridge: not bridgehub or era chain"
);
_;
}

/// @notice used by bridgehub to aquire mintValue. If l2Tx fails refunds are sent to refundrecipient on L2
/// we also use it to keep to track each chain's assets
function bridgehubDepositBaseToken(
uint256 _chainId,
address _prevMsgSender,
address _l1Token,
uint256 _amount
) external payable onlyBridgehubOrTestEra(_chainId) override {
if (_l1Token == ETH_TOKEN_ADDRESS) {
require(msg.value == _amount, "L1SharedBridge: msg.value not equal to amount");
} else {
// The Bridgehub also checks this, but we want to be sure
require(msg.value == 0, "ShB m.v > 0 b d.it");

uint256 amount = _depositFunds(_prevMsgSender, _l1Token, _amount); // note if _prevMsgSender is this contract, this will return 0. This does not happen.
require(amount == _amount, "3T"); // The token has non-standard transfer logic
}

if (!hyperbridgingEnabled[_chainId]) {
chainBalance[_chainId][_l1Token] += _amount;
}
// Note we don't save the deposited amount, as this is for the base token, which gets sent to the refundRecipient if the tx fails
}
}

0 comments on commit e94be5a

Please sign in to comment.