Skip to content

Commit

Permalink
Writing script to generate abis (#10)
Browse files Browse the repository at this point in the history
* Finished Testing Mentor Token

* Created script to generate abi's upon deployment
  • Loading branch information
Jovells authored Nov 1, 2023
1 parent 1c6aed4 commit acd9aca
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
13 changes: 12 additions & 1 deletion blockchain/scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ethers } from "hardhat";
import dotenv from "dotenv";

const generateAbis = require('./generateAbis.js');

dotenv.config();

async function main() {
Expand All @@ -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", [
Expand All @@ -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
Expand Down
48 changes: 48 additions & 0 deletions blockchain/scripts/generateAbis.js
Original file line number Diff line number Diff line change
@@ -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();
26 changes: 26 additions & 0 deletions blockchain/test/MentorToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
});
});
3 changes: 3 additions & 0 deletions frontend/nextjs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ next-env.d.ts
# Miscellaneous
package-lock.json
yarn.lock

# contract deployment files
/src/deployments

0 comments on commit acd9aca

Please sign in to comment.