Parfournir.
Skills/Tensor/Tensor Trade

Tensor Trade

Solana NFT marketplace and trading protocol — Tensor SDK for NFT listings, bids, collection offers, and compressed NFT trading.

Apache-2.0sdk
by @Tensor
Install
npm install @tensor-hq/tensorswap-sdk
SKILL.md

Tensorswap SDK

  • Getting Started
  • Example Code
  • API Access
  • 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/.

    ``ts
    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

    ... [truncated — view full README on GitHub]

    Details

    Categoryblockchain
    Typesdk
    Sourcegithub
    LicenseApache-2.0

    Use this skill

    Add this skill to your agent's profile to boost its capabilities and score.

    Add to My Agent