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

@solxtence/solana-swap

v1.0.13

Published

A Solana library for swapping any tokens easily!

Downloads

206

Readme

Solana Swap

Package for performing token swaps on Solana - Jito integrated 🚀

Made with the Public and Free Swap API by Solxtence <3

Useful for Sniper Bots, Volume Bots, Trading bots, Swap Platforms, DeFi applications...

Supported Platforms ⭐️

  • Pump.fun
  • Moonshot
  • Raydium
  • Raydium CPMM
  • Orca
  • Jupiter AMMs

⚠️ Important Security Note

Your private key is never sent to the Solxtence API or any third party. It's only used locally to sign the transaction in your code. However, it's always recommended to review the code of any packages you are using for potential security issues or malicious code.

Prerequisites

Installation

  1. Create a new project folder

    mkdir my-solana-swap
    cd my-solana-swap
  2. Initialize your project

    npm init -y
  3. Install the solana-swap package

    npm i @solxtence/solana-swap
  4. Create a new file named swap.js in your project folder

Building Your Swap Script

Open swap.js in a text editor and let's build the script step by step:

  1. Import required modules:

    const { Keypair } = require("@solana/web3.js");
    const bs58 = require("bs58");
    const { SolanaSwap } = require("@solxtence/solana-swap");

    This imports the necessary functions and classes we'll use.

  2. Create the main function:

    async function swapIt(useJito = false, jitoTip = 0.002) {
      // `jitoTip` in SOL amount
      // We'll add the swap logic here
    }

    This function will contain our swap logic.

  3. Set up the keypair:

    const privateKey = "YOUR_PRIVATE_KEY_HERE";
    const keypair = Keypair.fromSecretKey(bs58.decode(privateKey));

    Replace YOUR_PRIVATE_KEY_HERE with your actual Solana wallet private key. This creates a keypair for signing transactions.

  4. Initialize SolanaSwap:

    const solanaSwap = new SolanaSwap(
      keypair,
      "https://api.mainnet-beta.solana.com"
    );

    This sets up the SolanaSwap instance with your keypair and a Solana RPC endpoint.

  5. Get swap instructions:

    const swapResponse = await solanaSwap.getSwapInstructions(
      "So11111111111111111111111111111111111111112", // From Token (SOL)
      "4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R", // To Token (example)
      0.005, // Amount to swap
      10, // Slippage tolerance (in percentage)
      keypair.publicKey.toBase58(),
      0.0005, // Priority fee
      useJito ? jitoTip : undefined  // If `useJito` is true, a jito tip instruction will be included in the TX
    );

    This fetches the instructions from our Swap API for the swap. Customize the token addresses, amount, slippage, and fees as needed.

  6. Perform the swap:

    try {
      const txid = await solanaSwap.performSwap(swapResponse, {
        sendConfig: { skipPreflight: true },
        maxConfirmationAttempts: 30,
        confirmationTimeout: 500,
        commitmentLevel: "processed",
        useJito,
      });
    
      console.log("TX Hash:", txid);
      console.log("TX on Solscan:", `https://solscan.io/tx/${txid}`);
    } catch (error) {
      console.error("Error while performing the swap:", error.message);
    }

    This executes the swap and handles the result, whether successful or not.

  7. Call the function:

    swapIt(false); // Set to true to use Jito for faster transactions

    If you want to send the transaction with Jito, excute the function like so:

    swapIt(true, 0.003); // Adjust the number of your jito tip

Find the full example here.

Running Your Script

Run your script using:

node swap.js

Important Notes

  • Always double-check token addresses and amounts before swapping
  • Keep your private key secret and secure
  • It's good practice to review any code that interacts with your wallet or performs financial transactions

Troubleshooting

  • If you get an error about missing modules, make sure you've installed all required packages (step 3)
  • If the swap fails, check that you have enough balance and that the token addresses are correct
  • If any unexpected error happens, just open an issue here and we will fix it as soon as possible <3

Remember, when dealing with real funds, start with small amounts to test everything works correctly!

Resources

FAQ

Is there a fee for using this API?

No, the Swap API is completely free - no fees included!