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

PERC20 (Private ERC20) Deploy, Mint and Transfer.

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

  1. Make an .env file for store your PK (Private Key) to access your wallet.

  2. Ensure you have enough funds token to doing the task.

  3. Make 3 file (deploy.js, mint.js and transfer.js) inside the script folder

  4. 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;
});
  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;
});
  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;
});
  1. Don't forget to compile the contract

npm run compile
  1. Execute the following command in your terminal to run the deploy script using the Swisstronik network

npm run deploy
  1. Execute the following command in your terminal to run the mint script using the Swisstronik network

npm run mint
  1. 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 🎉

Last updated 1 year ago

Was this helpful?

⛑️
▶️