1.0.2 • Published 10 months ago

@js-code-crafter/solana-token v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

SolanaToken - A JavaScript Library for Solana SPL Token Management

Welcome to the SolanaToken npm package, a comprehensive library designed to simplify the interaction with the Solana blockchain's SPL tokens. It provides an easy-to-use interface for wallet management, token transfers, and interaction with different Solana clusters.

Table of Contents

Installation

You can install the solana-token package using npm. Run the following command in your terminal:

npm install @js-code-crafter/solana-token

Usage

This package provides utility methods to interact with Solana\'s SPL token ecosystem. You can manage wallets, carry out token transfers, and interact with various clusters such as devnet, testnet, and mainnet.

To use this package, simply import it into your JavaScript application:

const { splFactory, Spl, walletFactory, Wallet } = require("solana-token");

Wallet Management

You can create or restore wallets using the provided methods. For example, to restore a wallet from a mnemonic:

const wallet = Wallet.restoreFromMnemonic("your mnemonic seed phrase here");

Token Management

Create Token

const token = await spl.createToken(wallet);

Get Token Balance

const balance = await spl.getTokenAccountBalance(accountPublicKey);

Transfer Tokens

You can transfer SPL tokens between two wallets:

await spl.transferToken(tokenPublicKey, fromWallet, toWallet, amount);

Transaction Management

The library allows you to manage transactions easily:

Begin a Transaction

const transaction = spl.beginTransaction();

End a Transaction

const confirmation = await spl.endTransaction(transaction);

Example Usage

Here’s a simple example of how to use the SolanaToken package with an Express.js application to manage wallets and transfer tokens:

const express = require("express");
const router = express.Router();
const { splFactory, Spl, walletFactory, Wallet } = require("solana_token");
const { PublicKey } = require("@solana/web3.js");

router.get("/", async (req, res) => {
  const w1 = walletFactory.restoreFromMnemonic("your mnemonic here");
  const w2 = walletFactory.restoreFromMnemonic("another mnemonic here");
  const s = splFactory.create(splFactory.clusters().dev);

  // Airdrop some SOL to the wallets
  await s.getSomeSol(w1);
  await s.getSomeSol(w2);

  // Getting balances
  const w1Balance = await s.getSolBalance(w1);
  const w2Balance = await s.getSolBalance(w2);

  // Token actions began
  const tokenPublicKey = new PublicKey("your_token_address_here");
  const account1 = await s.getOrCreateTokenAccount(w1, tokenPublicKey);
  const account2 = await s.getOrCreateTokenAccount(w2, tokenPublicKey);

  // Transfer tokens
  await s.transferToken(tokenPublicKey, w1, w2, "0.1");

  res.send({
    w1Balance: w1Balance,
    w2Balance: w2Balance,
    account1: account1.tokenAccountPublicKey.toString(),
    account2: account2.tokenAccountPublicKey.toString(),
  });
});

module.exports = router;

Some Methods

connect(cluster): Connect to a specified Solana cluster.
getSomeSol(publicKey, amount): Request an airdrop of SOL to the specified wallet.
transferSol(from, to, amount): Transfer SOL tokens from one wallet to another.
transferToken(tokenPublicKey, fromKeypair, toKeypair, amount): Transfer SPL tokens from one wallet to another.

The code is thoroughly documented. Refer to the code for additional details.

Contributing

We welcome contributions! If you'd like to enhance the functionality of the solana-token package, please fork the repository and submit a pull request. For large changes, please open an issue first to discuss what you would like to change.

License

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