1 | const xhr = require("axios");
|
2 | const byteArray2hexStr = require("../utils/bytes").byteArray2hexStr;
|
3 | const deserializeTransaction = require("../protocol/serializer").deserializeTransaction;
|
4 | const bytesToString = require("../utils/bytes").bytesToString;
|
5 | const {base64DecodeFromString} = require("../utils/bytes");
|
6 | const {Block, Transaction} = require("../protocol/core/Tron_pb");
|
7 | const {AccountList, NumberMessage, WitnessList} = require("../protocol/api/api_pb");
|
8 | const {TransferContract} = require("../protocol/core/Contract_pb");
|
9 |
|
10 | class HttpClient {
|
11 |
|
12 | constructor(options = {}) {
|
13 | this.hostname = options.hostname || "tronscan.io";
|
14 | this.port = options.port || 80;
|
15 |
|
16 | |
17 |
|
18 |
|
19 | this.url = `https://${this.hostname}`;
|
20 | if (this.port !== 80) {
|
21 | this.url+= `:${this.port}`;
|
22 | }
|
23 | }
|
24 |
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 | async getLatestBlock() {
|
31 | let {data} = await xhr.get(`${this.url}/getBlockToView`);
|
32 | let currentBlock = base64DecodeFromString(data);
|
33 | let block = Block.deserializeBinary(currentBlock);
|
34 |
|
35 | return {
|
36 | number: block.getBlockHeader().getRawData().getNumber(),
|
37 | witnessId: block.getBlockHeader().getRawData().getWitnessId(),
|
38 | };
|
39 | }
|
40 |
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 | async getBlockByNum(blockNumber) {
|
47 | let {data} = await xhr.get(`${this.url}/getBlockByNumToView?num=${blockNumber}`);
|
48 | let currentBlock = base64DecodeFromString(data);
|
49 | let blockData = Block.deserializeBinary(currentBlock);
|
50 |
|
51 | let recentBlock = base64DecodeFromString(data);
|
52 |
|
53 | return {
|
54 | size: recentBlock.length,
|
55 | number: blockData.getBlockHeader().getRawData().getNumber(),
|
56 | witnessAddress: byteArray2hexStr(blockData.getBlockHeader().getRawData().getWitnessAddress()),
|
57 | time: blockData.getBlockHeader().getRawData().getTimestamp(),
|
58 | transactionsCount: blockData.getTransactionsList().length,
|
59 | contraxtType: Transaction.Contract.ContractType,
|
60 | transactions: blockData.getTransactionsList().map(deserializeTransaction),
|
61 | };
|
62 | }
|
63 |
|
64 | |
65 |
|
66 |
|
67 |
|
68 | async getTotalNumberOfTransactions() {
|
69 | let {data} = await xhr.get(`${this.url}/getTotalTransaction`);
|
70 | let totalTransaction = base64DecodeFromString(data);
|
71 | let totalData = NumberMessage.deserializeBinary(totalTransaction);
|
72 | return totalData.getNum();
|
73 | }
|
74 |
|
75 | |
76 |
|
77 |
|
78 |
|
79 | async getAccountList() {
|
80 |
|
81 | let {data} = await xhr.get(`${this.url}/accountList`);
|
82 |
|
83 | let bytesAccountList = base64DecodeFromString(data);
|
84 | let account = AccountList.deserializeBinary(bytesAccountList);
|
85 | let accountList = account.getAccountsList();
|
86 |
|
87 | return accountList.map(account => {
|
88 | let name = bytesToString(account.getAccountName());
|
89 | let address = byteArray2hexStr(account.getAddress());
|
90 | let balance = account.getBalance();
|
91 | let balanceNum = 0;
|
92 | if (balance !== 0) {
|
93 | balanceNum = (balance / 1000000).toFixed(6);
|
94 | }
|
95 | return {
|
96 | name,
|
97 | address,
|
98 | balance,
|
99 | balanceNum,
|
100 | };
|
101 | });
|
102 | }
|
103 |
|
104 | |
105 |
|
106 |
|
107 |
|
108 |
|
109 | async getWitnesses() {
|
110 | let {data} = await xhr.get(`${this.url}/witnessList`);
|
111 |
|
112 | let bytesWitnessList = base64DecodeFromString(data);
|
113 | let witness = WitnessList.deserializeBinary(bytesWitnessList);
|
114 | let witnessList = witness.getWitnessesList();
|
115 |
|
116 | return witnessList.map(witness => {
|
117 |
|
118 | return {
|
119 | address: byteArray2hexStr(witness.getAddress()),
|
120 | latestBlockNumber: witness.getLatestblocknum(),
|
121 | producedTotal: witness.getTotalproduced(),
|
122 | missedTotal: witness.getTotalmissed(),
|
123 | votes: witness.getVotecount(),
|
124 | };
|
125 | });
|
126 | }
|
127 |
|
128 | }
|
129 |
|
130 | module.exports = HttpClient;
|