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()
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
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.Import SwisstronikJS & Hardhat in the
totalSupply.js
script
Let's add the code to implement a function called
sendShieldedQuery
to useencryptDataField()
anddecryptNodeResponse()
You can reuse this sendShieldedQuery
code snippet to make calls to any smart contracts in the Swisstronik blockchain.
Let's add the
main
function to use thesendShieldedQuery
to make a call to thefunction 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
Run
npx hardhat run scripts/totalSupply.js --network swisstronik
You should see the decoded number printed in your console 🎉
Last updated