# 5. Interact with the contract - Transaction

{% 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 `setMessage.js`

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

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

* Import hardhat and swisstronikJS functions

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

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

{% code lineNumbers="true" %}

```javascript
/**
 * Send a shielded transaction to the Swisstronik blockchain.
 *
 * @param {object} signer - The signer object for sending the transaction.
 * @param {string} destination - The address of the contract to interact with.
 * @param {string} data - Encoded data for the transaction.
 * @param {number} value - Amount of value to send with the transaction.
 *
 * @returns {Promise} - The transaction object.
 */
const sendShieldedTransaction = async (signer, destination, data, value) => {
  // Get the RPC link from the network configuration
  const rpclink = hre.network.config.url;

  // Encrypt transaction data
  const [encryptedData] = await encryptDataField(rpclink, data);

  // Construct and sign transaction with encrypted data
  return await signer.sendTransaction({
    from: signer.address,
    to: destination,
    data: encryptedData,
    value,
  });
};
```

{% endcode %}

* Write the `main` script to use `sendShieldedTransaction` 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
  const contractFactory = await hre.ethers.getContractFactory("Swisstronik");
  const contract = contractFactory.attach(contractAddress);

  // Send a shielded transaction to set a message in the contract
  const setMessageTx = await sendShieldedTransaction(signer, contractAddress, contract.interface.encodeFunctionData("setMessage", ["Hello Swisstronik!!"]), 0);
  await setMessageTx.wait();

  //It should return a TransactionResponse object
  console.log("Transaction Receipt: ", setMessageTx);
}
```

{% endcode %}

* Add this by hardhat default

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

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

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

const sendShieldedTransaction = async (signer, destination, data, value) => {
  const rpclink = hre.network.config.url;
  const [encryptedData] = await encryptDataField(rpclink, data);
  return await signer.sendTransaction({
    from: signer.address,
    to: destination,
    data: encryptedData,
    value,
  });
};

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 = "setMessage";
  const messageToSet = "Hello Swisstronik!!";
  const setMessageTx = await sendShieldedTransaction(signer, contractAddress, contract.interface.encodeFunctionData(functionName, [messageToSet]), 0);
  await setMessageTx.wait();
  console.log("Transaction Receipt: ", setMessageTx);
}

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

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

   `npx hardhat run scripts/setMessage.js --network swisstronik`
4. Upon successful execution, your terminal should display `Transaction Receipt: TransactionResponse {...}` , now it's time to retrieve this message🎉


---

# 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/5.-interact-with-the-contract-transaction.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.
