This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
test_tx_iris.js
442 lines (393 loc) · 14.5 KB
/
test_tx_iris.js
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
const Irisnet = require('../index');
const chai = require('chai');
const assert = chai.assert;
const common = require('./common');
const url ="https://irisnet-lcd.dev.bianjie.ai/tx/broadcast?commit=true";
const chainName ="iris";
describe('iris transaction', function () {
let chain_id = "rainbow-dev";
let from = "faa176dd0tgn38grpc8hpxfmwl6sl8jfmknesgawx7";
let gas = 100000;
let account_number = 3;
let fees = {denom: "iris-atto", amount: 600000000000000000};
let memo = "1";
let privateKey = "A3ACB76D1047ED290D3D1E3ABB51375F16D2887DBC6821F43B9965C4D11F00FA";
let pubKey = "fap1addwnpepqwqw5pshzzswemf6t00xvf0ccf2fxslaz40dp76uyad5mgujfju4zt8km3u";
let chain = Irisnet.config.chain.iris;
//测试热钱包相关交易
describe('test warm wallet tx', function () {
it('test simulate transfer', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 57,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.transfer.type,
mode: Irisnet.config.iris.mode.try,
msg: {
to: "faa1s6v9qgu8ye7d884s8kpye64x66znndg8t6eztj",
coins: [
{
denom: "iris-atto",
amount: 10000000000000000000
}
]
}
};
simulate(tx);
});
it('test transfer', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 57,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.transfer.type,
msg: {
to: "faa1s6v9qgu8ye7d884s8kpye64x66znndg8t6eztj",
coins: [
{
denom: "iris-atto",
amount: 10000000000000000000
}
]
}
};
common.verifyTx(url,tx,privateKey,chainName,verify);
});
it('test delegate', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 12,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.delegate.type,
msg: {
validator_addr: "fva1aake3umjllpd9es5d3qmry4egcne0f8ajd7vdp",
delegation: {
denom: "iris-atto",
amount: 1000000000000000000000000
}
}
};
common.verifyTx(url,tx,privateKey,chainName,verify);
});
it('test BeginUnbonding', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 43,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.undelegate.type,
msg: {
validator_addr: "fva1aake3umjllpd9es5d3qmry4egcne0f8ajd7vdp",
shares_amount: "1000000000000000000000000"
}
};
common.verifyTx(url,tx,privateKey,chainName,verify);
});
it('test BeginRedelegate', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 22,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.redelegate.type,
msg: {
validator_src_addr: "fva1kca5vw7r2k72d5zy0demszmrhdz4dp8t4uat0c",
validator_dst_addr: "fva1rz7jxmgsgyjwa6erusxlzrmg2aw3cvyf3c3x6v",
shares_amount: "1000000000000000000000000"
}
};
common.verifyTx(url,tx,privateKey,chainName,verify);
});
it('test MsgWithdrawValidatorRewardsAll', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 32,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.withdrawValidatorRewardsAll.type,
msg: {
validator_addr: "fva186qhtc62cf6ejlt3erw6zk28mgw8ne7grhmyfn",
}
//mode: Irisnet.config.iris.mode.try
};
// extracted(tx);
common.verifyTx(url,tx,privateKey,chainName,verify);
});
it('test MsgWithdrawDelegatorRewardsAll', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 25,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.withdrawDelegationRewardsAll.type,
//mode: Irisnet.config.iris.mode.try
};
common.verifyTx(url,tx,privateKey,chainName,verify);
});
it('test MsgWithdrawDelegatorReward', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 94,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.withdrawDelegationReward.type,
msg: {
validator_addr: "fva16jr2kxm8xwaux7tkv0sux0qgn8xqp9nkj27ane",
}
};
common.verifyTx(url,tx,privateKey,chainName,verify);
});
it('test MsgSetWithdrawAddress', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 121,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.setWithdrawAddress.type,
msg: {
withdraw_addr: "faa1ljemm0yznz58qxxs8xyak7fashcfxf5lssn6jm",
}
};
common.verifyTx(url,tx,privateKey,chainName,verify);
});
});
//测试冷钱包相关交易
describe('test cold wallet tx ', function () {
it('test transfer', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 53,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.transfer.type,
msg: {
to: "faa1s6v9qgu8ye7d884s8kpye64x66znndg8t6eztj",
coins: [
{
denom: "iris-atto",
amount: 10000000000000000000
}
]
}
};
extracted(tx);
});
it('test delegate', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 55,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.delegate.type,
msg: {
validator_addr: "fva1kca5vw7r2k72d5zy0demszmrhdz4dp8t4uat0c",
delegation: {
denom: "iris-atto",
amount: 10000000000000000000
}
}
};
extracted(tx);
});
it('test BeginUnbonding', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 45,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.undelegate.type,
msg: {
validator_addr: "fva16h3uazd2wknrae7ql0dqpjw69s5kp44slme6hr",
shares_amount: "10000000000000000000"
}
};
extracted(tx);
});
it('test BeginRedelegate', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 20,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.redelegate.type,
msg: {
validator_src_addr: "fva1cr6xfpp078nm7yfsh36850ftu20fl3c9duchrk",
validator_dst_addr: "fva1xde0yh9vmc8mnkdvdr5krllfe3gslw9d4qp2wd",
shares_amount: "10000000000000000000"
}
};
extracted(tx);
});
it('test MsgWithdrawDelegatorRewardsAll', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 46,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.withdrawDelegationRewardsAll.type,
};
extracted(tx);
});
it('test MsgWithdrawDelegatorReward', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 26,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.withdrawDelegationReward.type,
msg: {
validator_addr: "fva1xde0yh9vmc8mnkdvdr5krllfe3gslw9d4qp2wd",
}
};
extracted(tx);
});
it('test MsgSetWithdrawAddress', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 112,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.setWithdrawAddress.type,
msg: {
withdraw_addr: "faa1ljemm0yznz58qxxs8xyak7fashcfxf5lssn6jm",
}
};
extracted(tx);
});
it('test MsgDeposit', function () {
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 11,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.deposit.type,
msg: {
proposal_id : 3,
amount : [
{
denom: "iris-atto",
amount: 3000000000000000000000
}
]
}
};
extracted(tx);
});
it('test MsgVote', function () {
this.timeout(10000);
let tx = {
chain_id: chain_id,
from: from,
account_number: account_number,
sequence: 13,
fees: fees,
gas: gas,
memo: memo,
type: Irisnet.config.iris.tx.vote.type,
msg: {
proposal_id : 3,
option: 0x01
}
};
extracted(tx);
});
});
//冷钱包调用
function extracted(tx, chain = 'iris') {
let builder = Irisnet.getBuilder(chain,'testnet');
//①先用联网的钱包构造一笔交易
let stdTx = builder.buildTx(tx);
//②把步骤①的结构序列化为字符串,装入二维码
let signStr = JSON.stringify(stdTx.GetSignBytes());
//③用未联网的钱包(存有账户秘钥)扫描步骤②的二维码,拿到待签名的字符串,调用signTx签名
console.log("GetSignBytes:", signStr);
let signature = builder.sign(signStr, privateKey);
//④
let signatureStr = JSON.stringify(signature);//二维码字符串
stdTx.SetSignature(signatureStr);
//console.log("======待提交交易======");
//④步骤③的结果调用GetData,得到交易字符串,回传给联网的钱包,并发送该内容给irishub-server
console.log(JSON.stringify(stdTx.GetData()));
//以下步骤为异常处理:在请求irishub-server超时的时候,服务器可能没有任何返回结果,这笔交易状态为止,所以需要客户端计算出
//本次交易的hash,校准该笔交易的状态。调用步骤③结构的Hash,可以得到交易hash以及本次交易内容的base64编码(以后考虑使用该编码内容替换
// GetPostData,解耦crypto和irishub交易结构的依赖)
let postTx = stdTx.GetData();
postTx.mode = "commit";
let resp = common.sendBySync("POST",url,postTx);
let result = stdTx.Hash();
console.log("data:", result.data);
console.log("hash", result.hash);
console.log(`hash=${result.hash}`);
assert.notExists(resp.code,`tx commit failed,${resp.raw_log}`);
//console.log("displayContent", JSON.stringify(stdTx.GetDisplayContent()));
}
//simulate
function simulate(tx) {
let builder = Irisnet.getBuilder(chain);
let stdTx = builder.buildAndSignTx(tx);
stdTx.SetPubKey(pubKey);
console.log("======stdTx======");
console.log(JSON.stringify(stdTx.GetData()));
// console.log("======待提交交易======");
let result = stdTx.Hash();
console.log("hash", result.hash);
}
});
function verify(act,exp,data) {
console.log('result:',act,exp,data);
assert.notExists(act.check_tx.code,`tx commit failed,${act.check_tx.log}`);
}