Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix linting #486

Merged
merged 10 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions l1-contracts/contracts/bridge/L1NativeTokenVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import {ReentrancyGuard} from "../common/ReentrancyGuard.sol";
import {IL1StandardAsset} from "./interfaces/IL1StandardAsset.sol";

import {IL1SharedBridge} from "./interfaces/IL1SharedBridge.sol";
import {ETH_TOKEN_ADDRESS, TWO_BRIDGES_MAGIC_VALUE, NATIVE_TOKEN_VAULT_VIRTUAL_ADDRESS} from "../common/Config.sol";

import {IBridgehub, L2TransactionRequestTwoBridgesInner, L2TransactionRequestDirect} from "../bridgehub/IBridgehub.sol";
import {ETH_TOKEN_ADDRESS, NATIVE_TOKEN_VAULT_VIRTUAL_ADDRESS} from "../common/Config.sol";

/// @author Matter Labs
/// @custom:security-contact [email protected]
Expand All @@ -37,6 +35,10 @@ contract L1NativeTokenVault is
/// @dev Era's chainID
uint256 public immutable ERA_CHAIN_ID;

/// @dev Indicates whether the hyperbridging is enabled for a given chain.
// slither-disable-next-line uninitialized-state
mapping(uint256 chainId => bool enabled) public hyperbridgingEnabled;

/// @dev Maps token balances for each chain to prevent unauthorized spending across hyperchains.
/// This serves as a security measure until hyperbridging is implemented.
/// NOTE: this function may be removed in the future, don't rely on it!
Expand Down Expand Up @@ -88,7 +90,7 @@ contract L1NativeTokenVault is
/// @dev If the corresponding L2 transaction fails, refunds are issued to a refund recipient on L2.
function bridgeBurn(
uint256 _chainId,
uint256 _mintValue,
uint256,
bytes32 _assetInfo,
address _prevMsgSender,
bytes calldata _data
Expand All @@ -114,6 +116,7 @@ contract L1NativeTokenVault is
chainBalance[_chainId][l1Token] += _depositAmount;
}

// solhint-disable-next-line func-named-parameters
_bridgeMintData = abi.encode(amount, _prevMsgSender, _l2Receiver, _getERC20Getters(l1Token), l1Token); // to do add l2Receiver in here
}

Expand Down Expand Up @@ -148,13 +151,14 @@ contract L1NativeTokenVault is
}

function bridgeMint(uint256 _chainId, bytes32 _assetInfo, bytes calldata _data) external payable override {
// if (!hyperbridgingEnabled[_chainId]) {
// // Check that the chain has sufficient balance
// require(chainBalance[_chainId][l1Token] >= amount, "NTV not enough funds 2"); // not enough funds
// chainBalance[_chainId][l1Token] -= amount;
// }
address l1Token = tokenAddress[_assetInfo];
(uint256 _amount, address l1Receiver) = abi.decode(_data, (uint256, address));
if (!hyperbridgingEnabled[_chainId]) {
// Add back
// Check that the chain has sufficient balance
require(chainBalance[_chainId][l1Token] >= _amount, "NTV not enough funds 2"); // not enough funds
chainBalance[_chainId][l1Token] -= _amount;
}
if (l1Token == ETH_TOKEN_ADDRESS) {
bool callSuccess;
// Low-level assembly call, to avoid any memory copying (save gas)
Expand All @@ -171,7 +175,7 @@ contract L1NativeTokenVault is
function bridgeClaimFailedBurn(
uint256 _chainId,
bytes32 _assetInfo,
address _prevMsgSender,
address,
bytes calldata _data
) external payable override {
(uint256 _amount, address _depositSender) = abi.decode(_data, (uint256, address));
Expand Down
70 changes: 35 additions & 35 deletions l1-contracts/contracts/bridge/L1SharedBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pragma solidity 0.8.24;
import {Ownable2StepUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
import {PausableUpgradeable} from "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";

import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

Expand All @@ -20,9 +19,8 @@ import {L2Message, TxStatus} from "../common/Messaging.sol";
import {UnsafeBytes} from "../common/libraries/UnsafeBytes.sol";
import {ReentrancyGuard} from "../common/ReentrancyGuard.sol";
import {AddressAliasHelper} from "../vendor/AddressAliasHelper.sol";
import {ETH_TOKEN_ADDRESS, NATIVE_TOKEN_VAULT_VIRTUAL_ADDRESS, TWO_BRIDGES_MAGIC_VALUE} from "../common/Config.sol";
import {NATIVE_TOKEN_VAULT_VIRTUAL_ADDRESS, TWO_BRIDGES_MAGIC_VALUE, ETH_TOKEN_ADDRESS} from "../common/Config.sol";
import {IBridgehub, L2TransactionRequestTwoBridgesInner, L2TransactionRequestDirect} from "../bridgehub/IBridgehub.sol";
import {IGetters} from "../state-transition/chain-interfaces/IGetters.sol";
import {L2_BASE_TOKEN_SYSTEM_CONTRACT_ADDR} from "../common/L2ContractAddresses.sol";

/// @author Matter Labs
Expand Down Expand Up @@ -211,17 +209,18 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade
uint256 _amount
) external payable virtual onlyBridgehubOrEra(_chainId) whenNotPaused {
(address l1Asset, bytes32 assetInfo) = _getAssetProperties(_assetInfo);
_transferAllowanceToNTV(assetInfo, _amount, _prevMsgSender);
IL1StandardAsset(l1Asset).bridgeBurn{value: msg.value}(
_chainId,
0,
assetInfo,
_prevMsgSender,
abi.encode(_amount, address(0))
);
bytes memory bridgeMintData = IL1StandardAsset(l1Asset).bridgeBurn{value: msg.value}({
_chainId: _chainId,
_mintValue: 0,
_assetInfo: assetInfo,
_prevMsgSender: _prevMsgSender,
_data: abi.encode(_amount, address(0))
});

// Note that we don't save the deposited amount, as this is for the base token, which gets sent to the refundRecipient if the tx fails
emit BridgehubDepositBaseTokenInitiated(_chainId, _prevMsgSender, _assetInfo, _amount);
// We could use this data instead of the above
emit BridgehubMintData(bridgeMintData);
}

/// @dev for backwards compatibility and to automatically register l1 assets, and we return the correct info.
Expand All @@ -236,6 +235,7 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade
}

function decodeLegacyData(bytes calldata _data, address _prevMsgSender) external returns (bytes32, bytes memory) {
require(msg.sender == address(this));
kelemeno marked this conversation as resolved.
Show resolved Hide resolved
(address _l1Token, uint256 _depositAmount, address _l2Receiver) = abi.decode(
_data,
(address, uint256, address)
Expand All @@ -261,6 +261,7 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade
IERC20 l1Token = IERC20(l1TokenAddress);

if (l1Token.allowance(_prevMsgSender, address(this)) >= _amount) {
// slither-disable-next-line arbitrary-send-erc20
l1Token.safeTransferFrom(_prevMsgSender, address(this), _amount);
l1Token.safeIncreaseAllowance(address(nativeTokenVault), _amount);
}
Expand Down Expand Up @@ -294,16 +295,21 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade

require(BRIDGE_HUB.baseToken(_chainId) != assetAddress[_assetInfo], "ShB: baseToken deposit not supported");

(bytes memory bridgeMintCalldata, bytes32 assetInfo) = _burn(
_chainId,
_l2Value,
_assetInfo,
_prevMsgSender,
_assetData
);
(bytes memory bridgeMintCalldata, bytes32 assetInfo) = _burn({
_chainId: _chainId,
_l2Value: _l2Value,
_assetInfo: _assetInfo,
_prevMsgSender: _prevMsgSender,
_assetData: _assetData
});

bytes32 txDataHash = keccak256(abi.encode(_prevMsgSender, assetInfo, _assetData));
request = _requestToBridge(_chainId, _prevMsgSender, assetInfo, bridgeMintCalldata, txDataHash);
request = _requestToBridge({
_chainId: _chainId,
_assetInfo: assetInfo,
_bridgeMintCalldata: bridgeMintCalldata,
_txDataHash: txDataHash
});

emit BridgehubDepositInitiated({
chainId: _chainId,
Expand All @@ -324,25 +330,24 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade
) internal returns (bytes memory bridgeMintCalldata, bytes32 assetInfo) {
address l1Asset;
(l1Asset, assetInfo) = _getAssetProperties(_assetInfo);
bridgeMintCalldata = IL1StandardAsset(l1Asset).bridgeBurn{value: msg.value}(
_chainId,
_l2Value,
assetInfo,
_prevMsgSender,
_assetData
);
bridgeMintCalldata = IL1StandardAsset(l1Asset).bridgeBurn{value: msg.value}({
_chainId: _chainId,
_mintValue: _l2Value,
_assetInfo: assetInfo,
_prevMsgSender: _prevMsgSender,
_data: _assetData
});
}

/// @dev The request data that is passed to the bridgehub
function _requestToBridge(
uint256 _chainId,
address _prevMsgSender,
bytes32 _assetInfo,
bytes memory _bridgeMintCalldata,
bytes32 _txDataHash
) internal view returns (L2TransactionRequestTwoBridgesInner memory request) {
// Request the finalization of the deposit on the L2 side
bytes memory l2TxCalldata = _getDepositL2Calldata(_prevMsgSender, _assetInfo, _bridgeMintCalldata);
bytes memory l2TxCalldata = _getDepositL2Calldata(_assetInfo, _bridgeMintCalldata);

request = L2TransactionRequestTwoBridgesInner({
magicValue: TWO_BRIDGES_MAGIC_VALUE,
Expand All @@ -366,11 +371,7 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade
}

/// @dev Generate a calldata for calling the deposit finalization on the L2 bridge contract
function _getDepositL2Calldata(
address _l1Sender,
bytes32 _assetInfo,
bytes memory _assetData
) internal pure returns (bytes memory) {
function _getDepositL2Calldata(bytes32 _assetInfo, bytes memory _assetData) internal view returns (bytes memory) {
return abi.encodeCall(IL2Bridge.finalizeDeposit, (_assetInfo, _assetData));
}

Expand Down Expand Up @@ -699,9 +700,8 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade
// chainBalance[ERA_CHAIN_ID][_l1Asset] += _amount;
// }

bytes memory bridgeMintData = abi.encode(_amount); //todo
bytes memory bridgeMintData = abi.encode(_amount);
bytes memory l2TxCalldata = _getDepositL2Calldata(
_prevMsgSender,
IL1NativeTokenVault(nativeTokenVault).getAssetInfoFromLegacy(_l1Asset),
bridgeMintData
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

pragma solidity 0.8.24;

import {L2TransactionRequestTwoBridgesInner} from "../../bridgehub/IBridgehub.sol";
import {IL1SharedBridge} from "./IL1SharedBridge.sol";

/// @title L1 Native token vault contract interface
Expand Down
2 changes: 2 additions & 0 deletions l1-contracts/contracts/bridge/interfaces/IL1SharedBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ interface IL1SharedBridge {
uint256 amount
);

event BridgehubMintData(bytes bridgeMintData);

event BridgehubDepositFinalized(
uint256 indexed chainId,
bytes32 indexed txDataHash,
Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/src.ts/deploy-test-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export async function registerHyperchainWithBridgeRegistration(
await registerHyperchain(deployer, onlyVerifier, extraFacets, gasPrice, baseTokenName, chainId, true);
const l1SharedBridge = deployer.defaultSharedBridge(deployer.deployWallet);
const upgradeCall = l1SharedBridge.interface.encodeFunctionData("initializeChainGovernance", [chainId, ADDRESS_ONE]);
const txHash = await deployer.executeUpgrade(l1SharedBridge.address, 0, upgradeCall);
await deployer.executeUpgrade(l1SharedBridge.address, 0, upgradeCall);
}

// This is used to deploy the diamond and bridge such that they can be upgraded using UpgradeHyperchain.sol
Expand Down
1 change: 1 addition & 0 deletions l1-contracts/src.ts/deploy-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export async function deployBytecodeViaCreate2(
export async function deployContractWithArgs(
wallet: ethers.Wallet,
contractName: string,
// eslint-disable-next-line
args: any[],
ethTxOptions: ethers.providers.TransactionRequest
) {
Expand Down
18 changes: 7 additions & 11 deletions l1-contracts/src.ts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "@nomiclabs/hardhat-ethers";
import type { BigNumberish, providers, Signer, Wallet, Contract } from "ethers";
import { ethers } from "ethers";
import { hexlify, Interface } from "ethers/lib/utils";
import { Wallet as ZkWallet } from "zksync-ethers";
import type { Wallet as ZkWallet } from "zksync-ethers";

import type { DeployedAddresses } from "./deploy-utils";
import {
Expand All @@ -22,7 +22,6 @@ import {
PubdataPricingMode,
hashL2Bytecode,
DIAMOND_CUT_DATA_ABI_STRING,
REQUIRED_L2_GAS_PRICE_PER_PUBDATA,
} from "./utils";
import type { FacetCut } from "./diamondCut";
import { diamondCut, getCurrentFacetCutsForAdd } from "./diamondCut";
Expand Down Expand Up @@ -243,15 +242,12 @@ export class Deployer {
}
// Note: we cannot deploy using Create2, as the owner of the ProxyAdmin is msg.sender

let proxyAdmin;
let rec;

ethTxOptions.gasLimit ??= 10_000_000;
const contractFactory = await hardhat.ethers.getContractFactory("ProxyAdmin", {
signer: this.deployWallet,
});
proxyAdmin = await contractFactory.deploy(...[ethTxOptions]);
rec = await proxyAdmin.deployTransaction.wait();
const proxyAdmin = await contractFactory.deploy(...[ethTxOptions]);
const rec = await proxyAdmin.deployTransaction.wait();

if (this.verbose) {
console.log(
Expand Down Expand Up @@ -686,7 +682,7 @@ export class Deployer {
}
}

public async sharedBridgeSetEraPostUpgradeFirstBatch(ethTxOptions: ethers.providers.TransactionRequest) {
public async sharedBridgeSetEraPostUpgradeFirstBatch() {
const sharedBridge = L1SharedBridgeFactory.connect(this.addresses.Bridges.SharedBridgeProxy, this.deployWallet);
const storageSwitch = getNumberFromEnv("CONTRACTS_SHARED_BRIDGE_UPGRADE_STORAGE_SWITCH");
const tx = await sharedBridge.setEraPostUpgradeFirstBatch(storageSwitch);
Expand All @@ -696,7 +692,7 @@ export class Deployer {
}
}

public async registerSharedBridge(ethTxOptions: ethers.providers.TransactionRequest) {
public async registerSharedBridge() {
const bridgehub = this.bridgehubContract(this.deployWallet);

/// registering ETH as a valid token, with address 1.
Expand All @@ -711,11 +707,11 @@ export class Deployer {
]);
await this.executeUpgrade(this.addresses.Bridgehub.BridgehubProxy, 0, upgradeData2);
if (this.verbose) {
console.log(`Shared bridge was registered in Bridgehub`);
console.log("Shared bridge was registered in Bridgehub");
}
}

public async registerTokenInNativeTokenVault(token: string, ethTxOptions: ethers.providers.TransactionRequest) {
public async registerTokenInNativeTokenVault(token: string) {
const nativeTokenVault = this.nativeTokenVault(this.deployWallet);

const data = nativeTokenVault.interface.encodeFunctionData("registerToken", [token]);
Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/test/test_config/constant/hardhat.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@
"decimals": 18,
"address": "0x086B74711475975053de3dEaD77FEDaF87b60a65"
}
]
]
4 changes: 2 additions & 2 deletions l1-contracts/test/unit_tests/custom_base_token.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as hardhat from "hardhat";
import { ethers, Wallet } from "ethers";
import { Interface } from "ethers/lib/utils";

import type { TestnetERC20Token } from "../../typechain";
import { L1NativeTokenVault, TestnetERC20TokenFactory } from "../../typechain";
import type { TestnetERC20Token, L1NativeTokenVault } from "../../typechain";
import { TestnetERC20TokenFactory } from "../../typechain";
import type { IBridgehub } from "../../typechain/IBridgehub";
import { IBridgehubFactory } from "../../typechain/IBridgehubFactory";
import type { IL1SharedBridge } from "../../typechain/IL1SharedBridge";
Expand Down
18 changes: 5 additions & 13 deletions l1-contracts/test/unit_tests/l1_shared_bridge_test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ import { expect } from "chai";
import { ethers, Wallet } from "ethers";
import { Interface } from "ethers/lib/utils";
import * as hardhat from "hardhat";
import type { L1SharedBridge, Bridgehub, WETH9 } from "../../typechain";
import {
L1SharedBridgeFactory,
BridgehubFactory,
WETH9Factory,
TestnetERC20TokenFactory,
L1NativeTokenVault,
} from "../../typechain";
import type { L1SharedBridge, Bridgehub, L1NativeTokenVault } from "../../typechain";
import { L1SharedBridgeFactory, BridgehubFactory, WETH9Factory, TestnetERC20TokenFactory } from "../../typechain";
import { L1NativeTokenVaultFactory } from "../../typechain/L1NativeTokenVaultFactory";

import { getTokens } from "../../src.ts/deploy-token";
import { Action, facetCut } from "../../src.ts/diamondCut";
import { ADDRESS_ONE, ethTestConfig } from "../../src.ts/utils";
import { ethTestConfig } from "../../src.ts/utils";
import type { Deployer } from "../../src.ts/deploy";
import { initialTestnetDeploymentProcess } from "../../src.ts/deploy-test-process";

Expand All @@ -28,8 +22,6 @@ describe("Shared Bridge tests", () => {
let bridgehub: Bridgehub;
let l1NativeTokenVault: L1NativeTokenVault;
let l1SharedBridge: L1SharedBridge;
let l1SharedBridgeInterface: Interface;
let l1Weth: WETH9;
let erc20TestToken: ethers.Contract;
const functionSignature = "0x6c0960f9";
const ERC20functionSignature = "0x11a2ccc1";
Expand Down Expand Up @@ -124,7 +116,7 @@ describe("Shared Bridge tests", () => {
const balanceBefore = await erc20TestToken.balanceOf(await randomSigner.getAddress());
const balanceNTVBefore = await erc20TestToken.balanceOf(l1NativeTokenVault.address);

const assetInfo = await l1NativeTokenVault.getAssetInfoFromLegacy(erc20TestToken.address);
await l1NativeTokenVault.getAssetInfoFromLegacy(erc20TestToken.address);
await (await erc20TestToken.connect(randomSigner).approve(l1NativeTokenVault.address, amount.mul(10))).wait();
await bridgehub.connect(randomSigner).requestL2TransactionTwoBridges(
{
Expand Down Expand Up @@ -161,7 +153,7 @@ describe("Shared Bridge tests", () => {
const balanceBefore = await erc20TestToken.balanceOf(await randomSigner.getAddress());
const balanceNTVBefore = await erc20TestToken.balanceOf(l1NativeTokenVault.address);

const assetInfo = await l1NativeTokenVault.getAssetInfoFromLegacy(erc20TestToken.address);
await l1NativeTokenVault.getAssetInfoFromLegacy(erc20TestToken.address);
await (await erc20TestToken.connect(randomSigner).approve(l1NativeTokenVault.address, amount.mul(10))).wait();
await bridgehub.connect(randomSigner).requestL2TransactionTwoBridges(
{
Expand Down
Loading
Loading