Swisstronik Docs
  • 🇨🇭About Swisstronik
    • What is Swisstronik?
    • Swisstronik vs Ethereum
    • Useful links
  • 🌐General
    • Intel SGX
    • Governance
      • On-Chain Proposal
      • Formatting a Proposal
      • Submitting a Proposal
      • Governable Parameters
    • Community Pool
    • Accounts
    • Transactions
    • Gas and Fees
    • Coin & Tokens
  • ⚒️Development
    • Endpoints
    • Connect wallets
      • MetaMask (EVM) - Manual
      • Keplr (Cosmos)
      • Testnet Metamask (EVM) - Chainlist
      • Testnet MetaMask (EVM) - Manual
      • Testnet Keplr (Cosmos)
    • Get test coins
    • SwisstronikJS SDK
      • SwisstronikJS Functions
      • Swisstronik & Ethereum transactions
      • Transaction - code sample
      • Swisstronik & Ethereum calls
      • Call - code sample
    • Swisstronik CLI
      • Installation
      • Configuration
      • Key management
      • Queries
      • Sending SWTR
      • For SDI Issuers
    • Guides
      • Contract deployment - Hardhat
        • 1. Setting up the Hardhat environment
        • 2. Configure hardhat.config.js
        • 3. Write and compile the smart contract
        • 4. Deploy the smart contract
        • 5. Interact with the contract - Transaction
        • 6. Interact with the contract - Call
      • Contract deployment - Remix IDE
      • Deployment & Interaction PERC-20
        • ERC-20 & PERC-20
        • Sample PERC20 contract
        • Deployment & Interaction with contract
        • How to use encryption
      • Contract verification
      • SDI for dapp developers
    • Local testnet
    • Web3JS, Ethers, Viem and other third-party libraries
  • 🖥️Node setup
    • Setup SGX
      • Supported Hardware
      • Setup Intel SGX
    • Setup node
      • Mainnet
        • v1.0.1 Upgrade
      • Testnet
        • Install from .deb package
        • Configure node
        • Upgrade your testnet node
          • Swisstronik v3 Testnet
          • Swisstronik v4 Testnet
          • Swisstronik v5 Testnet
          • Swisstronik v5.1 Testnet
          • Swisstronik v6 Testnet
          • Swisstronik v7 Testnet
          • Swisstronik v8 Testnet
        • Seed Node
        • Live Peers
        • Genesis Mirror Download
    • CLI Cheatsheet
  • 🤝Delegators
    • What is a delegator?
    • Delegator Guide (CLI)
    • Delegators Security
Powered by GitBook
On this page
  • Ethers.js
  • Web3.js Plugin
  • Viem client
  1. Development

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

PreviousLocal testnetNextSetup SGX

Last updated 6 months ago

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

You can check out example usage code here: 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:

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

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

Installation

Note: Please make sure to use viem version 2.21.19 or higher.

npm install @swisstronik/viem-client@latest viem@latest --save

Example usage is pretty straightforward:

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

You can use the Swisstronik Viem client located here:

⚒️
https://github.com/SigmaGmbH/ethers.js
https://github.com/SigmaGmbH/swisstronik-ethersjs-example-usage
https://github.com/SigmaGmbH/web3-plugin-swisstronik
https://github.com/SigmaGmbH/swisstronik-viem-client