Skip to content

Commit

Permalink
Refactor function overrite
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Sep 16, 2024
1 parent bef3572 commit 6a40828
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1,334 deletions.
58 changes: 45 additions & 13 deletions contracts/v2/PolygonZkEVMBridgeV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ contract PolygonZkEVMBridgeV2 is
// WETH address
TokenWrapped public WETHToken;

/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
*/
uint256[50] private _gap;

/**
* @dev Emitted when bridge assets or messages to another network
*/
Expand Down Expand Up @@ -150,7 +156,7 @@ contract PolygonZkEVMBridgeV2 is
IBasePolygonZkEVMGlobalExitRoot _globalExitRootManager,
address _polygonRollupManager,
bytes memory _gasTokenMetadata
) external virtual initializer {
) public virtual initializer {
networkID = _networkID;
globalExitRootManager = _globalExitRootManager;
polygonRollupManager = _polygonRollupManager;
Expand Down Expand Up @@ -238,7 +244,7 @@ contract PolygonZkEVMBridgeV2 is
// In case ether is the native token, WETHToken will be 0, and the address 0 is already checked
if (token == address(WETHToken)) {
// Burn tokens
TokenWrapped(token).burn(msg.sender, amount);
_bridgeWrappedAsset(TokenWrapped(token), amount);

// Both origin network and originTokenAddress will be 0
// Metadata will be empty
Expand All @@ -250,8 +256,7 @@ contract PolygonZkEVMBridgeV2 is
if (tokenInfo.originTokenAddress != address(0)) {
// The token is a wrapped token from another network

// Burn tokens
TokenWrapped(token).burn(msg.sender, amount);
_bridgeWrappedAsset(TokenWrapped(token), amount);

originTokenAddress = tokenInfo.originTokenAddress;
originNetwork = tokenInfo.originNetwork;
Expand Down Expand Up @@ -314,6 +319,36 @@ contract PolygonZkEVMBridgeV2 is
}
}

/**
* @notice Burn tokens from wrapped token to execute the bridge
* note This function has been extracted to be able to override it by other contracts like Bridge2SovereignChain
* @param tokenWrapped Wrapped token to burnt
* @param amount Amount of tokens
*/
function _bridgeWrappedAsset(
TokenWrapped tokenWrapped,
uint256 amount
) internal virtual {
// Burn tokens
tokenWrapped.burn(msg.sender, amount);
}

/**
* @notice Mints tokens from wrapped token to proceed with the claim
* note This function has been extracted to be able to override it by other contracts like Bridge2SovereignChain
* @param tokenWrapped Wrapped token to mint
* @param destinationAddress Minted token receiver
* @param amount Amount of tokens
*/
function _claimWrappedAsset(
TokenWrapped tokenWrapped,
address destinationAddress,
uint256 amount
) internal virtual {
// Burn tokens
tokenWrapped.mint(destinationAddress, amount);
}

/**
* @notice Bridge message and send ETH value
* note User/UI must be aware of the existing/available networks when choosing the destination network
Expand Down Expand Up @@ -364,7 +399,7 @@ contract PolygonZkEVMBridgeV2 is
}

// Burn wETH tokens
WETHToken.burn(msg.sender, amountWETH);
_bridgeWrappedAsset(WETHToken, amountWETH);

_bridgeMessage(
destinationNetwork,
Expand Down Expand Up @@ -492,7 +527,7 @@ contract PolygonZkEVMBridgeV2 is
}
} else {
// Claim wETH
WETHToken.mint(destinationAddress, amount);
_claimWrappedAsset(WETHToken, destinationAddress, amount);
}
} else {
// Check if it's gas token
Expand Down Expand Up @@ -536,7 +571,7 @@ contract PolygonZkEVMBridgeV2 is
);

// Mint tokens for the destination address
newWrappedToken.mint(destinationAddress, amount);
_claimWrappedAsset(newWrappedToken, destinationAddress, amount);

// Create mappings
tokenInfoToWrappedToken[tokenInfoHash] = address(
Expand All @@ -555,10 +590,7 @@ contract PolygonZkEVMBridgeV2 is
);
} else {
// Use the existing wrapped erc20
TokenWrapped(wrappedToken).mint(
destinationAddress,
amount
);
_claimWrappedAsset(TokenWrapped(wrappedToken), destinationAddress, amount);
}
}
}
Expand Down Expand Up @@ -608,7 +640,7 @@ contract PolygonZkEVMBridgeV2 is
address destinationAddress,
uint256 amount,
bytes calldata metadata
) external ifNotEmergencyState {
) external virtual ifNotEmergencyState {
// Destination network must be this networkID
if (destinationNetwork != networkID) {
revert DestinationNetworkInvalid();
Expand Down Expand Up @@ -646,7 +678,7 @@ contract PolygonZkEVMBridgeV2 is
);
} else {
// Mint wETH tokens
WETHToken.mint(destinationAddress, amount);
_claimWrappedAsset(WETHToken, destinationAddress, amount);

// Execute message
/* solhint-disable avoid-low-level-calls */
Expand Down
162 changes: 6 additions & 156 deletions contracts/v2/interfaces/IBridgeL2SovereignChains.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,176 +2,30 @@

pragma solidity ^0.8.20;
import "../../interfaces/IBasePolygonZkEVMGlobalExitRoot.sol";
import "./IPolygonZkEVMBridgeV2.sol";

interface IBridgeL2SovereignChains {
interface IBridgeL2SovereignChains is IPolygonZkEVMBridgeV2 {
/**
* @dev Thrown when the destination network is invalid
*/
error DestinationNetworkInvalid();

/**
* @dev Thrown when the amount does not match msg.value
*/
error AmountDoesNotMatchMsgValue();

/**
* @dev Thrown when user is bridging tokens and is also sending a value
*/
error MsgValueNotZero();

/**
* @dev Thrown when the Ether transfer on claimAsset fails
*/
error EtherTransferFailed();

/**
* @dev Thrown when the message transaction on claimMessage fails
*/
error MessageFailed();

/**
* @dev Thrown when the global exit root does not exist
*/
error GlobalExitRootInvalid();

/**
* @dev Thrown when the smt proof does not match
*/
error InvalidSmtProof();

/**
* @dev Thrown when an index is already claimed
*/
error AlreadyClaimed();

/**
* @dev Thrown when the owner of permit does not match the sender
*/
error NotValidOwner();

/**
* @dev Thrown when the spender of the permit does not match this contract address
*/
error NotValidSpender();

/**
* @dev Thrown when the amount of the permit does not match
*/
error NotValidAmount();

/**
* @dev Thrown when the permit data contains an invalid signature
* @dev Thrown when try to set a zero address to a non valid zero address field
*/
error NotValidSignature();
error InvalidZeroAddress();

/**
* @dev Thrown when sender is not the rollup manager
* @dev Thrown when the origin network is invalid
*/
error OnlyRollupManager();
error OriginNetworkInvalid();

/**
* @dev Thrown when sender is not the bridge manager
* @notice Bridge manager can set custom mapping for any token
*/
error OnlyBridgeManager();

/**
* @dev Thrown when the permit data contains an invalid signature
*/
error NativeTokenIsEther();

/**
* @dev Thrown when the permit data contains an invalid signature
*/
error NoValueInMessagesOnGasTokenNetworks();

/**
* @dev Thrown when the permit data contains an invalid signature
*/
error GasTokenNetworkMustBeZeroOnEther();

/**
* @dev Thrown when the wrapped token deployment fails
*/
error FailedTokenWrappedDeployment();

/**
* @dev Thrown when bridge manager address is invalid
*/
error NotValidBridgeManager();

/**
* @dev Thrown when try to set a zero address to a non valid zero address field
*/
error InvalidZeroAddress();

/**
* @dev Thrown when the origin network is invalid
*/
error OriginNetworkInvalid();

function wrappedTokenToTokenInfo(
address destinationAddress
) external view returns (uint32, address);

function updateGlobalExitRoot() external;

function activateEmergencyState() external;

function deactivateEmergencyState() external;

function bridgeAsset(
uint32 destinationNetwork,
address destinationAddress,
uint256 amount,
address token,
bool forceUpdateGlobalExitRoot,
bytes calldata permitData
) external payable;

function bridgeMessage(
uint32 destinationNetwork,
address destinationAddress,
bool forceUpdateGlobalExitRoot,
bytes calldata metadata
) external payable;

function bridgeMessageWETH(
uint32 destinationNetwork,
address destinationAddress,
uint256 amountWETH,
bool forceUpdateGlobalExitRoot,
bytes calldata metadata
) external;

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

function claimMessage(
bytes32[32] calldata smtProofLocalExitRoot,
bytes32[32] calldata smtProofRollupExitRoot,
uint256 globalIndex,
bytes32 mainnetExitRoot,
bytes32 rollupExitRoot,
uint32 originNetwork,
address originAddress,
uint32 destinationNetwork,
address destinationAddress,
uint256 amount,
bytes calldata metadata
) external;

function initialize(
uint32 _networkID,
address _gasTokenAddress,
Expand All @@ -181,8 +35,4 @@ interface IBridgeL2SovereignChains {
bytes memory _gasTokenMetadata,
address _bridgeManager
) external;

function getTokenMetadata(
address token
) external view returns (bytes memory);
}
11 changes: 0 additions & 11 deletions contracts/v2/interfaces/IPolygonZkEVMBridgeV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ interface IPolygonZkEVMBridgeV2 {
*/
error OnlyRollupManager();

/**
* @dev Thrown when sender is not the bridge manager
* @notice Bridge manager can set custom mapping for any token
*/
error OnlyBridgeManager();

/**
* @dev Thrown when the permit data contains an invalid signature
*/
Expand All @@ -95,11 +89,6 @@ interface IPolygonZkEVMBridgeV2 {
*/
error FailedTokenWrappedDeployment();

/**
* @dev Thrown when bridge manager address is invalid
*/
error NotValidBridgeManager();

function wrappedTokenToTokenInfo(
address destinationAddress
) external view returns (uint32, address);
Expand Down
Loading

0 comments on commit 6a40828

Please sign in to comment.