LOGOSNODOS
  • LOGOSNODOS
  • 🖥️Mainnet Node
    • Avail Network
      • ▶️Installation
      • ▶️Staking / Bonding
    • BlockX
      • ▶️Installation
      • ▶️Cheat Sheet
    • Muon Network
      • ▶️Installation
      • ▶️Troubleshooting
    • RaiblocksOne
      • ▶️Installation
      • ▶️Cheat Sheet
    • SGE Network
      • ▶️Installation
      • ▶️Cheat Sheet
    • Quicksilver Protocol
      • ▶️Installation
      • ▶️Cheat Sheet
  • 🛠️TESTNET NODE
    • 0gchain
      • ▶️Installation
      • ▶️Cheat Sheet
    • Airchains
      • ▶️Installation
      • ▶️Cheat Sheet
    • Avail Network
      • ▶️Installation
      • ▶️Challenge Set Identity
      • ▶️Challenge Add Pool
    • Cortensor
      • ▶️Staking and Funding
      • ▶️Installation
      • ▶️Node Management
      • ▶️Monitoring
    • CrossFi
      • ▶️Installation
      • ▶️Cheat Sheet
    • Juneo Network
      • ▶️Installation
      • ▶️Create Supernet
      • ▶️Deploy a VM
    • Namada
      • ▶️Installation
      • ▶️Cheat Sheet
    • Pryzm
      • ▶️Installation
      • ▶️Cheat Sheet
    • Quicksilver Protocol
      • ▶️Installation
      • ▶️Cheat Sheet
    • Swisstronik
      • ▶️Installation
      • ▶️Cheat Sheet
    • Symphony
      • ▶️Installation
      • ▶️Cheat Sheet
  • ⛑️Solidity Compiler
    • Hardhat
      • ▶️Installation
      • ▶️Setup Hardhat on Swisstronik Network
      • ▶️Write and compile the smart contract
      • ▶️Deploy the smart contract
      • ▶️Interact with the contract-Transaction
      • ▶️Interact with the contract-Call
      • ▶️Crafts and Mint Token ERC20
      • ▶️PERC20 (Private ERC20) Deploy, Mint and Transfer.
      • ▶️JSON RPC Call using ETH getStorage
Powered by GitBook
On this page

Was this helpful?

  1. Solidity Compiler
  2. Hardhat

Crafts and Mint Token ERC20

Task Details: Craft an ERC20 contract that mints at least 100 tokens with Hardhat (in 7 Steps)

Last updated 1 year ago

Was this helpful?

Make sure you have installed SwisstronikJS by running npm i @swisstronik/swisstronik.js

  1. Within the scripts folder, create a file called deploy.js and mint.js

  1. 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;
});
  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;
});
  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
  1. Execute the following command in your terminal to run the mint script using the Swisstronik network

npx hardhat run scripts/mint.js --network swisstronik
  1. Upon successful execution, your terminal should display the latest message you've defined in the contract 🎉

⛑️
▶️