# 6. Interact with the contract - Call

{% hint style="warning" %}
Make sure you have installed SwisstronikJS by running `npm i @swisstronik/utils`
{% endhint %}

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

<figure><img src="/files/TN6yVUfgjN2METHhlYrX" alt=""><figcaption></figcaption></figure>

2. Let's write our `getMessage.js` script

* Import hardhat and swisstronikJS functions

{% code lineNumbers="true" %}

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

{% endcode %}

* Let's use `sendShieldedQuery` to send a call/query to the blockchain

{% code lineNumbers="true" %}

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

{% endcode %}

* Write the `main` script to use `sendShieldedQuery` function

{% code lineNumbers="true" %}

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

{% endcode %}

* Add this by hardhat default

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

{% hint style="success" %}
The final `getMessage.js` script should look like this:
{% endhint %}

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

</code></pre>

3. Execute the following command in your terminal to run the script using the Swisstronik network

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

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


---

# 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/guides/contract-deployment-hardhat/6.-interact-with-the-contract-call.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.
