快速参考:DAS API为所有Solana资产提供统一接口。使用getAsset获取单个资产,使用getAssetsByOwner获取钱包持有,使用searchAssets进行过滤查询,并为压缩NFT提供专门方法。

什么是DAS?

数字资产标准(DAS)API是一个开源规范,提供了一个统一接口,用于在Solana上与所有类型的数字资产进行交互:
  • 常规NFT(非同质化代币)
  • 压缩NFT(状态压缩)
  • 同质化代币(包括SPL代币和Token-2022)
  • 铭文和SPL-20代币(实验性)

主网端点

https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY

开发网端点

https://devnet.helius-rpc.com/?api-key=YOUR_API_KEY
  • 统一访问所有Solana资产类型
  • 全面的元数据检索
  • 链下数据索引(Arweave, IPFS)
  • 支持压缩资产的Merkle证明
  • 验证代币的价格数据
  • 高级过滤和搜索功能

API方法

获取单个资产

这些方法允许您通过资产的ID检索特定资产的详细信息。
// Get a single asset by its ID
const url = `https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY`;

const getAsset = async (id) => {
  const response = await fetch(url, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      jsonrpc: "2.0",
      id: "my-request-id",
      method: "getAsset",
      params: {
        id: id,
      },
    }),
  });
  
  const data = await response.json();
  return data.result;
};

// Example usage
getAsset("FNt6A9Mfnqbwc1tY7uwAguKQ1JcpBrxmhczDgbdJy5AC");

API 参考

查看 getAsset 的详细文档

获取资产集合

这些方法允许您根据所有权、创建、权限或集合成员资格检索资产。
DAS API 方法中的所有分页从1开始(而不是0)。
// Get all assets owned by a wallet address
const url = `https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY`;

const getAssetsByOwner = async (ownerAddress) => {
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      jsonrpc: '2.0',
      id: 'my-request-id',
      method: 'getAssetsByOwner',
      params: {
        ownerAddress: ownerAddress,
        page: 1,
        limit: 1000,
        displayOptions: {
          showFungible: true, // Include SPL tokens
          showNativeBalance: true, // Include SOL balance
          showInscription: true, // Include inscription data
        },
      },
    }),
  });
  
  const { result } = await response.json();
  return result;
};

// Example usage
getAssetsByOwner("86xCnPeV69n6t3DnyGvkKobf9FdN2H9oiVDdaMpo2MMY");

API 参考

查看 getAssetsByOwner 的详细文档

高级查询方法

这些方法允许进行更专业的资产查询,包括交易历史、NFT版本和复杂的搜索过滤器。
// Search for assets with flexible criteria
const url = `https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY`;

const searchAssets = async (searchParams) => {
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      jsonrpc: '2.0',
      id: 'my-request-id',
      method: 'searchAssets',
      params: searchParams,
    }),
  });
  
  const { result } = await response.json();
  return result;
};

// Example: Search for NFTs with specific traits
searchAssets({
  ownerAddress: "86xCnPeV69n6t3DnyGvkKobf9FdN2H9oiVDdaMpo2MMY",
  grouping: ["collection", "J1S9H3QjnRtBbbuD4HjPV6RpRhwuk4zKbxsnCHuTgh9w"],
  traits: [
    { trait_type: "Background", values: ["Blue"] }
  ],
  limit: 100
});

API 参考

查看 searchAssets 的详细文档

处理特殊资产类型

可替代代币

Helius 已扩展 DAS API 以支持所有代币,包括普通的 SPL 代币(无元数据)和 Token-2022(及其扩展)。可替代代币支持通过 getAssetsByOwnersearchAssets 方法提供。
// Get all tokens for a wallet including price data
const url = `https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY`;

const getTokensWithPrices = async (ownerAddress) => {
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      jsonrpc: '2.0',
      id: 'my-request-id',
      method: 'getAssetsByOwner',
      params: {
        ownerAddress: ownerAddress,
        displayOptions: {
          showFungible: true,
        },
      },
    }),
  });

  const { result } = await response.json();
  
  // Filter tokens that have price data
  const tokensWithPrices = result.items.filter(
    asset => asset.token_info?.price_info
  );
  
  return tokensWithPrices;
};

// Example usage
getTokensWithPrices("86xCnPeV69n6t3DnyGvkKobf9FdN2H9oiVDdaMpo2MMY");

压缩 NFT

状态压缩 NFT(cNFT)需要特殊处理以进行诸如检索 Merkle 证明和交易历史等操作:
// Common operations with compressed NFTs
const url = `https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY`;

// 1. Get a compressed NFT
const getCompressedNft = async (id) => {
  const response = await fetch(url, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      jsonrpc: '2.0',
      id: 'my-request-id',
      method: 'getAsset',
      params: { id },
    }),
  });
  
  const { result } = await response.json();
  return result;
};

// 2. Get Merkle proof for verification
const getProof = async (id) => {
  const response = await fetch(url, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      jsonrpc: '2.0',
      id: 'my-request-id',
      method: 'getAssetProof',
      params: { id },
    }),
  });
  
  const { result } = await response.json();
  return result;
};

// 3. Get transaction history
const getTransactionHistory = async (id) => {
  const response = await fetch(url, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      jsonrpc: '2.0',
      id: 'my-request-id',
      method: 'getSignaturesForAsset',
      params: { 
        id,
        page: 1,
        limit: 100
      },
    }),
  });
  
  const { result } = await response.json();
  return result;
};

铭文 & SPL-20

您可以选择使用 showInscription 标志显示铭文和 SPL-20 代币数据。这是一个实验性功能。
// Get inscriptions for a wallet
const url = `https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY`;

const getInscriptionsForWallet = async (ownerAddress) => {
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      jsonrpc: '2.0',
      id: 'my-request-id',
      method: 'getAssetsByOwner',
      params: {
        ownerAddress: ownerAddress,
        displayOptions: {
          showInscription: true,
        },
      },
    }),
  });

  const { result } = await response.json();
  
  // Filter results to only include assets with inscription data
  const inscriptions = result.items.filter(
    asset => asset.inscription
  );
  
  return inscriptions;
};

// Example usage
getInscriptionsForWallet("6GmTFg5SCs4zGfDEidUAJjS5pSrXEPwW8Rpfs3RHrbc5");

链下数据

最佳实践

分页

  • 从第 1 页开始(而不是 0)
  • 使用合理的限制值(100-1000)
  • 为大型数据集实现健壮的分页

错误处理

  • 始终检查响应中的错误
  • 实现指数退避的重试机制
  • 处理无效参数等边缘情况

性能

  • 尽可能使用批处理方法
  • 在适当的时候缓存响应
  • 避免在一个请求可以完成时链式多个请求

安全

  • 切勿在客户端代码中暴露您的 API 密钥
  • 使用服务器端函数代理 API 请求
  • 设置适当的 CORS 策略

资源