Deployment & Interaction with contract

(Source code you can find here: https://github.com/SigmaGmbH/swisstronik-tutorials/blob/main/PERC20_interaction)

In this guide, we will use the PERC20 contract, which we have seen in the previous step. Also, we will use hardhat for example of deployment script. Since Swisstronik has almost the same JSON-RPC as Ethereum, you can use any library/tool you like

You can deploy the PrivateSWTR contract from the sample hardhat-basic project with a deployment script like this:

const { ethers } = require("hardhat");

async function main() {
  const perc20 = await ethers.deployContract("PERC20Sample");
  await perc20.waitForDeployment();
  
  console.log(`PERC20Sample 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;
});

Before running this script, make sure to modify your hardhat.config.js file by adding the node address and your private key account. Here you can read more about the configuration of hardhat: https://hardhat.org/hardhat-runner/docs/config

require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config()

module.exports = {
  defaultNetwork: "swisstronik",
  solidity: "0.8.18",
  networks: {
    swisstronik: {
      // If you're using local testnet, replace `url` with local json-rpc address 
      url: "https://json-rpc.testnet.swisstronik.com/",
      accounts: [`0x` + `${process.env.PRIVATE_KEY}`],
    },
  },
};

Then you will be able to deploy your contract by running:

npx hardhat run --network $YOUR_NETWORK $PATH_TO_YOUR_SCRIPT

Interact with contract

You can see how we interact with PERC20 contract in tests for PERC20Sample. You can find it here: https://github.com/SigmaGmbH/swisstronik-tutorials/blob/main/PERC20_interaction/test/test.js

As you can see there, to interact with a deployed contract you need a few helper functions which will do encryption/decryption of query and encrypt transaction data field. Every method which contains data requires encryption, except contract deployment. In the table below you can find which methods require encrypted payload

Last updated