Web3JS, Ethers 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

import { ethers, providers, Wallet } from "ethers";

to

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.

npm install @swisstronik/web3-plugin-swisstronik web3@latest --save

Example usage is pretty straightforward:

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);

Last updated