Skip to content

Commit bf9b49c

Browse files
committed
add tests
1 parent 293bc67 commit bf9b49c

File tree

3 files changed

+181
-104
lines changed

3 files changed

+181
-104
lines changed

packages/sdk/src/mintlayer-connect-sdk.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2338,6 +2338,10 @@ class Client {
23382338
const response = await fetch(`${this.getApiServer()}/pool/${pool_id}/delegations`);
23392339
const data = await response.json();
23402340

2341+
if (!response.ok) {
2342+
throw new Error('Failed to fetch delegation id');
2343+
}
2344+
23412345
const delegationIdMap = data.reduce((acc: { [key: string]: string }, item: any) => {
23422346
acc[item.spend_destination] = item.delegation_id;
23432347
return acc;

packages/sdk/tests/__snapshots__/staking.test.ts.snap

Lines changed: 128 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -231,56 +231,71 @@ exports[`delegate staking - snaphsot 1`] = `
231231
],
232232
"outputs": [
233233
Uint8Array [
234-
6,
234+
5,
235+
1,
235236
7,
236-
0,
237-
16,
238-
165,
239-
212,
240-
232,
241-
109,
242-
61,
243-
61,
244-
216,
245-
42,
246-
173,
247-
158,
248-
59,
249-
189,
250-
40,
251-
132,
252-
167,
253-
34,
254-
120,
255-
238,
256-
48,
257-
62,
258-
119,
259-
49,
260-
3,
261-
170,
262-
159,
263-
60,
264-
54,
265-
25,
237+
36,
238+
182,
239+
153,
240+
140,
241+
199,
242+
148,
243+
248,
244+
72,
245+
234,
246+
171,
247+
44,
248+
46,
249+
233,
250+
79,
251+
111,
252+
39,
253+
17,
254+
17,
255+
196,
256+
107,
266257
131,
267-
33,
268-
6,
269-
53,
270-
9,
271-
194,
258+
159,
259+
8,
260+
143,
261+
221,
262+
250,
263+
137,
264+
187,
265+
241,
266+
209,
267+
28,
268+
186,
269+
1,
270+
79,
271+
49,
272+
153,
273+
202,
272274
53,
275+
219,
276+
13,
277+
234,
278+
45,
279+
0,
280+
240,
281+
195,
282+
251,
283+
24,
284+
12,
285+
84,
286+
185,
287+
228,
273288
],
274289
Uint8Array [
275290
0,
276291
0,
277292
15,
278293
224,
279-
52,
280-
115,
281-
15,
282-
247,
283-
11,
294+
68,
295+
24,
296+
228,
297+
223,
298+
12,
284299
6,
285300
1,
286301
134,
@@ -305,9 +320,9 @@ exports[`delegate staking - snaphsot 1`] = `
305320
33,
306321
],
307322
],
308-
"transactionsize": 216,
323+
"transactionsize": 231,
309324
},
310-
"HEXRepresentation_unsigned": "0100040000af3b5fad20f6f97eb210934e942176f7f7d0f70423590659ee0e0217053a7cab010000000806070010a5d4e86d3d3dd82aad9e3bbd2884a72278ee303e773103aa9f3c36198321063509c23500000fe034730ff70b060186ec450457d09ad9807393d89cec9c71d6206021",
325+
"HEXRepresentation_unsigned": "0100040000af3b5fad20f6f97eb210934e942176f7f7d0f70423590659ee0e0217053a7cab010000000805010724b6998cc794f848eaab2c2ee94f6f271111c46b839f088fddfa89bbf1d11cba014f3199ca35db0dea2d00f0c3fb180c54b9e400000fe04418e4df0c060186ec450457d09ad9807393d89cec9c71d6206021",
311326
"JSONRepresentation": {
312327
"inputs": [
313328
{
@@ -332,27 +347,24 @@ exports[`delegate staking - snaphsot 1`] = `
332347
],
333348
"outputs": [
334349
{
335-
"amount": {
336-
"atoms": "1000000000000",
337-
"decimal": "10",
338-
},
339-
"delegation_id": "tdelg1d57nmkp24k0rh0fgsjnjy78wxql8wvgr420ncdsesvssvdgfcg6sx6262w",
340-
"type": "DelegateStaking",
350+
"destination": "tmt1qyrjfd5e3nref7zga24jcthffahjwyg3csxu3xgc",
351+
"pool_id": "tpool1dwpe7zy0mhagnwl36ywt5q20xxvu5dwmph4z6q8sc0a3srz5h8jqr0r2yg",
352+
"type": "CreateDelegationId",
341353
},
342354
{
343355
"destination": "tmt1qxrwc3gy2lgf4kvqwwfa388vn3cavgrqyyrgswe6",
344356
"type": "Transfer",
345357
"value": {
346358
"amount": {
347-
"atoms": "1702005604300000",
348-
"decimal": "17020.056043",
359+
"atoms": "1703005604300000",
360+
"decimal": "17030.056043",
349361
},
350362
"type": "Coin",
351363
},
352364
},
353365
],
354366
},
355-
"transaction_id": "7265e382aa4ab9f8db50cbf858a6f9b3e5fcca8d89ca2b104229cdeb0a38676e",
367+
"transaction_id": "9a2173442aec3f56e834675033e40e65e1b6ba9c09e6be8599d3db8bc8a69c5e",
356368
}
357369
`;
358370

@@ -403,56 +415,71 @@ exports[`delegate staking providing only pool_id - snaphsot 1`] = `
403415
],
404416
"outputs": [
405417
Uint8Array [
406-
6,
418+
5,
419+
1,
407420
7,
408-
0,
409-
16,
410-
165,
411-
212,
412-
232,
413-
109,
414-
61,
415-
61,
416-
216,
417-
42,
418-
173,
419-
158,
420-
59,
421-
189,
422-
40,
423-
132,
424-
167,
425-
34,
426-
120,
427-
238,
428-
48,
429-
62,
430-
119,
431-
49,
432-
3,
433-
170,
434-
159,
435-
60,
436-
54,
437-
25,
421+
36,
422+
182,
423+
153,
424+
140,
425+
199,
426+
148,
427+
248,
428+
72,
429+
234,
430+
171,
431+
44,
432+
46,
433+
233,
434+
79,
435+
111,
436+
39,
437+
17,
438+
17,
439+
196,
440+
107,
438441
131,
439-
33,
440-
6,
441-
53,
442-
9,
443-
194,
442+
159,
443+
8,
444+
143,
445+
221,
446+
250,
447+
137,
448+
187,
449+
241,
450+
209,
451+
28,
452+
186,
453+
1,
454+
79,
455+
49,
456+
153,
457+
202,
444458
53,
459+
219,
460+
13,
461+
234,
462+
45,
463+
0,
464+
240,
465+
195,
466+
251,
467+
24,
468+
12,
469+
84,
470+
185,
471+
228,
445472
],
446473
Uint8Array [
447474
0,
448475
0,
449476
15,
450477
224,
451-
52,
452-
115,
453-
15,
454-
247,
455-
11,
478+
68,
479+
24,
480+
228,
481+
223,
482+
12,
456483
6,
457484
1,
458485
134,
@@ -477,9 +504,9 @@ exports[`delegate staking providing only pool_id - snaphsot 1`] = `
477504
33,
478505
],
479506
],
480-
"transactionsize": 216,
507+
"transactionsize": 231,
481508
},
482-
"HEXRepresentation_unsigned": "0100040000af3b5fad20f6f97eb210934e942176f7f7d0f70423590659ee0e0217053a7cab010000000806070010a5d4e86d3d3dd82aad9e3bbd2884a72278ee303e773103aa9f3c36198321063509c23500000fe034730ff70b060186ec450457d09ad9807393d89cec9c71d6206021",
509+
"HEXRepresentation_unsigned": "0100040000af3b5fad20f6f97eb210934e942176f7f7d0f70423590659ee0e0217053a7cab010000000805010724b6998cc794f848eaab2c2ee94f6f271111c46b839f088fddfa89bbf1d11cba014f3199ca35db0dea2d00f0c3fb180c54b9e400000fe04418e4df0c060186ec450457d09ad9807393d89cec9c71d6206021",
483510
"JSONRepresentation": {
484511
"inputs": [
485512
{
@@ -504,26 +531,23 @@ exports[`delegate staking providing only pool_id - snaphsot 1`] = `
504531
],
505532
"outputs": [
506533
{
507-
"amount": {
508-
"atoms": "1000000000000",
509-
"decimal": "10",
510-
},
511-
"delegation_id": "tdelg1d57nmkp24k0rh0fgsjnjy78wxql8wvgr420ncdsesvssvdgfcg6sx6262w",
512-
"type": "DelegateStaking",
534+
"destination": "tmt1qyrjfd5e3nref7zga24jcthffahjwyg3csxu3xgc",
535+
"pool_id": "tpool1dwpe7zy0mhagnwl36ywt5q20xxvu5dwmph4z6q8sc0a3srz5h8jqr0r2yg",
536+
"type": "CreateDelegationId",
513537
},
514538
{
515539
"destination": "tmt1qxrwc3gy2lgf4kvqwwfa388vn3cavgrqyyrgswe6",
516540
"type": "Transfer",
517541
"value": {
518542
"amount": {
519-
"atoms": "1702005604300000",
520-
"decimal": "17020.056043",
543+
"atoms": "1703005604300000",
544+
"decimal": "17030.056043",
521545
},
522546
"type": "Coin",
523547
},
524548
},
525549
],
526550
},
527-
"transaction_id": "7265e382aa4ab9f8db50cbf858a6f9b3e5fcca8d89ca2b104229cdeb0a38676e",
551+
"transaction_id": "9a2173442aec3f56e834675033e40e65e1b6ba9c09e6be8599d3db8bc8a69c5e",
528552
}
529553
`;

packages/sdk/tests/staking.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,52 @@ test('delegate staking providing only pool_id - snaphsot', async () => {
8989

9090
expect(result).toMatchSnapshot();
9191
})
92+
93+
test('delegate staking providing only wrong pool_id', async () => {
94+
fetchMock.mockIf('https://api-server-lovelace.mintlayer.org/api/v2/pool/wrong_pool_id/delegations', async () => {
95+
return {
96+
body: JSON.stringify({"error":"Invalid pool Id"}),
97+
status: 400,
98+
headers: {
99+
'Content-Type': 'application/json',
100+
}
101+
};
102+
});
103+
104+
const client = await Client.create({ network: 'testnet', autoRestore: false });
105+
106+
await client.connect();
107+
108+
await expect(client.delegationStake({
109+
pool_id: 'wrong_pool_id',
110+
amount: 10,
111+
})).rejects.toThrow('Failed to fetch delegation id')
112+
})
113+
114+
test('delegate staking providing only pool_id user not delegated to', async () => {
115+
fetchMock.mockIf('https://api-server-lovelace.mintlayer.org/api/v2/pool/tpool1tl784md209n53kuuwqxu68zav5lu5pdg8ca7kuhs6jg5lw24827q6qgxka/delegations', async () => {
116+
return {
117+
body: JSON.stringify([
118+
{
119+
"balance": {
120+
"atoms": "0",
121+
"decimal": "0"
122+
},
123+
"creation_block_height": 195930,
124+
"delegation_id": "tdelg1d57nmkp24k0rh0fgsjnjy78wxql8wvgr420ncdsesvssvdgfcg6sx6262w",
125+
"next_nonce": 0,
126+
"spend_destination": "tmt1q86huq7e03hmk6wj8sf7hezqgnshhtwy6s8gz3ut" // not belonging to the user
127+
}
128+
]),
129+
};
130+
});
131+
132+
const client = await Client.create({ network: 'testnet', autoRestore: false });
133+
134+
await client.connect();
135+
136+
await expect(client.delegationStake({
137+
pool_id: 'tpool1tl784md209n53kuuwqxu68zav5lu5pdg8ca7kuhs6jg5lw24827q6qgxka', // pool_id user not delegated to
138+
amount: 10,
139+
})).rejects.toThrow('No delegation id found for the given pool id')
140+
})

0 commit comments

Comments
 (0)