Using with Upgrades - OpenZeppelin Docs (2024)

If your contract is going to be deployed with upgradeability, such as using the OpenZeppelin Upgrades Plugins, you will need to use the Upgrade Safe variant of OpenZeppelin Contracts.

This variant is available as a separate package called @openzeppelin/contracts-upgradeable, which is hosted in the repository OpenZeppelin/openzeppelin-contracts-upgradeable.

It follows all of the rules for Writing Upgradeable Contracts: constructors are replaced by initializer functions, state variables are initialized in initializer functions, and we additionally check for storage incompatibilities across minor versions.

Overview

Installation

$ npm install @openzeppelin/contracts-upgradeable

Usage

The package replicates the structure of the main OpenZeppelin Contracts package, but every file and contract has the suffix Upgradeable.

-import "@openzeppelin/contracts/token/ERC721/ERC721.sol";+import "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol";-contract MyCollectible is ERC721 {+contract MyCollectible is ERC721Upgradeable {

Constructors are replaced by internal initializer functions following the naming convention __{ContractName}_init. Since these are internal, you must always define your own public initializer function and call the parent initializer of the contract you extend.

- constructor() ERC721("MyCollectible", "MCO") public {+ function initialize() initializer public {+ __ERC721_init("MyCollectible", "MCO"); }
Use with multiple inheritance requires special attention. See the section below titled Multiple Inheritance.

Once this contract is set up and compiled, you can deploy it using the Upgrades Plugins. The following snippet shows an example deployment script using Hardhat.

// scripts/deploy-my-collectible.jsconst { ethers, upgrades } = require("hardhat");async function main() { const MyCollectible = await ethers.getContractFactory("MyCollectible"); const mc = await upgrades.deployProxy(MyCollectible); await mc.deployed(); console.log("MyCollectible deployed to:", mc.address);}main();

Further Notes

Multiple Inheritance

Initializer functions are not linearized by the compiler like constructors. Because of this, each __{ContractName}_init function embeds the linearized calls to all parent initializers. As a consequence, calling two of these init functions can potentially initialize the same contract twice.

The function __{ContractName}_init_unchained found in every contract is the initializer function minus the calls to parent initializers, and can be used to avoid the double initialization problem, but doing this manually is not recommended. We hope to be able to implement safety checks for this in future versions of the Upgrades Plugins.

Storage Gaps

You may notice that every contract includes a state variable named __gap. This is empty reserved space in storage that is put in place in Upgrade Safe contracts. It allows us to freely add new state variables in the future without compromising the storage compatibility with existing deployments.

It isn’t safe to simply add a state variable because it "shifts down" all of the state variables below in the inheritance chain. This makes the storage layouts incompatible, as explained in Writing Upgradeable Contracts. The size of the __gap array is calculated so that the amount of storage used by a contract always adds up to the same number (in this case 50 storage slots).

Using with Upgrades - OpenZeppelin Docs (2024)
Top Articles
Top 20 weakest currencies in the world | CurrencyTransfer
GCF of 20 and 25 | How to Find GCF of 20, 25?
Devotion Showtimes Near Xscape Theatres Blankenbaker 16
Kathleen Hixson Leaked
Wordscapes Level 6030
Access-A-Ride – ACCESS NYC
Phone Number For Walmart Automotive Department
Kobold Beast Tribe Guide and Rewards
Holly Ranch Aussie Farm
DL1678 (DAL1678) Delta Historial y rastreo de vuelos - FlightAware
Nordstrom Rack Glendale Photos
Encore Atlanta Cheer Competition
Www Thechristhospital Billpay
Waive Upgrade Fee
biBERK Business Insurance Provides Essential Insights on Liquor Store Risk Management and Insurance Considerations
Premier Boating Center Conroe
Love Compatibility Test / Calculator by Horoscope | MyAstrology
Leeks — A Dirty Little Secret (Ingredient)
Huge Boobs Images
Daily Voice Tarrytown
Skyward Login Jennings County
CANNABIS ONLINE DISPENSARY Promo Code — $100 Off 2024
Hdmovie 2
Hdmovie2 Sbs
Robeson County Mugshots 2022
Diakimeko Leaks
Airline Reception Meaning
Bj's Tires Near Me
Kelley Fliehler Wikipedia
Hypixel Skyblock Dyes
The Wichita Beacon from Wichita, Kansas
Uhaul Park Merced
Covalen hiring Ai Annotator - Dutch , Finnish, Japanese , Polish , Swedish in Dublin, County Dublin, Ireland | LinkedIn
11 Pm Pst
Naya Padkar Newspaper Today
Crystal Mcbooty
Why Gas Prices Are So High (Published 2022)
Plead Irksomely Crossword
T&Cs | Hollywood Bowl
968 woorden beginnen met kruis
Lyndie Irons And Pat Tenore
Foxxequeen
Booknet.com Contract Marriage 2
Satucket Lectionary
Academic Notice and Subject to Dismissal
Amy Zais Obituary
Canada Life Insurance Comparison Ivari Vs Sun Life
Neil Young - Sugar Mountain (2008) - MusicMeter.nl
Yosemite Sam Hood Ornament
Pronósticos Gulfstream Park Nicoletti
View From My Seat Madison Square Garden
Grace Charis Shagmag
Latest Posts
Article information

Author: Annamae Dooley

Last Updated:

Views: 5791

Rating: 4.4 / 5 (65 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Annamae Dooley

Birthday: 2001-07-26

Address: 9687 Tambra Meadow, Bradleyhaven, TN 53219

Phone: +9316045904039

Job: Future Coordinator

Hobby: Archery, Couponing, Poi, Kite flying, Knitting, Rappelling, Baseball

Introduction: My name is Annamae Dooley, I am a witty, quaint, lovely, clever, rich, sparkling, powerful person who loves writing and wants to share my knowledge and understanding with you.