Task Details: Craft an ERC20 contract that mints at least 100 tokens with Hardhat (in 7 Steps)
Last updated
Was this helpful?
Make sure you have installed SwisstronikJS by running npm i @swisstronik/swisstronik.js
Within the scripts folder, create a file called deploy.js and mint.js
Let's write our deploy.js script
// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat
// will compile your contracts, add the Hardhat Runtime Environment's members to the
// global scope, and execute the script.
const hre = require("hardhat");
async function main() {
const contract = await hre.ethers.deployContract("TestToken");
await contract.waitForDeployment();
console.log(`deployed to ${contract.target}`);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
And then let's write our mint.js script
// Import necessary modules from Hardhat and SwisstronikJS
const hre = require("hardhat");
const { encryptDataField, decryptNodeResponse } = require("@swisstronik/swisstronik.js");
// Function to send a shielded transaction using the provided signer, destination, data, and value
const sendShieldedTransaction = async (signer, destination, data, value) => {
// Get the RPC link from the network configuration
const rpcLink = hre.network.config.url;
// Encrypt transaction data
const [encryptedData] = await encryptDataField(rpcLink, data);
// Construct and sign transaction with encrypted data
return await signer.sendTransaction({
from: signer.address,
to: destination,
data: encryptedData,
value,
});
};
async function main() {
// Address of the deployed contract
const contractAddress = "0x649f418470A681ddd505d6537f61Da58b58B7FD0";
// Get the signer (your account)
const [signer] = await hre.ethers.getSigners();
// Create a contract instance
const contractFactory = await hre.ethers.getContractFactory("TestToken");
const contract = contractFactory.attach(contractAddress);
// Send a shielded transaction to mint 100 tokens in the contract
const functionName = "mint100tokens";
const mint100TokensTx = await sendShieldedTransaction(signer, contractAddress, contract.interface.encodeFunctionData(functionName), 0);
await mint100TokensTx.wait();
// It should return a TransactionReceipt object
// console.log("Transaction Receipt: ", mint100TokensTx);
}
// Using async/await pattern to handle errors properly
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Execute the following command in your terminal to run the deploy script using the Swisstronik network
npx hardhat run scripts/deploy.js --network swisstronik
Execute the following command in your terminal to run the mint script using the Swisstronik network
npx hardhat run scripts/mint.js --network swisstronik
Upon successful execution, your terminal should display the latest message you've defined in the contract 🎉