-
Notifications
You must be signed in to change notification settings - Fork 35
/
soroban_test_atomic_swap.dart
700 lines (580 loc) · 27.9 KB
/
soroban_test_atomic_swap.dart
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:stellar_flutter_sdk/stellar_flutter_sdk.dart';
void main() {
String testOn = 'testnet'; // futurenet
SorobanServer sorobanServer = testOn == 'testnet'
? SorobanServer("https://soroban-testnet.stellar.org")
: SorobanServer("https://rpc-futurenet.stellar.org");
StellarSDK sdk =
testOn == 'testnet' ? StellarSDK.TESTNET : StellarSDK.FUTURENET;
Network network = testOn == 'testnet' ? Network.TESTNET : Network.FUTURENET;
KeyPair adminKeypair = KeyPair.random();
String adminId = adminKeypair.accountId;
KeyPair aliceKeypair = KeyPair.random();
String aliceId = aliceKeypair.accountId;
KeyPair bobKeypair = KeyPair.random();
String bobId = bobKeypair.accountId;
String tokenContractPath =
"/Users/chris/Soneso/github/stellar_flutter_sdk/test/wasm/soroban_token_contract.wasm";
String swapContractPath =
"/Users/chris/Soneso/github/stellar_flutter_sdk/test/wasm/soroban_atomic_swap_contract.wasm";
String? tokenAContractWasmId;
String? tokenAContractId;
String? tokenBContractWasmId;
String? tokenBContractId;
String? swapContractWasmId;
String? swapContractId;
setUp(() async {
sorobanServer.enableLogging = true;
try {
await sdk.accounts.account(adminId);
} catch (e) {
if (testOn == 'testnet') {
await FriendBot.fundTestAccount(adminId);
} else if (testOn == 'futurenet') {
await FuturenetFriendBot.fundTestAccount(adminId);
}
print("admin " + adminId + " : " + adminKeypair.secretSeed);
}
try {
await sdk.accounts.account(aliceId);
} catch (e) {
if (testOn == 'testnet') {
await FriendBot.fundTestAccount(aliceId);
} else if (testOn == 'futurenet') {
await FuturenetFriendBot.fundTestAccount(aliceId);
}
print("alice " + aliceId + " : " + aliceKeypair.secretSeed);
}
try {
await sdk.accounts.account(bobId);
} catch (e) {
if (testOn == 'testnet') {
await FriendBot.fundTestAccount(bobId);
} else if (testOn == 'futurenet') {
await FuturenetFriendBot.fundTestAccount(bobId);
}
print("bob " + bobId + " : " + bobKeypair.secretSeed);
}
});
// poll until success or error
Future<GetTransactionResponse> pollStatus(String transactionId) async {
var status = GetTransactionResponse.STATUS_NOT_FOUND;
GetTransactionResponse? transactionResponse;
while (status == GetTransactionResponse.STATUS_NOT_FOUND) {
await Future.delayed(const Duration(seconds: 3), () {});
transactionResponse = await sorobanServer.getTransaction(transactionId);
assert(transactionResponse.error == null);
status = transactionResponse.status!;
if (status == GetTransactionResponse.STATUS_FAILED) {
assert(transactionResponse.resultXdr != null);
assert(false);
} else if (status == GetTransactionResponse.STATUS_SUCCESS) {
assert(transactionResponse.resultXdr != null);
}
}
return transactionResponse!;
}
Future<String> installContract(String contractCodePath) async {
await Future.delayed(Duration(seconds: 5));
// load account
Account? account = await sorobanServer.getAccount(adminId);
assert(account != null);
Account submitter = account!;
// load contract wasm file
Uint8List contractCode = await Util.readFile(contractCodePath);
// upload contract
InvokeHostFunctionOperation operation =
InvokeHostFuncOpBuilder(UploadContractWasmHostFunction(contractCode))
.build();
Transaction transaction =
new TransactionBuilder(submitter).addOperation(operation).build();
// simulate first to obtain the transaction data + resource fee
var request = SimulateTransactionRequest(transaction);
SimulateTransactionResponse simulateResponse =
await sorobanServer.simulateTransaction(request);
assert(!simulateResponse.isErrorResponse);
assert(simulateResponse.transactionData != null);
// set transaction data, add resource fee and sign transaction
transaction.sorobanTransactionData = simulateResponse.transactionData;
transaction.addResourceFee(simulateResponse.minResourceFee!);
transaction.sign(adminKeypair, network);
// check transaction xdr encoding and decoding back and forth
String transactionEnvelopeXdr = transaction.toEnvelopeXdrBase64();
assert(transactionEnvelopeXdr ==
AbstractTransaction.fromEnvelopeXdrString(transactionEnvelopeXdr)
.toEnvelopeXdrBase64());
// send transaction to soroban rpc server
SendTransactionResponse sendResponse =
await sorobanServer.sendTransaction(transaction);
assert(!sendResponse.isErrorResponse);
assert(sendResponse.status != SendTransactionResponse.STATUS_ERROR);
GetTransactionResponse rpcTransactionResponse =
await pollStatus(sendResponse.hash!);
String? wasmId = rpcTransactionResponse.getWasmId();
assert(wasmId != null);
return wasmId!;
}
Future<String> createContract(String wasmId) async {
await Future.delayed(Duration(seconds: 5));
// reload account for current sequence nr
Account? account = await sorobanServer.getAccount(adminId);
assert(account != null);
Account submitter = account!;
// build the operation for creating the contract
CreateContractHostFunction function =
CreateContractHostFunction(Address.forAccountId(adminId), wasmId);
InvokeHostFunctionOperation operation =
InvokeHostFuncOpBuilder(function).build();
// build the transaction for creating the contract
Transaction transaction =
new TransactionBuilder(submitter).addOperation(operation).build();
// simulate first to obtain the transaction data + resource fee
var request = SimulateTransactionRequest(transaction);
SimulateTransactionResponse simulateResponse =
await sorobanServer.simulateTransaction(request);
assert(!simulateResponse.isErrorResponse);
assert(simulateResponse.resultError == null);
// set transaction data, add resource fee and sign transaction
transaction.sorobanTransactionData = simulateResponse.transactionData;
transaction.addResourceFee(simulateResponse.minResourceFee!);
transaction.setSorobanAuth(simulateResponse.sorobanAuth);
transaction.sign(adminKeypair, network);
// send transaction to soroban rpc server
SendTransactionResponse sendResponse =
await sorobanServer.sendTransaction(transaction);
assert(!sendResponse.isErrorResponse);
assert(sendResponse.status != SendTransactionResponse.STATUS_ERROR);
GetTransactionResponse statusResponse =
await pollStatus(sendResponse.hash!);
String? contractId = statusResponse.getCreatedContractId();
assert(contractId != null);
return contractId!;
}
Future<void> createToken(
String contractId, String name, String symbol) async {
// see https://soroban.stellar.org/docs/reference/interfaces/token-interface
await Future.delayed(Duration(seconds: 5));
// reload account for sequence number
Account? account = await sorobanServer.getAccount(adminId);
assert(account != null);
Account invoker = account!;
Address adminAddress = Address.forAccountId(adminId);
String functionName = "initialize";
XdrSCVal tokenName = XdrSCVal.forString(name);
XdrSCVal tokenSymbol = XdrSCVal.forString(symbol);
List<XdrSCVal> args = [
adminAddress.toXdrSCVal(),
XdrSCVal.forU32(8),
tokenName,
tokenSymbol
];
InvokeContractHostFunction hostFunction =
InvokeContractHostFunction(contractId, functionName, arguments: args);
InvokeHostFunctionOperation operation =
InvokeHostFuncOpBuilder(hostFunction).build();
Transaction transaction =
new TransactionBuilder(invoker).addOperation(operation).build();
// simulate first to obtain the transaction data + resource fee
var request = SimulateTransactionRequest(transaction);
SimulateTransactionResponse simulateResponse =
await sorobanServer.simulateTransaction(request);
assert(simulateResponse.error == null);
assert(simulateResponse.resultError == null);
assert(simulateResponse.footprint != null);
// set transaction data, add resource fee and sign transaction
transaction.sorobanTransactionData = simulateResponse.transactionData;
transaction.addResourceFee(simulateResponse.minResourceFee!);
transaction.setSorobanAuth(simulateResponse.sorobanAuth);
transaction.sign(adminKeypair, network);
// check transaction xdr encoding and decoding back and forth
String transactionEnvelopeXdr = transaction.toEnvelopeXdrBase64();
assert(transactionEnvelopeXdr ==
AbstractTransaction.fromEnvelopeXdrString(transactionEnvelopeXdr)
.toEnvelopeXdrBase64());
// send the transaction
SendTransactionResponse sendResponse =
await sorobanServer.sendTransaction(transaction);
assert(sendResponse.error == null);
assert(sendResponse.status != SendTransactionResponse.STATUS_ERROR);
GetTransactionResponse statusResponse =
await pollStatus(sendResponse.hash!);
String status = statusResponse.status!;
assert(status == GetTransactionResponse.STATUS_SUCCESS);
}
Future<void> mint(String contractId, String toAccountId, int amount) async {
// see https://soroban.stellar.org/docs/reference/interfaces/token-interface
await Future.delayed(Duration(seconds: 5));
// reload account for sequence number
Account? account = await sorobanServer.getAccount(adminId);
assert(account != null);
Account invoker = account!;
Address toAddress = Address.forAccountId(toAccountId);
XdrSCVal amountVal = XdrSCVal.forI128(XdrInt128Parts.forHiLo(0, amount));
String functionName = "mint";
List<XdrSCVal> args = [toAddress.toXdrSCVal(), amountVal];
InvokeContractHostFunction hostFunction =
InvokeContractHostFunction(contractId, functionName, arguments: args);
InvokeHostFunctionOperation operation =
(InvokeHostFuncOpBuilder(hostFunction)).build();
Transaction transaction =
new TransactionBuilder(invoker).addOperation(operation).build();
// simulate first to obtain the transaction data + resource fee
var request = SimulateTransactionRequest(transaction);
SimulateTransactionResponse simulateResponse =
await sorobanServer.simulateTransaction(request);
assert(simulateResponse.error == null);
assert(simulateResponse.resultError == null);
assert(simulateResponse.transactionData != null);
// set transaction data, add resource fee and sign transaction
transaction.sorobanTransactionData = simulateResponse.transactionData;
transaction.addResourceFee(simulateResponse.minResourceFee!);
transaction.setSorobanAuth(simulateResponse.sorobanAuth);
transaction.sign(adminKeypair, network);
// check transaction xdr encoding and decoding back and forth
String transactionEnvelopeXdr = transaction.toEnvelopeXdrBase64();
assert(transactionEnvelopeXdr ==
AbstractTransaction.fromEnvelopeXdrString(transactionEnvelopeXdr)
.toEnvelopeXdrBase64());
// send the transaction
SendTransactionResponse sendResponse =
await sorobanServer.sendTransaction(transaction);
assert(sendResponse.error == null);
assert(sendResponse.status != SendTransactionResponse.STATUS_ERROR);
GetTransactionResponse statusResponse =
await pollStatus(sendResponse.hash!);
String status = statusResponse.status!;
assert(status == GetTransactionResponse.STATUS_SUCCESS);
}
Future<int> balance(String contractId, String accountId) async {
await Future.delayed(Duration(seconds: 5));
// reload account for sequence number
Account? account = await sorobanServer.getAccount(adminId);
assert(account != null);
Account invoker = account!;
Address address = Address.forAccountId(accountId);
String functionName = "balance";
List<XdrSCVal> args = [address.toXdrSCVal()];
InvokeContractHostFunction hostFunction =
InvokeContractHostFunction(contractId, functionName, arguments: args);
InvokeHostFunctionOperation operation =
InvokeHostFuncOpBuilder(hostFunction).build();
Transaction transaction =
new TransactionBuilder(invoker).addOperation(operation).build();
// simulate first to obtain the transaction data + resource fee
var request = SimulateTransactionRequest(transaction);
SimulateTransactionResponse simulateResponse =
await sorobanServer.simulateTransaction(request);
assert(simulateResponse.error == null);
assert(simulateResponse.resultError == null);
assert(simulateResponse.transactionData != null);
// set transaction data, add resource fee and sign transaction
transaction.sorobanTransactionData = simulateResponse.transactionData;
transaction.addResourceFee(simulateResponse.minResourceFee!);
transaction.sign(adminKeypair, network);
// check transaction xdr encoding and decoding back and forth
String transactionEnvelopeXdr = transaction.toEnvelopeXdrBase64();
assert(transactionEnvelopeXdr ==
AbstractTransaction.fromEnvelopeXdrString(transactionEnvelopeXdr)
.toEnvelopeXdrBase64());
// send the transaction
SendTransactionResponse sendResponse =
await sorobanServer.sendTransaction(transaction);
assert(sendResponse.error == null);
GetTransactionResponse statusResponse =
await pollStatus(sendResponse.hash!);
String status = statusResponse.status!;
assert(status == GetTransactionResponse.STATUS_SUCCESS);
assert(statusResponse.getResultValue()?.i128 != null);
XdrInt128Parts parts = statusResponse.getResultValue()!.i128!;
return parts.lo.uint64;
}
Future restoreContractFootprint(String contractCodePath) async {
await Future.delayed(Duration(seconds: 5));
// load account
Account? account = await sorobanServer.getAccount(adminId);
assert(account != null);
Account accountA = account!;
// load contract wasm file
Uint8List contractCode = await Util.readFile(contractCodePath);
UploadContractWasmHostFunction uploadFunction =
UploadContractWasmHostFunction(contractCode);
InvokeHostFunctionOperation operation =
InvokeHostFuncOpBuilder(uploadFunction).build();
Transaction transaction =
new TransactionBuilder(accountA).addOperation(operation).build();
// simulate first to obtain the transaction data + resource fee
var request = SimulateTransactionRequest(transaction);
SimulateTransactionResponse simulateResponse =
await sorobanServer.simulateTransaction(request);
assert(simulateResponse.error == null);
assert(simulateResponse.resultError == null);
assert(simulateResponse.transactionData != null);
XdrSorobanTransactionData transactionData =
simulateResponse.transactionData!;
transactionData.resources.footprint.readWrite
.addAll(transactionData.resources.footprint.readOnly);
transactionData.resources.footprint.readOnly =
List<XdrLedgerKey>.empty(growable: false);
account = await sorobanServer.getAccount(adminId);
assert(account != null);
accountA = account!;
RestoreFootprintOperation restoreOp =
RestoreFootprintOperationBuilder().build();
transaction =
new TransactionBuilder(accountA).addOperation(restoreOp).build();
transaction.sorobanTransactionData = transactionData;
// simulate first to obtain the transaction data + resource fee
request = SimulateTransactionRequest(transaction);
simulateResponse = await sorobanServer.simulateTransaction(request);
assert(simulateResponse.error == null);
assert(simulateResponse.resultError == null);
assert(simulateResponse.transactionData != null);
assert(simulateResponse.minResourceFee != null);
// set transaction data, add resource fee and sign transaction
transaction.sorobanTransactionData = simulateResponse.transactionData;
transaction.addResourceFee(simulateResponse.minResourceFee!);
transaction.sign(adminKeypair, network);
// check transaction xdr encoding and decoding back and forth
String transactionEnvelopeXdr = transaction.toEnvelopeXdrBase64();
assert(transactionEnvelopeXdr ==
AbstractTransaction.fromEnvelopeXdrString(transactionEnvelopeXdr)
.toEnvelopeXdrBase64());
// send transaction to soroban rpc server
SendTransactionResponse sendResponse =
await sorobanServer.sendTransaction(transaction);
assert(sendResponse.error == null);
assert(sendResponse.hash != null);
assert(sendResponse.status != null);
assert(sendResponse.status != SendTransactionResponse.STATUS_ERROR);
GetTransactionResponse rpcTransactionResponse =
await pollStatus(sendResponse.hash!);
assert(
GetTransactionResponse.STATUS_SUCCESS == rpcTransactionResponse.status);
}
Future extendContractCodeFootprintTTL(String wasmId, int extendTo) async {
await Future.delayed(Duration(seconds: 5));
// load account
Account? account = await sorobanServer.getAccount(adminId);
assert(account != null);
Account accountA = account!;
ExtendFootprintTTLOperation bumpFunction =
ExtendFootprintTTLOperationBuilder(extendTo).build();
// create transaction for bumping
Transaction transaction =
new TransactionBuilder(accountA).addOperation(bumpFunction).build();
List<XdrLedgerKey> readOnly = List<XdrLedgerKey>.empty(growable: true);
List<XdrLedgerKey> readWrite = List<XdrLedgerKey>.empty(growable: false);
XdrLedgerKey codeKey = XdrLedgerKey(XdrLedgerEntryType.CONTRACT_CODE);
codeKey.contractCode = XdrLedgerKeyContractCode(
XdrHash(Util.hexToBytes(wasmId)));
readOnly.add(codeKey);
XdrLedgerFootprint footprint = XdrLedgerFootprint(readOnly, readWrite);
XdrSorobanResources resources = XdrSorobanResources(
footprint, XdrUint32(0), XdrUint32(0), XdrUint32(0));
XdrSorobanTransactionData transactionData =
XdrSorobanTransactionData(XdrExtensionPoint(0), resources, XdrInt64(0));
transaction.sorobanTransactionData = transactionData;
// simulate first to obtain the transaction data + resource fee
var request = SimulateTransactionRequest(transaction);
SimulateTransactionResponse simulateResponse =
await sorobanServer.simulateTransaction(request);
assert(simulateResponse.error == null);
assert(simulateResponse.resultError == null);
assert(simulateResponse.transactionData != null);
account = await sorobanServer.getAccount(adminId);
assert(account != null);
accountA = account!;
// set transaction data, add resource fee and sign transaction
transaction.sorobanTransactionData = simulateResponse.transactionData;
transaction.addResourceFee(simulateResponse.minResourceFee!);
transaction.sign(adminKeypair, network);
// check transaction xdr encoding and decoding back and forth
String transactionEnvelopeXdr = transaction.toEnvelopeXdrBase64();
assert(transactionEnvelopeXdr ==
AbstractTransaction.fromEnvelopeXdrString(transactionEnvelopeXdr)
.toEnvelopeXdrBase64());
// send transaction to soroban rpc server
SendTransactionResponse sendResponse =
await sorobanServer.sendTransaction(transaction);
assert(sendResponse.error == null);
assert(sendResponse.hash != null);
assert(sendResponse.status != null);
assert(sendResponse.status != SendTransactionResponse.STATUS_ERROR);
GetTransactionResponse rpcTransactionResponse =
await pollStatus(sendResponse.hash!);
assert(
GetTransactionResponse.STATUS_SUCCESS == rpcTransactionResponse.status);
await Future.delayed(Duration(seconds: 5));
// check horizon responses decoding
TransactionResponse transactionResponse =
await sdk.transactions.transaction(sendResponse.hash!);
assert(transactionResponse.operationCount == 1);
assert(transactionEnvelopeXdr == transactionResponse.envelopeXdr);
// check operation response from horizon
Page<OperationResponse> operations =
await sdk.operations.forTransaction(sendResponse.hash!).execute();
assert(operations.records.isNotEmpty);
OperationResponse operationResponse = operations.records.first;
if (operationResponse is ExtendFootprintTTLOperationResponse) {
assert("extend_footprint_ttl" == operationResponse.type);
} else {
assert(false);
}
}
group('all tests', () {
test('test restore footprint', () async {
await restoreContractFootprint(tokenContractPath);
await restoreContractFootprint(swapContractPath);
});
test('test install contracts', () async {
tokenAContractWasmId = await installContract(tokenContractPath);
await Future.delayed(Duration(seconds: 5));
await extendContractCodeFootprintTTL(tokenAContractWasmId!, 100000);
tokenBContractWasmId = await installContract(tokenContractPath);
await Future.delayed(Duration(seconds: 5));
await extendContractCodeFootprintTTL(tokenBContractWasmId!, 100000);
var contractInfo = await sorobanServer.loadContractInfoForWasmId(tokenBContractWasmId!);
assert(contractInfo != null);
assert(contractInfo!.specEntries.length > 0);
assert(contractInfo!.metaEntries.length > 0);
swapContractWasmId = await installContract(swapContractPath);
await extendContractCodeFootprintTTL(swapContractWasmId!, 100000);
contractInfo = await sorobanServer.loadContractInfoForWasmId(swapContractWasmId!);
assert(contractInfo != null);
assert(contractInfo!.specEntries.length > 0);
assert(contractInfo!.metaEntries.length > 0);
await Future.delayed(Duration(seconds: 5));
});
test('test create contracts', () async {
tokenAContractId = await createContract(tokenAContractWasmId!);
print("Token A Contract ID: " + tokenAContractId!);
await Future.delayed(Duration(seconds: 5));
tokenBContractId = await createContract(tokenBContractWasmId!);
print("Token B Contract ID: " + tokenBContractId!);
var contractInfo = await sorobanServer.loadContractInfoForContractId(tokenBContractId!);
assert(contractInfo != null);
assert(contractInfo!.specEntries.length > 0);
assert(contractInfo!.metaEntries.length > 0);
await Future.delayed(Duration(seconds: 5));
swapContractId = await createContract(swapContractWasmId!);
print("SWAP Contract ID: " + swapContractId!);
contractInfo = await sorobanServer.loadContractInfoForContractId(swapContractId!);
assert(contractInfo != null);
assert(contractInfo!.specEntries.length > 0);
assert(contractInfo!.metaEntries.length > 0);
await Future.delayed(Duration(seconds: 5));
});
test('test create tokens', () async {
await createToken(tokenAContractId!, "TokenA", "TokenA");
await Future.delayed(Duration(seconds: 5));
await createToken(tokenBContractId!, "TokenB", "TokenB");
await Future.delayed(Duration(seconds: 5));
});
test('test mint tokens', () async {
await mint(tokenAContractId!, aliceId, 10000000000000);
await Future.delayed(Duration(seconds: 5));
await mint(tokenBContractId!, bobId, 10000000000000);
await Future.delayed(Duration(seconds: 5));
int aliceTokenABalance = await balance(tokenAContractId!, aliceId);
assert(aliceTokenABalance == 10000000000000);
await Future.delayed(Duration(seconds: 5));
int bobTokenBBalance = await balance(tokenBContractId!, bobId);
assert(bobTokenBBalance == 10000000000000);
await Future.delayed(Duration(seconds: 5));
});
test('test atomic swap', () async {
// See https://soroban.stellar.org/docs/how-to-guides/atomic-swap
// See https://soroban.stellar.org/docs/learn/authorization
await Future.delayed(const Duration(seconds: 10), () {});
KeyPair swapSubmitterKp = adminKeypair;
String swapSubmitterAccountId = swapSubmitterKp.accountId;
KeyPair aliceKp = aliceKeypair;
String aliceAccountId = aliceKp.accountId;
KeyPair bobKp = bobKeypair;
String bobAccountId = bobKp.accountId;
String atomicSwapContractId = swapContractId!;
String tokenACId = tokenAContractId!;
String tokenBCId = tokenBContractId!;
Address addressAlice = Address.forAccountId(aliceAccountId);
Address addressBob = Address.forAccountId(bobAccountId);
XdrSCVal amountA = XdrSCVal.forI128(XdrInt128Parts.forHiLo(0, 1000));
XdrSCVal minBForA = XdrSCVal.forI128(XdrInt128Parts.forHiLo(0, 4500));
XdrSCVal amountB = XdrSCVal.forI128(XdrInt128Parts.forHiLo(0, 5000));
XdrSCVal minAForB = XdrSCVal.forI128(XdrInt128Parts.forHiLo(0, 950));
String swapFuntionName = "swap";
List<XdrSCVal> invokeArgs = [
addressAlice.toXdrSCVal(),
addressBob.toXdrSCVal(),
Address.forContractId(tokenACId).toXdrSCVal(),
Address.forContractId(tokenBCId).toXdrSCVal(),
amountA,
minBForA,
amountB,
minAForB
];
// load submitter account for sequence number
Account? account = await sorobanServer.getAccount(swapSubmitterAccountId);
assert(account != null);
Account swapSubmitter = account!;
InvokeContractHostFunction hostFunction = InvokeContractHostFunction(
atomicSwapContractId, swapFuntionName,
arguments: invokeArgs);
InvokeHostFunctionOperation op =
InvokeHostFuncOpBuilder(hostFunction).build();
Transaction transaction =
new TransactionBuilder(swapSubmitter).addOperation(op).build();
// simulate first to obtain the transaction data + resource fee
var request = new SimulateTransactionRequest(transaction);
SimulateTransactionResponse simulateResponse =
await sorobanServer.simulateTransaction(request);
assert(simulateResponse.error == null);
assert(simulateResponse.resultError == null);
assert(simulateResponse.transactionData != null);
// set transaction data, add resource fee and sign transaction
/*int instructions =
simulateResponse.transactionData!.resources.instructions.uint32;
instructions += (instructions / 4).round();
simulateResponse.transactionData!.resources.instructions =
XdrUint32(instructions);
simulateResponse.minResourceFee =
simulateResponse.minResourceFee! + 1005000;*/
transaction.sorobanTransactionData = simulateResponse.transactionData;
transaction.addResourceFee(simulateResponse.minResourceFee!);
// sign auth
List<SorobanAuthorizationEntry>? auth = simulateResponse.sorobanAuth;
assert(auth != null);
GetLatestLedgerResponse latestLedgerResponse =
await sorobanServer.getLatestLedger();
for (SorobanAuthorizationEntry a in auth!) {
a.credentials.addressCredentials!.signatureExpirationLedger =
latestLedgerResponse.sequence! + 10;
if (a.credentials.addressCredentials!.address.accountId == aliceId) {
a.sign(aliceKeypair, network);
}
if (a.credentials.addressCredentials!.address.accountId == bobId) {
a.sign(bobKeypair, network);
}
}
transaction.setSorobanAuth(auth);
transaction.sign(swapSubmitterKp, network);
// check transaction xdr encoding and decoding back and forth
String transactionEnvelopeXdr = transaction.toEnvelopeXdrBase64();
assert(transactionEnvelopeXdr ==
AbstractTransaction.fromEnvelopeXdrString(transactionEnvelopeXdr)
.toEnvelopeXdrBase64());
// send the transaction
SendTransactionResponse sendResponse =
await sorobanServer.sendTransaction(transaction);
assert(sendResponse.error == null);
assert(sendResponse.status != SendTransactionResponse.STATUS_ERROR);
GetTransactionResponse statusResponse =
await pollStatus(sendResponse.hash!);
String status = statusResponse.status!;
assert(status == GetTransactionResponse.STATUS_SUCCESS);
print("Result " +
statusResponse.getResultValue()!.toBase64EncodedXdrString());
});
});
}