ToolszkEVMArchitectureHigh levelSmart contractsApi

PolygonZkEVMBridgeV2.sol

Bridge contract deployed on Ethereum and all Polygon rollups. Responsible for managing the token interactions with other networks. ## Functions ### Disable init

Bridge contract deployed on Ethereum and all Polygon rollups. Responsible for managing the token interactions with other networks.

Functions

constructor

Disable initializers on the implementation following best practices.

  function constructor(
  ) public

initialize

The value of _polygonRollupManager on the L2 deployment of the contract is address(0), so an emergency state is not possible for the L2 deployment of the bridge.

  function initialize(
    uint32 _networkID,
    address _gasTokenAddress,
    uint32 _gasTokenNetwork,
    contract IBasePolygonZkEVMGlobalExitRoot _globalExitRootManager,
    address _polygonRollupManager,
    bytes _gasTokenMetadata
  ) external
Parameters
NameTypeDescription
_networkIDuint32networkID
_gasTokenAddressaddressgas token address
_gasTokenNetworkuint32gas token network
_globalExitRootManagercontract IBasePolygonZkEVMGlobalExitRootglobal exit root manager address
_polygonRollupManageraddresspolygonZkEVM address
_gasTokenMetadatabytesAbi encoded gas token metadata

bridgeAsset

Deposit add a new leaf to the Merkle tree.

  function bridgeAsset(
    uint32 destinationNetwork,
    address destinationAddress,
    uint256 amount,
    address token,
    bool forceUpdateGlobalExitRoot,
    bytes permitData
  ) public
  • If this function is called with a reentrant token, it is possible to claimTokens in the same call, thus reducing the supply of tokens on this contract, and actually locking tokens in the contract.
  • Therefore we recommend to third-party bridges that if they do implement reentrant beforeTransfer for some reentrant tokens, not to call any external address.
  • User/UI must be aware of the existing/available networks when choosing the destination network.
Parameters
NameTypeDescription
destinationNetworkuint32Network destination
destinationAddressaddressAddress destination
amountuint256Amount of tokens
tokenaddressToken address, 0 address is reserved for ether
forceUpdateGlobalExitRootboolIndicates if the new global exit root is updated or not
permitDatabytesRaw data of the call permit of the token

bridgeMessage

Bridge message and send ETH value.

  function bridgeMessage(
    uint32 destinationNetwork,
    address destinationAddress,
    bool forceUpdateGlobalExitRoot,
    bytes metadata
  ) external
  • User/UI must be aware of the existing/available networks when choosing the destination network.
Parameters
NameTypeDescription
destinationNetworkuint32Network destination
destinationAddressaddressAddress destination
forceUpdateGlobalExitRootboolIndicates if the new global exit root is updated or not
metadatabytesMessage metadata

bridgeMessageWETH

Bridge message and send ETH value.

  function bridgeMessageWETH(
    uint32 destinationNetwork,
    address destinationAddress,
    uint256 amountWETH,
    bool forceUpdateGlobalExitRoot,
    bytes metadata
  ) external
  • User/UI must be aware of the existing/available networks when choosing the destination network
Parameters
NameTypeDescription
destinationNetworkuint32Network destination
destinationAddressaddressAddress destination
amountWETHuint256Amount of WETH tokens
forceUpdateGlobalExitRootboolIndicates if the new global exit root is updated or not
metadatabytesMessage metadata

_bridgeMessage

Bridge message and send ETH value.

  function _bridgeMessage(
    uint32 destinationNetwork,
    address destinationAddress,
    uint256 amountEther,
    bool forceUpdateGlobalExitRoot,
    bytes metadata
  ) internal
Parameters
NameTypeDescription
destinationNetworkuint32Network destination
destinationAddressaddressAddress destination
amountEtheruint256Amount of ether along with the message
forceUpdateGlobalExitRootboolIndicates if the new global exit root is updated or not
metadatabytesMessage metadata

claimAsset

Verify merkle proof and withdraw tokens/ETH.

  function claimAsset(
    bytes32[32] smtProofLocalExitRoot,
    bytes32[32] smtProofRollupExitRoot,
    uint256 globalIndex,
    bytes32 mainnetExitRoot,
    bytes32 rollupExitRoot,
    uint32 originNetwork,
    address originTokenAddress,
    uint32 destinationNetwork,
    address destinationAddress,
    uint256 amount,
    bytes metadata
  ) external
Parameters

s

NameTypeDescription
smtProofLocalExitRootbytes32[32]Smt proof to proof the leaf against the network exit root
smtProofRollupExitRootbytes32[32]Smt proof to proof the rollupLocalExitRoot against the rollups exit root
globalIndexuint256Global index is defined as:
191 bits1 bit32 bits
0mainnetFlagrollupIndex
note that only the rollup index will be used only in case the mainnet flag is 0
note that global index do not assert the unused bits to 0.
This means that when synching the events, the globalIndex must be decoded the same way that in the Smart contract
to avoid possible synch attacks
mainnetExitRootbytes32Mainnet exit root
rollupExitRootbytes32Rollup exit root
originNetworkuint32Origin network
originTokenAddressaddressOrigin token address, 0 address is reserved for ether
destinationNetworkuint32Network destination
destinationAddressaddressAddress destination
amountuint256Amount of tokens
metadatabytesAbi encoded metadata if any, empty otherwise

claimMessage

Verify merkle proof and execute message. If the receiving address is an EOA, the call results as a success which means that the amount of ether transfers correctly, but the message does not trigger any execution.

  function claimMessage(
    bytes32[32] smtProofLocalExitRoot,
    bytes32[32] smtProofRollupExitRoot,
    uint256 globalIndex,
    bytes32 mainnetExitRoot,
    bytes32 rollupExitRoot,
    uint32 originNetwork,
    address originAddress,
    uint32 destinationNetwork,
    address destinationAddress,
    uint256 amount,
    bytes metadata
  ) external
Parameters
NameTypeDescription
smtProofLocalExitRootbytes32[32]Smt proof to proof the leaf against the exit root
smtProofRollupExitRootbytes32[32]Smt proof to proof the rollupLocalExitRoot against the rollups exit root
globalIndexuint256Global index is defined as:
191 bits1 bit32 bits
0mainnetFlagrollupIndex
note that only the rollup index will be used only in case the mainnet flag is 0
note that global index do not assert the unused bits to 0.
This means that when synching the events, the globalIndex must be decoded the same way that in the Smart contract
to avoid possible synch attacks
mainnetExitRootbytes32Mainnet exit root
rollupExitRootbytes32Rollup exit root
originNetworkuint32Origin network
originAddressaddressOrigin address
destinationNetworkuint32Network destination
destinationAddressaddressAddress destination
amountuint256message value
metadatabytesAbi encoded metadata if any, empty otherwise

precalculatedWrapperAddress

Returns the precalculated address of a wrapper using the token information.

  function precalculatedWrapperAddress(
    uint32 originNetwork,
    address originTokenAddress,
    string name,
    string symbol,
    uint8 decimals
  ) public returns (address)
  • Updating the metadata of a token is not supported.
  • Since the metadata has relevance in the address deployed, this function does not return a valid wrapped address if the metadata provided is not the original one.
Parameters
NameTypeDescription
originNetworkuint32Origin network
originTokenAddressaddressOrigin token address, 0 address is reserved for ether
namestringName of the token
symbolstringSymbol of the token
decimalsuint8Decimals of the token

getTokenWrappedAddress

Returns the address of a wrapper using the token information if already exist.

  function getTokenWrappedAddress(
    uint32 originNetwork,
    address originTokenAddress
  ) external returns (address)
Parameters
NameTypeDescription
originNetworkuint32Origin network
originTokenAddressaddressOrigin token address, 0 address is reserved for ether

activateEmergencyState

Function to activate the emergency state. Can only be called by the Polygon ZK-EVM in extreme situations.

  function activateEmergencyState(
  ) external

deactivateEmergencyState

Function to deactivate the emergency state. Can only be called by the Polygon ZK-EVM.

  function deactivateEmergencyState(
  ) external

_verifyLeaf

Verify leaf and checks that it has not been claimed.

  function _verifyLeaf(
    bytes32[32] smtProofLocalExitRoot,
    bytes32[32] smtProofRollupExitRoot,
    uint256 globalIndex,
    bytes32 mainnetExitRoot,
    bytes32 rollupExitRoot,
    bytes32 leafValue
  ) internal
Parameters
NameTypeDescription
smtProofLocalExitRootbytes32[32]Smt proof
smtProofRollupExitRootbytes32[32]Smt proof
globalIndexuint256Index of the leaf
mainnetExitRootbytes32Mainnet exit root
rollupExitRootbytes32Rollup exit root
leafValuebytes32leaf value

isClaimed

Function to check if an index is claimed or not.

  function isClaimed(
    uint32 leafIndex,
    uint32 sourceBridgeNetwork
  ) external returns (bool)
Parameters
NameTypeDescription
leafIndexuint32Index
sourceBridgeNetworkuint32Origin network

updateGlobalExitRoot

Function to update the globalExitRoot if the last deposit is not submitted.

  function updateGlobalExitRoot(
  ) external

_updateGlobalExitRoot

Function to update the globalExitRoot.

  function _updateGlobalExitRoot(
  ) internal

_permit

Function to call token permit method of extended ERC20 + @param token ERC20 token address.

  function _permit(
    address amount,
    uint256 permitData
  ) internal
Parameters
NameTypeDescription
amountaddressQuantity that is expected to be allowed
permitDatauint256Raw data of the call permit of the token

_deployWrappedToken

Internal function that uses create2 to deploy the wrapped tokens.

  function _deployWrappedToken(
    bytes32 salt,
    bytes constructorArgs
  ) internal returns (contract TokenWrapped newWrappedToken)
Parameters
NameTypeDescription
saltbytes32Salt used in create2 params,
tokenInfoHash will be used as salt for all wrappeds except for bridge native WETH, that will be bytes32(0)
constructorArgsbytesEncoded constructor args for the wrapped token

_safeSymbol

Provides a safe ERC20.symbol version which returns NO_SYMBOL as fallback string.

  function _safeSymbol(
    address token
  ) internal returns (string)
Parameters
NameTypeDescription
tokenaddressThe address of the ERC-20 token contract

_safeName

Provides a safe ERC20.name version which returns NO_NAME as fallback string.

  function _safeName(
    address token
  ) internal returns (string)
Parameters
NameTypeDescription
tokenaddressThe address of the ERC-20 token contract.

_safeDecimals

Provides a safe ERC20.decimals version which returns 18 as fallback value.

  function _safeDecimals(
    address token
  ) internal returns (uint8)
  • Tokens with (decimals > 255) are not supported.
Parameters
NameTypeDescription
tokenaddressThe address of the ERC-20 token contract

_returnDataToString

Function to convert returned data to string; returns NOT_VALID_ENCODING as fallback value.

  function _returnDataToString(
    bytes data
  ) internal returns (string)
Parameters
NameTypeDescription
databytesreturned data

getTokenMetadata

Returns the encoded token metadata.

  function getTokenMetadata(
    address token
  ) public returns (bytes)
Parameters
NameTypeDescription
tokenaddressAddress of the token

calculateTokenWrapperAddress

Returns the precalculated address of a wrapper using the token address.

  function calculateTokenWrapperAddress(
    uint32 originNetwork,
    address originTokenAddress,
    address token
  ) external returns (address)
  • Updating the metadata of a token is not supported.
  • Since the metadata has relevance in the address deployed, this function does not return a valid wrapped address if the metadata provided is not the original one.
Parameters
NameTypeDescription
originNetworkuint32Origin network
originTokenAddressaddressOrigin token address, 0 address is reserved for ether
tokenaddressAddress of the token to calculate the wrapper address

Events

BridgeEvent

Emitted when bridge assets or messages to another network.

  event BridgeEvent(
  )

ClaimEvent

Emitted when a claim is done from another network.

  event ClaimEvent(
  )

NewWrappedToken

Emitted when a new wrapped token is created.

  event NewWrappedToken(
  )
Edit on GitHub

Last updated on