Solana SPL 代币接口

SPL-Token 接口可以查询 Solana 区块链上的 Token 的账户信息,包括令牌持有者、余额等。

这些接口提供了对 Solana 区块链上 Token 账户的灵活查询和管理,可以帮助开发者和投资者更好地了解和操作 Token 账户信息。

1. 查询账户信息

使用 getTokenAccountsByOwner 方法可以查询特定 Token 的所有者为指定地址的账户信息。

我们知道 SPL-Token 的结构为:

pub struct Account {
  /// The mint associated with this account
  pub mint: Pubkey,
  /// The owner of this account.
  pub owner: Pubkey,
  /// The amount of tokens this account holds.
  pub amount: u64,
  /// If `delegate` is `Some` then `delegated_amount` represents
  /// the amount authorized by the delegate
  pub delegate: COption,
  /// The account's state
  pub state: AccountState,
  /// If is_native.is_some, this is a native token, and the value logs the rent-exempt reserve. An
  /// Account is required to be rent-exempt, so the value is used by the Processor to ensure that
  /// wrapped SOL accounts do not drop below this threshold.
  pub is_native: COption,
  /// The amount delegated
  pub delegated_amount: u64,
  /// Optional authority to close the account.
  pub close_authority: COption,
}

我们可以查询某个 Token 下,所有 owner 为某人的 Token 账号,或者 delegate 为某人的所有账号。

我们在命令行中,通过 curl 命令发送 POST 请求到 SolanaRPC 接口。

curl  https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d '
{
  "jsonrpc": "2.0",
    "id": 1,
    "method": "getTokenAccountsByOwner",
    "params": [
      "Czorr4y9oFvE3VdfCLVFuKDYxaNUG1iyQomR7kMZUuzi",
      {
        "mint": "7vtXvye2ECB1T5Se8E1KebNfmV7t4VkaULDjf2v1xpA9"
      },
      {
        "encoding": "jsonParsed"
      }
    ]
}
'

上线的示例中的请求中包含了 Token 地址和所有者地址,返回了所有符合条件的账户信息列表。

这里查询到这个token:7vtXvye2ECB1T5Se8E1KebNfmV7t4VkaULDjf2v1xpA9 的 owner 为 CnjrCefFBHmWnKcwH5T8DFUQuVEmUJwfBL3Goqj6YhKw 的所有账号。

{
  "jsonrpc": "2.0",
  "result": {
    "context": {
      "apiVersion": "1.16.5",
      "slot": 234689258
    },
    "value": [
      {
        "account": {
          "data": {
            "parsed": {
              "info": {
                "isNative": false,
                "mint": "7vtXvye2ECB1T5Se8E1KebNfmV7t4VkaULDjf2v1xpA9",
                "owner": "Czorr4y9oFvE3VdfCLVFuKDYxaNUG1iyQomR7kMZUuzi",
                "state": "initialized",
                "tokenAmount": {
                  "amount": "99000000000",
                  "decimals": 9,
                  "uiAmount": 99.0,
                  "uiAmountString": "99"
                }
              },
              "type": "account"
            },
            "program": "spl-token",
            "space": 165
          },
          "executable": false,
          "lamports": 2039280,
          "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
          "rentEpoch": 0,
          "space": 165
        },
        "pubkey": "EZhhUANUMKsRhRMArczio1kLc9axefTUAh5xofGX35AK"
      }
    ]
  },
  "id": 1
}

而通过查询命令:

curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d '
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "getTokenAccountsByDelegate",
  "params": [
    "Czorr4y9oFvE3VdfCLVFuKDYxaNUG1iyQomR7kMZUuzi",
    {
      "mint": "7vtXvye2ECB1T5Se8E1KebNfmV7t4VkaULDjf2v1xpA9"
    },
    {
      "encoding": "jsonParsed"
    }
  ]
}
'

因为我们没有设置代理操作,所以这里得到的结果为空。

2. 查询账户余额

通过 getTokenAccountBalance 方法可以直接查询指定 Token 账户的余额信息。

我们在命令行中,通过 curl 命令发送 POST 请求到 SolanaRPC 接口。

curl  https://api.devnet.solana.com  -X POST -H "Content-Type: application/json" -d '
{
  "jsonrpc": "2.0", "id": 1,
  "method": "getTokenAccountBalance",
  "params": [
    "EZhhUANUMKsRhRMArczio1kLc9axefTUAh5xofGX35AK"
  ]
}
'

示例请求中通过指定账户地址来查询账户的余额信息。

返回结果:

{
  "jsonrpc": "2.0",
  "result": {
    "context": {
    "apiVersion": "1.16.3",
    "slot": 209132550
    },
    "value": {
    "amount": "99000000000",
    "decimals": 9,
    "uiAmount": 99.0,
    "uiAmountString": "99"
    }
  },
  "id": 1
}

这里可以看到,uiAmount 是显示的数量,是做了精度转换的,精度和真实的 amount 都包含在查询结果中。