跳转到主要内容

参数

mint
string
必填
代币铸造的公钥(base-58 编码)
config
object
包含以下内容的配置对象:
commitment
string
Commitment 级别(processed、confirmed、finalized)

响应

result
object
包含以下内容的对象:
context
object
slot
number
处理请求时的 slot
value
object
amount
string
代币的原始数量(字符串格式)
decimals
number
代币铸造配置的小数位数
uiAmount
number
考虑小数位数后的代币数量(浮点数)
uiAmountString
string
考虑小数位数后的代币数量(字符串)

代码示例

基本请求

curl https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY -X POST -H "Content-Type: application/json" -d '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "getTokenSupply",
  "params": [
    "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
  ]
}'

使用 web3.js

import { Connection, PublicKey } from '@solana/web3.js';

const connection = new Connection('https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY');

// Get token supply
const mint = new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v');
const supply = await connection.getTokenSupply(mint);
console.log('Token supply:', supply);

// Get token supply with analysis
async function getTokenSupplyWithAnalysis(
  mint: PublicKey,
  config: { commitment?: string }
) {
  const supply = await connection.getTokenSupply(mint, config);
  
  return {
    supply,
    analysis: {
      rawAmount: BigInt(supply.value.amount),
      formattedAmount: supply.value.uiAmountString,
      decimals: supply.value.decimals,
      metadata: {
        timestamp: Date.now(),
        commitment: config.commitment
      }
    }
  };
}

注意事项

  1. 返回代币铸造的总供应量
  2. 供应量包括所有流通中的代币
  3. 响应是即时的,因为它从当前状态读取
  4. 供应量可能随代币铸造和销毁而变化
  5. 铸造必须是有效的代币铸造

最佳实践

  1. 根据需求使用适当的 commitment 级别
  2. 在适当时缓存结果以减少 RPC 负载
  3. 监控代币供应量的变化
  4. 考虑使用 WebSocket 订阅获取实时更新
  5. 适当处理网络错误并重试

常见错误

错误码消息解决方案
-32601Method not found验证是否连接到 Solana RPC 节点
-32602Invalid params检查铸造公钥和配置
-32007Mint not found验证铸造是否存在

用例

  1. 代币供应分析
    interface TokenSupplyAnalysis {
      mint: string;
      supply: {
        rawAmount: string;
        formattedAmount: string;
        decimals: number;
      };
      metrics: {
        maxSupply?: string;
        burnedAmount?: string;
        circulatingSupply?: string;
        inflationRate?: number;
      };
      metadata: {
        timestamp: number;
        commitment?: string;
      };
    }
    
    class TokenSupplyAnalyzer {
      private readonly maxSupply: Record<string, bigint> = {
        'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v': BigInt(1000000000000000) // USDC
      };
      
      async analyzeTokenSupply(
        mint: PublicKey,
        config: { commitment?: string }
      ): Promise<TokenSupplyAnalysis> {
        const supply = await connection.getTokenSupply(mint, config);
        const rawAmount = BigInt(supply.value.amount);
        const maxSupply = this.maxSupply[mint.toBase58()];
        
        return {
          mint: mint.toBase58(),
          supply: {
            rawAmount: supply.value.amount,
            formattedAmount: supply.value.uiAmountString!,
            decimals: supply.value.decimals
          },
          metrics: {
            maxSupply: maxSupply?.toString(),
            burnedAmount: maxSupply ? (maxSupply - rawAmount).toString() : undefined,
            circulatingSupply: supply.value.uiAmountString,
            inflationRate: maxSupply ? Number((rawAmount * BigInt(100)) / maxSupply) : undefined
          },
          metadata: {
            timestamp: Date.now(),
            commitment: config.commitment
          }
        };
      }
    }
    
  2. 代币供应监控
    interface TokenSupplyChange {
      mint: string;
      changes: {
        previousAmount: string;
        currentAmount: string;
        difference: string;
        percentageChange: number;
      };
      metadata: {
        timestamp: number;
      };
    }
    
    class TokenSupplyMonitor {
      private previousSupply: Map<string, string> = new Map();
      
      async monitorTokenSupply(
        mint: PublicKey,
        config: { commitment?: string }
      ): Promise<TokenSupplyChange | null> {
        const supply = await connection.getTokenSupply(mint, config);
        const currentAmount = supply.value.amount;
        const previousAmount = this.previousSupply.get(mint.toBase58());
        
        if (previousAmount && previousAmount !== currentAmount) {
          const difference = BigInt(currentAmount) - BigInt(previousAmount);
          const percentageChange = Number((difference * BigInt(100)) / BigInt(previousAmount));
          
          this.previousSupply.set(mint.toBase58(), currentAmount);
          
          return {
            mint: mint.toBase58(),
            changes: {
              previousAmount,
              currentAmount,
              difference: difference.toString(),
              percentageChange
            },
            metadata: {
              timestamp: Date.now()
            }
          };
        }
        
        this.previousSupply.set(mint.toBase58(), currentAmount);
        return null;
      }
    }
    
  3. 代币供应规划
    interface TokenSupplyPlan {
      mint: string;
      currentSupply: {
        rawAmount: string;
        formattedAmount: string;
        decimals: number;
      };
      recommendations: Array<{
        type: 'mint' | 'burn' | 'freeze';
        amount?: string;
        reason: string;
      }>;
      metadata: {
        timestamp: number;
      };
    }
    
    class TokenSupplyPlanner {
      private readonly targetInflationRate = 0.02; // 2% annual inflation
      private readonly maxSupply: Record<string, bigint> = {
        'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v': BigInt(1000000000000000) // USDC
      };
      
      async planTokenSupply(
        mint: PublicKey,
        config: { commitment?: string }
      ): Promise<TokenSupplyPlan> {
        const supply = await connection.getTokenSupply(mint, config);
        const rawAmount = BigInt(supply.value.amount);
        const maxSupply = this.maxSupply[mint.toBase58()];
        
        const recommendations: Array<{
          type: 'mint' | 'burn' | 'freeze';
          amount?: string;
          reason: string;
        }> = [];
        
        if (maxSupply) {
          const currentInflationRate = Number((rawAmount * BigInt(100)) / maxSupply) / 100;
          const targetAmount = maxSupply * BigInt(Math.floor(this.targetInflationRate * 100)) / BigInt(100);
          
          if (currentInflationRate > this.targetInflationRate) {
            const burnAmount = rawAmount - targetAmount;
            recommendations.push({
              type: 'burn',
              amount: burnAmount.toString(),
              reason: `Current inflation rate (${(currentInflationRate * 100).toFixed(2)}%) exceeds target (${(this.targetInflationRate * 100).toFixed(2)}%)`
            });
          } else if (currentInflationRate < this.targetInflationRate) {
            const mintAmount = targetAmount - rawAmount;
            recommendations.push({
              type: 'mint',
              amount: mintAmount.toString(),
              reason: `Current inflation rate (${(currentInflationRate * 100).toFixed(2)}%) below target (${(this.targetInflationRate * 100).toFixed(2)}%)`
            });
          }
        }
        
        return {
          mint: mint.toBase58(),
          currentSupply: {
            rawAmount: supply.value.amount,
            formattedAmount: supply.value.uiAmountString!,
            decimals: supply.value.decimals
          },
          recommendations,
          metadata: {
            timestamp: Date.now()
          }
        };
      }
    }