Skip to content

Commit

Permalink
chore: add upgradable smart contracts verification example (#837)
Browse files Browse the repository at this point in the history
* chore: add upgradable contracts to verify example
  • Loading branch information
nikola-bozin-txfusion authored Mar 8, 2024
1 parent 5964e3f commit 3f37d90
Show file tree
Hide file tree
Showing 19 changed files with 665 additions and 31 deletions.
5 changes: 0 additions & 5 deletions examples/upgradable-example/scripts/deploy-box-beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ async function main() {
box.connect(zkWallet);
const value = await box.retrieve();
console.info(chalk.cyan('Box value is: ', value));

const chainId = await hre.network.provider.send('eth_chainId', []);
if (chainId === '0x12c') {
const _ = hre.run('verify:verify', { address: await box.getAddress() });
}
}

main().catch((error) => {
Expand Down
5 changes: 0 additions & 5 deletions examples/upgradable-example/scripts/deploy-box-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ async function main() {
box.connect(zkWallet);
const value = await box.retrieve();
console.info(chalk.cyan('Box value is: ', value));

const chainId = await hre.network.provider.send('eth_chainId', []);
if (chainId === '0x12c') {
const _ = hre.run('verify:verify', { address: await box.getAddress() });
}
}

main().catch((error) => {
Expand Down
5 changes: 0 additions & 5 deletions examples/upgradable-example/scripts/deploy-box-uups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ async function main() {
box.connect(zkWallet);
const value = await box.retrieve();
console.info(chalk.cyan('Box value is: ', value));

const chainId = await hre.network.provider.send('eth_chainId', []);
if (chainId === '0x12c') {
const _ = hre.run('verify:verify', { address: await box.getAddress() });
}
}

main().catch((error) => {
Expand Down
5 changes: 0 additions & 5 deletions examples/upgradable-example/scripts/upgrade-box-beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ async function main() {
await new Promise((resolve) => setTimeout(resolve, 2000));
const value = await upgradedBox.retrieve();
console.info(chalk.cyan('New box value is', value));

const chainId = await hre.network.provider.send('eth_chainId', []);
if (chainId === '0x12c') {
const _ = hre.run('verify:verify', { address: await upgradedBox.getAddress() });
}
}

main().catch((error) => {
Expand Down
5 changes: 0 additions & 5 deletions examples/upgradable-example/scripts/upgrade-box-uups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ async function main() {
await new Promise((resolve) => setTimeout(resolve, 2000));
const value = await upgradedBox.retrieve();
console.info(chalk.cyan('BoxUups value is', value));

const chainId = await hre.network.provider.send('eth_chainId', []);
if (chainId === '0x12c') {
const _ = hre.run('verify:verify', { address: await upgradedBox.getAddress() });
}
}

main().catch((error) => {
Expand Down
5 changes: 0 additions & 5 deletions examples/upgradable-example/scripts/upgrade-box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ async function main() {
await new Promise((resolve) => setTimeout(resolve, 2000));
const value = await upgradedBox.retrieve();
console.info(chalk.cyan('Box value is', value));

const chainId = await hre.network.provider.send('eth_chainId', []);
if (chainId === '0x12c') {
const _ = hre.run('verify:verify', { address: await upgradedBox.getAddress() });
}
}

main().catch((error) => {
Expand Down
27 changes: 27 additions & 0 deletions examples/verify-example/contracts/Box.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

contract Box is Initializable {
uint256 private value;
uint256 private secondValue;
uint256 private thirdValue;

function initialize(uint256 initValue) public initializer {
value = initValue;
}

// Reads the last stored value
function retrieve() public view returns (uint256) {
return value;
}

// Stores a new value in the contract
function store(uint256 newValue) public {
value = newValue;
emit ValueChanged(newValue);
}
// Emitted when the stored value changes
event ValueChanged(uint256 newValue);
}
33 changes: 33 additions & 0 deletions examples/verify-example/contracts/BoxUups.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;
import '@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol';
import '@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol';

contract BoxUups is Initializable, UUPSUpgradeable, OwnableUpgradeable {
uint256 private value;
uint256 private secondValue;
uint256 private thirdValue;

function initialize(uint256 initValue) public initializer {
value = initValue;
__Ownable_init();
__UUPSUpgradeable_init();
}

// Reads the last stored value
function retrieve() public view returns (uint256) {
return value;
}

// Stores a new value in the contract
function store(uint256 newValue) public {
value = newValue;
emit ValueChanged(newValue);
}

function _authorizeUpgrade(address) internal override onlyOwner {}

// Emitted when the stored value changes
event ValueChanged(uint256 newValue);
}
54 changes: 54 additions & 0 deletions examples/verify-example/contracts/BoxUupsV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;
import '@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol';
import '@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol';

contract BoxUupsV2 is Initializable, UUPSUpgradeable, OwnableUpgradeable {
uint256 private value;
uint256 private secondValue;
uint256 private thirdValue;

function initialize(uint256 initValue) public initializer {
value = initValue;
}

// Reads the last stored value and returns it with a prefix
function retrieve() public view returns (string memory) {
return string(abi.encodePacked('V2: ', uint2str(value)));
}

// Converts a uint to a string
function uint2str(uint _i) internal pure returns (string memory) {
if (_i == 0) {
return '0';
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len;
while (_i != 0) {
k = k - 1;
uint8 temp = (48 + uint8(_i - (_i / 10) * 10));
bytes1 b1 = bytes1(temp);
bstr[k] = b1;
_i /= 10;
}
return string(bstr);
}

// Stores a new value in the contract
function store(uint256 newValue) public {
value = newValue;
emit ValueChanged(newValue);
}

function _authorizeUpgrade(address) internal override onlyOwner {}

// Emitted when the stored value changes
event ValueChanged(uint256 newValue);
}
51 changes: 51 additions & 0 deletions examples/verify-example/contracts/BoxV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

contract BoxV2 is Initializable{
uint256 private value;
uint256 private secondValue;
uint256 private thirdValue;

// Emitted when the stored value changes
event ValueChanged(uint256 newValue);

function initialize(uint256 initValue) public initializer {
value = initValue;
}

// Stores a new value in the contract
function store(uint256 newValue) public {
value = newValue;
emit ValueChanged(newValue);
}

// Reads the last stored value and returns it with a prefix
function retrieve() public view returns (string memory) {
return string(abi.encodePacked("V2: ", uint2str(value)));
}

// Converts a uint to a string
function uint2str(uint _i) internal pure returns (string memory) {
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len;
while (_i != 0) {
k = k - 1;
uint8 temp = (48 + uint8(_i - (_i / 10) * 10));
bytes1 b1 = bytes1(temp);
bstr[k] = b1;
_i /= 10;
}
return string(bstr);
}
}
5 changes: 5 additions & 0 deletions examples/verify-example/contracts/Empty.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract EmptyContract {
}
21 changes: 21 additions & 0 deletions examples/verify-example/contracts/Factory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./Empty.sol";

contract Factory {
address[] public deployedContracts;

function initialize() public{
deployEmptyContract();
}

function deployEmptyContract() public {
EmptyContract newContract = new EmptyContract();
deployedContracts.push(address(newContract));
}

function getNumberOfDeployedContracts() public view returns (uint) {
return deployedContracts.length;
}
}
33 changes: 33 additions & 0 deletions examples/verify-example/contracts/FactoryUups.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import '@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol';
import "./Empty.sol";


contract FactoryUups is Initializable, UUPSUpgradeable, OwnableUpgradeable {
address[] public deployedContracts;

function initialize() public initializer{
__Ownable_init();
__UUPSUpgradeable_init();
deployEmptyContract();
}

function deployEmptyContract() public {
EmptyContract newContract = new EmptyContract();
deployedContracts.push(address(newContract));
}

function getNumberOfDeployedContracts() public view returns (uint) {
return deployedContracts.length;
}

function storeNothing() public pure {

}

function _authorizeUpgrade(address) internal override onlyOwner {}
}
53 changes: 53 additions & 0 deletions examples/verify-example/contracts/FactoryUupsV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import '@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol';
import "./Empty.sol";


contract FactoryUupsV2 is Initializable, UUPSUpgradeable, OwnableUpgradeable {
address[] public deployedContracts;

function initialize() public initializer{
deployEmptyContract();
}

function deployEmptyContract() public {
EmptyContract newContract = new EmptyContract();
deployedContracts.push(address(newContract));
}

function getNumberOfDeployedContracts() public view returns (uint) {
return deployedContracts.length;
}

function storeNothing() public pure {

}

function _authorizeUpgrade(address) internal override onlyOwner {}

function uint2str(uint _i) internal pure returns (string memory) {
if (_i == 0) {
return '0';
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len;
while (_i != 0) {
k = k - 1;
uint8 temp = (48 + uint8(_i - (_i / 10) * 10));
bytes1 b1 = bytes1(temp);
bstr[k] = b1;
_i /= 10;
}
return string(bstr);
}
}
Loading

0 comments on commit 3f37d90

Please sign in to comment.