Call - code sample

The full repository for this code sample can be found here https://github.com/SigmaGmbH/swisstronik-tutorials/tree/main/ERC20_interaction

Making Calls in Swisstronik

Let's implement a function called sendShieldedQuery to make a call to a smart contract, using SwisstronikJS.

For this example, we have deployed an ERC20 token at 0x7D804090e7a1FF0709d743d115bccE6757Bbe208. Let's create the code to make a call to the function totalSupply()

  1. Open your Hardhat project and run npm i @swisstronik/utils it from the main directory.

NOTE: If you don't know how to set up a Hardhat project, please review the Contract deployment - Hardhat

  1. Create a file totalSupply.js within the scripts folder in your Hardhat project, this file will contain the code to run the call to get the total supply.

  2. Import SwisstronikJS & Hardhat in the totalSupply.js script

const hre = require("hardhat");
const { encryptDataField, decryptNodeResponse } = require("@swisstronik/utils");
  1. Let's add the code to implement a function called sendShieldedQuery to use encryptDataField() and decryptNodeResponse()

You can reuse this sendShieldedQuery code snippet to make calls to any smart contracts in the Swisstronik blockchain.

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

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

  // Encrypt the call data using SwisstronikJS's encryption function
  const [encryptedData, usedEncryptionKey] = await encryptDataField(rpcLink, data);

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

  // Decrypt the response using SwisstronikJS's decryption function
  return await decryptNodeResponse(rpcLink, response, usedEncryptionKey);
};
  1. Let's add the main function to use the sendShieldedQuery to make a call to the function totalSupply() external view returns (uint256) in the ERC20 smart contract.

You can reuse this script to make calls in Swisstronik (make sure you are calling a reading-only function) and don't forget to replace the variables marked with the replace_ prefix with your own values in the main function

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

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

  // Encrypt the call data using SwisstronikJS's encryption function
  const [encryptedData, usedEncryptionKey] = await encryptDataField(rpcLink, data);

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

  // Decrypt the response using SwisstronikJS's decryption function
  return await decryptNodeResponse(rpcLink, response, usedEncryptionKey);
};
// Main function to interact with the contract and retrieve data
async function main() {
  // Address of the deployed contract
  const replace_contractAddress = "0x7D804090e7a1FF0709d743d115bccE6757Bbe208";

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

  // Create a contract instance
  const replace_contractFactory = await hre.ethers.getContractFactory("TestToken");
  const contract = replace_contractFactory.attach(replace_contractAddress);

  // Perform a shielded query to retrieve data from the contract
  const replace_functionName = "totalSupply";
  const replace_functionArgs = ""; // Replace with specific arguments if required
  const responseMessage = await sendShieldedQuery(signer.provider, replace_contractAddress, contract.interface.encodeFunctionData(replace_functionName, replace_functionArgs));

  // Decode the Uint8Array response into a readable string
  console.log("Decoded response:", contract.interface.decodeFunctionResult(replace_functionName, responseMessage)[0]);
}

// Using async/await pattern to handle errors properly
main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
  1. Run npx hardhat run scripts/totalSupply.js --network swisstronik

  2. You should see the decoded number printed in your console 🎉

Last updated