rippled提供3中api接口:websocket 协议、jsonrpc协议、和本地命令行调用。
具体接口内容可以看官网说明:
https://xrpl.org/account_info.html
下面是根据协议和接口文档做的python测试效果
#!/usr/bin/python3
# coding: utf-8
# rippledWebSocket.py
# 使用ws/wss接口demo
# https://xrpl.org/account_info.html
# 需要安装websocket-clinet
#pip install websocket-client
from websocket import create_connection
import json
# 测试xrpapi调用
def testGetInfo(json_params, ws_url, json_type = True):
ws = create_connection(ws_url)
if json_type:
ws.send(json.dumps(json_params))
else:
ws.send(json_params)
result = ws.recv()
print(str(result))
# 本地运行的rippled,采用git上面的默认配置,ws协议端口6006
wsurl = "ws://127.0.0.1:6006/"
# json字符串
params_test01 = '{"id": 14, "command": "ledger", "ledger_index": "validated", "full": false, "accounts": false, "transactions": false, "expand": false, "owner_funds": false}'
# json对象
params_test02 = {
"id": 2,
"command": "account_info",
"account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"strict": True,
"ledger_index": "validated"
}
# account_channels
account_channels = {
"id": 1,
"command": "account_channels",
"account": "rN7n7otQDd6FczFgLdSqtcsAUxDkw6fzRH",
"destination_account": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
"ledger_index": "validated"
}
# account_currencies
account_currencies = {
"command": "account_currencies",
"account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"strict": True,
"ledger_index": "validated"
}
if __name__ == "__main__":
print("\naccount_info")
testGetInfo(params_test01, wsurl, False)
print("\nledger")
testGetInfo(params_test02, wsurl)
# account_channels
print("\naccount_channels")
testGetInfo(account_channels, wsurl)
# account_currencies
print("\naccount_currencies")
testGetInfo(account_currencies, wsurl)
运行结果
account_info
{"id":14,"result":{"ledger":{"accepted":true,"account_hash":"4D6FA0EAD351DDE454AACB038A1F574AE137FFE3405316FEA59C802BA7BB9B15","close_flags":0,"close_time":667187590,"close_time_human":"2021-Feb-21 01:53:10.0000000 UTC","close_time_resolution":10,"closed":true,"hash":"EF0C3418EE38820D2C590C9FD6CF6EA60B07DC74BFCF92DAEC48E94A6AC40CB4","ledger_hash":"EF0C3418EE38820D2C590C9FD6CF6EA60B07DC74BFCF92DAEC48E94A6AC40CB4","ledger_index":"61722367","parent_close_time":667187581,"parent_hash":"310E42AC654CD22E5184056238DE9DADFB8D612D23E4FF9C80D0C713368C15A8","seqNum":"61722367","totalCoins":"99990700724043204","total_coins":"99990700724043204","transaction_hash":"E2D2E2228935EF283AE8A94A2A5234246E897B9EABAA80A395B4D7805CCAF2F2"},"ledger_hash":"EF0C3418EE38820D2C590C9FD6CF6EA60B07DC74BFCF92DAEC48E94A6AC40CB4","ledger_index":61722367,"validated":true},"status":"success","type":"response"}
ledger
{"id":2,"result":{"account_data":{"Account":"r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59","Balance":"13315612685","Flags":0,"LedgerEntryType":"AccountRoot","OwnerCount":17,"PreviousTxnID":"D2FA1C28EF87E53029327AA832C378674B3ACA0551CF9EA1F65BB8CA34913FAB","PreviousTxnLgrSeq":55180009,"Sequence":1406,"index":"4F83A2CF7E70F77F79A307E6A472BFC2585B806A70833CCD1C26105BAE0D6E05"},"ledger_hash":"EF0C3418EE38820D2C590C9FD6CF6EA60B07DC74BFCF92DAEC48E94A6AC40CB4","ledger_index":61722367,"validated":true},"status":"success","type":"response"}
account_channels
{"id":1,"result":{"account":"rN7n7otQDd6FczFgLdSqtcsAUxDkw6fzRH","channels":[],"ledger_hash":"EF0C3418EE38820D2C590C9FD6CF6EA60B07DC74BFCF92DAEC48E94A6AC40CB4","ledger_index":61722367,"validated":true},"status":"success","type":"response"}
account_currencies
{"result":{"ledger_hash":"EF0C3418EE38820D2C590C9FD6CF6EA60B07DC74BFCF92DAEC48E94A6AC40CB4","ledger_index":61722367,"receive_currencies":["BTC","CNY","DYM","EUR","JOE","MXN","USD","015841551A748AD2C1F76FF6ECB0CCCD00000000"],"send_currencies":["ASP","BTC","CHF","CNY","DYM","EUR","JOE","JPY","MXN","USD"],"validated":true},"status":"success","type":"response"}
#!/usr/bin/python
# coding: utf-8
# rippledJsonRPC.py
# 使用rippled的默认配置,http协议端口好默认配置是5005
import requests
session = requests.Session()
# 使用jsonrpc接口demo
# https://xrpl.org/account_info.html
method = 'account_info'
params = [
{
"account": "rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn",
"strict": True,
"ledger_index": "current",
"queue": True
}
]
'''
payload= {"jsonrpc":"2.0",
"method":method,
"params":params,
"id":2
}
'''
payload= {"jsonrpc":"2.0",
"method":method,
"params":params
}
headers = {'Content-type': 'application/json'}
response = session.post('http://127.0.0.1:5005', json=payload, headers=headers)
print('\nraw json response: {}'.format(response.json()))
print('\nnetwork id: {}'.format(response.json()['result']))
运行效果
raw json response: {'result': {'account_data': {'Account': 'rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn', 'Balance': '9976', 'Flags': 1114112, 'LedgerEntryType': 'AccountRoot', 'MessageKey': '0200000000000000000000000038901D3A772963CF12FF7C0E010FE350B6CCC45D', 'OwnerCount': 0, 'PreviousTxnID': '85BC80F7FECB57FD074BBA54517C469B4BC242AEF894A8ED272798AA5394F5CC', 'PreviousTxnLgrSeq': 60003347, 'RegularKey': 'rhLkGGNZdjSpnHJw4XAFw1Jy7PD8TqxoET', 'Sequence': 192221, 'index': '92FA6A9FC8EA6018D5D16532D7795C91BFB0831355BDFDA177E86C8BF997985F'}, 'ledger_current_index': 61725000, 'queue_data': {'txn_count': 0}, 'status': 'success', 'validated': False}}
network id: {'account_data': {'Account': 'rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn', 'Balance': '9976', 'Flags': 1114112, 'LedgerEntryType': 'AccountRoot', 'MessageKey': '0200000000000000000000000038901D3A772963CF12FF7C0E010FE350B6CCC45D', 'OwnerCount': 0, 'PreviousTxnID': '85BC80F7FECB57FD074BBA54517C469B4BC242AEF894A8ED272798AA5394F5CC', 'PreviousTxnLgrSeq': 60003347, 'RegularKey': 'rhLkGGNZdjSpnHJw4XAFw1Jy7PD8TqxoET', 'Sequence': 192221, 'index': '92FA6A9FC8EA6018D5D16532D7795C91BFB0831355BDFDA177E86C8BF997985F'}, 'ledger_current_index': 61725000, 'queue_data': {'txn_count': 0}, 'status': 'success', 'validated': False}
rippled --conf ..\etc\rippled.cfg account_info rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn validated strict
D:\git\rippled\buildlib\install\rippled\bin>rippled --conf ..\etc\rippled.cfg account_info rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn validated strict
Loading: "..\etc\rippled.cfg"
2021-Feb-21 01:57:47.9259447 UTC HTTPClient:NFO Connecting to 127.0.0.1:5005
{
"result" : {
"account_data" : {
"Account" : "rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn",
"Balance" : "9976",
"Flags" : 1114112,
"LedgerEntryType" : "AccountRoot",
"MessageKey" : "0200000000000000000000000038901D3A772963CF12FF7C0E010FE350B6CCC45D",
"OwnerCount" : 0,
"PreviousTxnID" : "85BC80F7FECB57FD074BBA54517C469B4BC242AEF894A8ED272798AA5394F5CC",
"PreviousTxnLgrSeq" : 60003347,
"RegularKey" : "rhLkGGNZdjSpnHJw4XAFw1Jy7PD8TqxoET",
"Sequence" : 192221,
"index" : "92FA6A9FC8EA6018D5D16532D7795C91BFB0831355BDFDA177E86C8BF997985F"
},
"ledger_hash" : "830FD444D7FC5453017AC67913EE6E825B6F3227ACBDA9C587E5E822534ECC73",
"ledger_index" : 61722438,
"status" : "success",
"validated" : true
}
}