forked from stellar/js-stellar-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_airdrop.test.ts
More file actions
609 lines (525 loc) · 24.5 KB
/
request_airdrop.test.ts
File metadata and controls
609 lines (525 loc) · 24.5 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
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
import { describe, it, beforeEach, afterEach, expect, vi } from "vitest";
import { StellarSdk } from "../../../test-utils/stellar-sdk-import";
import { serverUrl } from "../../../constants";
const { Keypair, Networks, xdr } = StellarSdk;
const { Server } = StellarSdk.rpc;
// ripped out of ./get_transaction_test.js
describe("Server#requestAirdrop", () => {
let server: any;
let mockPost: any;
function accountLedgerEntryData() {
// Create a valid account ledger entry
const accountId = Keypair.fromPublicKey(
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI",
).xdrPublicKey();
const accountEntry = new xdr.AccountEntry({
accountId,
balance: xdr.Int64.fromString("1000000000"),
seqNum: xdr.Int64.fromString("1234"),
numSubEntries: 0,
inflationDest: null,
flags: 0,
homeDomain: "",
thresholds: Buffer.from("AQAAAA==", "base64"),
signers: [],
ext: new (xdr.AccountEntryExt as any)(0),
});
return xdr.LedgerEntryData.account(accountEntry);
}
// Create a mock transaction meta for the account we're going to request an airdrop for
function transactionMetaFor() {
// Create a proper TransactionMetaV2 that includes account creation
// We need to create a meta with a ledgerEntryCreated change for an account
const accountId = Keypair.fromPublicKey(
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI",
).xdrPublicKey();
const accountEntry = new xdr.AccountEntry({
accountId,
balance: xdr.Int64.fromString("1000000000"),
seqNum: xdr.Int64.fromString("1234"),
numSubEntries: 0,
inflationDest: null,
flags: 0,
homeDomain: "",
thresholds: Buffer.from("AQAAAA==", "base64"),
signers: [],
ext: new (xdr.AccountEntryExt as any)(0),
});
// Create a ledger entry change that represents account creation
const ledgerEntryData = xdr.LedgerEntryData.account(accountEntry);
const ledgerEntry = new xdr.LedgerEntry({
lastModifiedLedgerSeq: 1234,
data: ledgerEntryData,
ext: new (xdr.LedgerEntryExt as any)(0),
});
const ledgerEntryChange =
xdr.LedgerEntryChange.ledgerEntryCreated(ledgerEntry);
const operationMeta = new xdr.OperationMeta({
changes: [ledgerEntryChange],
});
const transactionMetaV2 = new xdr.TransactionMetaV2({
txChangesBefore: [],
operations: [operationMeta],
txChangesAfter: [],
});
return new (xdr.TransactionMeta as any)(2, transactionMetaV2);
}
beforeEach(() => {
server = new Server(serverUrl);
mockPost = vi.spyOn(server.httpClient, "post");
});
afterEach(() => {
vi.clearAllMocks();
});
it("returns true when the account is created", async () => {
const friendbotUrl = "https://friendbot.stellar.org";
const accountId =
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI";
const networkResult = {
friendbotUrl,
passphrase: Networks.FUTURENET,
protocolVersion: 20,
};
const networkResponse = { data: { result: networkResult } };
const resultMetaXdr = transactionMetaFor().toXDR("base64");
// Mock the friendbot call with result_meta_xdr
const friendbotResponse = {
status: 200,
data: {
result_meta_xdr: resultMetaXdr,
},
};
// Mock the sequence of calls
mockPost
.mockResolvedValueOnce(networkResponse) // getNetwork call
.mockResolvedValueOnce(friendbotResponse); // friendbot call
const result = await server.requestAirdrop(accountId);
expect(result).toBeInstanceOf(StellarSdk.Account);
expect(result.accountId()).toBe(accountId);
expect(result.sequence.toNumber()).toEqual(1234);
expect(mockPost).toHaveBeenCalledWith(serverUrl, {
jsonrpc: "2.0",
id: 1,
method: "getNetwork",
params: null,
});
expect(mockPost).toHaveBeenCalledWith(`${friendbotUrl}?addr=${accountId}`);
expect(mockPost).toHaveBeenCalledTimes(2);
});
it("returns false if the account already exists", async () => {
const friendbotUrl = "https://friendbot.stellar.org";
const accountId =
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI";
const networkResult = {
friendbotUrl,
passphrase: Networks.FUTURENET,
protocolVersion: 20,
};
const networkResponse = { data: { result: networkResult } };
// Mock the friendbot call to return 400 error for existing account
const friendbotError = {
response: {
status: 400,
detail: "createAccountAlreadyExist",
},
};
// Mock getLedgerEntry call for existing account
const accountEntry = accountLedgerEntryData();
const accountKeypair = Keypair.fromPublicKey(accountId);
const ledgerKey = xdr.LedgerKey.account(
new xdr.LedgerKeyAccount({
accountId: accountKeypair.xdrPublicKey(),
}),
);
const ledgerEntryResponse = {
data: {
result: {
latestLedger: 0,
entries: [
{
key: ledgerKey.toXDR("base64"),
xdr: accountEntry.toXDR("base64"),
},
],
},
},
};
// Mock the sequence of calls
mockPost
.mockResolvedValueOnce(networkResponse) // getNetwork call
.mockRejectedValueOnce(friendbotError) // friendbot call fails with 400
.mockResolvedValueOnce(ledgerEntryResponse); // getLedgerEntry call
const result = await server.requestAirdrop(accountId);
expect(result).toBeInstanceOf(StellarSdk.Account);
expect(result.accountId()).toBe(accountId);
expect(mockPost).toHaveBeenCalledWith(serverUrl, {
jsonrpc: "2.0",
id: 1,
method: "getNetwork",
params: null,
});
expect(mockPost).toHaveBeenCalledWith(`${friendbotUrl}?addr=${accountId}`);
expect(mockPost).toHaveBeenCalledWith(serverUrl, {
jsonrpc: "2.0",
id: 1,
method: "getLedgerEntries",
params: { keys: [ledgerKey.toXDR("base64")] },
});
expect(mockPost).toHaveBeenCalledTimes(3);
});
it("throws an error if friendbot is not available", async () => {
const friendbotUrl = "https://friendbot.stellar.org";
const accountId =
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI";
const networkResult = {
friendbotUrl,
passphrase: Networks.FUTURENET,
protocolVersion: 20,
};
const networkResponse = { data: { result: networkResult } };
// Mock the sequence of calls
mockPost
.mockResolvedValueOnce(networkResponse) // getNetwork call
.mockRejectedValueOnce(new Error("Network error")); // friendbot call fails
await expect(server.requestAirdrop(accountId)).rejects.toThrow(
"Network error",
);
expect(mockPost).toHaveBeenCalledWith(serverUrl, {
jsonrpc: "2.0",
id: 1,
method: "getNetwork",
params: null,
});
expect(mockPost).toHaveBeenCalledWith(`${friendbotUrl}?addr=${accountId}`);
expect(mockPost).toHaveBeenCalledTimes(2);
});
it("throws an error if no friendbot URL is configured", async () => {
const accountId =
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI";
const networkResult = {
friendbotUrl: undefined,
passphrase: Networks.FUTURENET,
protocolVersion: 20,
};
const networkResponse = { data: { result: networkResult } };
mockPost.mockResolvedValue(networkResponse);
await expect(server.requestAirdrop(accountId)).rejects.toThrow(
"No friendbot URL configured",
);
expect(mockPost).toHaveBeenCalledWith(serverUrl, {
jsonrpc: "2.0",
id: 1,
method: "getNetwork",
params: null,
});
expect(mockPost).toHaveBeenCalledTimes(1);
});
it("uses passed in friendbot url if provided", async () => {
const customFriendbotUrl = "https://custom-friendbot.example.com";
const accountId =
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI";
const resultMetaXdr = transactionMetaFor().toXDR("base64");
// Mock the friendbot call with result_meta_xdr
const friendbotResponse = {
status: 200,
data: {
result_meta_xdr: resultMetaXdr,
},
};
mockPost.mockResolvedValueOnce(friendbotResponse);
const result = await server.requestAirdrop(accountId, customFriendbotUrl);
expect(result).toBeInstanceOf(StellarSdk.Account);
expect(result.accountId()).toBe(accountId);
expect(result.sequence.toNumber()).toEqual(1234);
// Should not call getNetwork
expect(mockPost).not.toHaveBeenCalledWith(serverUrl, {
jsonrpc: "2.0",
id: 1,
method: "getNetwork",
params: null,
});
// Should call custom friendbot URL
expect(mockPost).toHaveBeenCalledWith(
`${customFriendbotUrl}?addr=${accountId}`,
);
expect(mockPost).toHaveBeenCalledTimes(1);
});
it("rejects invalid addresses", async () => {
const invalidAddress = "INVALID_ADDRESS";
await expect(server.requestAirdrop(invalidAddress)).rejects.toThrow();
});
it("falls back to RPC if no meta is present", async () => {
const friendbotUrl = "https://friendbot.stellar.org";
const accountId =
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI";
const hash =
"ae9f315c048d87a5f853bc15bf284a2c3c89eb0e1cb38c10409b77a877b830a8";
const networkResult = {
friendbotUrl,
passphrase: Networks.FUTURENET,
protocolVersion: 20,
};
const networkResponse = { data: { result: networkResult } };
// Mock the friendbot call WITHOUT result_meta_xdr
const friendbotResponse = {
status: 200,
data: {
hash,
// No result_meta_xdr field
},
};
// Mock the getTransaction call - use meta with account creation info
const resultMetaXdr = transactionMetaFor().toXDR("base64");
const successInfo = {
ledger: 12345,
createdAt: 123456789010,
applicationOrder: 2,
feeBump: false,
envelopeXdr:
"AAAAAgAAAAAT/LQZdYz0FcQ4Xwyg8IM17rkUx3pPCCWLu+SowQ/T+gBLB24poiQa9iwAngAAAAEAAAAAAAAAAAAAAABkwdeeAAAAAAAAAAEAAAABAAAAAC/9E8hDhnktyufVBS5tqA734Yz5XrLX2XNgBgH/YEkiAAAADQAAAAAAAAAAAAA1/gAAAAAv/RPIQ4Z5Lcrn1QUubagO9+GM+V6y19lzYAYB/2BJIgAAAAAAAAAAAAA1/gAAAAQAAAACU0lMVkVSAAAAAAAAAAAAAFDutWuu6S6UPJBrotNSgfmXa27M++63OT7TYn1qjgy+AAAAAVNHWAAAAAAAUO61a67pLpQ8kGui01KB+Zdrbsz77rc5PtNifWqODL4AAAACUEFMTEFESVVNAAAAAAAAAFDutWuu6S6UPJBrotNSgfmXa27M++63OT7TYn1qjgy+AAAAAlNJTFZFUgAAAAAAAAAAAABQ7rVrrukulDyQa6LTUoH5l2tuzPvutzk+02J9ao4MvgAAA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pLpQ8kGui01KB+Zdrbsz77rc5PtNifWqODL4AABv52PPa5wAAAAAv/RPIQ4Z5Lcrn1QUubagO9+GM+V6y19lzYAYB/2BJIgAAAAAAAAAAAAA9pAAAAAA=",
resultMetaXdr,
events: {
contractEventsXdr: [],
transactionEventsXdr: [],
},
};
const getTransactionResponse = {
data: {
id: 1,
result: {
status: "SUCCESS",
txHash: hash,
latestLedger: 100,
latestLedgerCloseTime: 12345,
oldestLedger: 50,
oldestLedgerCloseTime: 500,
...successInfo,
},
},
};
// Mock the sequence of calls
mockPost
.mockResolvedValueOnce(networkResponse) // getNetwork call
.mockResolvedValueOnce(friendbotResponse) // friendbot call without meta
.mockResolvedValueOnce(getTransactionResponse); // getTransaction call
const result = await server.requestAirdrop(accountId);
expect(result).toBeInstanceOf(StellarSdk.Account);
expect(result.accountId()).toBe(accountId);
expect(result.sequence.toNumber()).toEqual(1234);
expect(mockPost).toHaveBeenCalledWith(serverUrl, {
jsonrpc: "2.0",
id: 1,
method: "getNetwork",
params: null,
});
expect(mockPost).toHaveBeenCalledWith(`${friendbotUrl}?addr=${accountId}`);
expect(mockPost).toHaveBeenCalledWith(serverUrl, {
jsonrpc: "2.0",
id: 1,
method: "getTransaction",
params: { hash },
});
expect(mockPost).toHaveBeenCalledTimes(3);
});
});
describe("Server#fundAddress", () => {
let server: any;
let mockPost: any;
// Valid XDR for transaction meta (from get_transaction.test.ts)
const metaV4Xdr =
"AAAAAgAAAAIAAAADAtL5awAAAAAAAAAAS0CFMhOtWUKJWerx66zxkxORaiH6/3RUq7L8zspD5RoAAAAAAcm9QAKVkpMAAHpMAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAwAAAAAC0vi5AAAAAGTB02oAAAAAAAAAAQLS+WsAAAAAAAAAAEtAhTITrVlCiVnq8eus8ZMTkWoh+v90VKuy/M7KQ+UaAAAAAAHJvUAClZKTAAB6TQAAA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AAAAAAEC0vlrAAAAAAAAAABuHMLu9cxzV8d4xws0RjiLYXJ38EYHzdn/wKBxSyzHJQAAAB9+/s10Al3UUwAAEmQAAAKSAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAABAAAAWwVyiIoAAAAYWVsojgAAAAIAAAAAAAAAAAAAAAAAAAADAAAAAALS9GsAAAAAZMG6qwAAAAAAAAAA";
const successTxResponse = (hash: string) => ({
data: {
id: 1,
result: {
status: "SUCCESS",
txHash: hash,
latestLedger: 100,
latestLedgerCloseTime: 12345,
oldestLedger: 50,
oldestLedgerCloseTime: 500,
ledger: 12345,
createdAt: 123456789010,
applicationOrder: 2,
feeBump: false,
envelopeXdr:
"AAAAAgAAAAAT/LQZdYz0FcQ4Xwyg8IM17rkUx3pPCCWLu+SowQ/T+gBLB24poiQa9iwAngAAAAEAAAAAAAAAAAAAAABkwdeeAAAAAAAAAAEAAAABAAAAAC/9E8hDhnktyufVBS5tqA734Yz5XrLX2XNgBgH/YEkiAAAADQAAAAAAAAAAAAA1/gAAAAAv/RPIQ4Z5Lcrn1QUubagO9+GM+V6y19lzYAYB/2BJIgAAAAAAAAAAAAA1/gAAAAQAAAACU0lMVkVSAAAAAAAAAAAAAFDutWuu6S6UPJBrotNSgfmXa27M++63OT7TYn1qjgy+AAAAAVNHWAAAAAAAUO61a67pLpQ8kGui01KB+Zdrbsz77rc5PtNifWqODL4AAAACUEFMTEFESVVNAAAAAAAAAFDutWuu6S6UPJBrotNSgfmXa27M++63OT7TYn1qjgy+AAAAAlNJTFZFUgAAAAAAAAAAAABQ7rVrrukulDyQa6LTUoH5l2tuzPvutzk+02J9ao4MvgAAAAAAAAACwQ/T+gAAAEA+ztVEKWlqHXNnqy6FXJeHr7TltHzZE6YZm5yZfzPIfLaqpp+5cyKotVkj3d89uZCQNsKsZI48uoyERLne+VwL/2BJIgAAAEA7323gPSaezVSa7Vi0J4PqsnklDH1oHLqNBLwi5EWo5W7ohLGObRVQZ0K0+ufnm4hcm9J4Cuj64gEtpjq5j5cM",
resultXdr:
"AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAANAAAAAAAAAAUAAAACZ4W6fmN63uhVqYRcHET+D2NEtJvhCIYflFh9GqtY+AwAAAACU0lMVkVSAAAAAAAAAAAAAFDutWuu6S6UPJBrotNSgfmXa27M++63OT7TYn1qjgy+AAAYW0toL2gAAAAAAAAAAAAANf4AAAACcgyAkXD5kObNTeRYciLh7R6ES/zzKp0n+cIK3Y6TjBkAAAABU0dYAAAAAABQ7rVrrukulDyQa6LTUoH5l2tuzPvutzk+02J9ao4MvgAAGlGnIJrXAAAAAlNJTFZFUgAAAAAAAAAAAABQ7rVrrukulDyQa6LTUoH5l2tuzPvutzk+02J9ao4MvgAAGFtLaC9oAAAAApmc7UgUBInrDvij8HMSridx2n1w3I8TVEn4sLr1LSpmAAAAAlBBTExBRElVTQAAAAAAAABQ7rVrrukulDyQa6LTUoH5l2tuzPvutzk+02J9ao4MvgAAIUz88EqYAAAAAVNHWAAAAAAAUO61a67pLpQ8kGui01KB+Zdrbsz77rc5PtNifWqODL4AABpRpyCa1wAAAAKYUsaaCZ233xB1p+lG7YksShJWfrjsmItbokiR3ifa0gAAAAJTSUxWRVIAAAAAAAAAAAAAUO61a67pLpQ8kGui01KB+Zdrbsz77rc5PtNifWqODL4AABv52PPa5wAAAAJQQUxMQURJVU0AAAAAAAAAUO61a67pLpQ8kGui01KB+Zdrbsz77rc5PtNifWqODL4AACFM/PBKmAAAAAJnhbp+Y3re6FWphFwcRP4PY0S0m+EIhh+UWH0aq1j4DAAAAAAAAAAAAAA9pAAAAAJTSUxWRVIAAAAAAAAAAAAAUO61a67pLpQ8kGui01KB+Zdrbsz77rc5PtNifWqODL4AABv52PPa5wAAAAAv/RPIQ4Z5Lcrn1QUubagO9+GM+V6y19lzYAYB/2BJIgAAAAAAAAAAAAA9pAAAAAA=",
resultMetaXdr: metaV4Xdr,
events: {
contractEventsXdr: [],
transactionEventsXdr: [],
},
},
},
});
beforeEach(() => {
server = new Server(serverUrl);
mockPost = vi.spyOn(server.httpClient, "post");
});
afterEach(() => {
vi.clearAllMocks();
});
it("funds an account address (G...) and returns transaction response", async () => {
const friendbotUrl = "https://friendbot.stellar.org";
const accountId =
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI";
const hash =
"ae9f315c048d87a5f853bc15bf284a2c3c89eb0e1cb38c10409b77a877b830a8";
const networkResult = {
friendbotUrl,
passphrase: Networks.FUTURENET,
protocolVersion: 20,
};
const networkResponse = { data: { result: networkResult } };
const friendbotResponse = {
status: 200,
data: { hash },
};
mockPost
.mockResolvedValueOnce(networkResponse) // getNetwork call
.mockResolvedValueOnce(friendbotResponse) // friendbot call
.mockResolvedValueOnce(successTxResponse(hash)); // getTransaction call
const result = await server.fundAddress(accountId);
expect(result.status).toBe("SUCCESS");
expect(result.txHash).toBe(hash);
expect(mockPost).toHaveBeenCalledWith(`${friendbotUrl}?addr=${accountId}`);
expect(mockPost).toHaveBeenCalledTimes(3);
});
it("funds a contract address (C...) and returns transaction response", async () => {
const friendbotUrl = "https://friendbot.stellar.org";
const contractId = StellarSdk.StrKey.encodeContract(
Buffer.from("0".repeat(64), "hex"),
);
const hash =
"ae9f315c048d87a5f853bc15bf284a2c3c89eb0e1cb38c10409b77a877b830a8";
const networkResult = {
friendbotUrl,
passphrase: Networks.FUTURENET,
protocolVersion: 20,
};
const networkResponse = { data: { result: networkResult } };
const friendbotResponse = {
status: 200,
data: { hash },
};
mockPost
.mockResolvedValueOnce(networkResponse) // getNetwork call
.mockResolvedValueOnce(friendbotResponse) // friendbot call
.mockResolvedValueOnce(successTxResponse(hash)); // getTransaction call
const result = await server.fundAddress(contractId);
expect(result.status).toBe("SUCCESS");
expect(result.txHash).toBe(hash);
expect(mockPost).toHaveBeenCalledWith(`${friendbotUrl}?addr=${contractId}`);
expect(mockPost).toHaveBeenCalledTimes(3);
});
it("uses custom friendbot URL when provided", async () => {
const customFriendbotUrl = "https://custom-friendbot.example.com";
const accountId =
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI";
const hash = "somehash123";
const friendbotResponse = {
status: 200,
data: { hash },
};
mockPost
.mockResolvedValueOnce(friendbotResponse) // friendbot call
.mockResolvedValueOnce(successTxResponse(hash)); // getTransaction call
const result = await server.fundAddress(accountId, customFriendbotUrl);
expect(result.status).toBe("SUCCESS");
// Should not call getNetwork when custom URL provided
expect(mockPost).not.toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ method: "getNetwork" }),
);
expect(mockPost).toHaveBeenCalledWith(
`${customFriendbotUrl}?addr=${accountId}`,
);
expect(mockPost).toHaveBeenCalledTimes(2);
});
it("throws error when no friendbot URL is configured", async () => {
const accountId =
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI";
const networkResult = {
friendbotUrl: undefined,
passphrase: Networks.FUTURENET,
protocolVersion: 20,
};
const networkResponse = { data: { result: networkResult } };
mockPost.mockResolvedValueOnce(networkResponse);
await expect(server.fundAddress(accountId)).rejects.toThrow(
"No friendbot URL configured for current network",
);
});
it("throws error when funding fails", async () => {
const friendbotUrl = "https://friendbot.stellar.org";
const accountId =
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI";
const hash = "somehash123";
const networkResult = {
friendbotUrl,
passphrase: Networks.FUTURENET,
protocolVersion: 20,
};
const networkResponse = { data: { result: networkResult } };
const friendbotResponse = {
status: 200,
data: { hash },
};
const failedTxResponse = {
data: {
id: 1,
result: {
status: "FAILED",
txHash: hash,
latestLedger: 100,
latestLedgerCloseTime: 12345,
oldestLedger: 50,
oldestLedgerCloseTime: 500,
ledger: 12345,
createdAt: 123456789010,
applicationOrder: 2,
feeBump: false,
envelopeXdr:
"AAAAAgAAAAAT/LQZdYz0FcQ4Xwyg8IM17rkUx3pPCCWLu+SowQ/T+gBLB24poiQa9i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r1LSpmAAAAAlBBTExBRElVTQAAAAAAAABQ7rVrrukulDyQa6LTUoH5l2tuzPvutzk+02J9ao4MvgAAIUz88EqYAAAAAVNHWAAAAAAAUO61a67pLpQ8kGui01KB+Zdrbsz77rc5PtNifWqODL4AABpRpyCa1wAAAAKYUsaaCZ233xB1p+lG7YksShJWfrjsmItbokiR3ifa0gAAAAJTSUxWRVIAAAAAAAAAAAAAUO61a67pLpQ8kGui01KB+Zdrbsz77rc5PtNifWqODL4AABv52PPa5wAAAAJQQUxMQURJVU0AAAAAAAAAUO61a67pLpQ8kGui01KB+Zdrbsz77rc5PtNifWqODL4AACFM/PBKmAAAAAJnhbp+Y3re6FWphFwcRP4PY0S0m+EIhh+UWH0aq1j4DAAAAAAAAAAAAAA9pAAAAAJTSUxWRVIAAAAAAAAAAAAAUO61a67pLpQ8kGui01KB+Zdrbsz77rc5PtNifWqODL4AABv52PPa5wAAAAAv/RPIQ4Z5Lcrn1QUubagO9+GM+V6y19lzYAYB/2BJIgAAAAAAAAAAAAA9pAAAAAA=",
resultMetaXdr: metaV4Xdr,
},
},
};
mockPost
.mockResolvedValueOnce(networkResponse)
.mockResolvedValueOnce(friendbotResponse)
.mockResolvedValueOnce(failedTxResponse);
await expect(server.fundAddress(accountId)).rejects.toThrow(
`Funding address ${accountId} failed: transaction status FAILED`,
);
});
it("throws error when HTTP request fails", async () => {
const friendbotUrl = "https://friendbot.stellar.org";
const accountId =
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI";
const networkResult = {
friendbotUrl,
passphrase: Networks.FUTURENET,
protocolVersion: 20,
};
const networkResponse = { data: { result: networkResult } };
mockPost
.mockResolvedValueOnce(networkResponse)
.mockRejectedValueOnce(new Error("Network error"));
await expect(server.fundAddress(accountId)).rejects.toThrow(
"Network error",
);
});
it("rejects invalid addresses", async () => {
const invalidAddress = "INVALID_ADDRESS";
await expect(server.fundAddress(invalidAddress)).rejects.toThrow(
"Invalid address: INVALID_ADDRESS. Expected a Stellar account (G...) or contract (C...) address.",
);
});
});