npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@swisstronik/viem-client

v1.0.0

Published

Viem client for Swisstronik network

Downloads

2

Readme

Swisstronik Viem Client

Swisstronik Viem Client allows the users to use viem library with Swisstronik

Supported methods:

  • eth_estimateGas
  • eth_call
  • eth_sendTransaction
  • Custom - eth_getNodePublicKey

Installation

Note: Make sure you are using viem version 2.21.19 or higher in your project.

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

Usage

import { createSwisstronikClient, swisstronikTestnet } from "@swisstronik/viem-client";

// Client with decorated Actions, which includes all the Actions available in the library.
const swisstronikClient = createSwisstronikClient({
  chain: swisstronikTestnet,
});

// 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..",
});
console.log(balance);

Sending transactions & performing calls

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

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

Interacting with a Smart Contract

import { createSwisstronikClient, swisstronikTestnet } from "@swisstronik/viem-client";
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
});

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

const receipt = await swisstronikClient.waitForTransactionReceipt({
  hash,
});
console.log(receipt);

Swisstronik Light Weight Client

You can use the Client as-is, with no decorated Actions, to maximize tree-shaking in your app. This is useful if you are concerned about bundle size and want to only include the Actions you use.

import { createSwisstronikClient, swisstronikTestnet } from "@swisstronik/viem-client";
import { privateKeyToAccount } from "viem/accounts";
import {
  call,
  estimateGas,
  getBalance,
  getBlockNumber,
  readContract,
  sendTransaction,
  waitForTransactionReceipt,
  writeContract,
} from "viem/actions";

const account = privateKeyToAccount("0x...");

const swisstronikClient = createSwisstronikLightWeightClient({
  chain: swisstronikTestnet,
  account, // Optional: Needed to send/sign transactions
});

const blockNumber = await getBlockNumber(swisstronikClient);
console.log(blockNumber);

const balance = await getBalance(swisstronikClient, {
  address: account.address,
});
console.log(balance);

const hash = await sendTransaction(swisstronikClient, {
  to: "0x0497cc339c0397b7Addd591B2160dd2f5371eA3b",
  value: parseEther("0.001"),
});
console.log(hash);

const receipt = await waitForTransactionReceipt(swisstronikClient, {
  hash,
});
console.log(receipt);

const balanceOf = await readContract(swisstronikClient, {
  address: ERC20_CONTRACT_ADDRESS,
  abi,
  functionName: "balanceOf",
  args: [account.address],
});
console.log(balanceOf);

const hash = await writeContract(swisstronikClient, {
  address: ERC20_CONTRACT_ADDRESS,
  abi,
  functionName: "transfer",
  args: [account.address, 5n],
});
console.log(hash);

Refer to Swisstronik Developer Docs for more information & usage scenarios.

Publishing

To publish a new version of the package to npm, run the following command:

npm run build

npm publish

Resources

Safety

This is experimental software and subject to change over time.

This package is not audited and has not been tested for security. Use at your own risk. I do not give any warranties and will not be liable for any loss incurred through any use of this codebase.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT