TestDeployer.t.sol
1 pragma solidity >=0.6.0; 2 3 import "truffle/Assert.sol"; 4 //import "truffle/DeployedAddresses.sol"; 5 import "../../../contracts/deployment/Deployer.sol"; 6 import "../../common/mock/ERC20Mock.t.sol"; 7 import "../../common/mock/EthFeedMock.t.sol"; 8 import "../../common/mock/ManagedContractMock.t.sol"; 9 import "../../common/mock/TimeProviderMock.t.sol"; 10 import "../../common/mock/UniswapV2RouterMock.t.sol"; 11 12 contract TestDeployer { 13 14 Deployer d_g = new Deployer(address(0)); 15 16 function testContractInitialization() public { 17 18 Deployer d = new Deployer(address(0)); 19 20 ManagedContractMock c = new ManagedContractMock(); 21 d.setContractAddress("ManagedContract", address(c)); 22 23 Assert.isFalse(c.getInitialized(), "contract !initialized"); 24 25 d.deploy(); 26 27 ManagedContractMock p = ManagedContractMock( 28 d.getContractAddress("ManagedContract") 29 ); 30 31 Assert.notEqual(address(p), address(c), "different addresses"); 32 33 Assert.isFalse(c.getInitialized(), "contract !initialized again"); 34 Assert.isTrue(p.getInitialized(), "proxy initialized"); 35 } 36 37 function testResetAndRedeploy() public { 38 39 Deployer d = new Deployer(address(0)); 40 41 ManagedContractMock c = new ManagedContractMock(); 42 d.setContractAddress("ManagedContract", address(c)); 43 44 d.deploy(); 45 46 ManagedContractMock p1 = ManagedContractMock( 47 d.getContractAddress("ManagedContract") 48 ); 49 50 d.reset(); 51 d.deploy(); 52 53 ManagedContractMock p2 = ManagedContractMock( 54 d.getContractAddress("ManagedContract") 55 ); 56 57 Assert.notEqual(address(p1), address(c), "different addresses: p1, c"); 58 Assert.notEqual(address(p2), address(c), "different addresses: p2, c"); 59 Assert.notEqual(address(p1), address(p2), "different addresses: p1, p2"); 60 61 Assert.isFalse(c.getInitialized(), "contract !initialized"); 62 Assert.isTrue(p1.getInitialized(), "proxy 1 initialized"); 63 Assert.isTrue(p2.getInitialized(), "proxy 2 initialized"); 64 } 65 66 function testUpgradeImplementationFromOwnerAddress() public { 67 68 Deployer d = new Deployer(address(0)); 69 70 ManagedContractMock c1 = new ManagedContractMock(); 71 d.setContractAddress("ManagedContract", address(c1)); 72 73 d.deploy(address(this)); 74 Proxy p = Proxy( 75 d.getPayableContractAddress("ManagedContract") 76 ); 77 78 Assert.equal(getImplementation(p), address(c1), "initial implementation"); 79 80 ManagedContractMock c2 = new ManagedContractMock(); 81 82 (bool success,) = address(p).call( 83 abi.encodePacked( 84 p.setImplementation.selector, 85 abi.encode(address(c2)) 86 ) 87 ); 88 89 Assert.isTrue(success, "setImplementation should succed"); 90 Assert.equal(getImplementation(p), address(c2), "updated implementation"); 91 Assert.notEqual(address(c1), address(c2), "different addresses"); 92 } 93 94 function testUpgradeImplementationFromAnotherAddress() public { 95 96 Deployer d = new Deployer(address(0)); 97 98 ManagedContractMock c1 = new ManagedContractMock(); 99 d.setContractAddress("ManagedContract", address(c1)); 100 101 d.deploy(address(0x0000000000000000000000000000000000000001)); 102 Proxy p = Proxy( 103 d.getPayableContractAddress("ManagedContract") 104 ); 105 106 ManagedContractMock c2 = new ManagedContractMock(); 107 108 (bool success,) = address(p).call( 109 abi.encodePacked( 110 p.setImplementation.selector, 111 abi.encode(address(c2)) 112 ) 113 ); 114 115 Assert.isFalse(success, "setImplementation should fail"); 116 } 117 118 function testSetNonUpgradable() public { 119 120 Deployer d = new Deployer(address(0)); 121 122 ManagedContractMock c1 = new ManagedContractMock(); 123 d.setContractAddress("ManagedContract", address(c1)); 124 125 d.deploy(address(this)); 126 Proxy p = Proxy( 127 d.getPayableContractAddress("ManagedContract") 128 ); 129 130 ManagedContractMock c2 = new ManagedContractMock(); 131 132 (bool s1,) = address(p).call( 133 abi.encodePacked( 134 p.setImplementation.selector, 135 abi.encode(address(c2)) 136 ) 137 ); 138 Assert.isTrue(s1, "setImplementation should succed"); 139 140 p.setNonUpgradable(); 141 142 (bool s2,) = address(p).call( 143 abi.encodePacked( 144 p.setImplementation.selector, 145 abi.encode(address(c2)) 146 ) 147 ); 148 Assert.isFalse(s2, "setImplementation should fail"); 149 } 150 151 function testMigrationInitialization() public { 152 153 d_g.deploy(); 154 } 155 156 function getImplementation(Proxy p) private view returns (address) { 157 158 return ManagedContract(address(p)).getImplementation(); 159 } 160 }