getBalances - 获取地址余额
getBalances
调用返回指定地址的确认余额。
如果调用时未指定tips参数,那么返回的余额未最近已确认里程碑时 的余额。
注意:只有当节点同步后,getBalances
才会返回结果。
调用参数
- addresses:要检查余额的地址字符串数组,不包含校验和,必须
- threshold:确认数预置,整数,0~100,必须
- tips:tips数组,可选
返回结果
getBalances
调用返回一个JSON对象,其结构如下:
- balances:余额数组,其次序与addresses参数一致
- references:用于参照的tips数组,如果未指定tips参数,则该字段值为最近里程碑
- milestoneIndex:确认最近余额的里程碑索引
- duration:请求执行用时,单位:毫秒
调用成功的HTTP状态码为200,响应示例如下:
{
"balances":["197"],
"references":[
"GSBROIMQWTOQTFGJHHJPMCZR9DIRN9CQGUBKTGSOQLZRGKFBJFMRIGNGWZDNWKADGMNR9TMLRMLIUZ999"
],
"milestoneIndex":1084812,
"duration":0
}
调用失败的HTTP状态码为400,响应示例如下:
{
"error": "'command' parameter has not been specified"
}
示例代码
Python
import urllib2
import json
command = {
"command": "getBalances",
"addresses": [
"DE9DVSOWIIIKEBAAHCKBWNXGXTOKVLZPLRAGKZG9GXKFRFWERKBFYMPRLAGVZTRVYPEPHBMUPDMRQ9DPZ"
],
"threshold": 100
}
stringified = json.dumps(command)
headers = {
'content-type': 'application/json',
'X-IOTA-API-Version': '1'
}
request = urllib2.Request(url="http://localhost:14265", data=stringified, headers=headers)
returnData = urllib2.urlopen(request).read()
jsonData = json.loads(returnData)
print jsonData
Node.js
var request = require('request');
var command = {
"command": "getBalances",
"addresses": [
"DE9DVSOWIIIKEBAAHCKBWNXGXTOKVLZPLRAGKZG9GXKFRFWERKBFYMPRLAGVZTRVYPEPHBMUPDMRQ9DPZ"
],
"threshold": 100
};
var options = {
url: 'http://localhost:14265',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-IOTA-API-Version': '1',
'Content-Length': Buffer.byteLength(JSON.stringify(command))
},
json: command
};
request(options, function (error, response, data) {
if (!error && response.statusCode == 200) {
console.log(data);
}
});
命令行
curl http://localhost:14265 \
-X POST \
-H 'Content-Type: application/json' \
-H 'X-IOTA-API-Version: 1' \
-d '{
"command": "getBalances",
"addresses": [
"DE9DVSOWIIIKEBAAHCKBWNXGXTOKVLZPLRAGKZG9GXKFRFWERKBFYMPRLAGVZTRVYPEPHBMUPDMRQ9DPZ"
],
"threshold": 100
}'