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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@thespidercode/openbook-swap

v0.0.51

Published

Ready-to-use swap tool using Openbook DEX

Readme

OpenBookSwap Overview

OpenBookSwap aims to make token swaps on Solana easy with the Serum/OpenBook DEX smart contract v1 (v2 coming soon). This repository is a plug and play react component that can be implemented into your web application within minutes. Example: OpenBonk.io

Serum/OpenBook DEX

The OpenBook DEX is a Solana smart contract, forked from Serum, that provides a complete limit order book on-chain.

Features

  • Customizable Colors
  • Only 1 Transaction:
    • Create an Open Orders Account
    • Create the Associated Token Accounts
    • Initialize Accounts
    • Send an IOC Order (Immediate or Cancel)
    • Settle
  • US Dollar Translations
  • RPC Built In
  • Low Liquidity Warnings

Support

This project has received support from the Solana Foundation and Bonk Inu. We are committed to providing support atleast until July 2024. Feel free to contact @swolsol on telegram for support.

How to Use?

Installation

npm i @thespidercode/openbook-swap
npm i @solana/web3.js

*Optional*
npm install @solana/wallet-adapter-react

Usage

Step 1/2 Define your Markets

Decide which markets you want to include. Add a market.constant.ts file in your project, which is an array of SwapMarket objects.

SwapMarket

interface SwapMarket {
    address: PublicKey;
    base: SwapMarketToken;
    quote: SwapMarketToken;
    minBase: number;
    swapMargin: number;
}

Example of market.constant

import { SwapMarket } from "@thespidercode/openbook-swap";
import { PublicKey } from "@solana/web3.js";

export const marketPairs: SwapMarket[] =[
    {
        address: new PublicKey('8PhnCfgqpgFM7ZJvttGdBVMXHuU4Q23ACxCvWkbs1M71'),
        base: {
            name: "BONK",
            logo: "https://s2.coinmarketcap.com/static/img/coins/64x64/23095.png",
            mint: new PublicKey('DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263'),
            vault: new PublicKey('A9yRKSx8SyqNdCtCMUgr6wDXUs1JmVFkVno6FcscSD6m'),
        },
        quote: {
            name: "USDC",
            logo: "https://s2.coinmarketcap.com/static/img/coins/64x64/3408.png",
            mint: new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'),
            vault: new PublicKey('D9dojzvwJGs4q3Cx8ytvD8kWVVZszoVKvPZEZ5D8PV1Y'),
        },
        minBase: 1000,
        swapMargin: 0.0004
    },
    {
        address: new PublicKey('Hs97TCZeuYiJxooo3U73qEHXg3dKpRL4uYKYRryEK9CF'),
        base: {
            name: "BONK",
            logo: "https://s2.coinmarketcap.com/static/img/coins/64x64/23095.png",
            mint: new PublicKey('DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263'),
            vault: new PublicKey('AVnL1McPPrn1dZyHGThwXzwYaHBp6sxB44vXoETPqH45'),
        },
        quote: {
            name: "SOL",
            logo: "https://s2.coinmarketcap.com/static/img/coins/64x64/5426.png",
            mint: new PublicKey('So11111111111111111111111111111111111111112'),
            vault: new PublicKey('8KftabityJoWgvUb6wwAPZww8mYmLq8WTMuQGGPoGiKM'),
        },
        minBase: 1000,
        swapMargin: 0.0004
    },
]

Step 2/2 Call the Package

There are two options to call the package:

  • Let the package handle the transaction by passing an instance of the wallet adaptor. This requires installing the wallet adaptor as mentioned above. The package will then manage the transaction delivery internally.
  • Manually manage the delivery of the transaction. In this approach, the package provides a transaction that is ready to be sent. You have the responsibility of handling the transaction delivery process yourself.

Using wallet adaptor

import { WalletContextState } from "@solana/wallet-adapter-react";
import { SwapContainer, SwapError, SwapLoading, SwapSuccess } from "@thespidercode/openbook-swap";
import { marketPairs } from '../constants/market.constant';
import { Connection } from '@solana/web3.js';

export function Swap(props: { connection: Connection, wallet: WalletContextState }) {
  const { connection, wallet } = props;
    
    const onSwapError = (error: SwapError): void => {
        console.log(error);
    }

    const onSwapLoading = (loading: SwapLoading): void => {
        console.log(loading);
    }

    const onSwapSuccess = (success: SwapSuccess): void => {
        console.log(success);
    }

    return (
        <div>
            <SwapContainer
                title='Swap'
                colors={{
                    primary: "grey",
                    secondary: "#3a3a3a",
                    background: "#1b1717",
                    swapButton: "grey",
                    text: "#fff",
                }}
                markets={marketPairs} 
                connection={connection}
                onSwapError={onSwapError}
                onSwapLoading={onSwapLoading}
                onSwapSuccess={onSwapSuccess}
                wallet={wallet}
            />
        </div>
        
    )
}

Not using wallet adaptor (don't forget to replace RPC URL with your RPC)

import * as web3 from '@solana/web3.js';
import { ManualSwap, SwapContainer, SwapError } from "@thespidercode/openbook-swap";
import { marketPairs } from '../constants/market.constant';
import { useState } from 'react';

export function App() {
    const connection = new web3.Connection('<RPC URL>');
    const [provider, setProvider] = useState<any>(null);

    const getProvider = async (): Promise<any> => {
      if ("solana" in window) {
        try {
          await ((window  as any).solana).connect();
          const provider = (window  as any).solana;
          if ((provider as any).isPhantom) {
            setProvider(provider);
            return provider;
          }
        } catch (error) {
          console.log(error);
        }
      } else {
        window.open("https://www.phantom.app/", "_blank");
      }
    }
    
    const onSwapError = (error: SwapError): void => {
        console.log(error);
    }

    const onSwap = async (loading: ManualSwap): Promise<void> => {
      try {
        const provider = await getProvider();
        if (!provider) return;
        
        if (!loading.swapResult.transaction || !provider.publicKey) {
          console.log('Loading object is missing required properties');
          return;
        }
        
        loading.setLoadingSwap(true);

        const latestBlockHash = await connection.getLatestBlockhash();
        loading.swapResult.transaction.transaction.recentBlockhash = latestBlockHash.blockhash;
        loading.swapResult.transaction.transaction.feePayer = new web3.PublicKey(provider.publicKey);

        for (let i = 0; i < loading.swapResult.transaction.signers.length; i++) {
          loading.swapResult.transaction.transaction.partialSign(loading.swapResult.transaction.signers[i]);
        }

        const signed = await provider.signTransaction(loading.swapResult.transaction.transaction);
        const orderSignature = await connection.sendRawTransaction(signed.serialize());

        await connection.confirmTransaction({
          signature: orderSignature, 
          blockhash: latestBlockHash.blockhash, 
          lastValidBlockHeight: latestBlockHash.lastValidBlockHeight
        }, 'finalized');

        loading.refreshUserBalances(provider.publicKey);
        loading.setLoadingSwap(false);
      } catch (error) {
        console.log(error);
        if (loading.setLoadingSwap) {
          loading.setLoadingSwap(false);
        }
      }
    }

    return (
        <div>
            {
                provider && provider.publicKey ?
                <SwapContainer
                    title='Swap'
                    colors={{
                        primary: "grey",
                        secondary: "#3a3a3a",
                        background: "#1b1717",
                        swapButton: "grey",
                        text: "#fff",
                    }}
                    markets={marketPairs} 
                    connection={connection}
                    onSwapError={onSwapError}
                    onSwap={onSwap}
                    manualTransaction={provider.publicKey}
                /> : 
                <button onClick={() => getProvider()}>CONNECT</button>
            }
        </div>
        
    )
}

Examples

You can find ready to use examples in the examples folder