OpenZeppelin Defender¶
Introduction¶
OpenZeppelin Defender is a web-based application that allows developers to perform and automate smart contract operations in a secure way. Defender offers different components:
- Admin — to automate and secure all your smart contract operations such as access controls, upgrades, and pausing
- Relay — to build with a private and secure transaction infrastructure with the implementation of private relayers
- Autotasks — to create automated scripts to interact with your smart contracts
- Sentinel — to monitor your smart contract's events, functions, and transactions, and receive notifications via email
OpenZeppelin Defender can now be used on Moonbeam, Moonriver, and the Moonbase Alpha TestNet. This guide will show you how to get started with Defender and demo the Admin component to pause a smart contract on Moonbase Alpha. This guide can be adapted for Moonbeam and Moonriver.
Getting Started with Defender¶
This section goes through the steps for getting started with OpenZeppelin Defender on Moonbase Alpha.
Checking Prerequisites¶
The steps described in this section assume you have MetaMask installed and connected to the Moonbase Alpha TestNet. If you haven't connected MetaMask to the TestNet, check out our MetaMask integration guide.
In addition, you need to sign up for a free OpenZeppelin Defender account, which you can do on the main Defender website.
The contract used in this guide is an extension of the Box.sol
contract used in the upgrading smart contracts guide, from the OpenZeppelin documentation. Also, the contract was made upgradable and pausable to take full advantage of the Admin component. You can deploy your contract using the following code and following the upgrading smart contracts guide:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
contract PausableBox is Initializable, PausableUpgradeable, OwnableUpgradeable {
uint256 private value;
// Emitted when the stored value changes
event ValueChanged(uint256 newValue);
// Initialize
function initialize() initializer public {
__Ownable_init();
__Pausable_init_unchained();
}
// Stores a new value in the contract
function store(uint256 newValue) whenNotPaused public {
value = newValue;
emit ValueChanged(newValue);
}
// Reads the last stored value
function retrieve() public view returns (uint256) {
return value;
}
function pause() public onlyOwner {
_pause();
}
function unpause() public onlyOwner {
_unpause();
}
}
Connecting Defender¶
Once you have an OpenZeppelin Defender account, log into the Defender App. In the main screen, with MetaMask connected to Moonbase Alpha click on the top right corner Connect wallet button:
If successful, you should see your address and a text stating Connected to Moonbase Alpha.
Using the Admin Component¶
This section goes through the steps for getting started with OpenZeppelin Defender Admin component to manage smart contracts on Moonbase Alpha.
Importing your Contract¶
The first step to using Defender Admin is to add the contract you want to manage. To do so, click on the Add contract button near the top right corner. This will take you to the import contract screen, where you need to:
- Set a contract name. This is only for display purposes
- Select the network where the contract that you want to manage is deployed. This is particularly useful when a contract is deployed with the same address to multiple networks. For this example, enter
Moonbase Alpha
- Enter the contract address
- Paste the contract ABI. This can be obtained either in Remix or in the
.json
file generally created after the compilation process (for example, in Truffle or Hardhat) - Check that the contract features were detected correctly
- Once you've checked all the information, click on the Add button
If everything was successfully imported, you should see your contract in the Admin component main screen:
Create a Contract Proposal¶
Proposals are actions to be carried out in the contract. At the time of writing, there are three main proposals/actions that can take place:
- Pause — available if the pause feature is detected. Pauses token transfers, minting and burning
- Upgrade — available if the upgrade feature is detected. Allows for a contract to be upgraded via a proxy contract
- Admin action — call to any function in the managed contract
In this case, a new proposal is created to pause the contract. To do so, take the following steps:
- Click on the New proposal button to see all the available options
- Click on Pause
This will open the proposal page, where all the details regarding the proposal need to be filled in. In this example, you need to provide the following information:
- Admin account address. You can also leave this field empty if you want to run the action from your current wallet (if it has all the necessary permissions)
- Title of the proposal
- Description of the proposal. In here, you should provide as much detail as possible for other members/managers of the contract (if using a MultiSig wallet)
- Click on Create pause proposal
Once the proposal is successfully created, it should be listed in the contract's admin dashboard.
Approve a Contract Proposal¶
With the contract proposal created, the next step is to approve and execute it. To do so, go to the proposal and click on Approve and Execute.
This will initiate a transaction that needs to be signed using MetaMask, after which the proposal state should change to Executed (confirmation pending). Once the transaction is processed, the status should show Executed.
You can also see that the contract's status has changed from Running to Paused. Great! You now know how to use the Admin component to manage your smart contracts.
| Created: April 30, 2021