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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@bbachain/spl-token-swap

v0.1.3

Published

JavaScript/TypeScript bindings for the Solana Program Library (SPL) Token Swap program.

Downloads

23

Readme

Token Swap JS Bindings

JavaScript/TypeScript bindings for the Solana Program Library (SPL) Token Swap program.

Installation

npm install @bbachain/spl-token-swap

Usage

Creating a Token Swap

import { 
  TokenSwap, 
  createInitializeInstruction,
  CurveType,
  Fees,
  SwapCurve 
} from '@bbachain/spl-token-swap';
import { Connection, PublicKey, Transaction } from '@bbachain/web3.js';

// Define fees structure
const fees: Fees = {
  tradeFeeNumerator: BigInt(25),        // 0.25% trade fee
  tradeFeeDenominator: BigInt(10000),
  ownerTradeFeeNumerator: BigInt(5),    // 0.05% owner fee
  ownerTradeFeeDenominator: BigInt(10000),
  ownerWithdrawFeeNumerator: BigInt(0),
  ownerWithdrawFeeDenominator: BigInt(0),
  hostFeeNumerator: BigInt(0),
  hostFeeDenominator: BigInt(0),
};

// Define swap curve (constant product like Uniswap)
const swapCurve: SwapCurve = {
  curveType: CurveType.ConstantProduct,
  calculator: new Array(32).fill(0), // 32 bytes for curve parameters
};

// Create initialize instruction
const initializeIx = createInitializeInstruction(
  {
    tokenSwap: tokenSwapAccount,
    authority: swapAuthority,
    tokenA: tokenAAccount,
    tokenB: tokenBAccount,
    poolMint: poolMint,
    feeAccount: feeAccount,
    destination: destinationAccount,
    tokenProgram: TOKEN_PROGRAM_ID,
  },
  { fees, swapCurve }
);

Performing a Swap

import { createSwapInstruction } from '@bbachain/spl-token-swap';

const swapIx = createSwapInstruction(
  {
    tokenSwap: tokenSwapAccount,
    authority: swapAuthority,
    userTransferAuthority: userAuthority,
    source: userSourceAccount,
    swapSource: poolTokenAAccount,
    swapDestination: poolTokenBAccount,
    destination: userDestinationAccount,
    poolMint: poolMint,
    feeAccount: feeAccount,
    tokenProgram: TOKEN_PROGRAM_ID,
    hostFeeAccount: hostFeeAccount, // Optional
  },
  {
    amountIn: BigInt(1000000),      // 1 token (6 decimals)
    minimumAmountOut: BigInt(950000), // Minimum 0.95 tokens out (slippage protection)
  }
);

Adding Liquidity

import { createDepositAllTokenTypesInstruction } from '@bbachain/spl-token-swap';

const depositIx = createDepositAllTokenTypesInstruction(
  {
    tokenSwap: tokenSwapAccount,
    authority: swapAuthority,
    userTransferAuthority: userAuthority,
    sourceA: userTokenAAccount,
    sourceB: userTokenBAccount,
    intoA: poolTokenAAccount,
    intoB: poolTokenBAccount,
    poolMint: poolMint,
    poolAccount: userPoolAccount,
    tokenProgram: TOKEN_PROGRAM_ID,
  },
  {
    poolTokenAmount: BigInt(1000000),     // Pool tokens to mint
    maximumTokenAAmount: BigInt(1000000), // Max token A to deposit
    maximumTokenBAmount: BigInt(1000000), // Max token B to deposit
  }
);

Removing Liquidity

import { createWithdrawAllTokenTypesInstruction } from '@bbachain/spl-token-swap';

const withdrawIx = createWithdrawAllTokenTypesInstruction(
  {
    tokenSwap: tokenSwapAccount,
    authority: swapAuthority,
    userTransferAuthority: userAuthority,
    poolMint: poolMint,
    source: userPoolAccount,
    fromA: poolTokenAAccount,
    fromB: poolTokenBAccount,
    userAccountA: userTokenAAccount,
    userAccountB: userTokenBAccount,
    feeAccount: feeAccount,
    tokenProgram: TOKEN_PROGRAM_ID,
  },
  {
    poolTokenAmount: BigInt(1000000),    // Pool tokens to burn
    minimumTokenAAmount: BigInt(950000), // Min token A to receive
    minimumTokenBAmount: BigInt(950000), // Min token B to receive
  }
);

Reading Token Swap State

const connection = new Connection('https://api.mainnet-beta.solana.com');

// Load token swap account
const tokenSwap = await TokenSwap.fromAccountAddress(
  connection,
  tokenSwapAccount
);

console.log('Token A:', tokenSwap.tokenA.toBase58());
console.log('Token B:', tokenSwap.tokenB.toBase58());
console.log('Pool Mint:', tokenSwap.poolMint.toBase58());
console.log('Trade Fee:', tokenSwap.fees.tradeFeeNumerator.toString(), '/', tokenSwap.fees.tradeFeeDenominator.toString());
console.log('Curve Type:', tokenSwap.swapCurve.curveType);

Types

CurveType

enum CurveType {
  ConstantProduct = 0, // x * y = k (like Uniswap)
  ConstantPrice = 1,   // Fixed exchange rate
  Stable = 2,          // StableSwap curve
  Offset = 3,          // Offset curve
}

Fees

type Fees = {
  tradeFeeNumerator: bigint;       // Trade fee numerator
  tradeFeeDenominator: bigint;     // Trade fee denominator
  ownerTradeFeeNumerator: bigint;  // Owner fee numerator
  ownerTradeFeeDenominator: bigint; // Owner fee denominator
  ownerWithdrawFeeNumerator: bigint; // Owner withdraw fee numerator
  ownerWithdrawFeeDenominator: bigint; // Owner withdraw fee denominator
  hostFeeNumerator: bigint;        // Host fee numerator
  hostFeeDenominator: bigint;      // Host fee denominator
};

Error Handling

The library includes comprehensive error types that match the Rust program:

import { 
  ExceededSlippageError,
  InvalidInputError,
  CalculationFailureError,
  errorFromCode 
} from '@bbachain/spl-token-swap';

try {
  // Transaction execution
} catch (error) {
  const programError = errorFromCode(error.code);
  if (programError instanceof ExceededSlippageError) {
    console.log('Swap failed due to slippage');
  }
}

Program Address

The Token Swap program ID: SwapD4hpSrcB23e4RGdXPBdNzgXoFGaTEa1ZwoouotX

License

Apache-2.0