sdk.js
7.01 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
'use strict';
const Service = require('egg').Service;
const request = require('request');
const net = {
oldNet: '',
newNet: ''
}
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, peer_url) {
const { config } = this;
const peer_host = await this.getPeerHost();
const url = peer_url ? `${peer_url}/api/accounts?address=${address}` : `${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);
}
console.log(url, body)
resolve(JSON.parse(body));
});
});
return result;
}
// 账户信息列表
async getAccountList(page, per_page, peer_url) {
const { config } = this;
const peer_host = await this.getPeerHost();
const offect = per_page * page
const url = peer_url ? `${peer_url}/api/accounts/top?offect=${offect}&limit=${per_page}` : `${peer_host}/api/accounts/top?page=${page}&per_page=${per_page}`;
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 getAccountCount(peer_url) {
const { config } = this;
const peer_host = await this.getPeerHost();
const url = peer_url ? `${peer_url}/api/accounts/count` : `${peer_host}/api/accounts/count`;
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, peer_url) {
console.log(trs)
const { config } = this;
const peer_host = await this.getPeerHost();
const url = peer_url ? `${peer_url}/peer/transactions` : `${peer_host}/api/transactions`;
let ddn_result = await new Promise(resolve => {
request({
url,
method: 'POST',
headers: {
'Content-Type': 'application/json',
version: '',
nethash: config.ed25519NetHash,
},
body: trs,
}, (error, response, body) => {
console.log(error, 'body', body)
resolve(JSON.parse(body));
});
});
console.log('ddn_result', ddn_result)
// ddn_result = JSON.parse(ddn_result);
return ddn_result;// 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;