This project provides a hands-on demonstration of using upgradeable smart contracts with OpenZeppelin's upgrades plugins in Hardhat. Specifically, it is a smart contract starter project that illustrates the use of both Proxies and Beacon Proxies.
A proxy is a smart contract that serves as an intermediary between the user and the logic contract. It forwards calls and data to the logic contract and then returns the results. The address of the logic contract can be updated, thus allowing changes to the contract's behavior over time without changing the contract's address.
A Beacon Proxy is a proxy that references a Beacon contract to determine its implementation. By updating the Beacon contract, all associated Beacon Proxies will point to the new implementation, allowing simultaneous upgrades of multiple contracts.
This project contains two contracts: Topic
and TopicV2
.
Topic
is a simple contract that enables storing and retrieving a Hedera Topic ID.TopicV2
is an enhanced version ofTopic
that introduces an additional feature: the ability to set a message.
We use OpenZeppelin's upgrades plugins to deploy these contracts as upgradeable contracts.
Before running this project, you need to set up your local node and private key in the .env
file.
ℹ️ Make sure you meet these requirements and docker settings for local node to work
-
Run the hedera local node:
hedera start -d
Note: You can run this in a separate terminal window and keep it running in the background.
-
Add a
ECDSA_PRIVATE_KEY_LOCAL
entry to the.env
file after the local node starts up and generates your accounts. This should be your local node's ECDSA private key, which you would use for signing transactions on the local network.ECDSA_PRIVATE_KEY_LOCAL=your-generated-private-ecdsa-key
-
Install the dependencies:
npm install
-
Compile the contracts:
npx hardhat compile
-
Deploy the
Topic
contract:npx hardhat run scripts/create-topic.ts --network local
This script deploys the
Topic
contract as a proxy contract, initializes it with a Topic ID '0.0.1234', and prints the address of the proxy contract. -
Upgrade the
Topic
contract toTopicV2
:npx hardhat run scripts/upgrade-topic.ts --network local
This script upgrades the
Topic
contract toTopicV2
, sets a new message, and prints the updated message. -
Deploy the
Topic
contract as a Beacon Proxy:npx hardhat run scripts/create-topic-beacon.ts --network local
This script deploys a Beacon and a Beacon Proxy for the
Topic
contract, initializes the Beacon Proxy with a Topic ID '0.0.1234', and prints the addresses of the Beacon and the Beacon Proxy. -
Upgrade the
Topic
contract toTopicV2
for the Beacon Proxy:npx hardhat run scripts/upgrade-topic-beacon.ts --network local
This script upgrades the Beacon to point to
TopicV2
, effectively upgrading all Beacon Proxies. It sets a new message on the Beacon Proxy and prints the updated message.
These scripts illustrate how proxies and beacon proxies allow the contract logic to be upgraded while keeping the contract address constant. The use of beacon proxies also demonstrates the power of simultaneous upgrades across multiple contracts.
To test the contracts, we've provided a testing suite in the test
directory. You can run these tests using the following commands:
-
Compile the contracts (if not already compiled):
npx hardhat compile
-
Run the tests:
npx hardhat test
The test suite uses Waffle for fast, reliable testing of Ethereum contracts. It tests the base functionality of the Topic
and TopicV2
contracts, as well as the upgradeability provided by the proxies.
Contributions are welcome. Please see the contributing guide to see how you can get involved.
This project is governed by the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code of conduct. Please report unacceptable behavior to [email protected]