DEPLOY CONTRACTS WITH OPENZEPPELIN DEFENDER RELAYER
Tags: web3, dapp, contracts, security
Published: 2022-03-30 18:11:48

Openzeppelin is one of the reputable libraries in the smart contract ecosystem that provides secured, reusable and quality contracts for development. One of the fastest and safest way to get your contracts deployed, whether to the mainnets or testnets, is to use the Openzeppelin Defender Relayer. 

In this article, you should learn of the processes of shipping faster and secured-laden contracts with the Openzeppelin Defender Relayer. Also, at the end of the article, you should have more than one reason why you should adopt it for your next contract deployment.

## What is a Defender Relayer?

The Openzeppelin Defender Relayer is a generated address that provides you an API key and secret that could stand in place of your wallet addresses. This generated addresses enhance fast deployment and reduces the risk of exploit that occurs during the processes of deployment. Like your wallet addresses, these addresses are capable of sending and receiving ethers and tokens.

## Processes for Contract Deployment

Before diving deep into the processes, it is important to mention that the contracts we will deploy at the end of the processes is only a simple ERC20 token, deployed on the polygon network.

- Create the working environment using [Hardhat](https://hardhat.org/getting-started/#installation) and Ethers JS.

```
mkdir defender-relayer
cd defender-relayer
```
When you have successfully created a folder for the project, install hardhat.
```
npx install hardhat
```

![Hardhat installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sovkjqhj7mos084d2gfx.PNG)

Install the Openzeppelin contracts and the defender-relay-client packages.

```
npm install @openzeppelin/contracts
```

```
npm install defender-relay-client
```

Do note that you can begin your project with yarn if you are fine with it. 

When you have all of these packages installed successfully, open the code on your VS code.

```
code .
```

- Write a simple ERC20 token named Defender Relayer with the symbol ODR.


![ERC20 Token Contract](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wonfrk2932229nra7tw.PNG)

Do ensure to import the Openzeppelin ERC20 token and then inherit it. From the image above, it shows also the import of hardhat console that gives us the liberty to see an output in our terminal. Isn't that amazing? 

Well, that's the simple token contract and our token name is _Defender Relayer_ with the symbol _ODR_.

- Then write the script for deployment.

![Deployment script.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxa5b234ndh00gq5z7m7.PNG)

`Line 1 to 7` deals with importing the required dependencies. `Line 9 to 29` helps with the getting of the contract and deployment. `Line 33 to 38` handles error if there is one.

- Sign up on [Openzeppelin Defender Relayer](https://defender.openzeppelin.com/). After successful registration, click on the relayer to generate an API_KEY and an API_SECRET. These are all what replaces your private key which you would have normally added if you are to deploy using your own development account.

Ensure that you take note of the API key and secret. Fund the address with the token or coin you intend to interact with. During the course of creating your relayer, ensure to select the network you want to deploy on. Copy and paste it in your `.env` file. Something similar to this below:

```
API_KEY=xxxxxxxxxxxxxxxxx
API_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```

- Get a RPC_URL from either [Moralis](https://admin.moralis.io/), [Alchemy](https://dashboard.alchemyapi.io/), or [Infura](https://infura.io/). For me, I used Moralis but anyone works fine.

If you are very much familiar with the usage of any of these node provider, you sure will understand the importance of the tool. If you are not, do kindly go through their site and documentation. The guide for usage is simple and clear.

On getting thee network you intend to deploy on, add it to your `.env` file and set your hardhat config.

```
MUMBAI_URL=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```
For the network configuration, check out the hardhat documentation.

- After completion of the set-up, it is now time to deploy our script with the defender relayer serving as the deployer of the contract.

```
npx hardhat run scripts/deploy.js --network mumbai
```

Viola! The deployment is successful. You can therefore check your relayer account, particularly the address you created, it will have a list of transaction that has occurred with the address.

## Why Adopt the Usage of Openzeppelin Defender Relayer?

There have been, more than we can count, cases of exploits due to the lapses of developers to properly securing their private keys during development. Aside from these overwhelming lapses, there are more technical processes involved in deploying with your private keys as the principal deployer.

What then are makes Openzeppelin defender relayer special?

- **Safety**

This tool has shielded developers from losing funds and their other properties during the course of development.

- **Fast**

Deploying with Openzeppelin Defender Relayer is fast. If you encounter errors during the course of deployment, its error messages are clear and straight to point. 


Do enjoy the easy and quick way of deployment!