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

smart-contract-interaction

v1.0.34

Published

Interact with the Nexlabs smart contracts

Downloads

808

Readme

smart-contract-interaction

A powerful SDK for interacting with smart contracts, supporting multiple index symbols and environments like wallet extensions and mainnets.

Installation

To install the package, run the following command:

npm i smart-contract-interaction

Usage

SDK Initialization

To begin, create a new instance of the NexIndex class with the index symbol and the current configuration object:

import { NexIndex, Environment } from 'smart-contract-interaction';

// Define the configuration for connecting to the smart contract
const CurrentConfig = {
  env: Environment.WALLET_EXTENSION, // Choose the environment (e.g., WALLET_EXTENSION, LOCAL, MAINNET)
  rpc: {
    local: '<your_local_url>',      // Add your local RPC URL here
    mainnet: '<your_rpc_url>',      // Add your mainnet RPC URL here
    wallet: window.ethereum         // If using a wallet extension like MetaMask, pass the wallet object
  },
  wallet: {
    address: '<your_address>',      // Your wallet address here
    privateKey: '<your_private_key>' // Your wallet's private key for write operations
  }
};

// Instantiate the NexIndex class with the chosen index symbol
const index = new NexIndex('ANFI', CurrentConfig);

Allowed Index Symbols

You can interact with any of the following index symbols:

  • ANFI
  • CRYPTO5
  • MAG7
  • ARBEI

Allowed From Mint Symbols

You can interact with any of the following index symbols:

  • USDT
  • USDC ( for MAG7 only )

Configuration Fields

  • env: Environment for smart contract interaction. Choose between:

    • LOCAL
    • MAINNET
    • WALLET_EXTENSION
  • rpc: RPC URLs for different environments.

  • wallet: Contains wallet details (wallet address and private key).

Methods

Read Methods

  1. getAddress()

    • Returns the connected user's wallet address.
    • Arguments: None
    const address = await index.getAddress();
    console.log(address);
  2. getBalance()

    • Returns the total balance of the selected index for the connected user.
    • Arguments: None
    const balance = await index.getBalance();
    console.log(balance);
  3. getAllowance()

    • Returns the allowance of the selected index.
    • Arguments: None
    const allowance = await index.getAllowance();
    console.log(allowance);
  4. getTotalSupply()

    • Returns the total supply of the selected index.
    • Arguments: None
    const totalSupply = await index.getTotalSupply();
    console.log(totalSupply);

Write Methods

  1. approve(fromSymbol, amount)

    • Approve the transfer of a specified amount of tokens from a given symbol.
    • Arguments:
      • fromSymbol: Symbol from which you want to approve the token.
      • amount: The amount of tokens to approve.
    await index.approve('USDT', 100);
  2. mint(fromSymbol, amount)

    • Mint tokens for the specified index.
    • Arguments:
      • fromSymbol: Symbol from which you want to mint the token.
      • amount: The amount of tokens to mint.
    await index.mint('USDT', 50);
  3. burn(amount)

    • Burn the specified amount of tokens.
    • Arguments:
      • amount: The amount of tokens to burn.
    await index.burn(25);

Example

// Import the necessary classes and environment enum
import { NexIndex, Environment } from 'smart-contract-interaction';

// Configuration for interacting with the smart contract
const CurrentConfig = {
  env: Environment.MAINNET,
  rpc: {
    local: 'http://localhost:8545',
    mainnet: 'https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID',
    wallet: window.ethereum
  },
  wallet: {
    address: '0xYourWalletAddress',
    privateKey: '0xYourPrivateKey'
  }
};

// Initialize the NexIndex instance with a chosen index symbol
const index = new NexIndex('MAG7', CurrentConfig);

// Get the connected user's address
const address = await index.getAddress();
console.log(`User address: ${address}`);

// Get the user's balance for the selected index
const balance = await index.getBalance();
console.log(`Balance: ${balance}`);

// Approve a token transfer
await index.approve('USDC', 100);

// Mint new tokens
await index.mint('USDC', 50);

// Burn tokens
await index.burn(20);

Widget Implementation

If the user wants to interact with our indices, they can do so via our provided widget.

Usage

To use the widget, integrate it into your front-end as shown below:

<Swap configs={configs} />

The configs object contains the necessary configuration for the swap widget to function correctly. An example configuration is as follows:

const configs: SwapPropsObjectType = {
  connectionConfig: CurrentConfig,   // Pass the connection config object
  mode: 'dark',                      // Set the theme to either 'dark' or 'light'
  onCompletion: handleSwapCallback,   // Callback function triggered after swap
  swapFromCurrSymbol: 'USDT',         // OPTIONAL: Currency symbol to swap from (default: USDT)
  swapToCurrSymbol: 'CRYPTO5',        // OPTIONAL: Index symbol to swap to (default: ANFI)
};

SwapPropsObjectType Definition

The widget requires a configuration object of type SwapPropsObjectType:

interface SwapPropsObjectType {
  connectionConfig: Config;                 // Configuration for connecting to the smart contract
  mode: modeType;                           // Choose between 'dark' or 'light' mode
  swapFromCurrSymbol?: allowedIndexSymbolProps | allowedMintSymbolProps  // Symbol for currency to swap from
  swapToCurrSymbol?: allowedIndexSymbolProps | allowedMintSymbolProps    // Symbol for index to swap to
  onCompletion: (data: SwapResult) => void; // Callback to handle swap completion
}

Types and Definitions

allowedMintSymbolProps

The allowed symbols for minting are:

type allowedMintSymbolProps = 'USDT' | 'USDC';

allowedIndexSymbolProps

The allowed index symbols for swapping are:

type allowedIndexSymbolProps = 'ANFI' | 'CRYPTO5' | 'MAG7' | 'ARBEI';

modeType

The widget can either be in dark or light mode:

type modeType = 'dark' | 'light';

SwapResult Interface

The SwapResult interface defines the structure of the callback data returned upon swap completion:

interface SwapResult {
  success: boolean;                  // Indicates if the swap was successful
  message: string;                   // Message describing the outcome of the swap
  error?: Error;                     // Optional error field if the swap fails
  receipt?: TransactionReceipt;      // Contains the transaction receipt in case of write functions
}

The onCompletion function will be invoked with the result of the swap, providing details such as whether the swap was successful, any errors, and the transaction receipt if available.

Example Widget Integration

Here's an example of how to integrate the swap widget into your project:

import { Swap, Config, SwapResult } from 'smart-contract-interaction';

const CurrentConfig: Config = {
  env: Environment.WALLET_EXTENSION, // Wallet extension environment (e.g., MetaMask)
  rpc: {
    local: 'http://localhost:8545',
    mainnet: 'https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID',
    wallet: window.ethereum
  },
  wallet: {
    address: '0xYourWalletAddress',
    privateKey: '0xYourPrivateKey'
  }
};

const configs: SwapPropsObjectType = {
  connectionConfig: CurrentConfig,
  mode: 'dark',                      // Set mode to 'dark'
  swapFromCurrSymbol: 'USDT',         // Swap from USDT
  swapToCurrSymbol: 'CRYPTO5',        // Swap to CRYPTO5 index
  onCompletion: (data: SwapResult) => {
    if (data.success) {
      console.log('Swap successful:', data.receipt);
    } else {
      console.error('Swap failed:', data.error);
   }
  }
};

<Swap configs={configs} />

The onCompletion callback will handle the result of the swap, providing either the transaction receipt for successful swaps or an error message if the swap fails.

License

This package is licensed under the MIT License.