From 65dfbe0c47da523bc60982b332e23b5c24a22449 Mon Sep 17 00:00:00 2001 From: Duc Tho Tran Date: Sun, 15 May 2022 08:58:22 +0700 Subject: [PATCH] Add migration contract (#4) --- .../sidechain/SidechainGatewayMigration.sol | 15 ++++ contracts/v0.8/migration/BridgeMigration.sol | 77 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 contracts/v0.5/chain/sidechain/SidechainGatewayMigration.sol create mode 100644 contracts/v0.8/migration/BridgeMigration.sol diff --git a/contracts/v0.5/chain/sidechain/SidechainGatewayMigration.sol b/contracts/v0.5/chain/sidechain/SidechainGatewayMigration.sol new file mode 100644 index 0000000..5c0b2a2 --- /dev/null +++ b/contracts/v0.5/chain/sidechain/SidechainGatewayMigration.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.5.17; + +import "../../references/ERC20/IERC20.sol"; +import "./SidechainGatewayStorage.sol"; + +contract SidechainGatewayMigration is SidechainGatewayStorage { + function migrateTo(address[] calldata _tokens, address _to) external onlyAdmin { + IERC20 _token; + for (uint256 _i; _i < _tokens.length; _i++) { + _token = IERC20(_tokens[_i]); + _token.transfer(_to, _token.balanceOf(address(this))); + } + } +} diff --git a/contracts/v0.8/migration/BridgeMigration.sol b/contracts/v0.8/migration/BridgeMigration.sol new file mode 100644 index 0000000..68c78e7 --- /dev/null +++ b/contracts/v0.8/migration/BridgeMigration.sol @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import "@openzeppelin/contracts/access/Ownable.sol"; +import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; + +interface IGateway { + function paused() external returns (bool); + + function pause() external; + + function unpause() external; + + function withdrawTokenFor( + uint256 _withdrawalId, + address _user, + address _token, + uint256 _amount, + bytes memory _signatures + ) external; +} + +interface IGatewayV2 { + function receiveEther() external payable; +} + +contract BridgeMigration is Ownable { + IGateway public gateway; + address public weth; + + constructor(IGateway _gateway, address _weth) { + gateway = _gateway; + weth = _weth; + } + + fallback() external payable {} + + receive() external payable {} + + function pauseGateway() external onlyOwner { + gateway.pause(); + } + + function unpauseGateway() external onlyOwner { + gateway.unpause(); + } + + function migrateAndTransfer( + uint256[] calldata _withdrawalIds, + address[] calldata _tokens, + uint256[] calldata _amounts, + bytes[] calldata _signatures, + IGatewayV2 _gatewayV2 + ) external onlyOwner { + require( + _withdrawalIds.length > 0 && + _withdrawalIds.length == _tokens.length && + _withdrawalIds.length == _amounts.length && + _withdrawalIds.length == _signatures.length, + "BridgeMigration: invalid array length" + ); + + gateway.unpause(); + for (uint256 _i; _i < _withdrawalIds.length; _i++) { + gateway.withdrawTokenFor(_withdrawalIds[_i], msg.sender, _tokens[_i], _amounts[_i], _signatures[_i]); + } + gateway.pause(); + + for (uint256 _i; _i < _tokens.length; _i++) { + if (_tokens[_i] == weth) { + _gatewayV2.receiveEther{ value: _amounts[_i] }(); + } else { + IERC20(_tokens[_i]).transfer(address(_gatewayV2), _amounts[_i]); + } + } + } +}