Deploying to Hardhat Network

Configuration

// File: hardhat.config.ts
import { HardhatUserConfig } from 'hardhat/config'
import '@nomicfoundation/hardhat-toolbox'
const config: HardhatUserConfig = {
solidity: '0.8.18',
networks: {
hardhat: {
chainId: 1,
forking: {
url: 'https://cloudflare-eth.com',
},
},
},
}
export default config

In this tutorial, we will be working with a Hardhat network that is forked from the Ethereum network. To ensure that Relic Protocol functions properly on this network, it is crucial to modify the Hardhat configuration as shown above. By default, this example uses JSON-RPC from Cloudflare, but feel free to use any other RPC of your preference.

Writing Script for Deployment

The deployment script is similar to the deployment fixture that was created for testing purposes. To begin with, we use the Relic Client SDK to retrieve the address of Reliquary contract:

import { ethers } from 'hardhat'
import { RelicClient } from '@relicprotocol/client'
const { addresses } = await RelicClient.fromProvider(ethers.provider)

In addition, we can obtain the required Signer and contract factory for deployment by following statements:

const [deployer] = await ethers.getSigners()
const Token = await ethers.getContractFactory('Token')

Then, the contract can be deployed to the Hardhat network by using the following code:

const token = await Token.deploy(addresses.reliquary)
await token.deployed()

By combining the above codes, you can create a deployment script as shown below:

// File: scripts/deploy.ts
import { ethers, artifacts } from 'hardhat'
import { RelicClient } from '@relicprotocol/client'
async function main() {
const { addresses } = await RelicClient.fromProvider(ethers.provider)
const [deployer] = await ethers.getSigners()
console.log('Deploying contracts with the account: ', deployer.address)
const Token = await ethers.getContractFactory('Token')
const token = await Token.deploy(addresses.reliquary)
await token.deployed()
console.log('Token address: ', token.address)
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error)
process.exitCode = 1
})

Deploying

Before deploying on the Hardhat Network, it is necessary to run a node on your local machine. To do so, execute the following command:

npx hardhat node

After running the node, open a new terminal and execute the deployment script using the following command:

npx hardhat run scripts/deploy.ts --network localhost

Upon successful deployment, the following information will be displayed:

Deploying contracts with the account: ... Token address: ...