This repository contains an example of how to deploy and interact with upgradeable contracts. Specifically, we leverage the OpenZeppelin Upgrades plugin to:
- Deploy an implementation token contract (v1)
- Deploy a Proxy contract pointing to the v1 contract
- Deploy an upgrade token contract (v2)
- Upgrade the Proxy contract to point to the v2 implementation
- Node
v20.11.0or greater - npm
- Wallet with Berachain Testnet $BERA tokens - See the Berachain bArtio Faucet
- Foundry - ensure
foundryupis run to install binaries
Copy the .env.example file to .env
# FROM: ./openzeppelin-upgrades
cp .env.example .env;Populate the .env file with your PK and then load the variable into your shell:
# FROM: ./openzeppelin-upgrades
source .env;Install dependencies by running:
forge install OpenZeppelin/openzeppelin-contracts openzeppelin-contracts-upgradeable OpenZeppelin/openzeppelin-foundry-upgrades foundry-rs/forge-std --no-commit --no-git;Start by compiling the contracts:
# FROM: ./openzeppelin-upgrades
forge build;Next, run the deployment script script/DeployProxy.s.sol (we pin the Solidity version for consistency):
# FROM: ./openzeppelin-upgrades
forge script script/DeployProxy.s.sol --broadcast --rpc-url https://bartio.rpc.berachain.com/ --private-key $PK --use 0.8.25;
Take note of your Proxy contract address.

# FROM: ./openzeppelin-upgrades
forge verify-contract <IMPLEMENTATION_ADDRESS> ./src/DeFiTokenV1.sol:DeFiToken --verifier-url 'https://api.routescan.io/v2/network/testnet/evm/80084/etherscan' --etherscan-api-key "verifyContract" --num-of-optimizations 200 --compiler-version 0.8.25;
Now, when you navigate to your proxy contract on Beratrail, you will see the ERC20 token attributes hooked up to the proxy contract.
Next, we upgrade the implementation contract to v2 (DeFiTokenV2.sol).
Clean the build artifacts and then run the upgrade script:
# FROM: ./openzeppelin-upgrades
forge clean;
forge script script/DeployUpgrade.s.sol --broadcast --rpc-url https://bartio.rpc.berachain.com/ --private-key $PK --use 0.8.25;Now, when checking your proxy contract on Beratrail explorer, you will notice the token name has been updated.
