这是我遇到的代码 TypeError: 'solders.rpc.responses.GetAccountInfoResp' object is not subscriptable (Solana program)。我已经试过了,但没有用。
import struct
from pprint import pprint as p
from solana.rpc.api import Client
from solders.pubkey import Pubkey
from solana.transaction import Transaction
from base58 import b58encode, b58decode as b58d
from base64 import b64decode as b64d
b58e = lambda x:b58encode(x).decode('ascii')
uri="http://api.mainnet-beta.solana.com"
client=Client(uri)
usdc=Pubkey.from_string("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v")
ai = client.get_account_info(usdc)
p(ai)
GetAccountInfoResp {
context: RpcResponseContext {
slot: 184983995,
api_version: Some(
"1.13.6",
),
},
value: Some(
Account(
Account {
lamports: 182698617139,
data.len: 82,
owner: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA,
executable: false,
rent_epoch: 361,
data: 010000001ce359ed5a012e04fa142b9c751a1c5e87cfd0a0161b9c85ffd31b78cdfcd8f6747a419443e311000601010000002a9e5edbb53c04679098ff7b1265,
},
),
),
}
account_data = ai["result"]["value"]["data"][0]
TypeError Traceback (most recent call last)
\<ipython-input-81-790a5356e04d\> in \<module\>
\----\> 1 account_data = ai\["result"\]\["value"\]\["data"\]\[0\]
TypeError: 'solders.rpc.responses.GetAccountInfoResp' object is not subscriptable
它假设 Api 响应必须以以下语法开始
{
"jsonrpc": "2.0",
"result": {"context": {
"slot": 1
},
"value": {
"data":
不管我得到这个。
GetAccountInfoResp {
context: RpcResponseContext {
slot: 184983995,
api_version: Some(
"1.13.6",
由于错误显示
TypeError: 'solders.rpc.responses.GetAccountInfoResp' object is not subscriptable
,这意味着它不是字典,您可以将其下标为my_instance['key']
,而是使用点符号获取字段,如my_instance.key
。尝试:
account_data = ai.value.data[0]
这是访问这些字段的示例测试:https://github.com/kevinheavey/solders/blob/f8a3c31dd95168ce72cf6eaf9a5a2504a258a01d/tests/test_rpc_responses.py#L157