⚒️ DevelopmentWeb3JS, 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.
Ethers.js
With Ethers.js, we've created a fork of the most popular Ethers.js v5.7.2 (source code is here - https://github.com/SigmaGmbH/ethers.js )
You can check out example usage code here:
https://github.com/SigmaGmbH/swisstronik-ethersjs-example-usage
In order to use your code with Swisstronik, just replace
Copy import { ethers, providers, Wallet } from "ethers";
to
Copy import { ethers, providers, Wallet } from "@swisstronik/ethers";
Now, sendTransaction, estimateGas, call
will be encrypted / decrypted as needed automatically.
Web3.js Plugin
With web3.js you can use a plugin, located here:
https://github.com/SigmaGmbH/web3-plugin-swisstronik
Installation
Note: Please make sure to use web3
version 4.0.3 or higher.
Copy npm install @swisstronik/web3-plugin-swisstronik web3@latest --save
Example usage is pretty straightforward:
Copy import { Web3 } from "web3";
import { SwisstronikPlugin } from "@swisstronik/web3-plugin-swisstronik";
const web3 = new Web3("https://json-rpc.testnet.swisstronik.com/"); // Any RPC node you wanted to connect with
web3.registerPlugin(new SwisstronikPlugin());
let wallet = web3.eth.accounts.wallet.add("0x..."); // Private Key
// Get node public key
let tx = {
to: '0x...',
from: wallet[0].address,
data: '0x61bc221a'
}
let callResult = await web3.swisstronik.call(tx);
console.log(callResult);
let estimateGasResult = await web3.swisstronik.estimateGas(tx);
console.log(estimateGasResult);
let sentTxReceipt = await web3.swisstronik.sendTransaction(tx);
console.log(sentTxReceipt);
const contract = new web3.eth.Contract(abi, ERC20_CONTRACT_ADDRESS);
const balanceOf = await contract.methods
.balanceOf(wallet[0].address)
.call();
console.log(balanceOf);
const gas = await contract.methods
.transfer(wallet[0].address, 5n)
.estimateGas({ from: wallet[0].address });
console.log(gas);
const sentTxReceipt = await contract.methods.transfer(wallet[0].address, 5n).send({from: wallet[0].address});
console.log(sentTxReceipt);
Viem client
We've setup a Viem client in order to handle interact with the Blockchain using viem
You can use the Swisstronik Viem client located here: https://github.com/SigmaGmbH/swisstronik-viem-client
Installation
Note: Please make sure to use viem
version 2.21.19 or higher.
Copy npm install @swisstronik/viem-client@latest viem@latest --save
Example usage is pretty straightforward:
Copy import { createSwisstronikClient, swisstronikTestnet } from "@swisstronik/viem-client";
import { parseEther } from "viem";
import { privateKeyToAccount } from "viem/accounts";
const account = privateKeyToAccount("0x...");
// Client with decorated Actions, which includes all the Actions available in the library.
const swisstronikClient = createSwisstronikClient({
chain: swisstronikTestnet,
account, // Optional: Needed to send/sign transactions
});
// Get node public key
const nodePublicKey = await swisstronikClient.getNodePublicKey();
console.log(nodePublicKey);
// Get block number
const blockNumber = await swisstronikClient.getBlockNumber();
console.log(blockNumber);
// Get balance
const balance = await swisstronikClient.getBalance({
address: "0x..",
});
const { data } = await swisstronikClient.call({
account: account.address,
to: "0xF8bEB8c8Be514772097103e39C2ccE057117CC92",
data: "0x61bc221a",
});
console.log(data);
const gas = await swisstronikClient.estimateGas({
account: account.address,
to: "0xF8bEB8c8Be514772097103e39C2ccE057117CC92",
data: "0x61bc221a",
});
console.log(gas);
const hash = await swisstronikClient.sendTransaction({
to: "0x0497cc339c0397b7Addd591B2160dd2f5371eA3b",
value: parseEther("0.001"),
});
console.log(hash);
const receipt = await swisstronikClient.waitForTransactionReceipt({
hash,
});
console.log(receipt);
const balanceOf = await swisstronikClient.readContract({
address: ERC20_CONTRACT_ADDRESS,
abi,
functionName: "balanceOf",
args: [account.address],
});
console.log(balanceOf);
const hash = await swisstronikClient.writeContract({
address: ERC20_CONTRACT_ADDRESS,
abi,
functionName: "transfer",
args: [account.address, 5n],
});
console.log(hash);
Last updated 3 months ago