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/tcomp-sdk

v6.0.0

Published

Anchor/JS SDK for interacting with TCOMP.

Downloads

7,482

Readme

TComp SDK

Getting Started

From npm/yarn (RECOMMENDED)

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

From source

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

Example Code

import { AnchorProvider, Wallet } from "@coral-xyz/anchor";
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
import { getLeafAssetId } from "@tensor-hq/tensor-common";
import {
  findListStatePda,
  findTreeAuthorityPda,
  TCompSDK,
} from "@tensor-oss/tcomp-sdk";
import BN from "bn.js";

const conn = new Connection("https://api.mainnet-beta.solana.com");
const provider = new AnchorProvider(
  conn,
  new Wallet(Keypair.generate()),
  AnchorProvider.defaultOptions()
);
const tcompSdk = new TCompSDK({ provider });

// Listing cNFT
const {
  tx: { ixs },
} = await tcompSdk.list({
  // Retrieve these fields from DAS API
  merkleTree,
  root,
  canopyDepth,
  index,
  proof,
  dataHash,
  creatorsHash,
  delegate: delegatePk,

  owner: ownerPk,
  rentPayer: rentPayerPk, // optional: payer and recipient of listing rent
  amount: new BN(priceLamports), // in lamports
  currency: null, // optional: list for SOL or SPL tokens
  expireInSec: expireIn ? new BN(expireIn) : null, // seconds until listing expires
  privateTaker: takerPk, // optional: only this wallet can buy this listing
});


// Fetching the listing
const nonce = new BN(index);
const [treeAuthority] = findTreeAuthorityPda({ merkleTree });
const assetId = getLeafAssetId(merkleTree, nonce);
const listState = findListStatePda({ assetId });
const {
  assetId: listStateAssetId,
  owner,
  amount,
  currency,
  expiry,
  privateTaker,
  makerBroker,
  rentPayer,
} = await tcompSdk.fetchListState(listState);


// Buying cNFT
const {
  tx: { ixs },
} = await tcompSdk.buy({
  // Retrieve these fields from DAS API
  merkleTree,
  root,
  canopyDepth,
  index,
  proof,
  sellerFeeBasisPoints,
  // For constructing metaHash, see example below
  metaHash,
  creators,

  payer: payerPk,
  buyer: buyerPk,
  owner: ownerPk,
  makerBroker,
  rentDest,
  maxAmount: new BN(priceLamports),
  optionalRoyaltyPct: 100, // currently required to be 100% (enforced)
});


// Bidding on a single cNFT
const {
  tx: { ixs },
} = await tcompSdk.bid({
  owner: ownerPk,
  rentPayer: rentPayerPK, // optional: payer and recipient of bid rent
  amount: new BN(priceLamports),
  expireInSec: expireIn ? new BN(expireIn) : null, // seconds until listing expires
  privateTaker: takerPk, // optional: only this wallet can sell into this bid
  bidId: new PublicKey(assetId), // asset ID of nft to bid on
  targetId: new PublicKey(assetId), // asset ID of nft to bid on
  target: Target.AssetId,
  quantity: 1,

  // Ignore these for now (advanced usage)
  margin: null,
  field: null,
  fieldId: null,
});

Constructing metaHash

const axios = require('axios');
const BN = require('bn.js');
const { PublicKey } = require("@solana/web3.js");
const { keccak_256 } = require('js-sha3');
const { computeMetadataArgsHash } = require("@tensor-hq/tensor-common");
const { TokenStandard } = require('@metaplex-foundation/mpl-bubblegum');

const url = `https://mainnet.helius-rpc.com/?api-key=<YOUR_HELIUS_API_KEY>`

const constructMetaHash = async (mint) => {

  // query DAS API for asset info
  const assetRes = await axios.post(url, {
    jsonrpc: '2.0',
    id: '0',
    method: 'getAsset',
    params: {
      id: mint
    }
  });
  const {
    compression,
    content,
    royalty,
    creators,
    uses,
    grouping,
    supply,
    ownership: { owner, delegate },
    mutable,
  } = assetRes.data.result;
  const coll = grouping.find((group) => group.group_key === "collection")?.group_value;
  const tokenStandard = content.metadata.token_standard
  const dataHashBuffer = new PublicKey(compression.data_hash).toBuffer();

  // construct metadataArgs to hash later
  // ordering follows https://docs.metaplex.com/programs/token-metadata/accounts
  var metadataArgs = {
    name: content?.metadata?.name ?? "",
    symbol: content?.metadata?.symbol ?? " ",
    uri: content?.json_uri ?? "",
    sellerFeeBasisPoints: royalty.basis_points,
    creators: creators.map((creator) => ({
      address: new PublicKey(creator.address),
      share: creator.share,
      verified: creator.verified,
    })),
    primarySaleHappened: royalty.primary_sale_happened,
    isMutable: mutable,
    editionNonce: supply?.edition_nonce != null ? supply!.edition_nonce : null,
    tokenStandard: tokenStandard === "Fungible" ? TokenStandard.Fungible :
      tokenStandard === "NonFungibleEdition" ? TokenStandard.NonFungibleEdition :
        tokenStandard === "FungibleAsset" ? TokenStandard.FungibleAsset :
          TokenStandard.NonFungible,

    // if Helius shows a collection in groupings for a cNFT then it's verified
    collection: coll ? { key: new PublicKey(coll), verified: true } : null,
    uses: uses
      ? {
        useMethod: uses.use_method === "Burn" ? 0 : uses.use_method === "Multiple" ? 1 : 2,
        remaining: uses.remaining,
        total: uses.total,
      }
      : null,

    // currently always Original (Token2022 not supported yet)
    tokenProgramVersion: 0,
  };
  const originalMetadata = { ...metadataArgs };
  const sellerFeeBasisPointsBuffer = new BN(royalty.basis_points).toBuffer("le", 2);

  // hash function on top of candidate metaHash to compare against data_hash
  const makeDataHash = (metadataArgs) =>
    Buffer.from(
      keccak_256.digest(
        Buffer.concat([
          new PublicKey(computeMetadataArgsHash(metadataArgs)).toBuffer(),
          sellerFeeBasisPointsBuffer,
        ])
      )
    );

  // try original metadataArgs
  var hash = makeDataHash(metadataArgs);
  if (hash.equals(dataHashBuffer)) return computeMetadataArgsHash(metadataArgs);

  // try tokenStandard = null
  metadataArgs.tokenStandard = null;
  hash = makeDataHash(metadataArgs);
  if (hash.equals(dataHashBuffer)) return computeMetadataArgsHash(metadataArgs);

  // try name + uri = "", tokenStandard = null
  metadataArgs.name = "";
  metadataArgs.uri = "";
  hash = makeDataHash(metadataArgs);
  if (hash.equals(dataHashBuffer)) return computeMetadataArgsHash(metadataArgs);

  // try name + uri = "", tokenStandard = 0
  metadataArgs.tokenStandard = 0;
  hash = makeDataHash(metadataArgs);
  if (hash.equals(dataHashBuffer)) return computeMetadataArgsHash(metadataArgs);

  // try reversing creators
  metadataArgs.creators.reverse();
  metadataArgs.name = originalMetadata.name;
  metadataArgs.uri = originalMetadata.uri;
  metadataArgs.tokenStandard = originalMetadata.tokenStandard;
  hash = makeDataHash(metadataArgs);
  if (hash.equals(dataHashBuffer)) return computeMetadataArgsHash(metadataArgs);

  // can't match - return null
  return null;
};

constructMetaHash("8H6C2tGh5Yu5jGXUbFtZ17FQTDyBAEQ4LfGA527QLXYQ");