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

wallact

v1.0.4

Published

This TypeScript library provides a simplified interface for interacting with smart contracts on the Ethereum blockchain.

Downloads

7

Readme

Wallact

This TypeScript library provides a simplified interface for interacting with smart contracts on the Ethereum blockchain. It abstracts the complexities of read and write operations, making it easier for developers to work with smart contracts within their applications.

Features

  • Simplified Contract Interaction: Offers easy-to-use methods for reading from and writing to smart contracts.
  • Flexible Contract Management: Supports multiple contract instances, allowing interactions with various entities and contracts through a single interface.
  • Error Handling: Implements try-catch blocks for robust error handling during contract interactions.

Getting Started

Prerequisites

  • Node.js (v14 or higher recommended)
  • npm (v6 or higher recommended)
  • An Ethereum wallet with some ETH for transactions
  • Access to an Ethereum node (via services like Infura or Alchemy)

Installation

  1. Install with npm:
npm i wallact

Usage

Creating Keys for Wallet

To create keys for a new wallet, use the createWallet method. This method generates a new wallet including a private key and address.

import { Wallact } from "wallact";

const wallet = Wallact.createWallet();
console.log(`Wallet Address: \${wallet.address}`);
console.log(`Private Key: \${wallet.privateKey}`);

Get Wallet Address from Key

To get wallet address from private key, use the getWalletAddress method. This will return the address.

import { Wallact } from "wallact";

const address = Wallact.getWalletAddress("0xYOUR_PRIVATE_KEY");
console.log(`Wallet Address: \${.address}`);

Reading from a Smart Contract

To read data from a smart contract, use the readContract method. This method requires the name of the contract method you wish to call and an array of arguments for that method.

import { Wallact } from "wallact";

async function readData() {
  const contractInterface = new Wallact(
    "https://rpc-url/",
    "0xContractddress",
    contractAbi
  );
  try {
    const data = await contractInterface.readContract("methodName", [
      "arg1",
      "arg2",
    ]);
    console.log("Contract data:", data);
  } catch (error) {
    console.error("Error reading from contract:", error);
  }
}

Writing to a Smart Contract

To write data to a smart contract, use the writeContract method. This method requires the name of the contract method you wish to call, an array of arguments for that method, and optionally the entity (contract instance) to use for the write operation.

import { Wallact } from "wallact";

async function writeData() {
  const contractInterface = new Wallact(
    "https://rpc-url/",
    "0xContractddress",
    contractAbi,
    "0xYourPrivateKey"
  );
  try {
    const receipt = await contractInterface.writeContract("methodName", [
      "arg1",
      "arg2",
    ]);
    console.log("Transaction receipt:", receipt);
  } catch (error) {
    console.error("Error writing to contract:", error);
  }
}

Writing to a Smart Contract with transaction Confirmation

To write data to a smart contract, use the writeContractWithConfirmations method. This method requires the name of the contract method you wish to call, an array of arguments for that method, and optionally the entity (contract instance) to use for the write operation.

import { Wallact } from "wallact";

async function writeData() {
  const contractInterface = new Wallact(
    "https://rpc-url/",
    "0xContractddress",
    contractAbi,
    "0xYourPrivateKey"
  );
  try {
    const receipt = await contractInterface.writeContractWithConfirmations(
      "methodName",
      ["arg1", "arg2"],
      5
    );
    console.log("Transaction receipt:", receipt);
  } catch (error) {
    console.error("Error writing to contract:", error);
  }
}

Writing to a Smart Contract with multiple entities

To write data to a smart contract with different entity, use the addEntityWallet method. This method requires the name of the entity and key for that entity.

import { Wallact } from "wallact";

async function writeData() {
  const contractInterface = new Wallact(
    "https://rpc-url/",
    "0xContractddress",
    contractAbi,
    "0xYourPrivateKey",
    "ownerEntity"
  );

  contractInterface.addEntityWallet("verifier", "0xVerifierPrivateKey");

  try {
    const receipt = await contractInterface.writeContract(
      "methodName",
      ["arg1", "arg2"],
      "verifier"
    );
    console.log("Transaction receipt:", receipt);
  } catch (error) {
    console.error("Error writing to contract:", error);
  }
}

Converting Ether to Wei

To convert Ether to Wei, which is necessary for many contract interactions requiring value transactions, use the convertToWei static method. This method takes the amount in Ether as an argument and returns its equivalent in Wei.

import { Wallact } from "wallact";

const etherAmount = 1; // Example amount in Ether
const weiAmount = Wallact.convertToWei(etherAmount);
console.log(`${etherAmount} Ether is equivalent to ${weiAmount} Wei.`);

Converting Wei to Ether

Conversely, to convert Wei back to Ether, perhaps for displaying balances in a more readable format, use the convertToEth static method. This method takes the amount in Wei as an argument and returns its equivalent in Ether.

import { Wallact } from "wallact";

const weiAmount = BigInt("1000000000000000000"); // Example amount in Wei
const etherAmount = Wallact.convertToEth(weiAmount.toString());
console.log(`${weiAmount} Wei is equivalent to ${etherAmount} Ether.`);

Validate an Ethereum Address

To check if an Ethereum address is valid, use the isValidEthereumAddress static method. This method takes an Ethereum address as a string argument and returns a boolean indicating whether the address is valid.

import { Wallact } from "wallact";

const address = "0x49A6F7Ece315a56C097c4Fc72F5aA2886B9c260a"; // Example Ethereum address
const isValid = Wallact.isValidEthereumAddress(address);
console.log(`Is the address valid? ${isValid}`);

Fetch Wallet Balance

To retrieve the balance of a wallet, use the fetchWalletBalance method. This method takes an Ethereum address as a string argument and returns the balance of the wallet in Wei.

import { Wallact } from "wallact";

async function getWalletBalance(address: string) {
  const wallactInstance = new Wallact("https://rpc-url/", abi); // Initialize with your provider URL
  try {
    const balanceWei = await wallactInstance.fetchWalletBalance(address);
    console.log(`Balance for ${address}: ${balanceWei} Wei`);
  } catch (error) {
    console.error("Failed to fetch wallet balance:", error);
  }
}

Fetch the Latest Block Number

To fetch the latest block number from the blockchain, use the fetchLatestBlock method. This method returns the latest block number.

import { Wallact } from "wallact";

async function fetchLatestBlockNumber() {
  const contractInterface = new Wallact(
    "https://rpc-url/",
    "0xContractddress",
    contractAbi
  );
  try {
    const data = await fetchLatestBlock();
    console.log("Latest Block Number:", data);
  } catch (error) {
    console.error("Error reading latest Block number:", error);
  }
}

Get Transaction Receipt

To get the transaction receipt for a given transaction hash, use the getTransactionReceipt method. This method returns the transaction receipt.

import { Wallact } from "wallact";

async function getTxReceipt() {
  const contractInterface = new Wallact(
    "https://rpc-url/",
    "0xContractddress",
    contractAbi
  );
  try {
    const data = await getTransactionReceipt("0xTRANSACTION_HASH");
    console.log("Latest Block Number:", data);
  } catch (error) {
    console.error("Error reading transaction Receipt:", error);
  }
}

Wait for Transaction Confirmation

To wait for a transaction to be confirmed a specified number of times, use the waitForTransaction method. This does not return a value but waits until the transaction has the specified number of confirmations (5 in this case).

import { Wallact } from "wallact";

async function getTxReceipt() {
  const contractInterface = new Wallact(
    "https://rpc-url/",
    "0xContractddress",
    contractAbi
  );
  try {
    await waitForTransaction("0xTRANSACTION_HASH", 5);
  } catch (error) {
    console.error("Error reading from contract:", error);
  }
}

Contributing

Contributions are welcome! Please feel free to submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.