• Stars
    star
    607
  • Rank 73,845 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Plugins for Hardhat and Foundry to deploy and manage upgradeable contracts on Ethereum.

OpenZeppelin Upgrades

Docs Coverage Status

Integrate upgrades into your existing workflow. Plugins for Hardhat and Truffle to deploy and manage upgradeable contracts on Ethereum.

  • Deploy upgradeable contracts.
  • Upgrade deployed contracts.
  • Manage proxy admin rights.
  • Easily use in tests.

Installation

Hardhat

npm install --save-dev @openzeppelin/hardhat-upgrades
npm install --save-dev @nomicfoundation/hardhat-ethers ethers # peer dependencies
// hardhat.config.js
require('@openzeppelin/hardhat-upgrades');

Truffle

npm install --save-dev @openzeppelin/truffle-upgrades

Usage

See the documentation for each plugin, or take a look at the sample code snippets below.

Hardhat Truffle

Hardhat users will be able to write scripts that use the plugin to deploy or upgrade a contract, and manage proxy admin rights.

const { ethers, upgrades } = require("hardhat");

async function main() {
  // Deploying
  const Box = await ethers.getContractFactory("Box");
  const instance = await upgrades.deployProxy(Box, [42]);
  await instance.waitForDeployment();

  // Upgrading
  const BoxV2 = await ethers.getContractFactory("BoxV2");
  const upgraded = await upgrades.upgradeProxy(await instance.getAddress(), BoxV2);
}

main();

Truffle users will be able to write migrations that use the plugin to deploy or upgrade a contract, or manage proxy admin rights.

const { deployProxy, upgradeProxy } = require('@openzeppelin/truffle-upgrades');

const Box = artifacts.require('Box');
const BoxV2 = artifacts.require('BoxV2');

module.exports = async function (deployer) {
  const instance = await deployProxy(Box, [42], { deployer });
  const upgraded = await upgradeProxy(instance.address, BoxV2, { deployer });
}

Whether you're using Hardhat or Truffle, you can use the plugin in your tests to ensure everything works as expected.

it('works before and after upgrading', async function () {
  const instance = await upgrades.deployProxy(Box, [42]);
  assert.strictEqual(await instance.retrieve(), 42);
  
  await upgrades.upgradeProxy(instance, BoxV2);
  assert.strictEqual(await instance.retrieve(), 42);
});

How do the plugins work?

Both plugins provide functions which take care of managing upgradeable deployments of your contracts.

For example, deployProxy does the following:

  1. Validate that the implementation is upgrade safe

  2. Deploy a proxy admin for your project (if needed)

  3. Check if there is an implementation contract deployed with the same bytecode, and deploy one if not

  4. Create and initialize the proxy contract

And when you call upgradeProxy:

  1. Validate that the new implementation is upgrade safe and is compatible with the previous one

  2. Check if there is an implementation contract deployed with the same bytecode, and deploy one if not

  3. Upgrade the proxy to use the new implementation contract

The plugins will keep track of all the implementation contracts you have deployed in an .openzeppelin folder in the project root, as well as the proxy admin. You will find one file per network there. It is advised that you commit to source control the files for all networks except the development ones (you may see them as .openzeppelin/unknown-*.json).

Note: the format of the files within the .openzeppelin folder is not compatible with those of the OpenZeppelin CLI. If you want to use these plugins for an existing OpenZeppelin CLI project, we will be sharing soon a guide on how to migrate.

Proxy patterns

The plugins support the UUPS, transparent, and beacon proxy patterns. UUPS and transparent proxies are upgraded individually, whereas any number of beacon proxies can be upgraded atomically at the same time by upgrading the beacon that they point to. For more details on the different proxy patterns available, see the documentation for Proxies.

For UUPS and transparent proxies, use deployProxy and upgradeProxy as shown above. For beacon proxies, use deployBeacon, deployBeaconProxy, and upgradeBeacon. See the documentation for Hardhat Upgrades and Truffle Upgrades for examples.

Managing ownership

Transparent proxies define an admin address which has the rights to upgrade them. By default, the admin is a proxy admin contract deployed behind the scenes. You can change the admin of a proxy by calling the admin.changeProxyAdmin function in the plugin. Keep in mind that the admin of a proxy can only upgrade it, but not interact with the implementation contract. Read here for more info on this restriction.

The proxy admin contract also defines an owner address which has the rights to operate it. By default, this address is the externally owned account used during deployment. You can change the proxy admin owner by calling the admin.transferProxyAdminOwnership function in the plugin. Note that changing the proxy admin owner effectively transfers the power to upgrade any proxy in your whole project to the new owner, so use with care. Refer to each plugin documentation for more details on the admin functions.

UUPS and beacon proxies do not use admin addresses. UUPS proxies rely on an _authorizeUpgrade function to be overridden to include access restriction to the upgrade mechanism, whereas beacon proxies are upgradable only by the owner of their corresponding beacon.

Once you have transferred the rights to upgrade a proxy or beacon to another address, you can still use your local setup to validate and deploy the implementation contract. The plugins include a prepareUpgrade function that will validate that the new implementation is upgrade-safe and compatible with the previous one, and deploy it using your local Ethereum account. You can then execute the upgrade itself from the admin or owner address. You can also use the proposeUpgrade function to automatically set up the upgrade in Defender Admin.

Community

Join the OpenZeppelin forum to ask questions or discuss about these plugins, smart contracts upgrades, or anything related to Ethereum development!

License

OpenZeppelin Upgrade plugins are released under the MIT License.

More Repositories

1

openzeppelin-contracts

OpenZeppelin Contracts is a library for secure smart contract development.
JavaScript
24,663
star
2

ethernaut

Web3/Solidity based wargame
JavaScript
1,960
star
3

openzeppelin-contracts-upgradeable

Upgradeable variant of OpenZeppelin Contracts, meant for use in upgradeable contracts.
JavaScript
978
star
4

cairo-contracts

OpenZeppelin Contracts written in Cairo for Starknet, a decentralized ZK Rollup
Rust
821
star
5

awesome-openzeppelin

Blockchain educational resources curated by the OpenZeppelin team
755
star
6

damn-vulnerable-defi

Solidity
654
star
7

solidity-docgen

Documentation generator for Solidity projects
TypeScript
441
star
8

merkle-tree

A JavaScript library to generate merkle trees and merkle proofs.
TypeScript
435
star
9

openzeppelin-sdk

OpenZeppelin SDK repository for CLI and upgrades.js. No longer actively developed.
JavaScript
434
star
10

workshops

Code and slides for OpenZeppelin Workshops
JavaScript
428
star
11

openzeppelin-test-helpers

Assertion library for Ethereum smart contract testing
JavaScript
416
star
12

openzeppelin-labs

A space for the community to interact and exchange ideas on the OpenZeppelin platform. Do not use in production!
JavaScript
374
star
13

nile

CLI tool to develop StarkNet projects written in Cairo
Python
321
star
14

contracts-wizard

Interactive smart contract generator based on OpenZeppelin Contracts.
TypeScript
241
star
15

exploit-uniswap

Exploiting a Uniswap exchange that uses an ERC777 token by leveraging the reentrant microtrading attack vector
JavaScript
171
star
16

openzeppelin-foundry-upgrades

Foundry library for deploying and managing upgradeable contracts
Solidity
157
star
17

openzeppelin-subgraphs

Subgraph schema and templates to index the activity of OpenZeppelin Contracts.
TypeScript
142
star
18

solidity-jwt

Experiments with Solidity JWT. Do not use in production.
Solidity
131
star
19

starter-kit

An OpenZeppelin starter kit containing React, OpenZeppelin SDK & OpenZeppelin Contracts.
JavaScript
121
star
20

token-vesting-ui

UI for TokenVesting contract from OpenZeppelin. No longer maintained
JavaScript
97
star
21

solidity-ast

TypeScript types and a JSON Schema for the Solidity AST
JavaScript
90
star
22

openzeppelin-test-environment

[Not actively maintained] One-line setup for blazing-fast smart contracts tests
Solidity
90
star
23

ctf-2024

⚡️ Ethernaut CTF 2024 Challenges & Solutions
Solidity
85
star
24

rust-contracts-stylus

A library for secure smart contract development written in Rust
Rust
80
star
25

contract-bots-gang

TypeScript
69
star
26

sample-crowdsale-starter

Empty sample starter truffle project for using zeppelin-solidity for a crowdsale
JavaScript
67
star
27

polkadot-runtime-templates

Runtime Templates for Polkadot Parachains
Rust
65
star
28

nile-rs

CLI tool to develop Starknet projects written in Cairo
Rust
56
star
29

defender-client

Monorepo for all defender-client npm packages
TypeScript
55
star
30

defender-autotask-examples

Example snippets for Defender Autotasks
JavaScript
49
star
31

crafty

A collectibles crafting game. Built using the OpenZeppelin SDK.
JavaScript
48
star
32

docs.openzeppelin.com

Source for the OpenZeppelin documentation site
SCSS
45
star
33

openzeppelin-network.js

An easy to use and reliable library that provides one line access to Web3 API.
TypeScript
44
star
34

starter-kit-gsn

An OpenZeppelin starter kit focused on GSN.
JavaScript
40
star
35

openzeppelin-gsn-provider

Web3 provider for the Gas Station Network
JavaScript
39
star
36

starter-kit-tutorial

An OpenZeppelin starter kit tutorial containing React, OpenZeppelin SDK & OpenZeppelin Contracts.
JavaScript
38
star
37

defender-docs

Security Management to Protect the Open Economy
31
star
38

openzeppelin.org

Source code for OpenZeppelin website
23
star
39

defender-templates

Templates for using OpenZeppelin Defender using Serverless configurations
JavaScript
23
star
40

compound-monitoring

JavaScript
22
star
41

openzeppelin-transpiler

TypeScript
22
star
42

gnosis-multisig

Allows multiple parties to agree on transactions before execution. Forked from Gnosis multisig repository.
CSS
22
star
43

openzeppelin-gsn-helpers

Test and development helper methods and scripts for GSN
JavaScript
21
star
44

upgrades-safe-app

TypeScript
20
star
45

configs

Code style guidelines and rules for OpenZeppelin projects
JavaScript
20
star
46

gsn-sample-chat_app

OpenZeppelin starter-kit based GSN tutorial for a Chat App
JavaScript
20
star
47

defender-serverless

Configure a Defender environment via code
TypeScript
20
star
48

openzeppelin-contracts-docs

OpenZeppelin documentation site configuration
JavaScript
19
star
49

defender-sdk

Defender SDK
TypeScript
18
star
50

defender-example-metatx-relay

Example meta-tx relay built using Defender
JavaScript
16
star
51

accesscontrol-explorer

Work in progress explorer for AccessControl roles
Svelte
15
star
52

erc20-onboarding

ERC20 token on-boarding on the OpenZeppelin SDK platform
JavaScript
15
star
53

sample-crosschain-env

Test environments for crosschain operations
Shell
15
star
54

openzeppelin-whitepaper

LaTeX sources for the OpenZeppelin Platform Whitepaper
Makefile
14
star
55

openzeppelin-contract-loader

Load contract ABIs from built artifacts and return contract objects
JavaScript
14
star
56

ethernaut-leaderboard

JavaScript
13
star
57

koba

Deploy Stylus contracts with Solidity constructors
Rust
12
star
58

openzeppelin-team-nft

NFT for OpenZeppelin Team
JavaScript
12
star
59

gsn-site

The Ethereum Gas Station Network Alliance Landing Page and Tools
JavaScript
12
star
60

solidity-loader

Solidity Hot Loader for Starter Kits. Not currently maintained
JavaScript
11
star
61

proxy-explorer

👷 Under construction!
TypeScript
11
star
62

openzeppelin-nile-upgrades

Plugin for Nile to deploy and manage upgradeable contracts on StarkNet.
Python
10
star
63

access-manager-explorer

TypeScript
10
star
64

proposal143

Solidity
10
star
65

compound-re-enable-dsr-proposal

Foundry simulation for Compound Proposal for re-enabling MakerDAO DSR
Solidity
10
star
66

defender-as-code

TypeScript
9
star
67

highlightjs-cairo

JavaScript
8
star
68

gsn-relayer

GSN relayer server, forked from openeth-dev/gsn
Go
8
star
69

web3-gsn-faucet-provider

JavaScript
8
star
70

token-vouching

Vouching contracts and scripts for the OpenZeppelin SDK token
JavaScript
7
star
71

compound-governance-proposal-behavior

Set of tests to reflect the proposal lifecycle behavior
Solidity
7
star
72

zeppelin.solutions

Institutional website
6
star
73

research-cryptography

OpenZeppelin Research group working repository
Jupyter Notebook
6
star
74

openzeppelin-token-registry

OpenZeppelin Ethereum Packages registry
JavaScript
5
star
75

compound-assets-listing

Template repo to define assets listing on Compound
4
star
76

compound-security-policies

Security roles and responsibilities for Compound
4
star
77

fuzzy-import-parser

TypeScript
4
star
78

gsn-tools

Set of tools to manage the GSN
JavaScript
4
star
79

docs-utils

Scripts used for docs previews in pull requests across OpenZeppelin projects
JavaScript
4
star
80

secure-development-cookbook

The essential blueprint for crafting secure protocols
JavaScript
4
star
81

discourse-highlightjs-langs

HTML
3
star
82

sgp

Solidity ANTLR4 grammar Python parser
Python
3
star
83

openzeppelin-upgrades-migration-example

3
star
84

governor-quorum-bot

Forta detection bot to alert about changes in quorum quantity in a Governor contract
TypeScript
3
star
85

ctf-infra

🧱 Infrastructure for Ethernaut CTF 2024
Python
3
star
86

polkadot-evm-runtime-template

EVM runtime template for Polkadot parachains
Rust
3
star
87

defender-serverless-workshop

JavaScript
2
star
88

futureswap-artifacts

Compiled bytecode related to our FutureSwap audits for public viewing
2
star
89

cto.openzeppelin.com

Description of the CTO job opening at OpenZeppelin
HTML
2
star
90

tech-coaching

A place to keep study materials associated with the tech coaching
HTML
2
star
91

slack.openzeppelin.org

https://openzeppelin-slack.netlify.com/ Source for https://slack.openzeppelin.org/
1
star
92

netlify-redirects

Redirection rules for various old websites
1
star
93

defender-subgraphs

Access Control subgraph toolkit for Defender
TypeScript
1
star