OpenZeppelin Foundry Upgrades API

Contract name formats

Contract names must be provided in specific formats depending on context. The following are the required formats for each context:

Foundry artifact format

Contexts:

  • contractName parameter
  • referenceContract option if referenceBuildInfoDir option is not set

Can be in any of the following forms according to Foundry’s getCode cheatcode:

  • the Solidity file name, e.g. ContractV1.sol
  • the Solidity file name and the contract name, e.g. ContractV1.sol:ContractV1
  • the artifact path relative to the project root directory, e.g. out/ContractV1.sol/ContractV1.json

Annotation format

Contexts:

  • @custom:oz-upgrades-from <reference> annotation
  • referenceContract option if referenceBuildInfoDir option is set

Can be in any of the following forms according to the OpenZeppelin Upgrades CLI:

  • the contract name, e.g. ContractV1
  • fully qualified contract name, e.g. contracts/tokens/ContractV1.sol:ContractV1

If the referenceBuildInfoDir option is set, include the build info directory short name as a prefix, resulting in one of the following forms:

  • the build info directory short name and the contract name, e.g. build-info-v1:ContractV1
  • the build info directory short name and the fully qualified contract name, e.g. build-info-v1:contracts/tokens/ContractV1.sol:ContractV1

Common Options

The following options can be used with some of the below functions. See Options.sol for detailed descriptions of each option.

Options πŸ“

import "@openzeppelin/src/Options.sol";

DefenderOptions πŸ“

import "@openzeppelin/src/Options.sol";

TxOverrides πŸ“

import "@openzeppelin/src/Options.sol";

Upgrades.sol

Upgrades πŸ“

import "@openzeppelin/src/Upgrades.sol";

Library for deploying and managing upgradeable contracts from Forge scripts or tests.

NOTE: Requires OpenZeppelin Contracts v5 or higher.

Functions

deployUUPSProxy(string contractName, bytes initializerData, struct Options opts) β†’ address

internal

Deploys a UUPS proxy using the given contract as the implementation.

deployUUPSProxy(string contractName, bytes initializerData) β†’ address

internal

Deploys a UUPS proxy using the given contract as the implementation.

deployTransparentProxy(string contractName, address initialOwner, bytes initializerData, struct Options opts) β†’ address

internal

Deploys a transparent proxy using the given contract as the implementation.

deployTransparentProxy(string contractName, address initialOwner, bytes initializerData) β†’ address

internal

Deploys a transparent proxy using the given contract as the implementation.

upgradeProxy(address proxy, string contractName, bytes data, struct Options opts)

internal

Upgrades a proxy to a new implementation contract. Only supported for UUPS or transparent proxies.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

upgradeProxy(address proxy, string contractName, bytes data)

internal

Upgrades a proxy to a new implementation contract. Only supported for UUPS or transparent proxies.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

upgradeProxy(address proxy, string contractName, bytes data, struct Options opts, address tryCaller)

internal

Upgrades a proxy to a new implementation contract. Only supported for UUPS or transparent proxies.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

This function provides an additional tryCaller parameter to test an upgrade using a specific caller address. Use this if you encounter OwnableUnauthorizedAccount errors in your tests.

upgradeProxy(address proxy, string contractName, bytes data, address tryCaller)

internal

Upgrades a proxy to a new implementation contract. Only supported for UUPS or transparent proxies.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

This function provides an additional tryCaller parameter to test an upgrade using a specific caller address. Use this if you encounter OwnableUnauthorizedAccount errors in your tests.

deployBeacon(string contractName, address initialOwner, struct Options opts) β†’ address

internal

Deploys an upgradeable beacon using the given contract as the implementation.

deployBeacon(string contractName, address initialOwner) β†’ address

internal

Deploys an upgradeable beacon using the given contract as the implementation.

upgradeBeacon(address beacon, string contractName, struct Options opts)

internal

Upgrades a beacon to a new implementation contract.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

upgradeBeacon(address beacon, string contractName)

internal

Upgrades a beacon to a new implementation contract.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

upgradeBeacon(address beacon, string contractName, struct Options opts, address tryCaller)

internal

Upgrades a beacon to a new implementation contract.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

This function provides an additional tryCaller parameter to test an upgrade using a specific caller address. Use this if you encounter OwnableUnauthorizedAccount errors in your tests.

upgradeBeacon(address beacon, string contractName, address tryCaller)

internal

Upgrades a beacon to a new implementation contract.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

This function provides an additional tryCaller parameter to test an upgrade using a specific caller address. Use this if you encounter OwnableUnauthorizedAccount errors in your tests.

deployBeaconProxy(address beacon, bytes data) β†’ address

internal

Deploys a beacon proxy using the given beacon and call data.

deployBeaconProxy(address beacon, bytes data, struct Options opts) β†’ address

internal

Deploys a beacon proxy using the given beacon and call data.

validateImplementation(string contractName, struct Options opts)

internal

Validates an implementation contract, but does not deploy it.

deployImplementation(string contractName, struct Options opts) β†’ address

internal

Validates and deploys an implementation contract, and returns its address.

validateUpgrade(string contractName, struct Options opts)

internal

Validates a new implementation contract in comparison with a reference contract, but does not deploy it.

Requires that either the referenceContract option is set, or the contract has a @custom:oz-upgrades-from <reference> annotation.

prepareUpgrade(string contractName, struct Options opts) β†’ address

internal

Validates a new implementation contract in comparison with a reference contract, deploys the new implementation contract, and returns its address.

Requires that either the referenceContract option is set, or the contract has a @custom:oz-upgrades-from <reference> annotation.

Use this method to prepare an upgrade to be run from an admin address you do not control directly or cannot use from your deployment environment.

getAdminAddress(address proxy) β†’ address

internal

Gets the admin address of a transparent proxy from its ERC1967 admin storage slot.

getImplementationAddress(address proxy) β†’ address

internal

Gets the implementation address of a transparent or UUPS proxy from its ERC1967 implementation storage slot.

getBeaconAddress(address proxy) β†’ address

internal

Gets the beacon address of a beacon proxy from its ERC1967 beacon storage slot.

UnsafeUpgrades πŸ“

import "@openzeppelin/src/Upgrades.sol";

Library for deploying and managing upgradeable contracts from Forge tests, without validations.

Can be used with forge coverage. Requires implementation contracts to be instantiated first. Does not require --ffi and does not require a clean compilation before each run.

Not supported for OpenZeppelin Defender deployments.

WARNING: Not recommended for use in Forge scripts. UnsafeUpgrades does not validate whether your contracts are upgrade safe or whether new implementations are compatible with previous ones. Use Upgrades if you want validations to be run.

NOTE: Requires OpenZeppelin Contracts v5 or higher.

Functions

deployUUPSProxy(address impl, bytes initializerData) β†’ address

internal

Deploys a UUPS proxy using the given contract address as the implementation.

deployTransparentProxy(address impl, address initialOwner, bytes initializerData) β†’ address

internal

Deploys a transparent proxy using the given contract address as the implementation.

upgradeProxy(address proxy, address newImpl, bytes data)

internal

Upgrades a proxy to a new implementation contract address. Only supported for UUPS or transparent proxies.

upgradeProxy(address proxy, address newImpl, bytes data, address tryCaller)

internal

Upgrades a proxy to a new implementation contract address. Only supported for UUPS or transparent proxies.

This function provides an additional tryCaller parameter to test an upgrade using a specific caller address. Use this if you encounter OwnableUnauthorizedAccount errors in your tests.

deployBeacon(address impl, address initialOwner) β†’ address

internal

Deploys an upgradeable beacon using the given contract address as the implementation.

upgradeBeacon(address beacon, address newImpl)

internal

Upgrades a beacon to a new implementation contract address.

upgradeBeacon(address beacon, address newImpl, address tryCaller)

internal

Upgrades a beacon to a new implementation contract address.

This function provides an additional tryCaller parameter to test an upgrade using a specific caller address. Use this if you encounter OwnableUnauthorizedAccount errors in your tests.

deployBeaconProxy(address beacon, bytes data) β†’ address

internal

Deploys a beacon proxy using the given beacon and call data.

getAdminAddress(address proxy) β†’ address

internal

Gets the admin address of a transparent proxy from its ERC1967 admin storage slot.

getImplementationAddress(address proxy) β†’ address

internal

Gets the implementation address of a transparent or UUPS proxy from its ERC1967 implementation storage slot.

getBeaconAddress(address proxy) β†’ address

internal

Gets the beacon address of a beacon proxy from its ERC1967 beacon storage slot.

LegacyUpgrades.sol

import "@openzeppelin/src/LegacyUpgrades.sol";

Library for managing upgradeable contracts from Forge scripts or tests.

NOTE: Only for upgrading existing deployments using OpenZeppelin Contracts v4. For new deployments, use OpenZeppelin Contracts v5 and Upgrades.sol.

Functions

upgradeProxy(address proxy, string contractName, bytes data, struct Options opts)

internal

Upgrades a proxy to a new implementation contract. Only supported for UUPS or transparent proxies.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

upgradeProxy(address proxy, string contractName, bytes data)

internal

Upgrades a proxy to a new implementation contract. Only supported for UUPS or transparent proxies.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

upgradeProxy(address proxy, string contractName, bytes data, struct Options opts, address tryCaller)

internal

Upgrades a proxy to a new implementation contract. Only supported for UUPS or transparent proxies.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

This function provides an additional tryCaller parameter to test an upgrade using a specific caller address. Use this if you encounter OwnableUnauthorizedAccount errors in your tests.

upgradeProxy(address proxy, string contractName, bytes data, address tryCaller)

internal

Upgrades a proxy to a new implementation contract. Only supported for UUPS or transparent proxies.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

This function provides an additional tryCaller parameter to test an upgrade using a specific caller address. Use this if you encounter OwnableUnauthorizedAccount errors in your tests.

upgradeBeacon(address beacon, string contractName, struct Options opts)

internal

Upgrades a beacon to a new implementation contract.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

upgradeBeacon(address beacon, string contractName)

internal

Upgrades a beacon to a new implementation contract.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

upgradeBeacon(address beacon, string contractName, struct Options opts, address tryCaller)

internal

Upgrades a beacon to a new implementation contract.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

This function provides an additional tryCaller parameter to test an upgrade using a specific caller address. Use this if you encounter OwnableUnauthorizedAccount errors in your tests.

upgradeBeacon(address beacon, string contractName, address tryCaller)

internal

Upgrades a beacon to a new implementation contract.

Requires that either the referenceContract option is set, or the new implementation contract has a @custom:oz-upgrades-from <reference> annotation.

This function provides an additional tryCaller parameter to test an upgrade using a specific caller address. Use this if you encounter OwnableUnauthorizedAccount errors in your tests.

validateUpgrade(string contractName, struct Options opts)

internal

Validates a new implementation contract in comparison with a reference contract, but does not deploy it.

Requires that either the referenceContract option is set, or the contract has a @custom:oz-upgrades-from <reference> annotation.

prepareUpgrade(string contractName, struct Options opts) β†’ address

internal

Validates a new implementation contract in comparison with a reference contract, deploys the new implementation contract, and returns its address.

Requires that either the referenceContract option is set, or the contract has a @custom:oz-upgrades-from <reference> annotation.

Use this method to prepare an upgrade to be run from an admin address you do not control directly or cannot use from your deployment environment.

getAdminAddress(address proxy) β†’ address

internal

Gets the admin address of a transparent proxy from its ERC1967 admin storage slot.

getImplementationAddress(address proxy) β†’ address

internal

Gets the implementation address of a transparent or UUPS proxy from its ERC1967 implementation storage slot.

getBeaconAddress(address proxy) β†’ address

internal

Gets the beacon address of a beacon proxy from its ERC1967 beacon storage slot.

UnsafeUpgrades πŸ“

import "@openzeppelin/src/LegacyUpgrades.sol";

Library for managing upgradeable contracts from Forge tests, without validations.

Can be used with forge coverage. Requires implementation contracts to be instantiated first. Does not require --ffi and does not require a clean compilation before each run.

Not supported for OpenZeppelin Defender deployments.

WARNING: Not recommended for use in Forge scripts. UnsafeUpgrades does not validate whether your contracts are upgrade safe or whether new implementations are compatible with previous ones. Use Upgrades if you want validations to be run.

NOTE: Only for upgrading existing deployments using OpenZeppelin Contracts v4. For new deployments, use OpenZeppelin Contracts v5 and Upgrades.sol.

Functions

upgradeProxy(address proxy, address newImpl, bytes data)

internal

Upgrades a proxy to a new implementation contract address. Only supported for UUPS or transparent proxies.

upgradeProxy(address proxy, address newImpl, bytes data, address tryCaller)

internal

Upgrades a proxy to a new implementation contract address. Only supported for UUPS or transparent proxies.

This function provides an additional tryCaller parameter to test an upgrade using a specific caller address. Use this if you encounter OwnableUnauthorizedAccount errors in your tests.

upgradeBeacon(address beacon, address newImpl)

internal

Upgrades a beacon to a new implementation contract address.

upgradeBeacon(address beacon, address newImpl, address tryCaller)

internal

Upgrades a beacon to a new implementation contract address.

This function provides an additional tryCaller parameter to test an upgrade using a specific caller address. Use this if you encounter OwnableUnauthorizedAccount errors in your tests.

getAdminAddress(address proxy) β†’ address

internal

Gets the admin address of a transparent proxy from its ERC1967 admin storage slot.

getImplementationAddress(address proxy) β†’ address

internal

Gets the implementation address of a transparent or UUPS proxy from its ERC1967 implementation storage slot.

getBeaconAddress(address proxy) β†’ address

internal

Gets the beacon address of a beacon proxy from its ERC1967 beacon storage slot.

Defender.sol

Defender πŸ“

import "@openzeppelin/src/Defender.sol";

Library for interacting with OpenZeppelin Defender from Forge scripts or tests.

Functions

deployContract(string contractName) β†’ address

internal

Deploys a contract to the current network using OpenZeppelin Defender.

WARNING: Do not use this function directly if you are deploying an upgradeable contract. This function does not validate whether the contract is upgrade safe.

NOTE: If using an EOA or Safe to deploy, go to Defender deploy to submit the pending deployment while the script is running. The script waits for the deployment to complete before it continues.

deployContract(string contractName, struct DefenderOptions defenderOpts) β†’ address

internal

Deploys a contract to the current network using OpenZeppelin Defender.

WARNING: Do not use this function directly if you are deploying an upgradeable contract. This function does not validate whether the contract is upgrade safe.

NOTE: If using an EOA or Safe to deploy, go to Defender deploy to submit the pending deployment while the script is running. The script waits for the deployment to complete before it continues.

deployContract(string contractName, bytes constructorData) β†’ address

internal

Deploys a contract with constructor arguments to the current network using OpenZeppelin Defender.

WARNING: Do not use this function directly if you are deploying an upgradeable contract. This function does not validate whether the contract is upgrade safe.

NOTE: If using an EOA or Safe to deploy, go to Defender deploy to submit the pending deployment while the script is running. The script waits for the deployment to complete before it continues.

deployContract(string contractName, bytes constructorData, struct DefenderOptions defenderOpts) β†’ address

internal

Deploys a contract with constructor arguments to the current network using OpenZeppelin Defender.

WARNING: Do not use this function directly if you are deploying an upgradeable contract. This function does not validate whether the contract is upgrade safe.

NOTE: If using an EOA or Safe to deploy, go to Defender deploy to submit the pending deployment while the script is running. The script waits for the deployment to complete before it continues.

proposeUpgrade(address proxyAddress, string newImplementationContractName, struct Options opts) β†’ struct ProposeUpgradeResponse

internal

Proposes an upgrade to an upgradeable proxy using OpenZeppelin Defender.

This function validates a new implementation contract in comparison with a reference contract, deploys the new implementation contract using Defender, and proposes an upgrade to the new implementation contract using an upgrade approval process on Defender.

Supported for UUPS or Transparent proxies. Not currently supported for beacon proxies or beacons. For beacons, use Upgrades.prepareUpgrade along with a transaction proposal on Defender to upgrade the beacon to the deployed implementation.

Requires that either the referenceContract option is set, or the contract has a @custom:oz-upgrades-from <reference> annotation.

WARNING: Ensure that the reference contract is the same as the current implementation contract that the proxy is pointing to. This function does not validate that the reference contract is the current implementation.

NOTE: If using an EOA or Safe to deploy, go to Defender deploy to submit the pending deployment of the new implementation contract while the script is running. The script waits for the deployment to complete before it continues.

getDeployApprovalProcess() β†’ struct ApprovalProcessResponse

internal

Gets the default deploy approval process configured for your deployment environment on OpenZeppelin Defender.

getUpgradeApprovalProcess() β†’ struct ApprovalProcessResponse

internal

Gets the default upgrade approval process configured for your deployment environment on OpenZeppelin Defender. For example, this is useful for determining the default multisig wallet that you can use in your scripts to assign as the owner of your proxy.

ProposeUpgradeResponse πŸ“

import "@openzeppelin/src/Defender.sol";

ApprovalProcessResponse πŸ“

import "@openzeppelin/src/Defender.sol";

On this page

Contract name formatsFoundry artifact formatAnnotation formatCommon OptionsOptions πŸ“DefenderOptions πŸ“TxOverrides πŸ“Upgrades.solUpgrades πŸ“FunctionsdeployUUPSProxy(string contractName, bytes initializerData, struct Options opts) β†’ addressdeployUUPSProxy(string contractName, bytes initializerData) β†’ addressdeployTransparentProxy(string contractName, address initialOwner, bytes initializerData, struct Options opts) β†’ addressdeployTransparentProxy(string contractName, address initialOwner, bytes initializerData) β†’ addressupgradeProxy(address proxy, string contractName, bytes data, struct Options opts)upgradeProxy(address proxy, string contractName, bytes data)upgradeProxy(address proxy, string contractName, bytes data, struct Options opts, address tryCaller)upgradeProxy(address proxy, string contractName, bytes data, address tryCaller)deployBeacon(string contractName, address initialOwner, struct Options opts) β†’ addressdeployBeacon(string contractName, address initialOwner) β†’ addressupgradeBeacon(address beacon, string contractName, struct Options opts)upgradeBeacon(address beacon, string contractName)upgradeBeacon(address beacon, string contractName, struct Options opts, address tryCaller)upgradeBeacon(address beacon, string contractName, address tryCaller)deployBeaconProxy(address beacon, bytes data) β†’ addressdeployBeaconProxy(address beacon, bytes data, struct Options opts) β†’ addressvalidateImplementation(string contractName, struct Options opts)deployImplementation(string contractName, struct Options opts) β†’ addressvalidateUpgrade(string contractName, struct Options opts)prepareUpgrade(string contractName, struct Options opts) β†’ addressgetAdminAddress(address proxy) β†’ addressgetImplementationAddress(address proxy) β†’ addressgetBeaconAddress(address proxy) β†’ addressUnsafeUpgrades πŸ“FunctionsdeployUUPSProxy(address impl, bytes initializerData) β†’ addressdeployTransparentProxy(address impl, address initialOwner, bytes initializerData) β†’ addressupgradeProxy(address proxy, address newImpl, bytes data)upgradeProxy(address proxy, address newImpl, bytes data, address tryCaller)deployBeacon(address impl, address initialOwner) β†’ addressupgradeBeacon(address beacon, address newImpl)upgradeBeacon(address beacon, address newImpl, address tryCaller)deployBeaconProxy(address beacon, bytes data) β†’ addressgetAdminAddress(address proxy) β†’ addressgetImplementationAddress(address proxy) β†’ addressgetBeaconAddress(address proxy) β†’ addressLegacyUpgrades.solFunctionsupgradeProxy(address proxy, string contractName, bytes data, struct Options opts)upgradeProxy(address proxy, string contractName, bytes data)upgradeProxy(address proxy, string contractName, bytes data, struct Options opts, address tryCaller)upgradeProxy(address proxy, string contractName, bytes data, address tryCaller)upgradeBeacon(address beacon, string contractName, struct Options opts)upgradeBeacon(address beacon, string contractName)upgradeBeacon(address beacon, string contractName, struct Options opts, address tryCaller)upgradeBeacon(address beacon, string contractName, address tryCaller)validateUpgrade(string contractName, struct Options opts)prepareUpgrade(string contractName, struct Options opts) β†’ addressgetAdminAddress(address proxy) β†’ addressgetImplementationAddress(address proxy) β†’ addressgetBeaconAddress(address proxy) β†’ addressUnsafeUpgrades πŸ“FunctionsupgradeProxy(address proxy, address newImpl, bytes data)upgradeProxy(address proxy, address newImpl, bytes data, address tryCaller)upgradeBeacon(address beacon, address newImpl)upgradeBeacon(address beacon, address newImpl, address tryCaller)getAdminAddress(address proxy) β†’ addressgetImplementationAddress(address proxy) β†’ addressgetBeaconAddress(address proxy) β†’ addressDefender.solDefender πŸ“FunctionsdeployContract(string contractName) β†’ addressdeployContract(string contractName, struct DefenderOptions defenderOpts) β†’ addressdeployContract(string contractName, bytes constructorData) β†’ addressdeployContract(string contractName, bytes constructorData, struct DefenderOptions defenderOpts) β†’ addressproposeUpgrade(address proxyAddress, string newImplementationContractName, struct Options opts) β†’ struct ProposeUpgradeResponsegetDeployApprovalProcess() β†’ struct ApprovalProcessResponsegetUpgradeApprovalProcess() β†’ struct ApprovalProcessResponseProposeUpgradeResponse πŸ“ApprovalProcessResponse πŸ“