▶️Interact with the contract-Transaction
Within the scripts folder, create a file called
setMessage.js

Let's write our
setMessage.js
script
Import hardhat and swisstronikJS functions
const hre = require("hardhat");
const { encryptDataField, decryptNodeResponse } = require("@swisstronik/swisstronik.js");
Let's use
sendShieldedTransaction
to send a call/query to the blockchain
/**
* 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,
});
};
Write the
main
script to usesendShieldedTransaction
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
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);
}
Add this by hardhat default
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
The final setMessage.js
script should look like this:
const hre = require("hardhat");
const { encryptDataField, decryptNodeResponse } = require("@swisstronik/swisstronik.js");
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;
});
Execute the following command in your terminal to run the script using the Swisstronik network
npx hardhat run scripts/setMessage.js --network swisstronik
Upon successful execution, your terminal should display
Transaction Receipt: TransactionResponse {...}
, now it's time to retrieve this message🎉
Last updated
Was this helpful?