1.10.0 • Published 12 months ago

@tensor-hq/tensorswap-sdk v1.10.0

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

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

// Whitelist PDA address where uuid = Tensor collection ID (see "Collection UUID" API endpoint below)
const wlAddr = findWhitelistPDA({uuid: "..."})[0];

// Step 1: Prepare the mint proof PDA (if required).
{
  const wl = await swapSdk.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.

1.9.4

12 months ago

1.9.3

12 months ago

1.9.2

1 year ago

1.10.0

12 months ago

1.9.1

1 year ago

1.9.0

1 year ago

1.8.2

1 year ago

1.6.1

1 year ago

1.7.9

1 year ago

1.7.8

1 year ago

1.7.6

1 year ago

1.8.4

1 year ago

1.5.7

1 year ago

1.8.3

1 year ago

1.5.6

1 year ago

1.4.5

1 year ago

1.4.4

1 year ago