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

@tensor-oss/tensorswap-sdk

v4.5.0

Published

Anchor/JS SDK for interacting with TensorSwap, TensorWhitelist and TensorBid.

Downloads

16,791

Readme

Tensorswap SDK

Getting Started

From npm/yarn (RECOMMENDED)

# yarn
yarn add @tensor-oss/tensorswap-sdk
# npm
npm install @tensor-oss/tensorswap-sdk

From source

git clone https://github.com/tensor-hq/tensorswap-sdk.git
cd tensorswap-sdk/
yarn
# Build JS files
yarn tsc

Example Code

Working examples can be found under examples/.

const { AnchorProvider, Wallet } = require("@project-serum/anchor");
const { Connection, Keypair, PublicKey } = require("@solana/web3.js");
const {
  TensorSwapSDK,
  TensorWhitelistSDK,
  computeTakerPrice,
  TakerSide,
  castPoolConfigAnchor,
  findWhitelistPDA,
} = require("@tensor-oss/tensorswap-sdk");

const conn = new Connection("https://api.mainnet-beta.solana.com");
const provider = new AnchorProvider(conn, new Wallet(Keypair.generate()));
const swapSdk = new TensorSwapSDK({ provider });
const wlSdk = new TensorWhitelistSDK({ provider });

// ========= Compute current price (Buy + sell)

// Fetch the pool PDA for its settings.
const pool = await swapSdk.fetchPool(new PublicKey("<address of target pool>"));
const config = castPoolConfigAnchor(pool.config);

const price = computeTakerPrice({
  takerSide: TakerSide.Buy, // or TakerSide.Sell for selling
  extraNFTsSelected: 0,

  // These fields can be extracted from the pool object above.
  config,
  takerSellCount: pool.takerSellCount,
  takerBuyCount: pool.takerBuyCount,
  maxTakerSellCount: pool.maxTakerSellCount,
  statsTakerSellCount: pool.stats.takerSellCount,
  slippage: <number>, // add optional slippage: in case pool updates on-chain
});



// ========= Buying
{
  const {
    tx: { ixs },
  } = await swapSdk.buyNft({
    // Whitelist PDA address where name = tensor slug (see TensorWhitelistSDK.nameToBuffer)
    whitelist,
    // NFT Mint address
    nftMint,
    // Buyer ATA account (destination)
    nftBuyerAcc,
    // owner of NFT (in pool PDA)
    owner,
    // buyer
    buyer,
    // PoolConfig object: construct from pool PDA
    config,
    // max price buyer is willing to pay (add ~0.1% for exponential pools b/c of rounding differences)
    // see `computeTakerPrice` above to get the current price
    maxPrice
  });
  const buyTx = new Transaction(...ixs);
}

// ========= Selling

// uuid = Tensor collection ID (see "Collection UUID" API endpoint below)
const uuid = "..."

// Remove "-" symbols from uuid, so it's within the 32 seed length limit. Additionally convert the uuid to a Uint8Array
const uuidArray = Buffer.from(uuid.replaceAll("-", "")).toJSON().data;

// Finding the PDA address
const wlAddr = findWhitelistPDA({uuid: uuidArray})[0];

// Step 1: Prepare the mint proof PDA (if required).
{
  const wl = await wlSdk.fetchWhitelist(wlAddr);

  // Proof is only required if rootHash is NOT a 0 array, o/w not necessary!
  if(JSON.stringify(wl.rootHash) !== JSON.stringify(Array(32).fill(0))) {
    // Off-chain merkle proof (see "Mint Proof" API endpoint below).
    const proof = ...;

    const {
      tx: { ixs },
    } = await wlSdk.initUpdateMintProof({
      // User signing the tx (the seller)
      user,
      whitelist: wlAddr,
      // (NFT) Mint address
      mint,
      proof,
    });
    const proofTx = new Transaction(...ixs);
  }
}

// Step 2: Send sell tx.
{
  const {
    tx: { ixs },
  } = await swapSdk.sellNft({
    type: "token", // or 'trade' for a trade pool
    whitelist: wlAddr,
    // NFT Mint address
    nftMint,
    // Token account holding seller's mint
    nftSellerAcc,
    // owner of NFT (in pool PDA)
    owner,
    // seller
    seller,
    // PoolConfig object: construct from pool PDA
    config,
    // min price seller is willing to receive (sub ~0.1% for exponential pools b/c of rounding differences)
    // see `computeTakerPrice` above to get the current price
    minPrice,
  });
  const sellTx = new Transaction(...ixs);
}

// ========= TODO: initPool / closePool / editPool / withdrawNft / depositNft / withdrawSol / depositSol

API Access

Docs can be found here.

Ping us in our Discord for access.

Collection UUID

You can query all Tensor collections and their metadata, including their id which corresponds to whitelist.uuid with this query.

Alternative, you can find a collection for a corresponding mint:

  1. Get the mint's Tensor slug
  2. Get the collection's ID
  3. Use the ID as the uuid for the whitelist

The ID for a collection will never change, so feel free to cache this locally.

A mint's collection will almost always never change (99% of the time), so feel free to cache this as needed and update if necessary.

Mint Proof

For selling and depositing and for some collections (where whitelist.rootHash is not a 0-zero), you will need to fetch the off-chain merkle proofs we use for collection whitelisting from our API.

Endpoint + example can be found here.