In hardhat projects layout, we keep the hardhat stuff in its own directory, maybe named contracts. When you created the .json files, take that json file and copy it in a client directory, maybe create a lib folder. You should have this file
client/lin/MyContract.json
to create the contract, we need to get the abi from json data.
// your directory path might be different
// in next.js it is easy importing from .json, you have to handle it differently in webpack
import abi from '../lib/MyContract.json'
Once you get the abi, you used it as abi.abi.
to create a contract write a function
const getEthereumContract = () => {
// instead of ethereum u might be using a different provider
const provider = new ethers.providers.Web3Provider(ethereum)
const signer = provider.getSigner()
// this is the contract
const MyContract = new ethers.Contract(
contractAddress,
abi.abi,
signer,
)
return MyContract
}
You get the contract address when you run the deploy script.
const main = async () => {
const transactionFactory = await hre.ethers.getContractFactory('Transactions')
const transactionContract = await transactionFactory.deploy()
await transactionContract.deployed()
// GET THIS CONTRACT ADDRESS
console.log('Transactions deployed to:', transactionContract.address)
}
;(async () => {
try {
await main()
process.exit(0)
} catch (error) {
console.error(error)
process.exit(1)
}
})()
Finally you should have this to create the contract: