> ## Documentation Index
> Fetch the complete documentation index at: https://www.helius.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 如何使用 getTokenSupply

> 了解 getTokenSupply 的使用案例、代码示例、请求参数、响应结构和提示。

[`getTokenSupply`](https://www.helius.dev/docs/api-reference/rpc/http/gettokensupply) RPC 方法返回特定 SPL 代币铸币的总供应量。这对于了解已创建代币的总体数量非常重要。

## 常见用例

* **显示代币信息：** 在浏览器或钱包界面上显示代币的总供应量。
* **代币经济学分析：** 了解代币的最大或当前总发行量。
* **验证：** 检查铸造账户本身报告的代币供应量。
* **监控供应变化：** 如果代币是可铸造的，这可以用于跟踪其总供应量随时间的变化（尽管对于可替代代币，供应量通常是固定的或由铸造机构管理）。

## 请求参数

1. **`mintAddress`** (string, 必填)：要查询其总供应量的代币铸币的 base-58 编码公钥。

2. **`options`** (object, 可选)：可选的配置对象，可包括：
   * **`commitment`** (string, 可选)：为查询指定[承诺级别](https://www.helius.dev/blog/solana-commitment-levels) （例如，`"finalized"`, `"confirmed"`, `"processed"`）。

## 响应结构

JSON-RPC 响应中的 `result.value` 字段是一个包含有关代币供应详细信息的对象：

* **`amount`** (string)：代币的总供应量，以最小单位（原始数量）作为字符串表示。此值未调整为小数。
* **`decimals`** (u8)：为此代币铸币定义的小数位数。这对于将原始 `amount` 转换为可读格式至关重要。
* **`uiAmount`** (number | null)：调整为代币的 `decimals` 后的代币总供应量，表示为浮点数。此字段可能为 null 或不太准确；`uiAmountString` 通常更适合显示。
* **`uiAmountString`** (string)：调整为代币的 `decimals` 后的代币总供应量，作为字符串。这是总供应量最方便用户的表示形式。

**示例响应：**

```json theme={"system"}
{
  "jsonrpc": "2.0",
  "result": {
    "context": { "slot": 123456789 },
    "value": {
      "amount": "1000000000000000", // e.g., 1,000,000,000 tokens with 6 decimals
      "decimals": 6,
      "uiAmount": 1000000000.0,
      "uiAmountString": "1000000000.0"
    }
  },
  "id": 1
}
```

## 代码示例

<CodeGroup>
  ```bash cURL theme={"system"}
  # Replace <TOKEN_MINT_PUBKEY> with the actual mint address
  curl -X POST -H "Content-Type: application/json" -d \
    '{
      "jsonrpc": "2.0",
      "id": 1,
      "method": "getTokenSupply",
      "params": [
        "<TOKEN_MINT_PUBKEY>"
      ]
    }' \
    <YOUR_RPC_URL>

  # Example with commitment level
  curl -X POST -H "Content-Type: application/json" -d \
    '{
      "jsonrpc": "2.0",
      "id": 1,
      "method": "getTokenSupply",
      "params": [
        "<TOKEN_MINT_PUBKEY>",
        { "commitment": "confirmed" }
      ]
    }' \
    <YOUR_RPC_URL>
  ```

  ```javascript JavaScript (using @solana/web3.js) theme={"system"}
  const { Connection, PublicKey } = require('@solana/web3.js');

  async function checkTokenSupply(mintAddress) {
    // Replace with your RPC endpoint
    const connection = new Connection('https://mainnet.helius-rpc.com/?api-key=<api-key>');
    const mintPublicKey = new PublicKey(mintAddress);

    try {
      const tokenSupply = await connection.getTokenSupply(mintPublicKey);
      console.log(`Token Supply for Mint ${mintAddress}:`);
      console.log(`  UI Amount: ${tokenSupply.value.uiAmountString}`);
      console.log(`  Raw Amount: ${tokenSupply.value.amount}`);
      console.log(`  Decimals: ${tokenSupply.value.decimals}`);
      // For full details:
      // console.log(JSON.stringify(tokenSupply, null, 2));
    } catch (error) {
      console.error(`Error fetching token supply for mint ${mintAddress}:`, error);
    }
  }

  // Replace with the actual token mint public key you want to query
  const exampleTokenMint = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'; // USDC mint
  checkTokenSupply(exampleTokenMint);

  // Example with a different mint (e.g., Raydium)
  // const raydiumMint = '4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R';
  // checkTokenSupply(raydiumMint);
  ```
</CodeGroup>

## 开发者提示

* **不可变供应（通常）：** 对于大多数 SPL 代币，铸币账户本身的总供应量一旦铸造即为固定，除非铸币有特定的铸造权威可以创建更多代币（或销毁它们，尽管销毁通常发生在代币账户，而不是铸币的供应直接）。
* **`decimals` 是关键：** 始终使用 `decimals` 字段正确解释 `amount` 或 `uiAmountString`。
* **数据源：** 此方法直接查询铸币账户以获取其供应信息。

本指南提供了使用 `getTokenSupply` RPC 方法有效查询 Solana 上 SPL 代币供应所需的信息。
