# Call - code sample

{% hint style="info" %}
The full repository for this code sample can be found here <https://github.com/SigmaGmbH/swisstronik-tutorials/tree/main/ERC20_interaction>
{% endhint %}

### Making Calls in Swisstronik

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

{% hint style="info" %}
For this example, we have deployed an ERC20 token at `0x7D804090e7a1FF0709d743d115bccE6757Bbe208`. Let's create the code to make a call to the function `totalSupply()`
{% endhint %}

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

{% hint style="warning" %}
NOTE: If you don't know how to set up a Hardhat project, please review the [Contract deployment - Hardhat ](https://docs.swisstronik.com/swisstronik-docs/development/guides/contract-deployment-hardhat/1.-setting-up-the-hardhat-environment)
{% endhint %}

2. 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.
3. Import SwisstronikJS & Hardhat in the `totalSupply.js` script

{% code lineNumbers="true" %}

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

{% endcode %}

4. Let's add the code to implement a function called `sendShieldedQuery` to use `encryptDataField()` and `decryptNodeResponse()`

{% hint style="success" %}
You can reuse this `sendShieldedQuery` code snippet to make calls to any smart contracts in the Swisstronik blockchain.
{% endhint %}

<pre class="language-javascript" data-line-numbers><code class="lang-javascript"><strong>const hre = require("hardhat");
</strong>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);
};
</code></pre>

5. 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.

{% hint style="success" %}
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
{% endhint %}

{% code lineNumbers="true" %}

```javascript
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;
});
```

{% endcode %}

6. Run `npx hardhat run scripts/totalSupply.js --network swisstronik`
7. You should see the decoded number printed in your console 🎉


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.swisstronik.com/swisstronik-docs/development/swisstronikjs-sdk/call-code-sample.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
