Skip to content

Commit

Permalink
feat: add upgrades extension in hre (#1286)
Browse files Browse the repository at this point in the history
* feat: refactor upgradable interface

* chore: apply lint

* fix: add additional example and remove unnessecary file

* fix: refactor code, adjust params and set proper version of that oz hardhat-upgradable plugin

* fix: add defender v1 and redesign code

* fix: compile missing contracts before verify

* fix: update check for contracts update

* fix: add paymaster params for proxy and implementation deployment

* fix: add paymaster params for admin and add other custom data

* fix: add verify for non zksync networks

* fix: change custom data for proxy admin

* fix: change custom data for proxy admin

* fix: change paymaster params to PaymasterParams from zksync-ethers

* fix: override verify etherscan task for non zksync networks

* chore: remove ethers and oz hardhat upgrades from examples

* chore: revert to the local host 3050

---------

Co-authored-by: Marko Arambasic <[email protected]>
  • Loading branch information
nikola-bozin-txfusion and kiriyaga authored Sep 16, 2024
1 parent f1e2508 commit 2bb7976
Show file tree
Hide file tree
Showing 62 changed files with 2,695 additions and 469 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,23 @@ jobs:
pnpm hardhat run scripts/deploy-box-uups.ts
pnpm hardhat run scripts/upgrade-box-beacon.ts
pnpm hardhat run scripts/upgrade-box-uups.ts
pnpm hardhat run scripts/upgrade-box.ts
pnpm hardhat run scripts/deploy-box-beacon-factory.ts
pnpm hardhat run scripts/deploy-box-proxy-factory.ts
pnpm hardhat run scripts/deploy-box-uups-factory.ts
pnpm hardhat run scripts/upgrade-box-uups-factory.ts
pnpm hardhat run scripts/upgrade-box-beacon-factory.ts
pnpm hardhat run scripts/upgrade-box-factory.ts
- name: Test upgradable example
run: |
cd examples/upgradable-example-l1
pnpm hardhat compile
pnpm hardhat run scripts/deploy-box-beacon.ts
pnpm hardhat run scripts/deploy-box-proxy.ts
pnpm hardhat run scripts/deploy-box-uups.ts
pnpm hardhat run scripts/upgrade-box-beacon.ts
pnpm hardhat run scripts/upgrade-box-uups.ts
pnpm hardhat run scripts/upgrade-box.ts
- name: Test zksync-ethers example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function main() {
'Something went wrong during deployment of a Factory contract. Initialize functions is probably not called.',
);
}
console.info("Successfully deployed upgradable factor contract.")
console.info("Successfully deployed upgradable factory contract.")
}

main().catch((error) => {
Expand Down
7 changes: 7 additions & 0 deletions examples/upgradable-example-l1/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
extends: [`${__dirname}/../../config/eslint/eslintrc.cjs`],
parserOptions: {
project: `${__dirname}/tsconfig.json`,
sourceType: "module",
},
};
67 changes: 67 additions & 0 deletions examples/upgradable-example-l1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# ZKsync Era upgradable example

This project demonstrates how to compile and deploy upgradable smart contracts in ZKsync Era using the Hardhat plugins.

## Prerequisites

- node.js 14.x or later.
- yarn.

## Configuration

Plugin configuration is located in [`hardhat.config.ts`](./hardhat.config.ts).
You should only change the ZKsync network configuration.

`hardhat.config.ts` example with ZKsync network configured with the name `zkTestnet` and `sepolia` used as the underlying layer 1 network:
```ts
import '@matterlabs/hardhat-zksync-solc';
import '@matterlabs/hardhat-zksync-deploy';
import '@matterlabs/hardhat-zksync-upgradable';

import { HardhatUserConfig } from 'hardhat/types';

const config: HardhatUserConfig = {
networks: {
sepolia: {
url: 'https://sepolia.infura.io/v3/<API_KEY>' // you can use either the URL of the Ethereum Web3 RPC, or the identifier of the network (e.g. `mainnet` or `rinkeby`)
},
zkTestnet: {
url: 'https://sepolia.era.zksync.dev', // you should use the URL of the ZKsync network RPC
ethNetwork: 'sepolia',
zksync: true
},
}
};

export default config;
```

If you don't specify ZKsync network (`--network`), `local-setup` with <http://localhost:8545> (Ethereum RPC URL) and <http://localhost:3050> (ZKsync RPC URL) will be used.

## Usage

Before using plugins, you need to build them first

```sh
# Run the following in the *root* of the repo.
yarn
yarn build
```

After that you should be able to run plugins:

```sh
# Run the following in `examples/upgradable-example` folder.
yarn
yarn hardhat compile
```

- `yarn hardhat compile`: compiles all the contracts in the `contracts` folder.

To run a specific end-to-end script in the `scripts` folder, use the following command

```
yarn hardhat run ./scripts/<SCRIPT_NAME>
```

- Example: `yarn hardhat run ./scripts/deploy-box-proxy.ts`
27 changes: 27 additions & 0 deletions examples/upgradable-example-l1/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/upgradable-example-l1/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/upgradable-example-l1/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/upgradable-example-l1/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);
}
}
16 changes: 16 additions & 0 deletions examples/upgradable-example-l1/contracts/Greeter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;
pragma abicoder v2;

contract Greeter {
string greeting;

function greet() public view returns (string memory) {
return greeting;
}

function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
}
35 changes: 35 additions & 0 deletions examples/upgradable-example-l1/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import '@matterlabs/hardhat-zksync-upgradable';
import "@matterlabs/hardhat-zksync-ethers"

import { HardhatUserConfig } from 'hardhat/config';

const config: HardhatUserConfig = {
zksolc: {
version: 'latest',
compilerSource: 'binary',
settings: {
optimizer: {
enabled: true,
},
},
},
networks: {
hardhat: {
zksync: false,
},
eth: {
zksync: true,
url: 'http://0.0.0.0:8545',
},
zkSyncNetwork: {
zksync: true,
ethNetwork: 'eth',
url: 'http://0.0.0.0:3050',
},
},
solidity: {
version: '0.8.20',
},
};

export default config;
52 changes: 52 additions & 0 deletions examples/upgradable-example-l1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "hardhat-zksync-upgradable-example",
"version": "0.1.0",
"author": "Matter Labs",
"license": "MIT",
"scripts": {
"lint": "pnpm eslint",
"prettier:check": "pnpm prettier --check",
"lint:fix": "pnpm eslint --fix",
"fmt": "pnpm prettier --write",
"eslint": "eslint scripts/*.ts",
"prettier": "prettier scripts/*.ts",
"test": "mocha test/tests.ts --exit",
"build": "tsc --build .",
"clean": "rimraf dist"
},
"devDependencies": {
"@openzeppelin/contracts": "^4.9.2",
"@types/node": "^18.11.17",
"@typescript-eslint/eslint-plugin": "6.13.1",
"@typescript-eslint/parser": "6.13.1",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-no-only-tests": "^3.1.0",
"eslint-plugin-prettier": "^5.0.1",
"prettier": "^3.3.0",
"rimraf": "^5.0.7",
"ts-node": "^10.9.2",
"typescript": "^5.3.0"
},
"dependencies": {
"@matterlabs/hardhat-zksync-deploy": "workspace:^",
"@matterlabs/hardhat-zksync-ethers": "workspace:^",
"@matterlabs/hardhat-zksync-solc": "workspace:^",
"@matterlabs/hardhat-zksync-upgradable": "workspace:^",
"@matterlabs/hardhat-zksync-verify": "workspace:^",
"@openzeppelin/contracts-upgradeable": "^4.9.2",
"chalk": "^4.1.2",
"ethers": "^6.12.2",
"hardhat": "^2.22.5",
"zksync": "^0.13.1",
"zksync-ethers": "^6.8.0"
},
"prettier": {
"tabWidth": 4,
"printWidth": 120,
"parser": "typescript",
"singleQuote": true,
"bracketSpacing": true
}
}
Loading

0 comments on commit 2bb7976

Please sign in to comment.