solana可用rpc如下:
HELIUS_API_KEY = 'your_api_key'
HELIUS_SOLANA_MAIN = 'https://mainnet.helius-rpc.com/?api-key={}'.format(HELIUS_API_KEY)
ALCHEMY_API_KEY = 'your_api_key'
ALCHEMY_SOLANA_MAIN = 'https://solana-mainnet.g.alchemy.com/v2/{}'.format(ALCHEMY_API_KEY)
GETBLOCK_SOLANA_MAIN = 'https://go.getblock.io/{your_getblock_api_key}'
CHAINSTACK_SOLANA_MAIN = 'https://solana-mainnet.core.chainstack.com/{your_chainstack_api_key}'
SOLANA_RPC_LIST = [HELIUS_SOLANA_MAIN, ALCHEMY_SOLANA_MAIN, GETBLOCK_SOLANA_MAIN, CHAINSTACK_SOLANA_MAIN]
def test_rpc():
rpc_url = SOLANA_RPC_LIST[random.randrange(0, len(SOLANA_RPC_LIST))]
http_client = Client(rpc_url,commitment=Confirmed)
blk_height = http_client.get_block_height().to_json()
height = json.loads(blk_height)['result']
print(height)
blk_info = http_client.get_block(height).to_json()
blk_result = json.loads(blk_info)['result']
with open('blk.txt','a+',encoding='utf8' ) as f:
f.write(json.dumps(blk_result))
f.write('\n')
使用solana的rpc服务时,如果直接调用get_block(height)时,报错信息如下:
solana.rpc.core.RPCException: UnsupportedTransactionVersionMessage { message: "Transaction version (0) is not supported by the requesting client. Please try the request again with the following configuration parameter: \"maxSupportedTransactionVersion\": 0" }
在stackexchange这个页面中,给出了回答:
curl --location --request POST 'http://api.mainnet-beta.solana.com/' \
--header 'Content-Type: application/json' --data-raw \
' { "jsonrpc": "2.0", "id": 1, "method": "getConfirmedTransaction", "params": ["5HbX6YFiQJsUhG1RM6YN8KNNY86rEpRJpG9WUWJmirAeiby5B6b6nH5qK43xctCxhecLKCmdKzuA1uFqFrLMfALg", {"encoding": "json", "maxSupportedTransactionVersion":0} ] }
观察发现多了两个参数: enconding和maxSupportedTransactionVersion,受到启发,在get_block中也增加了两个参数:
blk_info = http_client.get_block(height,encoding='json',max_supported_transaction_version=0)
即:
def test_rpc():
rpc_url = SOLANA_RPC_LIST[random.randrange(0, len(SOLANA_RPC_LIST))]
http_client = Client(rpc_url,commitment=Confirmed)
blk_height = http_client.get_block_height().to_json()
height = json.loads(blk_height)['result']
print(height)
blk_info = http_client.get_block(height,encoding='json',max_supported_transaction_version=0)
blk_json = blk_info.to_json()
blk_result = json.loads(blk_json)['result']
with open('blk.txt','a+',encoding='utf8' ) as f:
f.write(json.dumps(blk_result))
f.write('\n')
又报错了:
solana.rpc.core.RPCException: BlockCleanedUpMessage { message: "Block 272955789 cleaned up, does not exist on node. First available block: 293529227" }
读上去,感觉意思就是:你要查询到这个高度272955789被清理掉了,我这儿第一个能查询到的高度是293529227,比你的高度大些,你要查的查不到了。
那就改下吧,把max_supported_transaction_version改成1试试看
def test_rpc():
rpc_url = SOLANA_RPC_LIST[random.randrange(0, len(SOLANA_RPC_LIST))]
http_client = Client(rpc_url,commitment=Confirmed)
blk_height = http_client.get_block_height().to_json()
height = json.loads(blk_height)['result']
print(height)
blk_info = http_client.get_block(height,encoding='json',max_supported_transaction_version=1)
blk_json = blk_info.to_json()
blk_result = json.loads(blk_json)['result']
with open('blk.txt','a+',encoding='utf8' ) as f:
f.write(json.dumps(blk_result))
f.write('\n')
果然好使了,输出结果为:
272956345
blk.txt内容在这里