-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.test.ts
451 lines (398 loc) · 17.6 KB
/
client.test.ts
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
443
444
445
446
447
448
449
450
451
import { LumWallet, LumWalletFactory, LumClient, LumUtils, LumConstants, LumRegistry, LumTypes, LumMessages } from '../src';
import { BeamData, BeamState } from '../src/codec/lum-network/beam/beam';
import { requestCoinsFromFaucet } from './utils';
const randomString = (): string => {
return Math.random().toString(36).substring(7);
};
describe('LumClient', () => {
let clt: LumClient;
let w1: LumWallet;
let w2: LumWallet;
beforeAll(async () => {
clt = await LumClient.connect('https://node0.testnet.lum.network/rpc');
// Prepare the wallets
w1 = await LumWalletFactory.fromMnemonic(LumUtils.generateMnemonic());
w2 = await LumWalletFactory.fromMnemonic(LumUtils.generateMnemonic());
expect(w1.getAddress()).not.toEqual(w2.getAddress());
// Seed them with faucet coins each
await requestCoinsFromFaucet(clt, w1.getAddress());
await requestCoinsFromFaucet(clt, w2.getAddress());
});
afterAll(async () => {
clt.disconnect();
});
it('should allow to connect via webshockets', async () => {
const wsClt = await LumClient.connect('wss://node0.testnet.lum.network/rpc');
expect(await wsClt.status()).toBeTruthy();
wsClt.disconnect();
});
it.only('should be able to simulate transactions', async () => {
const w3 = await LumWalletFactory.fromMnemonic(LumUtils.generateMnemonic());
// Should reject invalid bech32 addresses
await expect(
clt.queryClient.tx.simulate([LumMessages.BuildMsgSend(w1.getAddress(), 'toto', [{ denom: LumConstants.MicroLumDenom, amount: '1' }])], 'hello', w1.getPublicKey(), 0),
).rejects.toThrow();
// Should reject invalid signer
await expect(
clt.queryClient.tx.simulate([LumMessages.BuildMsgSend(w3.getAddress(), w3.getAddress(), [{ denom: LumConstants.MicroLumDenom, amount: '1' }])], 'hello', w3.getPublicKey(), 0),
).rejects.toThrow();
// Should reject invalid amounts
await expect(
clt.queryClient.tx.simulate([LumMessages.BuildMsgSend(w1.getAddress(), w1.getAddress(), [{ denom: LumConstants.MicroLumDenom, amount: '-1' }])], 'hello', w3.getPublicKey(), 0),
).rejects.toThrow();
// Should reject invalid sequences
await expect(
clt.queryClient.tx.simulate([LumMessages.BuildMsgSend(w1.getAddress(), w1.getAddress(), [{ denom: LumConstants.MicroLumDenom, amount: '1' }])], 'hello', w3.getPublicKey(), -1),
).rejects.toThrow();
// Should return simulation in case of success
const res = await clt.queryClient.tx.simulate(
[LumMessages.BuildMsgSend(w1.getAddress(), w1.getAddress(), [{ denom: LumConstants.MicroLumDenom, amount: '1' }])],
'hello',
w3.getPublicKey(),
0,
);
expect(res).toBeTruthy();
expect(res.gasInfo).toBeTruthy();
expect(res.result).toBeTruthy();
});
it('should open a beam and close it', async () => {
const beamId = randomString();
let acc = await clt.getAccount(w1.getAddress());
expect(acc).toBeTruthy();
const chainId = await clt.getChainId();
const amount: LumTypes.Coin = {
amount: '1',
denom: LumConstants.MicroLumDenom,
};
const fee = {
amount: [{ denom: LumConstants.MicroLumDenom, amount: '1' }],
gas: '100000',
};
// Create the beam
let doc = {
accountNumber: acc.accountNumber,
chainId,
fee: fee,
memo: 'Beam review transaction',
messages: [LumMessages.BuildMsgOpenBeam(beamId, w1.getAddress(), '', amount, 'test', 'lum-network/review', null, 0, 0)],
signers: [
{
accountNumber: acc.accountNumber,
sequence: acc.sequence,
publicKey: w1.getPublicKey(),
},
],
};
const txCreate = await clt.signAndBroadcastTx(w1, doc);
expect(txCreate.deliverTx.code).toBe(0);
const beamAfterCreate = await clt.queryClient.beam.get(beamId);
expect(beamAfterCreate.status).toBe(BeamState.OPEN);
// Update the beam
acc = await clt.getAccount(w1.getAddress());
doc = {
accountNumber: acc.accountNumber,
chainId,
fee: fee,
memo: 'Beam review transaction',
messages: [LumMessages.BuildMsgUpdateBeam(beamId, w1.getAddress(), null, BeamState.CANCELED)],
signers: [
{
accountNumber: acc.accountNumber,
sequence: acc.sequence,
publicKey: w1.getPublicKey(),
},
],
};
const txUpdate = await clt.signAndBroadcastTx(w1, doc);
expect(txUpdate.deliverTx.code).toBe(0);
const beamAfterUpdate = await clt.queryClient.beam.get(beamId);
expect(beamAfterUpdate.status).toBe(BeamState.CANCELED);
});
it('should open a beam review transaction', async () => {
const beamId = randomString();
// Here we wait until the faucet transaction get dispatched and the account finally exists on the blockchain
// This should be improved since... you know...
const acc = await clt.getAccount(w1.getAddress());
expect(acc).toBeTruthy();
const chainId = await clt.getChainId();
const amount: LumTypes.Coin = {
amount: '1',
denom: LumConstants.MicroLumDenom,
};
const openBeamMsg = LumMessages.BuildMsgOpenBeam(
beamId,
w1.getAddress(),
'',
amount,
'test',
'lum-network/review',
BeamData.fromPartial({
reward: {
amount: 1,
status: 'pending',
trigger: 'purchase',
maxAmount: 2,
currency: 'EUR',
},
verifier: {
name: 'test',
url: 'https://test.com',
signature: 'test',
},
reviewer: {
reviewerId: 'kqjsndqj342',
name: 'John Doe',
isAnonymous: false,
},
merchantReview: {
reviewId: 'sjqdqsd444sq',
reviewUrl: 'https://google.com',
title: 'Good',
orderId: 'js44',
ratingUrl: 'https://google.com',
timestamp: new Date().toString(),
collectionMethod: 'purchase',
merchantUrl: 'https://google.com',
content: {
overall: 'Not bad not good',
customerService: 'Not good not bad',
},
ratings: {
nps: 3,
customerService: 3,
overall: 3,
},
},
productsReviews: [
{
title: 'Product',
timestamp: new Date().toString(),
ratingUrl: 'https://google.com',
reviewUrl: 'https://google.com',
orderId: '123445',
reviewId: '54321',
ratings: {
overall: 3,
quality: 3,
},
content: {
overall: 'a',
cons: 'b',
pros: 'd',
},
collectionMethod: 'purchase',
medias: [],
products: [],
},
],
}),
);
const fee = {
amount: [{ denom: LumConstants.MicroLumDenom, amount: '1' }],
gas: '100000',
};
const doc = {
accountNumber: acc.accountNumber,
chainId,
fee: fee,
memo: 'Beam review transaction',
messages: [openBeamMsg],
signers: [
{
accountNumber: acc.accountNumber,
sequence: acc.sequence,
publicKey: w1.getPublicKey(),
},
],
};
const tx = await clt.signAndBroadcastTx(w1, doc);
expect(tx.deliverTx.code).toBe(0);
});
it('Should open a beam reward transaction', async () => {
const beamId = randomString();
// Here we wait until the faucet transaction get dispatched and the account finally exists on the blockchain
// This should be improved since... you know...
const acc = await clt.getAccount(w1.getAddress());
expect(acc).toBeTruthy();
const chainId = await clt.getChainId();
const amount: LumTypes.Coin = {
amount: '1',
denom: LumConstants.MicroLumDenom,
};
const openBeamMsg = LumMessages.BuildMsgOpenBeam(
beamId,
w1.getAddress(),
'',
amount,
'test',
'lum-network/reward',
BeamData.fromPartial({
reward: {
amount: 1,
status: 'pending',
currency: 'EUR',
maxAmount: 2,
trigger: 'purchase',
details: [],
},
verifier: {
name: 'test',
url: 'https://test.com',
signature: 'test',
},
}),
);
const fee = {
amount: [{ denom: LumConstants.MicroLumDenom, amount: '1' }],
gas: '100000',
};
const doc = {
accountNumber: acc.accountNumber,
chainId,
fee: fee,
memo: 'Beam reward transaction',
messages: [openBeamMsg],
signers: [
{
accountNumber: acc.accountNumber,
sequence: acc.sequence,
publicKey: w1.getPublicKey(),
},
],
};
const tx = await clt.signAndBroadcastTx(w1, doc);
expect(tx.deliverTx.code).toBe(0);
});
it('Should expose basic information', async () => {
const height = (await clt.getBlockHeight()) - 1;
expect(clt.getChainId()).resolves.toEqual('lumnetwork-testnet');
expect(height).toBeGreaterThan(0);
expect(clt.getBlock(height)).resolves.toBeTruthy();
});
it('should expose tendermint rpcs', async () => {
const height = (await clt.getBlockHeight()) - 1;
expect(height).toBeGreaterThan(0);
expect(clt.tmClient.health()).resolves.toBeNull();
expect(clt.tmClient.status()).resolves.toBeTruthy();
expect(clt.tmClient.genesis()).resolves.toBeTruthy();
expect(clt.tmClient.abciInfo()).resolves.toBeTruthy();
expect(clt.tmClient.block(height)).resolves.toBeTruthy();
expect(clt.tmClient.blockResults(height)).resolves.toBeTruthy();
expect(clt.tmClient.blockchain(0, height)).resolves.toBeTruthy();
expect(clt.tmClient.validatorsAll(height)).resolves.toBeTruthy();
});
it('Should expose bank module', async () => {
const supplies = await clt.queryClient.bank.totalSupply();
expect(supplies).toBeTruthy();
expect(supplies.length).toBeGreaterThan(0);
const lumSupply = supplies.filter((c) => c.denom === LumConstants.MicroLumDenom)[0];
expect(lumSupply).toBeTruthy();
expect(parseFloat(lumSupply.amount)).toBeGreaterThan(0);
});
it('Should expose staking module', async () => {
const validators = await clt.tmClient.validatorsAll();
expect(validators.validators.length).toBeGreaterThanOrEqual(1);
const block = await clt.getBlock();
let found = false;
for (let v = 0; v < validators.validators.length; v++) {
const val = validators.validators[v];
if (LumUtils.toHex(val.address) === LumUtils.toHex(block.block.header.proposerAddress)) {
found = true;
break;
}
}
expect(found).toBeTruthy();
// Get first available block
const firstBlock = await clt.getBlock(2);
// Get boot val (genesis) with address genesis proposer address
const bootVal = validators.validators.filter((v) => LumUtils.toHex(v.address) === LumUtils.toHex(firstBlock.block.header.proposerAddress))[0];
expect(bootVal).toBeTruthy();
// Get staking validator by matching it using pubkeys
const stakers = await clt.queryClient.staking.validators('BOND_STATUS_BONDED');
const bootStak = stakers.validators.filter((s) => LumUtils.toHex((LumRegistry.decode(s.consensusPubkey) as LumTypes.PubKey).key) === LumUtils.toHex(bootVal.pubkey.data))[0];
expect(bootVal).toBeTruthy();
// Get account information by deriving the address from the operator address
const delegAddress = LumUtils.toBech32(LumConstants.LumBech32PrefixAccAddr, LumUtils.fromBech32(bootStak.operatorAddress).data);
const account = await clt.getAccount(delegAddress);
expect(account).toBeTruthy();
// Get account balances
const balances = await clt.getAllBalances(account.address);
expect(balances).toBeTruthy();
expect(balances.length).toBeGreaterThan(0);
const lumBalance = balances.filter((b) => b.denom === LumConstants.MicroLumDenom)[0];
expect(lumBalance).toBeTruthy();
expect(parseFloat(lumBalance.amount)).toBeGreaterThan(0);
});
it('Should expose distribution module', async () => {
// Get validators
const validators = await clt.tmClient.validatorsAll();
expect(validators.validators.length).toBeGreaterThanOrEqual(1);
// Get first available block
const firstBlock = await clt.getBlock(2);
// Get boot val (genesis) with address genesis proposer address
const bootVal = validators.validators.filter((v) => LumUtils.toHex(v.address) === LumUtils.toHex(firstBlock.block.header.proposerAddress))[0];
expect(bootVal).toBeTruthy();
// Get genesis validator account address
const stakers = await clt.queryClient.staking.validators('BOND_STATUS_BONDED');
const bootStak = stakers.validators.filter((s) => LumUtils.toHex((LumRegistry.decode(s.consensusPubkey) as LumTypes.PubKey).key) === LumUtils.toHex(bootVal.pubkey.data))[0];
expect(bootVal).toBeTruthy();
// Get account information by deriving the address from the operator address
const delegAddress = LumUtils.toBech32(LumConstants.LumBech32PrefixAccAddr, LumUtils.fromBech32(bootStak.operatorAddress).data);
const account = await clt.getAccount(delegAddress);
expect(account).toBeTruthy();
const deleg = await clt.queryClient.distribution.delegatorWithdrawAddress(account.address);
expect(deleg).toBeTruthy();
expect(deleg.withdrawAddress).toEqual(account.address);
const delegValidators = await clt.queryClient.distribution.delegatorValidators(account.address);
expect(delegValidators).toBeTruthy();
expect(delegValidators.validators.length).toBeGreaterThan(0);
});
it('Should expose the mint module', async () => {
const supply = await clt.getAllSupplies();
const params = await clt.queryClient.mint.params();
expect(params).toBeTruthy();
expect(params.inflationMin).toBeTruthy();
expect(params.inflationMax).toBeTruthy();
expect(params.inflationRateChange).toBeTruthy();
expect(params.mintDenom).toBeTruthy();
expect(params.blocksPerYear).toBeTruthy();
expect(params.goalBonded).toBeTruthy();
const inflation = await clt.queryClient.mint.inflation();
expect(parseInt(inflation)).toBeGreaterThan(0);
const annualProvisions = await clt.queryClient.mint.annualProvisions();
expect(parseInt(annualProvisions)).toBeGreaterThan(0);
});
it('Should allow multiple signers per transaction', async () => {
const acc1 = await clt.getAccount(w1.getAddress());
const acc2 = await clt.getAccount(w2.getAddress());
const chainId = await clt.getChainId();
const fee = {
amount: [{ denom: LumConstants.MicroLumDenom, amount: '1' }],
gas: '300000',
};
const doc = {
accountNumber: 0, //acc1.accountNumber,
chainId,
fee: fee,
memo: 'Just a open beam transaction',
messages: [
LumMessages.BuildMsgSend(w1.getAddress(), w2.getAddress(), [{ denom: LumConstants.MicroLumDenom, amount: '99' }]),
LumMessages.BuildMsgSend(w2.getAddress(), w1.getAddress(), [{ denom: LumConstants.MicroLumDenom, amount: '99' }]),
],
signers: [
{
accountNumber: acc1.accountNumber,
sequence: acc1.sequence,
publicKey: w1.getPublicKey(),
},
{
accountNumber: acc2.accountNumber,
sequence: acc2.sequence,
publicKey: w2.getPublicKey(),
},
],
};
const res = await clt.signAndBroadcastTxForMultiWallet([w1, w2], doc);
expect(LumUtils.broadcastTxCommitSuccess(res));
});
});