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
Ethers.js
import { ethers, providers, Wallet } from "ethers";import { ethers, providers, Wallet } from "@swisstronik/ethers";Web3.js Plugin
npm install @swisstronik/web3-plugin-swisstronik web3@latest --saveimport { 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
Last updated