diff --git a/artifacts/contracts/solidity/oz/ERC1967Upgrade/VoteV1.sol/VoteV1.json b/artifacts/contracts/solidity/oz/ERC1967Upgrade/VoteV1.sol/VoteV1.json index c531629cc..450e7a06f 100644 --- a/artifacts/contracts/solidity/oz/ERC1967Upgrade/VoteV1.sol/VoteV1.json +++ b/artifacts/contracts/solidity/oz/ERC1967Upgrade/VoteV1.sol/VoteV1.json @@ -8,11 +8,41 @@ "stateMutability": "nonpayable", "type": "constructor" }, + { + "inputs": [], + "name": "InvalidInitialization", + "type": "error" + }, + { + "inputs": [], + "name": "NotInitializing", + "type": "error" + }, { "inputs": [], "name": "Voter_Has_Already_Voted", "type": "error" }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint64", + "name": "version", + "type": "uint64" + } + ], + "name": "Initialized", + "type": "event" + }, + { + "inputs": [], + "name": "initialize", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, { "inputs": [], "name": "version", @@ -66,8 +96,8 @@ "type": "function" } ], - "bytecode": "0x608060405234801561001057600080fd5b506001600055610273806100256000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c8063350580ea1461005157806354fd4d501461006f578063632a9a5214610080578063aec2ccae1461008a575b600080fd5b6100596100c6565b60405161006691906101c0565b60405180910390f35b600054604051908152602001610066565b610088610128565b005b6100b661009836600461020d565b6001600160a01b031660009081526002602052604090205460ff1690565b6040519015158152602001610066565b6060600180548060200260200160405190810160405280929190818152602001828054801561011e57602002820191906000526020600020905b81546001600160a01b03168152600190910190602001808311610100575b5050505050905090565b3360008181526002602052604090205460ff161561015957604051633b1dbd5760e11b815260040160405180910390fd5b506001805480820182557fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf601805473ffffffffffffffffffffffffffffffffffffffff1916339081179091556000908152600260205260409020805460ff19169091179055565b6020808252825182820181905260009190848201906040850190845b818110156102015783516001600160a01b0316835292840192918401916001016101dc565b50909695505050505050565b60006020828403121561021f57600080fd5b81356001600160a01b038116811461023657600080fd5b939250505056fea2646970667358221220f6556c6dad22db499a7dba3e6368615ec9614b8fe420a0af27407d67738a632c64736f6c63430008140033", - "deployedBytecode": "0x608060405234801561001057600080fd5b506004361061004c5760003560e01c8063350580ea1461005157806354fd4d501461006f578063632a9a5214610080578063aec2ccae1461008a575b600080fd5b6100596100c6565b60405161006691906101c0565b60405180910390f35b600054604051908152602001610066565b610088610128565b005b6100b661009836600461020d565b6001600160a01b031660009081526002602052604090205460ff1690565b6040519015158152602001610066565b6060600180548060200260200160405190810160405280929190818152602001828054801561011e57602002820191906000526020600020905b81546001600160a01b03168152600190910190602001808311610100575b5050505050905090565b3360008181526002602052604090205460ff161561015957604051633b1dbd5760e11b815260040160405180910390fd5b506001805480820182557fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf601805473ffffffffffffffffffffffffffffffffffffffff1916339081179091556000908152600260205260409020805460ff19169091179055565b6020808252825182820181905260009190848201906040850190845b818110156102015783516001600160a01b0316835292840192918401916001016101dc565b50909695505050505050565b60006020828403121561021f57600080fd5b81356001600160a01b038116811461023657600080fd5b939250505056fea2646970667358221220f6556c6dad22db499a7dba3e6368615ec9614b8fe420a0af27407d67738a632c64736f6c63430008140033", + "bytecode": "0x608060405234801561001057600080fd5b5060016000556103a5806100256000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c8063350580ea1461005c57806354fd4d501461007a578063632a9a521461008b5780638129fc1c14610095578063aec2ccae1461009d575b600080fd5b6100646100d9565b60405161007191906102f2565b60405180910390f35b600054604051908152602001610071565b61009361013b565b005b6100936101d3565b6100c96100ab36600461033f565b6001600160a01b031660009081526002602052604090205460ff1690565b6040519015158152602001610071565b6060600180548060200260200160405190810160405280929190818152602001828054801561013157602002820191906000526020600020905b81546001600160a01b03168152600190910190602001808311610113575b5050505050905090565b3360008181526002602052604090205460ff161561016c57604051633b1dbd5760e11b815260040160405180910390fd5b506001805480820182557fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf601805473ffffffffffffffffffffffffffffffffffffffff1916339081179091556000908152600260205260409020805460ff19169091179055565b7ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00805468010000000000000000810460ff16159067ffffffffffffffff1660008115801561021e5750825b905060008267ffffffffffffffff16600114801561023b5750303b155b905081158015610249575080155b156102675760405163f92ee8a960e01b815260040160405180910390fd5b845467ffffffffffffffff19166001178555831561029b57845468ff00000000000000001916680100000000000000001785555b600160005583156102eb57845468ff000000000000000019168555604051600181527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d29060200160405180910390a15b5050505050565b6020808252825182820181905260009190848201906040850190845b818110156103335783516001600160a01b03168352928401929184019160010161030e565b50909695505050505050565b60006020828403121561035157600080fd5b81356001600160a01b038116811461036857600080fd5b939250505056fea26469706673582212205146ed303b9ede0c0d8554ecaa9f3f408bf1f0ec621c8c59edc7f2847f9bbc6364736f6c63430008140033", + "deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100575760003560e01c8063350580ea1461005c57806354fd4d501461007a578063632a9a521461008b5780638129fc1c14610095578063aec2ccae1461009d575b600080fd5b6100646100d9565b60405161007191906102f2565b60405180910390f35b600054604051908152602001610071565b61009361013b565b005b6100936101d3565b6100c96100ab36600461033f565b6001600160a01b031660009081526002602052604090205460ff1690565b6040519015158152602001610071565b6060600180548060200260200160405190810160405280929190818152602001828054801561013157602002820191906000526020600020905b81546001600160a01b03168152600190910190602001808311610113575b5050505050905090565b3360008181526002602052604090205460ff161561016c57604051633b1dbd5760e11b815260040160405180910390fd5b506001805480820182557fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf601805473ffffffffffffffffffffffffffffffffffffffff1916339081179091556000908152600260205260409020805460ff19169091179055565b7ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00805468010000000000000000810460ff16159067ffffffffffffffff1660008115801561021e5750825b905060008267ffffffffffffffff16600114801561023b5750303b155b905081158015610249575080155b156102675760405163f92ee8a960e01b815260040160405180910390fd5b845467ffffffffffffffff19166001178555831561029b57845468ff00000000000000001916680100000000000000001785555b600160005583156102eb57845468ff000000000000000019168555604051600181527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d29060200160405180910390a15b5050505050565b6020808252825182820181905260009190848201906040850190845b818110156103335783516001600160a01b03168352928401929184019160010161030e565b50909695505050505050565b60006020828403121561035157600080fd5b81356001600160a01b038116811461036857600080fd5b939250505056fea26469706673582212205146ed303b9ede0c0d8554ecaa9f3f408bf1f0ec621c8c59edc7f2847f9bbc6364736f6c63430008140033", "linkReferences": {}, "deployedLinkReferences": {} } diff --git a/artifacts/contracts/solidity/oz/ERC1967Upgrade/VoteV2.sol/VoteV2.json b/artifacts/contracts/solidity/oz/ERC1967Upgrade/VoteV2.sol/VoteV2.json index 2ae8aa600..2ff65e58c 100644 --- a/artifacts/contracts/solidity/oz/ERC1967Upgrade/VoteV2.sol/VoteV2.json +++ b/artifacts/contracts/solidity/oz/ERC1967Upgrade/VoteV2.sol/VoteV2.json @@ -8,6 +8,16 @@ "stateMutability": "nonpayable", "type": "constructor" }, + { + "inputs": [], + "name": "InvalidInitialization", + "type": "error" + }, + { + "inputs": [], + "name": "NotInitializing", + "type": "error" + }, { "inputs": [], "name": "Voter_Has_Already_Voted", @@ -18,6 +28,33 @@ "name": "Voter_Has_Not_Voted", "type": "error" }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint64", + "name": "version", + "type": "uint64" + } + ], + "name": "Initialized", + "type": "event" + }, + { + "inputs": [], + "name": "initialize", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "initializeV2", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, { "inputs": [], "name": "version", @@ -78,8 +115,8 @@ "type": "function" } ], - "bytecode": "0x608060405234801561001057600080fd5b506002600055610474806100256000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c8063350580ea1461005c57806354fd4d501461007a578063632a9a521461008b578063aec2ccae14610095578063d83eb231146100d1575b600080fd5b6100646100d9565b604051610071919061033a565b60405180910390f35b600054604051908152602001610071565b61009361013b565b005b6100c16100a3366004610387565b6001600160a01b031660009081526002602052604090205460ff1690565b6040519015158152602001610071565b6100936101c6565b6060600180548060200260200160405190810160405280929190818152602001828054801561013157602002820191906000526020600020905b81546001600160a01b03168152600190910190602001808311610113575b5050505050905090565b3360008181526002602052604090205460ff161561016c57604051633b1dbd5760e11b815260040160405180910390fd5b506001805480820182557fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf60180546001600160a01b031916339081179091556000908152600260205260409020805460ff19169091179055565b3360008181526002602052604090205460ff166101f657604051630919808b60e01b815260040160405180910390fd5b60005b6001548110156102ec57336001600160a01b031660018281548110610220576102206103b7565b6000918252602090912001546001600160a01b0316036102da57805b6001805461024a91906103e3565b8110156102d457600161025d82826103fc565b8154811061026d5761026d6103b7565b600091825260209091200154600180546001600160a01b039092169183908110610299576102996103b7565b600091825260209091200180546001600160a01b0319166001600160a01b0392909216919091179055806102cc8161040f565b91505061023c565b506102ec565b806102e48161040f565b9150506101f9565b50336000908152600260205260409020805460ff19169055600180548061031557610315610428565b600082815260209020810160001990810180546001600160a01b031916905501905550565b6020808252825182820181905260009190848201906040850190845b8181101561037b5783516001600160a01b031683529284019291840191600101610356565b50909695505050505050565b60006020828403121561039957600080fd5b81356001600160a01b03811681146103b057600080fd5b9392505050565b634e487b7160e01b600052603260045260246000fd5b634e487b7160e01b600052601160045260246000fd5b818103818111156103f6576103f66103cd565b92915050565b808201808211156103f6576103f66103cd565b600060018201610421576104216103cd565b5060010190565b634e487b7160e01b600052603160045260246000fdfea2646970667358221220d03626254e31edb4a34bdd3043f89c264e2515a2bf73b44a18be8a42725f7e1d64736f6c63430008140033", - "deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100575760003560e01c8063350580ea1461005c57806354fd4d501461007a578063632a9a521461008b578063aec2ccae14610095578063d83eb231146100d1575b600080fd5b6100646100d9565b604051610071919061033a565b60405180910390f35b600054604051908152602001610071565b61009361013b565b005b6100c16100a3366004610387565b6001600160a01b031660009081526002602052604090205460ff1690565b6040519015158152602001610071565b6100936101c6565b6060600180548060200260200160405190810160405280929190818152602001828054801561013157602002820191906000526020600020905b81546001600160a01b03168152600190910190602001808311610113575b5050505050905090565b3360008181526002602052604090205460ff161561016c57604051633b1dbd5760e11b815260040160405180910390fd5b506001805480820182557fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf60180546001600160a01b031916339081179091556000908152600260205260409020805460ff19169091179055565b3360008181526002602052604090205460ff166101f657604051630919808b60e01b815260040160405180910390fd5b60005b6001548110156102ec57336001600160a01b031660018281548110610220576102206103b7565b6000918252602090912001546001600160a01b0316036102da57805b6001805461024a91906103e3565b8110156102d457600161025d82826103fc565b8154811061026d5761026d6103b7565b600091825260209091200154600180546001600160a01b039092169183908110610299576102996103b7565b600091825260209091200180546001600160a01b0319166001600160a01b0392909216919091179055806102cc8161040f565b91505061023c565b506102ec565b806102e48161040f565b9150506101f9565b50336000908152600260205260409020805460ff19169055600180548061031557610315610428565b600082815260209020810160001990810180546001600160a01b031916905501905550565b6020808252825182820181905260009190848201906040850190845b8181101561037b5783516001600160a01b031683529284019291840191600101610356565b50909695505050505050565b60006020828403121561039957600080fd5b81356001600160a01b03811681146103b057600080fd5b9392505050565b634e487b7160e01b600052603260045260246000fd5b634e487b7160e01b600052601160045260246000fd5b818103818111156103f6576103f66103cd565b92915050565b808201808211156103f6576103f66103cd565b600060018201610421576104216103cd565b5060010190565b634e487b7160e01b600052603160045260246000fdfea2646970667358221220d03626254e31edb4a34bdd3043f89c264e2515a2bf73b44a18be8a42725f7e1d64736f6c63430008140033", + "bytecode": "0x608060405234801561001057600080fd5b506002600055610697806100256000396000f3fe608060405234801561001057600080fd5b506004361061007d5760003560e01c8063632a9a521161005b578063632a9a52146100bb5780638129fc1c146100c3578063aec2ccae146100cb578063d83eb2311461010757600080fd5b8063350580ea1461008257806354fd4d50146100a05780635cd8a76b146100b1575b600080fd5b61008a61010f565b604051610097919061055d565b60405180910390f35b600054604051908152602001610097565b6100b9610171565b005b6100b9610249565b6100b96102d4565b6100f76100d93660046105aa565b6001600160a01b031660009081526002602052604090205460ff1690565b6040519015158152602001610097565b6100b96103e9565b6060600180548060200260200160405190810160405280929190818152602001828054801561016757602002820191906000526020600020905b81546001600160a01b03168152600190910190602001808311610149575b5050505050905090565b7ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00805460029190600160401b900460ff16806101bb5750805467ffffffffffffffff808416911610155b156101d95760405163f92ee8a960e01b815260040160405180910390fd5b805468ffffffffffffffffff191667ffffffffffffffff8316908117600160401b1782556002600055815468ff0000000000000000191682556040519081527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d29060200160405180910390a15050565b3360008181526002602052604090205460ff161561027a57604051633b1dbd5760e11b815260040160405180910390fd5b506001805480820182557fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf60180546001600160a01b031916339081179091556000908152600260205260409020805460ff19169091179055565b7ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a008054600160401b810460ff16159067ffffffffffffffff1660008115801561031a5750825b905060008267ffffffffffffffff1660011480156103375750303b155b905081158015610345575080155b156103635760405163f92ee8a960e01b815260040160405180910390fd5b845467ffffffffffffffff19166001178555831561039257845468ff00000000000000001916600160401b1785555b600160005583156103e257845468ff000000000000000019168555604051600181527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d29060200160405180910390a15b5050505050565b3360008181526002602052604090205460ff1661041957604051630919808b60e01b815260040160405180910390fd5b60005b60015481101561050f57336001600160a01b031660018281548110610443576104436105da565b6000918252602090912001546001600160a01b0316036104fd57805b6001805461046d9190610606565b8110156104f7576001610480828261061f565b81548110610490576104906105da565b600091825260209091200154600180546001600160a01b0390921691839081106104bc576104bc6105da565b600091825260209091200180546001600160a01b0319166001600160a01b0392909216919091179055806104ef81610632565b91505061045f565b5061050f565b8061050781610632565b91505061041c565b50336000908152600260205260409020805460ff1916905560018054806105385761053861064b565b600082815260209020810160001990810180546001600160a01b031916905501905550565b6020808252825182820181905260009190848201906040850190845b8181101561059e5783516001600160a01b031683529284019291840191600101610579565b50909695505050505050565b6000602082840312156105bc57600080fd5b81356001600160a01b03811681146105d357600080fd5b9392505050565b634e487b7160e01b600052603260045260246000fd5b634e487b7160e01b600052601160045260246000fd5b81810381811115610619576106196105f0565b92915050565b80820180821115610619576106196105f0565b600060018201610644576106446105f0565b5060010190565b634e487b7160e01b600052603160045260246000fdfea2646970667358221220630d3e90f82dfc720dfcd547ec09d18171bd728b0d65ce0d415f17af7aee6ebf64736f6c63430008140033", + "deployedBytecode": "0x608060405234801561001057600080fd5b506004361061007d5760003560e01c8063632a9a521161005b578063632a9a52146100bb5780638129fc1c146100c3578063aec2ccae146100cb578063d83eb2311461010757600080fd5b8063350580ea1461008257806354fd4d50146100a05780635cd8a76b146100b1575b600080fd5b61008a61010f565b604051610097919061055d565b60405180910390f35b600054604051908152602001610097565b6100b9610171565b005b6100b9610249565b6100b96102d4565b6100f76100d93660046105aa565b6001600160a01b031660009081526002602052604090205460ff1690565b6040519015158152602001610097565b6100b96103e9565b6060600180548060200260200160405190810160405280929190818152602001828054801561016757602002820191906000526020600020905b81546001600160a01b03168152600190910190602001808311610149575b5050505050905090565b7ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00805460029190600160401b900460ff16806101bb5750805467ffffffffffffffff808416911610155b156101d95760405163f92ee8a960e01b815260040160405180910390fd5b805468ffffffffffffffffff191667ffffffffffffffff8316908117600160401b1782556002600055815468ff0000000000000000191682556040519081527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d29060200160405180910390a15050565b3360008181526002602052604090205460ff161561027a57604051633b1dbd5760e11b815260040160405180910390fd5b506001805480820182557fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf60180546001600160a01b031916339081179091556000908152600260205260409020805460ff19169091179055565b7ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a008054600160401b810460ff16159067ffffffffffffffff1660008115801561031a5750825b905060008267ffffffffffffffff1660011480156103375750303b155b905081158015610345575080155b156103635760405163f92ee8a960e01b815260040160405180910390fd5b845467ffffffffffffffff19166001178555831561039257845468ff00000000000000001916600160401b1785555b600160005583156103e257845468ff000000000000000019168555604051600181527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d29060200160405180910390a15b5050505050565b3360008181526002602052604090205460ff1661041957604051630919808b60e01b815260040160405180910390fd5b60005b60015481101561050f57336001600160a01b031660018281548110610443576104436105da565b6000918252602090912001546001600160a01b0316036104fd57805b6001805461046d9190610606565b8110156104f7576001610480828261061f565b81548110610490576104906105da565b600091825260209091200154600180546001600160a01b0390921691839081106104bc576104bc6105da565b600091825260209091200180546001600160a01b0319166001600160a01b0392909216919091179055806104ef81610632565b91505061045f565b5061050f565b8061050781610632565b91505061041c565b50336000908152600260205260409020805460ff1916905560018054806105385761053861064b565b600082815260209020810160001990810180546001600160a01b031916905501905550565b6020808252825182820181905260009190848201906040850190845b8181101561059e5783516001600160a01b031683529284019291840191600101610579565b50909695505050505050565b6000602082840312156105bc57600080fd5b81356001600160a01b03811681146105d357600080fd5b9392505050565b634e487b7160e01b600052603260045260246000fd5b634e487b7160e01b600052601160045260246000fd5b81810381811115610619576106196105f0565b92915050565b80820180821115610619576106196105f0565b600060018201610644576106446105f0565b5060010190565b634e487b7160e01b600052603160045260246000fdfea2646970667358221220630d3e90f82dfc720dfcd547ec09d18171bd728b0d65ce0d415f17af7aee6ebf64736f6c63430008140033", "linkReferences": {}, "deployedLinkReferences": {} } diff --git a/contracts/solidity/oz/ERC1967Upgrade/VoteV1.sol b/contracts/solidity/oz/ERC1967Upgrade/VoteV1.sol index b1a25f537..271076a01 100644 --- a/contracts/solidity/oz/ERC1967Upgrade/VoteV1.sol +++ b/contracts/solidity/oz/ERC1967Upgrade/VoteV1.sol @@ -2,11 +2,12 @@ pragma solidity ^0.8.20; import "@openzeppelin/contracts/utils/Context.sol"; +import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; /** * @dev This contract is an example of a simple voting system. */ -contract VoteV1 is Context { +contract VoteV1 is Context, Initializable { /** * @dev version of the vote system */ @@ -39,12 +40,19 @@ contract VoteV1 is Context { } /** - * @dev Initialize the vote system version 1 + * @dev Initializes the vote system version 1 */ constructor() { _version = 1; } + /** + * @dev Initializes the vote system version 1 + */ + function initialize() external initializer { + _version = 1; + } + /** * @dev Add a voter to the vote system */ diff --git a/contracts/solidity/oz/ERC1967Upgrade/VoteV2.sol b/contracts/solidity/oz/ERC1967Upgrade/VoteV2.sol index 0c5c1d9fe..8c2fabdd2 100644 --- a/contracts/solidity/oz/ERC1967Upgrade/VoteV2.sol +++ b/contracts/solidity/oz/ERC1967Upgrade/VoteV2.sol @@ -2,11 +2,12 @@ pragma solidity ^0.8.20; import './VoteV1.sol'; +import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; /** * @dev This contract is an upgraded version of the VoteV1 */ -contract VoteV2 is VoteV1 { +contract VoteV2 is Initializable, VoteV1 { /** * @dev The caller has not voted @@ -31,6 +32,13 @@ contract VoteV2 is VoteV1 { _version = 2; } + /** + * @dev Initializes the vote system version 2 + */ + function initializeV2() external reinitializer(2) { + _version = 2; + } + /** * @dev Allows callers to withdraw their votes */ diff --git a/test/solidity/oz/ERC1967Upgrade/Vote.js b/test/solidity/oz/ERC1967Upgrade/Vote.js index 5e970a6c8..f750d4d66 100644 --- a/test/solidity/oz/ERC1967Upgrade/Vote.js +++ b/test/solidity/oz/ERC1967Upgrade/Vote.js @@ -22,6 +22,7 @@ const { expect } = require('chai') const { ethers } = require('hardhat') const fs = require('fs') const path = require('path') +const { GAS_LIMIT_1_000_000, CALL_EXCEPTION } = require('../../../constants') const HederaSmartContractsRootPath = path.resolve( __dirname, '..', @@ -43,13 +44,12 @@ const VoteV2Artifact = JSON.parse( ) describe('@ERC1967Upgrade Upgradable Vote Tests', () => { + let admin, voter1, voter2 let voteV1, voteV2, proxiedVoteV1, proxiedVoteV2, voteProxy - let admin, voter - const CALL_EXCEPTION = 'CALL_EXCEPTION' const EMPTY_DATA = '0x' - beforeEach(async () => { - ;[admin, voter] = await ethers.getSigners() + before(async () => { + ;[admin, voter1, voter2] = await ethers.getSigners() const VoteV1Fac = await ethers.getContractFactory('VoteV1') voteV1 = await VoteV1Fac.deploy() @@ -67,8 +67,11 @@ describe('@ERC1967Upgrade Upgradable Vote Tests', () => { }) it('Should upgrade proxy vote to point to voteV2', async () => { - await voteProxy.upgradeToAndCall(voteV2.address, EMPTY_DATA) + const tx = await voteProxy.upgradeToAndCall(voteV2.address, EMPTY_DATA) + const receipt = await tx.wait() + const event = receipt.events.find((e) => e.event === 'Upgraded') + expect(event.args.implementation).to.eq(voteV2.address) expect(await voteProxy.implementation()).to.eq(voteV2.address) }) @@ -122,7 +125,7 @@ describe('@ERC1967Upgrade Upgradable Vote Tests', () => { }) it('Should be able to change the current proxy admin to a new address', async () => { - const tx = await voteProxy.changeAdmin(await voter.getAddress()) + const tx = await voteProxy.changeAdmin(await voter1.getAddress()) const receipt = await tx.wait() const [previousAdmin, newAdmin] = receipt.events.map( @@ -130,15 +133,15 @@ describe('@ERC1967Upgrade Upgradable Vote Tests', () => { )[0].args expect(previousAdmin).to.eq(await admin.getAddress()) - expect(newAdmin).to.eq(await voter.getAddress()) + expect(newAdmin).to.eq(await voter1.getAddress()) }) it('Should NOT be able to change the current proxy admin if the caller is not an admin', async () => { let error const tx = await voteProxy - .connect(voter) - .changeAdmin(await voter.getAddress()) + .connect(voter1) + .changeAdmin(await voter1.getAddress()) expect(tx.wait()).to.eventually.be.rejected.and.have.property( 'code', @@ -148,42 +151,69 @@ describe('@ERC1967Upgrade Upgradable Vote Tests', () => { }) describe('Implementation contract', () => { - beforeEach(async () => { + it('V1: Should load VoteV1 into proxy address', async () => { proxiedVoteV1 = new ethers.Contract( voteProxy.address, VoteV1Artifact.abi, admin ) + await proxiedVoteV1.initialize() + + expect(await proxiedVoteV1.version()).to.eq(1) + expect(proxiedVoteV1.address).to.eq(voteProxy.address) + }) + + it('V1: Should cast votes to the system', async () => { + await proxiedVoteV1.connect(voter1).vote() + await proxiedVoteV1.connect(voter2).vote() + + const voters = await proxiedVoteV1.voters() + + expect(voters[0]).to.eq(voter1.address) + expect(voters[1]).to.eq(voter2.address) + }) + + it('V1: Should check if an account has already voted', async () => { + const votedStatus = await proxiedVoteV1.voted(voter1.address) + expect(votedStatus).to.be.true + }) + + it('V1: Should NOT let an already voted account to cast another vote', async () => { + const invalidVoteTx = await proxiedVoteV1 + .connect(voter1) + .vote(GAS_LIMIT_1_000_000) + + expect(invalidVoteTx.wait()).to.eventually.be.rejected.and.have.property( + 'code', + CALL_EXCEPTION + ) + }) - await voteProxy.upgradeToAndCall(voteV2.address, EMPTY_DATA) + it('V2: Should load VoteV2 into proxy address', async () => { + const tx = await voteProxy.upgradeToAndCall(voteV2.address, EMPTY_DATA) + await tx.wait() proxiedVoteV2 = new ethers.Contract( voteProxy.address, VoteV2Artifact.abi, admin ) - }) + await proxiedVoteV2.initializeV2() - it('Should load VoteV1 into proxy address', () => { - expect(proxiedVoteV1.address).to.eq(voteProxy.address) + expect(await proxiedVoteV2.version()).to.eq(2) + expect(proxiedVoteV2.address).to.eq(voteProxy.address) }) - it('Should cast votes to the system', async () => { - await proxiedVoteV1.connect(voter).vote() - - const voters = await proxiedVoteV1.voters() + it('V2: Should correctly inherit the storage states from version 1', async () => { + const voters = await proxiedVoteV2.voters() - expect(voters.includes(await voter.getAddress())) + expect(voters[0]).to.eq(voter1.address) + expect(voters[1]).to.eq(voter2.address) }) - it('Should let voters withdraw their votes only available in VoteV2', async () => { - await proxiedVoteV2.connect(voter).vote() - - expect(await proxiedVoteV2.voted(await voter.getAddress())).to.be.true - - await proxiedVoteV2.connect(voter).withdrawVote({ gasLimit: 3000000 }) - - expect(await proxiedVoteV2.voted(await voter.getAddress())).to.be.false + it('V2: Should let voters withdraw their votes which is only available in VoteV2', async () => { + await proxiedVoteV2.connect(voter1).withdrawVote() + expect(await proxiedVoteV2.voted(voter1.address)).to.be.false }) }) })