Swisstronik Docs
  • 🇨🇭About Swisstronik
    • What is Swisstronik?
    • Swisstronik vs Ethereum
    • Useful links
  • 🌐General
    • Intel SGX
    • Governance
      • On-Chain Proposal
      • Formatting a Proposal
      • Submitting a Proposal
      • Governable Parameters
    • Community Pool
    • Accounts
    • Transactions
    • Gas and Fees
    • Coin & Tokens
  • ⚒️Development
    • Endpoints
    • Connect wallets
      • MetaMask (EVM) - Manual
      • Keplr (Cosmos)
      • Testnet Metamask (EVM) - Chainlist
      • Testnet MetaMask (EVM) - Manual
      • Testnet Keplr (Cosmos)
    • Get test coins
    • SwisstronikJS SDK
      • SwisstronikJS Functions
      • Swisstronik & Ethereum transactions
      • Transaction - code sample
      • Swisstronik & Ethereum calls
      • Call - code sample
    • Swisstronik CLI
      • Installation
      • Configuration
      • Key management
      • Queries
      • Sending SWTR
      • For SDI Issuers
    • Guides
      • Contract deployment - Hardhat
        • 1. Setting up the Hardhat environment
        • 2. Configure hardhat.config.js
        • 3. Write and compile the smart contract
        • 4. Deploy the smart contract
        • 5. Interact with the contract - Transaction
        • 6. Interact with the contract - Call
      • Contract deployment - Remix IDE
      • Deployment & Interaction PERC-20
        • ERC-20 & PERC-20
        • Sample PERC20 contract
        • Deployment & Interaction with contract
        • How to use encryption
      • Contract verification
      • SDI for dapp developers
    • Local testnet
    • Web3JS, Ethers, Viem and other third-party libraries
  • 🖥️Node setup
    • Setup SGX
      • Supported Hardware
      • Setup Intel SGX
    • Setup node
      • Mainnet
        • v1.0.1 Upgrade
      • Testnet
        • Install from .deb package
        • Configure node
        • Upgrade your testnet node
          • Swisstronik v3 Testnet
          • Swisstronik v4 Testnet
          • Swisstronik v5 Testnet
          • Swisstronik v5.1 Testnet
          • Swisstronik v6 Testnet
          • Swisstronik v7 Testnet
          • Swisstronik v8 Testnet
        • Seed Node
        • Live Peers
        • Genesis Mirror Download
    • CLI Cheatsheet
  • 🤝Delegators
    • What is a delegator?
    • Delegator Guide (CLI)
    • Delegators Security
Powered by GitBook
On this page
  1. Development
  2. Guides
  3. Contract deployment - Hardhat

6. Interact with the contract - Call

Previous5. Interact with the contract - TransactionNextContract deployment - Remix IDE

Last updated 1 year ago

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

  1. Within the scripts folder, create a file called getMessage.js

  1. Let's write our getMessage.js script

  • Import hardhat and swisstronikJS functions

//Import hardhat and swisstronikJS functions
const hre = require("hardhat");
const { encryptDataField, decryptNodeResponse } = require("@swisstronik/utils");
  • Let's use sendShieldedQuery to send a call/query to the blockchain

/**
 * Send a shielded query/call to the Swisstronik blockchain.
 *
 * @param {object} provider - The provider object for making the call.
 * @param {string} destination - The address of the contract to call.
 * @param {string} data - Encoded data for the function call.
 *
 * @returns {Uint8Array} - Encrypted response from the blockchain.
 */
const sendShieldedQuery = async (provider, destination, data) => {
  // Get the RPC link from the network configuration
  const rpclink = hre.network.config.url;

  // Encrypt the call data using the SwisstronikJS function encryptDataField()
  const [encryptedData, usedEncryptedKey] = await encryptDataField(rpclink, data);

  // Execute the call/query using the provider
  const response = await provider.call({
    to: destination,
    data: encryptedData,
  });

  // Decrypt the call result using SwisstronikJS function decryptNodeResponse()
  return await decryptNodeResponse(rpclink, response, usedEncryptedKey);
};
  • Write the main script to use sendShieldedQuery function

async function main() {
  // Address of the deployed contract
  const contractAddress = "0xf84Df872D385997aBc28E3f07A2E3cd707c9698a";

  // Get the signer (your account)
  const [signer] = await hre.ethers.getSigners();

  // Construct a contract instance (must match the name of contract)
  const contractFactory = await hre.ethers.getContractFactory("Swisstronik");
  const contract = contractFactory.attach(contractAddress);

  // Send a shielded query to retrieve data from the contract
  const functionName = "getMessage";
  const responseMessage = await sendShieldedQuery(signer.provider, contractAddress, contract.interface.encodeFunctionData(functionName));

  // Decode the Uint8Array response into a readable string
  console.log("Decoded response:", contract.interface.decodeFunctionResult(functionName, responseMessage)[0]);
}
  • Add this by hardhat default

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

The final getMessage.js script should look like this:

const hre = require("hardhat");
const { encryptDataField, decryptNodeResponse } = require("@swisstronik/utils");

const sendShieldedQuery = async (provider, destination, data) => {
  const rpclink = hre.network.config.url;
  const [encryptedData, usedEncryptedKey] = await encryptDataField(rpclink, data);
  const response = await provider.call({
    to: destination,
    data: encryptedData,
  });
  return await decryptNodeResponse(rpclink, response, usedEncryptedKey);
};

async function main() {
  const contractAddress = "0xf84Df872D385997aBc28E3f07A2E3cd707c9698a";
  const [signer] = await hre.ethers.getSigners();
  const contractFactory = await hre.ethers.getContractFactory("Swisstronik");
  const contract = contractFactory.attach(contractAddress);
  const functionName = "getMessage";
  const responseMessage = await sendShieldedQuery(signer.provider, contractAddress, contract.interface.encodeFunctionData(functionName));
  console.log("Decoded response:", contract.interface.decodeFunctionResult(functionName, responseMessage)[0]);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
  1. Execute the following command in your terminal to run the script using the Swisstronik network

npx hardhat run scripts/getMessage.js --network swisstronik

  1. Upon successful execution, your terminal should display Decoded response: Hello Swisstronik!! or the latest message you've defined in the contract 🎉

⚒️