From acd9aca86cfc0f4bd19e8383b565cbdd4cef7073 Mon Sep 17 00:00:00 2001 From: Jovells <35954298+Jovells@users.noreply.github.com> Date: Wed, 1 Nov 2023 16:32:25 +0000 Subject: [PATCH] Writing script to generate abis (#10) * Finished Testing Mentor Token * Created script to generate abi's upon deployment --- blockchain/scripts/deploy.ts | 13 +++++++- blockchain/scripts/generateAbis.js | 48 ++++++++++++++++++++++++++++++ blockchain/test/MentorToken.ts | 26 ++++++++++++++++ frontend/nextjs/.gitignore | 3 ++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 blockchain/scripts/generateAbis.js diff --git a/blockchain/scripts/deploy.ts b/blockchain/scripts/deploy.ts index 7d49934..1c85c45 100644 --- a/blockchain/scripts/deploy.ts +++ b/blockchain/scripts/deploy.ts @@ -1,6 +1,8 @@ import { ethers } from "hardhat"; import dotenv from "dotenv"; +const generateAbis = require('./generateAbis.js'); + dotenv.config(); async function main() { @@ -9,6 +11,7 @@ async function main() { // Deploy Marketplace Contract const emtMarketplace = await ethers.deployContract("EMTMarketplace"); emtMarketplace.waitForDeployment(); + // emtMarketplace.deploymentTransaction()?.chainId console.log("EMT Marketplace deployed at: ", emtMarketplace.target); const mentorToken = await ethers.deployContract("MentorToken", [ @@ -17,13 +20,21 @@ async function main() { ]); await mentorToken.waitForDeployment(); console.log("Mentor Token deployed at: ", mentorToken.target); - + const expertToken = await ethers.deployContract("ExpertToken", [ process.env.TOKEN_DEFAULT_ADMIN || owner.address, process.env.TOKEN_MINTER || emtMarketplace.target, ]); await expertToken.waitForDeployment(); console.log("Expert Token deployed at: ", expertToken.target); + + const networkname = (await ethers.provider.getNetwork())?.name + //Generate files containining Abis and contract addresses for use in frontend + generateAbis(networkname, { + EMTMarketplace: emtMarketplace.target, + MentorToken: mentorToken.target, + ExpertToken: expertToken.target, + }) } // We recommend this pattern to be able to use async/await everywhere diff --git a/blockchain/scripts/generateAbis.js b/blockchain/scripts/generateAbis.js new file mode 100644 index 0000000..c605912 --- /dev/null +++ b/blockchain/scripts/generateAbis.js @@ -0,0 +1,48 @@ +const fs = require('fs'); +const path = require('path'); + +console.log('cwd', process.cwd(), 'dirname', __dirname) + +const contractsDirectory = path.join(__dirname, '../artifacts/contracts'); +console.log('contractsDirectory', contractsDirectory) + +const generateAbis = (networkName, contractAddresses = { + EMTMarketplace: "0x5FbDB2315678afecb367f032d93F642f64180aa3" +}) => { + const outputDirectory = path.join(__dirname, '../../frontend/nextjs/src/deployments/'+networkName); + fs.readdirSync(contractsDirectory).forEach((contractFolderName) => { + const contractFilePath = path.join(contractsDirectory, contractFolderName); + const contractName = contractFolderName.replace(".sol", ""); + + if (fs.statSync(contractFilePath).isDirectory()) { + + const contractJsonPath = path.join(contractFilePath, contractName + ".json"); + + if (fs.existsSync(contractJsonPath)) { + const contractData = require(contractJsonPath); + + if (contractData && contractData.abi) { + const abiContent = JSON.stringify(contractData.abi, null, 2); + const outputFile = path.join(outputDirectory, `${contractName}.js`); + + if (!fs.existsSync(outputDirectory)){ + fs.mkdirSync(outputDirectory, { recursive: true }); + } + + fs.writeFileSync(outputFile, `export default { +${contractAddresses?.[contractName] ? 'address: ' + '"' + contractAddresses[contractName] + '",' : ""} +abi: ${abiContent} +};`, 'utf-8'); + console.log(`Generated ${outputFile}`); + } else { + console.error(`Contract JSON file for ${contractName} is missing ABI data.`); + } + } else { + console.error(`JSON file for ${contractName} not found.`); + } + } + }); +}; +module.exports = generateAbis; + +// generateAbis(); diff --git a/blockchain/test/MentorToken.ts b/blockchain/test/MentorToken.ts index 9f5e279..5497b80 100644 --- a/blockchain/test/MentorToken.ts +++ b/blockchain/test/MentorToken.ts @@ -55,4 +55,30 @@ describe("MentorToken", function () { await expect(mentorToken.connect(user).mint(user.address, 1)).to.be.revertedWithCustomError(mentorToken, 'AccessControlUnauthorizedAccount') }) }); + describe("Minting", function () { + it("Should mint correctly", async function () { + const { mentorToken, minter, user } = await loadFixture(deployMentorTokenFixture); + + const amountToMint = 1n + const initialBalance = await mentorToken.balanceOf(user.address); + const initialTotalSupply = await mentorToken.totalSupply(); + await mentorToken.connect(minter).mint(user.address, amountToMint); + const finalBalance = await mentorToken.balanceOf(user.address); + const finalTotalSupply = await mentorToken.totalSupply(); + expect(finalBalance).to.equal(initialBalance + amountToMint); + expect(finalTotalSupply).to.equal(initialTotalSupply + amountToMint); + }) + it("Should not mint if not minter", async function () { + const { mentorToken, user } = await loadFixture(deployMentorTokenFixture); + + const amountToMint = 1n + const initialBalance = await mentorToken.balanceOf(user.address); + const initialTotalSupply = await mentorToken.totalSupply(); + await expect(mentorToken.connect(user).mint(user.address, 1)).to.be.revertedWithCustomError(mentorToken, 'AccessControlUnauthorizedAccount') + const finalBalance = await mentorToken.balanceOf(user.address); + const finalTotalSupply = await mentorToken.totalSupply(); + expect(finalBalance).to.equal(initialBalance); + expect(finalTotalSupply).to.equal(initialTotalSupply); + }) + }); }); diff --git a/frontend/nextjs/.gitignore b/frontend/nextjs/.gitignore index 2ecacec..f221332 100644 --- a/frontend/nextjs/.gitignore +++ b/frontend/nextjs/.gitignore @@ -38,3 +38,6 @@ next-env.d.ts # Miscellaneous package-lock.json yarn.lock + +# contract deployment files +/src/deployments