|
| 1 | +pragma solidity ^0.5.16; |
| 2 | + |
| 3 | +import "./interfaces/ISynthetixBridgeToOptimism.sol"; |
| 4 | +import "./interfaces/ISynthetixBridgeEscrow.sol"; |
| 5 | +import "./interfaces/IERC20.sol"; |
| 6 | + |
| 7 | +interface IOwned { |
| 8 | + function owner() external view returns (address); |
| 9 | + |
| 10 | + function nominatedOwner() external view returns (address); |
| 11 | + |
| 12 | + function nominateNewOwner(address _owner) external; |
| 13 | + |
| 14 | + function acceptOwnership() external; |
| 15 | +} |
| 16 | + |
| 17 | +interface IOldSynthetixBridgeToOptimism { |
| 18 | + function activated() external view returns (bool); |
| 19 | + |
| 20 | + function migrateBridge(address newBridge) external; |
| 21 | +} |
| 22 | + |
| 23 | +contract BridgeMigrator { |
| 24 | + IERC20 public snx; |
| 25 | + |
| 26 | + address public oldBridge; |
| 27 | + address public newBridge; |
| 28 | + address public newEscrow; |
| 29 | + |
| 30 | + address public pdao; |
| 31 | + address public deployer; |
| 32 | + |
| 33 | + uint256 public migratedBalance; |
| 34 | + |
| 35 | + constructor( |
| 36 | + address _newBridge, |
| 37 | + address _newEscrow, |
| 38 | + string memory _network |
| 39 | + ) public { |
| 40 | + newBridge = _newBridge; |
| 41 | + newEscrow = _newEscrow; |
| 42 | + |
| 43 | + if (keccak256(abi.encodePacked(_network)) == keccak256(abi.encodePacked("mainnet"))) { |
| 44 | + oldBridge = 0x045e507925d2e05D114534D0810a1abD94aca8d6; |
| 45 | + pdao = 0xEb3107117FEAd7de89Cd14D463D340A2E6917769; |
| 46 | + deployer = 0xDe910777C787903F78C89e7a0bf7F4C435cBB1Fe; |
| 47 | + snx = IERC20(0xC011a73ee8576Fb46F5E1c5751cA3B9Fe0af2a6F); |
| 48 | + } else if (keccak256(abi.encodePacked(_network)) == keccak256(abi.encodePacked("kovan"))) { |
| 49 | + oldBridge = 0xE8Bf8fe5ce9e15D30F478E1647A57CB6B0271228; |
| 50 | + pdao = 0x73570075092502472E4b61A7058Df1A4a1DB12f2; |
| 51 | + deployer = 0x73570075092502472E4b61A7058Df1A4a1DB12f2; |
| 52 | + snx = IERC20(0xC011a73ee8576Fb46F5E1c5751cA3B9Fe0af2a6F); |
| 53 | + } else { |
| 54 | + revert("Unsupported network"); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // ---------------------------------------- |
| 59 | + // PUBLIC |
| 60 | + // ---------------------------------------- |
| 61 | + |
| 62 | + function execute() public { |
| 63 | + require(migratedBalance == 0, "Already migrated"); |
| 64 | + require(msg.sender == deployer, "Only deployer may execute"); |
| 65 | + |
| 66 | + _takeOwnership(); |
| 67 | + _validateBalancesBefore(); |
| 68 | + _provideAllowance(); |
| 69 | + _validateStateBefore(); |
| 70 | + |
| 71 | + _migrateSNX(); |
| 72 | + |
| 73 | + _validateStateAfter(); |
| 74 | + _validateBalancesAfter(); |
| 75 | + _relinquishOwnership(); |
| 76 | + } |
| 77 | + |
| 78 | + // ---------------------------------------- |
| 79 | + // INTERNAL |
| 80 | + // ---------------------------------------- |
| 81 | + |
| 82 | + function _takeOwnership() internal { |
| 83 | + require(IOwned(oldBridge).owner() == deployer || IOwned(oldBridge).owner() == pdao, "Unexpected old bridge owner"); |
| 84 | + require(IOwned(newEscrow).owner() == deployer || IOwned(newEscrow).owner() == pdao, "Unexpected new escrow owner"); |
| 85 | + |
| 86 | + IOwned(oldBridge).acceptOwnership(); |
| 87 | + IOwned(newEscrow).acceptOwnership(); |
| 88 | + |
| 89 | + require(IOwned(oldBridge).owner() == address(this), "Unable to take old bridge ownership"); |
| 90 | + require(IOwned(newEscrow).owner() == address(this), "Unable to take new escrow ownership"); |
| 91 | + } |
| 92 | + |
| 93 | + function _validateBalancesBefore() internal { |
| 94 | + require(snx.balanceOf(oldBridge) > 1000000 ether, "Unexpected initial old bridge balance"); |
| 95 | + require(snx.balanceOf(newEscrow) == 0, "Unexpected initial new escrow balance"); |
| 96 | + |
| 97 | + migratedBalance = snx.balanceOf(oldBridge); |
| 98 | + } |
| 99 | + |
| 100 | + function _provideAllowance() internal { |
| 101 | + ISynthetixBridgeEscrow(newEscrow).approveBridge(address(snx), newBridge, 0); |
| 102 | + ISynthetixBridgeEscrow(newEscrow).approveBridge(address(snx), newBridge, uint256(-1)); |
| 103 | + require(snx.allowance(newEscrow, newBridge) == uint256(-1), "Unexpected final new bridge allowance"); |
| 104 | + } |
| 105 | + |
| 106 | + function _validateStateBefore() internal { |
| 107 | + require(IOldSynthetixBridgeToOptimism(oldBridge).activated() == true, "Unexpected initial old bridge state"); |
| 108 | + } |
| 109 | + |
| 110 | + function _migrateSNX() internal { |
| 111 | + IOldSynthetixBridgeToOptimism(oldBridge).migrateBridge(newEscrow); |
| 112 | + } |
| 113 | + |
| 114 | + function _validateStateAfter() internal { |
| 115 | + require(IOldSynthetixBridgeToOptimism(oldBridge).activated() == false, "Unexpected final old bridge state"); |
| 116 | + } |
| 117 | + |
| 118 | + function _validateBalancesAfter() internal { |
| 119 | + require(snx.balanceOf(oldBridge) == 0, "Unexpected final old bridge balance"); |
| 120 | + require(snx.balanceOf(newEscrow) == migratedBalance, "Unexpected final new escrow balance"); |
| 121 | + } |
| 122 | + |
| 123 | + function _relinquishOwnership() internal { |
| 124 | + IOwned(oldBridge).nominateNewOwner(deployer); |
| 125 | + IOwned(newEscrow).nominateNewOwner(deployer); |
| 126 | + |
| 127 | + require(IOwned(oldBridge).nominatedOwner() == deployer, "Failed to relinquish old bridge ownership"); |
| 128 | + require(IOwned(newEscrow).nominatedOwner() == deployer, "Failed to relinquish new escrow ownership"); |
| 129 | + } |
| 130 | +} |
0 commit comments