如何获取 ThunderCore 账户的余额?
How do I get the balance of an account on ThunderCore?
我想以编程方式查询地址或地址列表的余额。最好的方法是什么?
要在最新区块获得 0xc466c8ff5dAce08A09cFC63760f7Cc63734501C1
的余额,请执行:
curl -X POST -H 'Content-Type: application/json' -s --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0xad23b02673214973e354d41e19999d9e01f3be58", "latest"], "id":1}' https://mainnet-rpc.thundercore.com/
输出:{"jsonrpc":"2.0","id":1,"result":"0xde0b6b3a7640000"}
获取单个账户余额 web3.js:
const Eth = require('web3-eth');
const Web3 = require('web3');
const web3Provider = () => {
return Eth.giveProvider || 'https://mainnet-rpc.thundercore.com';
}
const balance = async (address) => {
const eth = new Eth(web3Provider());
return Web3.utils.fromWei(await eth.getBalance(address));
}
示例会话
const address = '0xc466c8ff5dAce08A09cFC63760f7Cc63734501C1';
await balance(address) // -> '1'
单位
0xde0b6b3a7640000
等于 10**18
- 使用以太坊术语,
fromWei
将 10^18
Wei 转换为 1
Ether,或使用 Thunder 术语
- 使用 ThunderCore 术语,
fromWei
将 10^18
Ella 转换为 1
TT
fromWei(0xde0b6b3a7640000)
等于 fromWei(10**18)
等于 1
批量查询余额
- 对于地址数组,您可以使用 JSON-RPC 2.0 的 Batch Requests 来节省网络往返次数
- 查询
https://mainnet-rpc.thundercore.com
时,将批量大小限制在30
左右
下面的class包裹了web3.js-1.2.6
的BatchRequest
,使之成为return一个JavascriptPromise
:
class BatchRequest {
constructor(web3) {
this.b = new web3.BatchRequest();
this.results = [];
this.resolve = null;
this.reject = null;
this.resultsFilled = 0;
}
web3BatchRequestCallBack(index, err, result) {
/* if any request in our batch fails, reject the promise we return from `execute` */
if (err) {
this.reject(new Error(`request ${index} failed: ${err}`))
return;
}
this.results[index] = result;
this.resultsFilled++;
if (this.resultsFilled === this.results.length) {
this.resolve(this.results);
}
}
resultPromiseExecutor(resolve, reject) {
this.resolve = resolve;
this.reject = reject;
}
add(method /* web3-core-method.Method */) {
const index = this.results.length;
method.callback = (err, result) => {
this.web3BatchRequestCallBack(index, err, result)
};
this.b.add(method);
this.results.push(undefined);
}
execute() /*: Promise */ {
const p = new Promise((resolve, reject) => { this.resultPromiseExecutor(resolve, reject) });
/* must arrange for resultPromiseExecutor to be called before b.execute */
this.b.execute();
return p;
}
}
const balanceBatch = async (addresses) => {
const eth = new Eth(web3Provider());
const b = new BatchRequest(eth);
for (a of addresses) {
b.add(eth.getBalance.request(a));
}
const ellaS = await b.execute();
const ttS = [];
for (e of ellaS) {
ttS.push(Web3.utils.fromWei(e));
}
return ttS;
}
batch-balance-test.js
const Web3 = require('web3');
(async() => {
const web3 = new Web3('https://mainnet-rpc.thundercore.com');
const results = await balanceBatch([
'0xc466c8ff5dAce08A09cFC63760f7Cc63734501C1',
'0x4f3c8e20942461e2c3bdd8311ac57b0c222f2b82',
]);
console.log('results:', results);
})();
示例会话
$ node batch-balance-test.js
results: [ '1', '84.309961496' ]
在 field-support
存储库的 balance
分支中查看完整的项目设置 here。
我想以编程方式查询地址或地址列表的余额。最好的方法是什么?
要在最新区块获得 0xc466c8ff5dAce08A09cFC63760f7Cc63734501C1
的余额,请执行:
curl -X POST -H 'Content-Type: application/json' -s --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0xad23b02673214973e354d41e19999d9e01f3be58", "latest"], "id":1}' https://mainnet-rpc.thundercore.com/
输出:{"jsonrpc":"2.0","id":1,"result":"0xde0b6b3a7640000"}
获取单个账户余额 web3.js:
const Eth = require('web3-eth');
const Web3 = require('web3');
const web3Provider = () => {
return Eth.giveProvider || 'https://mainnet-rpc.thundercore.com';
}
const balance = async (address) => {
const eth = new Eth(web3Provider());
return Web3.utils.fromWei(await eth.getBalance(address));
}
示例会话
const address = '0xc466c8ff5dAce08A09cFC63760f7Cc63734501C1';
await balance(address) // -> '1'
单位
0xde0b6b3a7640000
等于10**18
- 使用以太坊术语,
fromWei
将10^18
Wei 转换为1
Ether,或使用 Thunder 术语 - 使用 ThunderCore 术语,
fromWei
将10^18
Ella 转换为1
TT fromWei(0xde0b6b3a7640000)
等于fromWei(10**18)
等于1
批量查询余额
- 对于地址数组,您可以使用 JSON-RPC 2.0 的 Batch Requests 来节省网络往返次数
- 查询
https://mainnet-rpc.thundercore.com
时,将批量大小限制在30
左右
下面的class包裹了web3.js-1.2.6
的BatchRequest
,使之成为return一个JavascriptPromise
:
class BatchRequest {
constructor(web3) {
this.b = new web3.BatchRequest();
this.results = [];
this.resolve = null;
this.reject = null;
this.resultsFilled = 0;
}
web3BatchRequestCallBack(index, err, result) {
/* if any request in our batch fails, reject the promise we return from `execute` */
if (err) {
this.reject(new Error(`request ${index} failed: ${err}`))
return;
}
this.results[index] = result;
this.resultsFilled++;
if (this.resultsFilled === this.results.length) {
this.resolve(this.results);
}
}
resultPromiseExecutor(resolve, reject) {
this.resolve = resolve;
this.reject = reject;
}
add(method /* web3-core-method.Method */) {
const index = this.results.length;
method.callback = (err, result) => {
this.web3BatchRequestCallBack(index, err, result)
};
this.b.add(method);
this.results.push(undefined);
}
execute() /*: Promise */ {
const p = new Promise((resolve, reject) => { this.resultPromiseExecutor(resolve, reject) });
/* must arrange for resultPromiseExecutor to be called before b.execute */
this.b.execute();
return p;
}
}
const balanceBatch = async (addresses) => {
const eth = new Eth(web3Provider());
const b = new BatchRequest(eth);
for (a of addresses) {
b.add(eth.getBalance.request(a));
}
const ellaS = await b.execute();
const ttS = [];
for (e of ellaS) {
ttS.push(Web3.utils.fromWei(e));
}
return ttS;
}
batch-balance-test.js
const Web3 = require('web3');
(async() => {
const web3 = new Web3('https://mainnet-rpc.thundercore.com');
const results = await balanceBatch([
'0xc466c8ff5dAce08A09cFC63760f7Cc63734501C1',
'0x4f3c8e20942461e2c3bdd8311ac57b0c222f2b82',
]);
console.log('results:', results);
})();
示例会话
$ node batch-balance-test.js
results: [ '1', '84.309961496' ]
在 field-support
存储库的 balance
分支中查看完整的项目设置 here。