Skip to content

Commit 2fa4ad2

Browse files
committed
test: add tests and catch case when utxo is not enough
1 parent 0929d70 commit 2fa4ad2

File tree

2 files changed

+236
-4
lines changed

2 files changed

+236
-4
lines changed

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

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,168 @@ exports[`buildTransaction for transfer - snapshot 1`] = `
164164
"transaction_id": "05d9ec811338b736816f64965f17844cdbf45711f0892534f3f7408344052bcc",
165165
}
166166
`;
167+
168+
exports[`token transfer builds correct transaction 1`] = `
169+
{
170+
"BINRepresentation": {
171+
"inputs": [
172+
Uint8Array [
173+
0,
174+
0,
175+
175,
176+
59,
177+
95,
178+
173,
179+
32,
180+
246,
181+
249,
182+
126,
183+
178,
184+
16,
185+
147,
186+
78,
187+
148,
188+
33,
189+
118,
190+
247,
191+
247,
192+
208,
193+
247,
194+
4,
195+
35,
196+
89,
197+
6,
198+
89,
199+
238,
200+
14,
201+
2,
202+
23,
203+
5,
204+
58,
205+
124,
206+
171,
207+
1,
208+
0,
209+
0,
210+
0,
211+
],
212+
],
213+
"outputs": [
214+
Uint8Array [
215+
0,
216+
0,
217+
7,
218+
0,
219+
16,
220+
165,
221+
212,
222+
232,
223+
1,
224+
118,
225+
148,
226+
121,
227+
186,
228+
231,
229+
213,
230+
53,
231+
208,
232+
151,
233+
222,
234+
253,
235+
255,
236+
15,
237+
78,
238+
113,
239+
1,
240+
102,
241+
157,
242+
74,
243+
25,
244+
],
245+
Uint8Array [
246+
0,
247+
0,
248+
15,
249+
224,
250+
100,
251+
133,
252+
126,
253+
200,
254+
11,
255+
6,
256+
1,
257+
134,
258+
236,
259+
69,
260+
4,
261+
87,
262+
208,
263+
154,
264+
217,
265+
128,
266+
115,
267+
147,
268+
216,
269+
156,
270+
236,
271+
156,
272+
113,
273+
214,
274+
32,
275+
96,
276+
33,
277+
],
278+
],
279+
"transactionsize": 206,
280+
},
281+
"HEXRepresentation_unsigned": "0100040000af3b5fad20f6f97eb210934e942176f7f7d0f70423590659ee0e0217053a7cab01000000080000070010a5d4e801769479bae7d535d097defdff0f4e7101669d4a1900000fe064857ec80b060186ec450457d09ad9807393d89cec9c71d6206021",
282+
"JSONRepresentation": {
283+
"inputs": [
284+
{
285+
"input": {
286+
"index": 1,
287+
"input_type": "UTXO",
288+
"source_id": "af3b5fad20f6f97eb210934e942176f7f7d0f70423590659ee0e0217053a7cab",
289+
"source_type": "Transaction",
290+
},
291+
"utxo": {
292+
"destination": "tmt1qxrwc3gy2lgf4kvqwwfa388vn3cavgrqyyrgswe6",
293+
"type": "Transfer",
294+
"value": {
295+
"amount": {
296+
"atoms": "1703205604300000",
297+
"decimal": "17032.056043",
298+
},
299+
"type": "Coin",
300+
},
301+
},
302+
},
303+
],
304+
"outputs": [
305+
{
306+
"destination": "tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc",
307+
"type": "Transfer",
308+
"value": {
309+
"amount": {
310+
"atoms": "1000000000000",
311+
"decimal": "10",
312+
},
313+
"type": "Coin",
314+
},
315+
},
316+
{
317+
"destination": "tmt1qxrwc3gy2lgf4kvqwwfa388vn3cavgrqyyrgswe6",
318+
"type": "Transfer",
319+
"value": {
320+
"amount": {
321+
"atoms": "1701805604300000",
322+
"decimal": "17018.056043",
323+
},
324+
"type": "Coin",
325+
},
326+
},
327+
],
328+
},
329+
"transaction_id": "05d9ec811338b736816f64965f17844cdbf45711f0892534f3f7408344052bcc",
330+
}
331+
`;

packages/sdk/tests/transfer.test.ts

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Client } from '../src/mintlayer-connect-sdk';
22
import fetchMock from 'jest-fetch-mock';
33

4-
import { addresses, utxos } from './__mocks__/accounts/account_01'
4+
import { addresses, utxos } from './__mocks__/accounts/account_01';
55

66
beforeEach(() => {
77
fetchMock.resetMocks();
@@ -24,17 +24,24 @@ beforeEach(() => {
2424

2525
// API /account
2626
fetchMock.mockIf('https://api.mintini.app/account', async () => {
27+
return {
28+
body: JSON.stringify({ utxos }),
29+
};
30+
});
31+
32+
fetchMock.mockIf(/^https:\/\/api-server-lovelace\.mintlayer\.org\/api\/v2\/token\//, async () => {
2733
return {
2834
body: JSON.stringify({
29-
utxos: utxos,
35+
token_id: 'tmltk1abc...',
36+
number_of_decimals: 2,
37+
authority: 'taddr1auth...',
3038
}),
3139
};
3240
});
3341
});
3442

3543
test('buildTransaction for transfer - snapshot', async () => {
3644
const client = await Client.create({ network: 'testnet', autoRestore: false });
37-
3845
const spy = jest.spyOn(Client.prototype as any, 'buildTransaction');
3946

4047
await client.connect();
@@ -45,6 +52,66 @@ test('buildTransaction for transfer - snapshot', async () => {
4552
});
4653

4754
const result = await spy.mock.results[0]?.value;
48-
4955
expect(result).toMatchSnapshot();
5056
});
57+
58+
test('transfer returns signed tx', async () => {
59+
const client = await Client.create({ network: 'testnet', autoRestore: false });
60+
await client.connect();
61+
62+
const result = await client.transfer({
63+
to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc',
64+
amount: 10,
65+
});
66+
67+
expect(result).toBe('signed-transaction');
68+
});
69+
70+
test('buildTransaction called with correct params', async () => {
71+
const spy = jest.spyOn(Client.prototype as any, 'buildTransaction');
72+
const client = await Client.create({ network: 'testnet', autoRestore: false });
73+
74+
await client.connect();
75+
76+
await client.transfer({
77+
to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc',
78+
amount: 5,
79+
});
80+
81+
expect(spy).toHaveBeenCalledWith({
82+
type: 'Transfer',
83+
params: expect.objectContaining({
84+
to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc',
85+
amount: 5,
86+
}),
87+
});
88+
});
89+
90+
test('token transfer builds correct transaction', async () => {
91+
const client = await Client.create({ network: 'testnet', autoRestore: false });
92+
await client.connect();
93+
94+
const result = await client.transfer({
95+
to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc',
96+
amount: 50,
97+
token_id: 'tmltk1wvfgu57geuqrjzxmnk48jmnp5salnd0ggmcymxl6u3h6wk7smnjqjrr0u6', // triggers token fetch
98+
});
99+
100+
expect(result).toBe('signed-transaction');
101+
});
102+
103+
test('fails transfer if not enough utxo', async () => {
104+
fetchMock.mockIf('https://api.mintini.app/account', async () => {
105+
return {
106+
body: JSON.stringify({ utxos: [] }), // no utxos
107+
};
108+
});
109+
110+
const client = await Client.create({ network: 'testnet', autoRestore: false });
111+
await client.connect();
112+
113+
await expect(await client.transfer({
114+
to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc',
115+
amount: 999999999,
116+
})).rejects.toThrow(/Not enough token UTXOs|Failed to fetch utxos|API error/);
117+
});

0 commit comments

Comments
 (0)