Task Details: Craft an ERC20 contract that mints at least 100 tokens with Hardhat (in 7 Steps)
Make sure you have installed SwisstronikJS by running npm i @swisstronik/swisstronik.js
Make an .env file for store your PK (Private Key) to access your wallet.
Ensure you have enough funds token to doing the task.
Make 3 file (deploy.js, mint.js and transfer.js) inside the script folder
Open the deploy.js file and write our script
const { ethers } = require("hardhat");
async function main() {
const perc20 = await ethers.deployContract("PERC20Sample");
await perc20.deployed();
console.log(`PERC20 was deployed to ${perc20.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;
});
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 = "0x2eEa4a8A9B7D8FE2b9d3A6e0e08708046d6bacfb";
// Get the signer (your account)
const [signer] = await hre.ethers.getSigners();
// Create a contract instance
const contractFactory = await hre.ethers.getContractFactory("PERC20Sample");
const contract = contractFactory.attach(contractAddress);
// Send a shielded transaction to mint 100 tokens in the contract
const functionName = "mint";
const mintToken = await sendShieldedTransaction(
signer,
contractAddress,
contract.interface.encodeFunctionData(functionName),
0
);
await mintToken.wait();
// It should return a TransactionReceipt object
console.log("Mint Transaction Hash: ", mintToken.hash);
}
// Using async/await pattern to handle errors properly
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
And the last write our transfer.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() {
// your deployed contract
const replace_contractAddress = "0x2eEa4a8A9B7D8FE2b9d3A6e0e08708046d6bacfb";
const [signer] = await hre.ethers.getSigners();
const replace_contractFactory = await hre.ethers.getContractFactory(
"PERC20Sample"
);
const contract = replace_contractFactory.attach(replace_contractAddress);
const replace_functionName = "transfer";
const replace_functionArgs = ["0x16af037878a6cAce2Ea29d39A3757aC2F6F7aac1","1",];
const transaction = await sendShieldedTransaction(
signer,
replace_contractAddress,
contract.interface.encodeFunctionData(
replace_functionName,
replace_functionArgs
),
0
);
await transaction.wait();
console.log("Transfer Transaction Hash: ", transaction.hash);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Don't forget to compile the contract
npm run compile
Execute the following command in your terminal to run the deploy script using the Swisstronik network
npm run deploy
Execute the following command in your terminal to run the mint script using the Swisstronik network
npm run mint
Execute the following command in your terminal to run the transfer script using the Swisstronik network
npm run transfer
Upon successful execution, your terminal should display the latest message you've defined in the contract 🎉