Web3JS, Ethers, Viem and other third-party libraries
Here you can find examples of how to use utilities for well-known libraries provided by Swisstronik
For Swisstronik, in order to execute transactions and call functions you have to use encryption to communicate with the blockchain node.
That means regular Ethereum libraries may not function correctly, and require additional modification.
Thankfully, Swisstronik team has prepared several options to facilitate blockchain interaction.
import { createSwisstronikClient, swisstronikTestnet } from"@swisstronik/viem-client";import { parseEther } from"viem";import { privateKeyToAccount } from"viem/accounts";constaccount=privateKeyToAccount("0x...");// Client with decorated Actions, which includes all the Actions available in the library.constswisstronikClient=createSwisstronikClient({ chain: swisstronikTestnet, account,// Optional: Needed to send/sign transactions});// Get node public keyconstnodePublicKey=awaitswisstronikClient.getNodePublicKey();console.log(nodePublicKey);// Get block numberconstblockNumber=awaitswisstronikClient.getBlockNumber();console.log(blockNumber);// Get balanceconstbalance=awaitswisstronikClient.getBalance({ address:"0x..",});const { data } =awaitswisstronikClient.call({ account:account.address, to:"0xF8bEB8c8Be514772097103e39C2ccE057117CC92", data:"0x61bc221a",});console.log(data);constgas=awaitswisstronikClient.estimateGas({ account:account.address, to:"0xF8bEB8c8Be514772097103e39C2ccE057117CC92", data:"0x61bc221a",});console.log(gas);consthash=awaitswisstronikClient.sendTransaction({ to:"0x0497cc339c0397b7Addd591B2160dd2f5371eA3b", value:parseEther("0.001"),});console.log(hash);constreceipt=awaitswisstronikClient.waitForTransactionReceipt({ hash,});console.log(receipt);constbalanceOf=awaitswisstronikClient.readContract({ address:ERC20_CONTRACT_ADDRESS, abi, functionName:"balanceOf", args: [account.address],});console.log(balanceOf);consthash=awaitswisstronikClient.writeContract({ address:ERC20_CONTRACT_ADDRESS, abi, functionName:"transfer", args: [account.address,5n],});console.log(hash);