sdk_20220214153103.js
5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'use strict';
const Service = require('egg').Service;
const request = require('request');
class DdnService extends Service {
async getPeers() {
const { config } = this;
const peer_host = await this.getPeerHost();
const url = `${peer_host}/api/peers`;
const result = await new Promise(resolve => {
request({
url,
method: 'GET',
headers: {
'Content-Type': 'application/json',
version: '',
nethash: config.nethash,
},
}, (error, response, body) => {
resolve(JSON.parse(body));
});
});
return result;
}
// 区块高度
async getHeight() {
const { config } = this;
const peer_host = await this.getPeerHost();
const url = `${peer_host}/api/blocks/getHeight`;
const result = await new Promise(resolve => {
request({
url,
method: 'GET',
headers: {
'Content-Type': 'application/json',
version: '',
nethash: config.nethash,
},
}, (error, response, body) => {
resolve(JSON.parse(body));
});
});
return result;
}
// 交易详情
async getTransaction(id) {
const { config } = this;
const peer_host = await this.getPeerHost();
const url = `${peer_host}/api/transactions/get?id=${id}`;
const result = await new Promise(resolve => {
request({
url,
method: 'GET',
headers: {
'Content-Type': 'application/json',
version: '',
nethash: config.nethash,
},
}, (error, response, body) => {
resolve(JSON.parse(body));
});
});
return result;
}
// 交易列表
async getTransactions(con, page = 1, per_page = 10) {
const { config } = this;
const peer_host = await this.getPeerHost();
const conditions = [];
if (con.sender_id) {
conditions.push('senderId=' + con.sender_id);
}
if (con.recipient_id) {
conditions.push('recipientId=' + con.recipient_id);
}
conditions.push('limit=' + Number(per_page));
conditions.push('offset=' + (Number(page) - 1) * Number(per_page));
const url = `${peer_host}/api/transactions?${conditions.join('&')}`;
const result = await new Promise(resolve => {
request({
url,
method: 'GET',
headers: {
'Content-Type': 'application/json',
version: '',
nethash: config.nethash,
},
}, (error, response, body) => {
resolve(JSON.parse(body));
});
});
return result;
}
// 账户信息
async getAccount(address) {
const { config } = this;
const peer_host = await this.getPeerHost();
const url = `${peer_host}/api/accounts?address=${address}`;
const result = await new Promise((resolve, reject) => {
request({
url,
method: 'GET',
headers: {
'Content-Type': 'application/json',
version: '',
nethash: config.nethash,
},
}, (error, response, body) => {
if (error) {
reject(error);
}
resolve(JSON.parse(body));
});
});
return result;
}
// 交易上链
async pushTransaction(trs) {
const { config } = this;
const peer_host = await this.getPeerHost();
const url = `${peer_host}/peer/transactions`;
let ddn_result = await new Promise(resolve => {
request({
url,
method: 'POST',
headers: {
'Content-Type': 'application/json',
version: '',
nethash: config.nethash,
},
body: trs,
}, (error, response, body) => {
resolve(body);
});
});
ddn_result = JSON.parse(ddn_result);
return ddn_result;
}
// 获取结点地址
async getPeerHost() {
// const peer_host = await this.service.sdk.redis.get('peer_host');
// if (!peer_host) {
const res = await this.checkPeerHost();
return res;
// }
// return peer_host.host;
}
// 检查结点并指定可用结点
async checkPeerHost() {
const { config } = this;
// const peers = this.config.peer_list;
// const peers = [ 'http://120.24.69.99:8003' ];
const peers = ['http://localhost:8001'];
const peers_height = {};
let max_height = null;
let min_height = null;
let best_peer = null;
for (let i = 0; i < peers.length; i++) {
const url = `${peers[i]}/api/blocks/getstatus`;
const result = await new Promise(resolve => {
request({
url,
method: 'GET',
headers: {
'Content-Type': 'application/json',
version: '',
nethash: config.nethash,
},
}, (error, response, body) => {
if (error) {
resolve({ success: false });
} else {
resolve(JSON.parse(body));
}
});
});
if (result.success) {
peers_height[peers[i]] = result.height;
if (!max_height || Number(result.height) > Number(max_height)) {
max_height = result.height;
}
if (!min_height || Number(result.height) < Number(min_height)) {
min_height = result.height;
}
if (!best_peer || Number(result.height) === Number(max_height)) {
best_peer = peers[i];
}
} else {
}
}
return best_peer;
}
}
module.exports = DdnService;