Solana RPC
getTokenAccountsByOwnerV2
getTokenAccountsByOwnerV2 は、カーソルページネーションと changedSinceSlot アップデートを備えた、大規模な Solana トークンポートフォリオ向けの拡張版 getTokenAccountsByOwner です。
POST
/
getTokenAccountsByOwnerV2
curl --request POST \
--url 'https://mainnet.helius-rpc.com/?api-key=' \
--header 'Content-Type: application/json' \
--data '
{
"jsonrpc": "2.0",
"id": "1",
"method": "getTokenAccountsByOwnerV2",
"params": [
"A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd",
{
"programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
},
{
"encoding": "jsonParsed",
"limit": 1000
}
]
}
'import requests
url = "https://mainnet.helius-rpc.com/?api-key="
payload = {
"jsonrpc": "2.0",
"id": "1",
"method": "getTokenAccountsByOwnerV2",
"params": [
"A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd",
{ "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" },
{
"encoding": "jsonParsed",
"limit": 1000
}
]
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
jsonrpc: '2.0',
id: '1',
method: 'getTokenAccountsByOwnerV2',
params: [
'A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd',
{programId: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'},
{encoding: 'jsonParsed', limit: 1000}
]
})
};
fetch('https://mainnet.helius-rpc.com/?api-key=', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://mainnet.helius-rpc.com/?api-key=",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'jsonrpc' => '2.0',
'id' => '1',
'method' => 'getTokenAccountsByOwnerV2',
'params' => [
'A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd',
[
'programId' => 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'
],
[
'encoding' => 'jsonParsed',
'limit' => 1000
]
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://mainnet.helius-rpc.com/?api-key="
payload := strings.NewReader("{\n \"jsonrpc\": \"2.0\",\n \"id\": \"1\",\n \"method\": \"getTokenAccountsByOwnerV2\",\n \"params\": [\n \"A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd\",\n {\n \"programId\": \"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA\"\n },\n {\n \"encoding\": \"jsonParsed\",\n \"limit\": 1000\n }\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://mainnet.helius-rpc.com/?api-key=")
.header("Content-Type", "application/json")
.body("{\n \"jsonrpc\": \"2.0\",\n \"id\": \"1\",\n \"method\": \"getTokenAccountsByOwnerV2\",\n \"params\": [\n \"A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd\",\n {\n \"programId\": \"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA\"\n },\n {\n \"encoding\": \"jsonParsed\",\n \"limit\": 1000\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://mainnet.helius-rpc.com/?api-key=")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"jsonrpc\": \"2.0\",\n \"id\": \"1\",\n \"method\": \"getTokenAccountsByOwnerV2\",\n \"params\": [\n \"A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd\",\n {\n \"programId\": \"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA\"\n },\n {\n \"encoding\": \"jsonParsed\",\n \"limit\": 1000\n }\n ]\n}"
response = http.request(request)
puts response.read_body{
"jsonrpc": "2.0",
"id": "1",
"result": {
"value": [
{
"pubkey": "BGocb4GEpbTFm8UFV2VsDSaBXHELPfAXrvd4vtt8QWrA",
"account": {
"lamports": 2039280,
"owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
"data": {
"program": "spl-token",
"parsed": {
"info": {
"isNative": false,
"mint": "2cHr7QS3xfuSV8wdxo3ztuF4xbiarF6Nrgx3qpx3HzXR",
"owner": "A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd",
"state": "initialized",
"tokenAmount": {
"amount": "420000000000000",
"decimals": 6,
"uiAmount": 420000000,
"uiAmountString": "420000000"
}
}
},
"space": 165
},
"executable": false,
"rentEpoch": 18446744073709552000,
"space": 165
}
}
],
"paginationKey": "8WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM"
}
}{
"jsonrpc": "2.0",
"error": {
"code": -32602,
"message": "Invalid params"
},
"id": "1"
}{
"jsonrpc": "2.0",
"error": {
"code": -32001,
"message": "Unauthorized"
},
"id": "1"
}{
"jsonrpc": "2.0",
"error": {
"code": -32005,
"message": "Too many requests"
},
"id": "1"
}{
"jsonrpc": "2.0",
"error": {
"code": -32603,
"message": "Internal error"
},
"id": "1"
}{
"jsonrpc": "2.0",
"error": {
"code": -32002,
"message": "Service unavailable"
},
"id": "1"
}{
"jsonrpc": "2.0",
"error": {
"code": -32003,
"message": "Gateway timeout"
},
"id": "1"
}概要
getTokenAccountsByOwnerV2 は、標準の getTokenAccountsByOwner メソッドを強化したバージョンで、トークンポートフォリオの効率的なクエリと広範なトークン保有を持つウォレットの処理に特化しています。このメソッドはカーソルベースのページネーションとインクリメンタルアップデート機能を導入しています。
V2の新機能:
- カーソルベースのページネーション: 1から10,000トークンアカウントまでのリクエスト制限の設定
- インクリメンタルアップデート: 最近変更されたトークンアカウントのみを取得するために
changedSinceSlotを使用 - ポートフォリオのスケーラビリティ: 数千のトークンアカウントを持つウォレットを効率的に処理
- 後方互換性: すべての既存の
getTokenAccountsByOwnerパラメータとフィルタをサポート - オプションの
withContext:trueはslotとapiVersionをresult.contextに追加します; 省略またはfalseで含まれません
フィルタ要件: クエリには
mint(特定のトークン)またはprogramId(SPLトークンまたはToken-2022プログラム)を含める必要があります。フィルタなしで所有者のすべてのトークンタイプをクエリすることはサポートされていません。主な利点
大規模ポートフォリオ
数千のトークンアカウントを持つウォレットを時間切れやメモリ問題なしに処理
リアルタイムトラッキング
インクリメンタルアップデートのための
changedSinceSlotを使用してポートフォリオの変化をリアルタイムで監視withContext (オプション)
コンフィグオブジェクト (params[2]) のブール値。result の形状のみが変わり、フィルタ、制限、ページネーションは変わりません。省略またはfalse: result.valueはトークンアカウントの 配列。true: result.contextとresult.valueを オブジェクト (accounts, paginationKey) に加えます。両方を処理する場合はArray.isArray(result.value)で分岐します。
// Omitted or false
{ "jsonrpc": "2.0", "id": "1", "result": { "value": [], "paginationKey": null } }
// true
{ "jsonrpc": "2.0", "id": "1", "result": {
"context": { "slot": 411895550, "apiVersion": "3.1.9" },
"value": { "accounts": [], "paginationKey": null }
}}
ページネーションのベストプラクティス
重要なページネーションの動作: ページネーションの終わりはトークンアカウントが返されないときにのみ示されます。フィルタリングのために、APIは制限よりも少ないアカウントを返すことがあります - 常に
paginationKey が null になるまでページネーションを続けてください。基本的なポートフォリオクエリ
// Get all SPL Token accounts for a wallet
let allTokenAccounts = [];
let paginationKey = null;
do {
const response = await fetch(`https://mainnet.helius-rpc.com/?api-key=${API_KEY}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
id: '1',
method: 'getTokenAccountsByOwnerV2',
params: [
"9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM", // wallet address
{ "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" },
{
encoding: 'jsonParsed',
limit: 1000,
...(paginationKey && { paginationKey })
}
]
})
});
const data = await response.json();
allTokenAccounts.push(...data.result.value);
paginationKey = data.result.paginationKey;
} while (paginationKey);
console.log(`Total token accounts: ${allTokenAccounts.length}`);
インクリメンタルポートフォリオアップデート
// Get only token accounts modified since a specific slot
const portfolioUpdates = await fetch(`https://mainnet.helius-rpc.com/?api-key=${API_KEY}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
id: '1',
method: 'getTokenAccountsByOwnerV2',
params: [
walletAddress,
{ "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" },
{
encoding: 'jsonParsed',
limit: 1000,
changedSinceSlot: lastUpdateSlot // Only get recent changes
}
]
})
});
トークンプログラムサポート
Token-2022 サポート:
TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEbをprogramIdとして、転送手数料、利息付トークンなどの拡張機能を備えたToken-2022アカウントをクエリします。// Query Token-2022 accounts (supports token extensions)
const token2022Response = await fetch(`https://mainnet.helius-rpc.com/?api-key=${API_KEY}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
id: '1',
method: 'getTokenAccountsByOwnerV2',
params: [
walletAddress,
{ "programId": "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb" }, // Token-2022
{ encoding: 'jsonParsed', limit: 1000 }
]
})
});
getTokenAccountsByOwner からの移行
移行は簡単です - 既存のクエリにページネーションパラメータを追加してください:{
"jsonrpc": "2.0",
"id": "1",
- "method": "getTokenAccountsByOwner",
+ "method": "getTokenAccountsByOwnerV2",
"params": [
"9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM",
{ "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" },
{
"encoding": "jsonParsed",
+ "limit": 1000
}
]
}
関連メソッド
getTokenAccountsByOwner
ページネーションなしのオリジナルメソッド
getProgramAccountsV2
プログラムアカウントクエリのためのV2メソッド
リクエストパラメータ
string
必須
Solanaウォレットアドレス(アカウント所有者のpubkey)をbase-58エンコードされた文字列として指定。
string
特定のトークンまたはNFTのアカウントのみを取得するための特定のSolanaトークンミントアドレス。
string
トークンアカウントを作成した特定のSolanaトークンプログラムID(通常はSPLトークンプログラム)。
string
リクエストのコミットメントレベル。
confirmedfinalizedprocessed
number
リクエストが評価されることができる最小スロット。
boolean
true の場合、result.context(スナップショットメタデータ: slot, apiVersion)を返し、
accountsとpaginationKeyをオブジェクトとしてresult.valueにネストされます。false
または省略時、 result.valueはこのページのトークンアカウント配列であり、paginationKey
はresultに配置されます。同じフィルタリングと制限が適用されます。object
アカウントデータのスライスを要求。
number
返すバイト数。
number
読み取りを開始するバイトオフセット。
string
アカウントデータのエンコード形式。
base58base64base64+zstdjsonParsed
number
リクエストごとに返されるトークンアカウントの最大数(1〜10,000)。
string
次のページを取得するためのBase-58エンコードされたページネーションクレデンシャル。前のレスポンスのpaginationKeyを使用。
number
このスロット番号以降に変更されたトークンアカウントのみを返します。インクリメンタルポートフォリオアップデートに便利です。
ボディ
application/json
JSON-RPCプロトコルバージョン。
利用可能なオプション:
2.0 例:
"2.0"
リクエストのためのユニークな識別子。
例:
"1"
呼び出すRPCメソッドの名前。
利用可能なオプション:
getTokenAccountsByOwnerV2 例:
"getTokenAccountsByOwnerV2"
特定の公開鍵に所有されるページネーションされたトークンアカウントを照会するためのパラメータ。
base-58でエンコードされた文字列としての、トークン保有を照会するためのアカウントオーナーのSolanaウォレットアドレス(pubkey)。
例:
"A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd"
レスポンス
所有者によるページネーションされたトークンアカウントを正常に取得しました。
このページは役に立ちましたか?
⌘I
getTokenAccountsByOwnerV2
curl --request POST \
--url 'https://mainnet.helius-rpc.com/?api-key=' \
--header 'Content-Type: application/json' \
--data '
{
"jsonrpc": "2.0",
"id": "1",
"method": "getTokenAccountsByOwnerV2",
"params": [
"A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd",
{
"programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
},
{
"encoding": "jsonParsed",
"limit": 1000
}
]
}
'import requests
url = "https://mainnet.helius-rpc.com/?api-key="
payload = {
"jsonrpc": "2.0",
"id": "1",
"method": "getTokenAccountsByOwnerV2",
"params": [
"A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd",
{ "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" },
{
"encoding": "jsonParsed",
"limit": 1000
}
]
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
jsonrpc: '2.0',
id: '1',
method: 'getTokenAccountsByOwnerV2',
params: [
'A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd',
{programId: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'},
{encoding: 'jsonParsed', limit: 1000}
]
})
};
fetch('https://mainnet.helius-rpc.com/?api-key=', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://mainnet.helius-rpc.com/?api-key=",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'jsonrpc' => '2.0',
'id' => '1',
'method' => 'getTokenAccountsByOwnerV2',
'params' => [
'A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd',
[
'programId' => 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'
],
[
'encoding' => 'jsonParsed',
'limit' => 1000
]
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://mainnet.helius-rpc.com/?api-key="
payload := strings.NewReader("{\n \"jsonrpc\": \"2.0\",\n \"id\": \"1\",\n \"method\": \"getTokenAccountsByOwnerV2\",\n \"params\": [\n \"A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd\",\n {\n \"programId\": \"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA\"\n },\n {\n \"encoding\": \"jsonParsed\",\n \"limit\": 1000\n }\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://mainnet.helius-rpc.com/?api-key=")
.header("Content-Type", "application/json")
.body("{\n \"jsonrpc\": \"2.0\",\n \"id\": \"1\",\n \"method\": \"getTokenAccountsByOwnerV2\",\n \"params\": [\n \"A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd\",\n {\n \"programId\": \"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA\"\n },\n {\n \"encoding\": \"jsonParsed\",\n \"limit\": 1000\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://mainnet.helius-rpc.com/?api-key=")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"jsonrpc\": \"2.0\",\n \"id\": \"1\",\n \"method\": \"getTokenAccountsByOwnerV2\",\n \"params\": [\n \"A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd\",\n {\n \"programId\": \"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA\"\n },\n {\n \"encoding\": \"jsonParsed\",\n \"limit\": 1000\n }\n ]\n}"
response = http.request(request)
puts response.read_body{
"jsonrpc": "2.0",
"id": "1",
"result": {
"value": [
{
"pubkey": "BGocb4GEpbTFm8UFV2VsDSaBXHELPfAXrvd4vtt8QWrA",
"account": {
"lamports": 2039280,
"owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
"data": {
"program": "spl-token",
"parsed": {
"info": {
"isNative": false,
"mint": "2cHr7QS3xfuSV8wdxo3ztuF4xbiarF6Nrgx3qpx3HzXR",
"owner": "A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd",
"state": "initialized",
"tokenAmount": {
"amount": "420000000000000",
"decimals": 6,
"uiAmount": 420000000,
"uiAmountString": "420000000"
}
}
},
"space": 165
},
"executable": false,
"rentEpoch": 18446744073709552000,
"space": 165
}
}
],
"paginationKey": "8WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM"
}
}{
"jsonrpc": "2.0",
"error": {
"code": -32602,
"message": "Invalid params"
},
"id": "1"
}{
"jsonrpc": "2.0",
"error": {
"code": -32001,
"message": "Unauthorized"
},
"id": "1"
}{
"jsonrpc": "2.0",
"error": {
"code": -32005,
"message": "Too many requests"
},
"id": "1"
}{
"jsonrpc": "2.0",
"error": {
"code": -32603,
"message": "Internal error"
},
"id": "1"
}{
"jsonrpc": "2.0",
"error": {
"code": -32002,
"message": "Service unavailable"
},
"id": "1"
}{
"jsonrpc": "2.0",
"error": {
"code": -32003,
"message": "Gateway timeout"
},
"id": "1"
}