@indigo-labs/indigo-sdk 0.3.25 → 0.3.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -208,6 +208,8 @@ test<IndigoTestContext>('Stableswap - Batch a single order to mint', async (cont
208
208
  context.systemParams,
209
209
  );
210
210
 
211
+ context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
212
+
211
213
  const [__, treasuryValChange] = await getValueChangeAtAddressAfterAction(
212
214
  context.lucid,
213
215
  mkTreasuryAddr(context.lucid, context.systemParams),
@@ -389,6 +391,192 @@ test<IndigoTestContext>('Stableswap - Batch a single order to mint with no minti
389
391
  );
390
392
  });
391
393
 
394
+ test<IndigoTestContext>('Stableswap - Batch a single order to mint with collateral asset in the pool', async (context: IndigoTestContext) => {
395
+ context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
396
+
397
+ await runCreateStableswapPool(
398
+ context.assetConfigs[0].iassetTokenNameAscii,
399
+ EXAMPLE_TOKEN_1,
400
+ context,
401
+ );
402
+
403
+ let stableswapPool = await findStableswapPool(
404
+ context.lucid,
405
+ context.systemParams.validatorHashes.cdpHash,
406
+ fromSystemParamsAsset(context.systemParams.stableswapParams.cdpToken),
407
+ fromText(context.assetConfigs[0].iassetTokenNameAscii),
408
+ EXAMPLE_TOKEN_1,
409
+ );
410
+
411
+ context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
412
+
413
+ await runAndAwaitTx(
414
+ context.lucid,
415
+ createStableswapOrder(
416
+ context.assetConfigs[0].iassetTokenNameAscii,
417
+ EXAMPLE_TOKEN_1,
418
+ 10_000_000n,
419
+ true,
420
+ stableswapPool.datum,
421
+ context.systemParams,
422
+ context.lucid,
423
+ ),
424
+ );
425
+
426
+ let stableswapOrder = await findSingleStableswapOrder(
427
+ context.lucid,
428
+ context.systemParams.validatorHashes.stableswapHash,
429
+ fromText(context.assetConfigs[0].iassetTokenNameAscii),
430
+ EXAMPLE_TOKEN_1,
431
+ );
432
+
433
+ await createUtxoAtTreasury(
434
+ mkLovelacesOf(2_000_000n),
435
+ context.systemParams,
436
+ context,
437
+ );
438
+
439
+ let treasuryUtxo = await findRandomTreasuryUtxoWithOnlyAda(
440
+ context.lucid,
441
+ context.systemParams,
442
+ );
443
+
444
+ context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
445
+
446
+ await runAndAwaitTx(
447
+ context.lucid,
448
+ batchProcessStableswapOrders(
449
+ [stableswapOrder.utxo],
450
+ stableswapPool.utxo,
451
+ treasuryUtxo,
452
+ context.systemParams,
453
+ context.lucid,
454
+ ),
455
+ );
456
+
457
+ context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
458
+
459
+ const initialUserValue = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
460
+ addAssets(acc, utxo.assets),
461
+ )(await context.lucid.utxosAt(context.users.user.address));
462
+
463
+ await runAndAwaitTx(
464
+ context.lucid,
465
+ createStableswapOrder(
466
+ context.assetConfigs[0].iassetTokenNameAscii,
467
+ EXAMPLE_TOKEN_1,
468
+ 10_000_000n,
469
+ true,
470
+ stableswapPool.datum,
471
+ context.systemParams,
472
+ context.lucid,
473
+ ),
474
+ );
475
+
476
+ stableswapOrder = await findSingleStableswapOrder(
477
+ context.lucid,
478
+ context.systemParams.validatorHashes.stableswapHash,
479
+ fromText(context.assetConfigs[0].iassetTokenNameAscii),
480
+ EXAMPLE_TOKEN_1,
481
+ );
482
+
483
+ const iassetAc = {
484
+ currencySymbol: fromHex(
485
+ context.systemParams.stableswapParams.iassetSymbol.unCurrencySymbol,
486
+ ),
487
+ tokenName: stableswapOrder.datum.iasset,
488
+ };
489
+
490
+ treasuryUtxo = await findRandomTreasuryUtxoWithAsset(
491
+ context.lucid,
492
+ context.systemParams,
493
+ iassetAc,
494
+ );
495
+
496
+ context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
497
+
498
+ const initialAdminValue = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
499
+ addAssets(acc, utxo.assets),
500
+ )(await context.lucid.utxosAt(context.users.admin.address));
501
+
502
+ stableswapPool = await findStableswapPool(
503
+ context.lucid,
504
+ context.systemParams.validatorHashes.cdpHash,
505
+ fromSystemParamsAsset(context.systemParams.stableswapParams.cdpToken),
506
+ fromText(context.assetConfigs[0].iassetTokenNameAscii),
507
+ EXAMPLE_TOKEN_1,
508
+ );
509
+
510
+ const [__, treasuryValChange] = await getValueChangeAtAddressAfterAction(
511
+ context.lucid,
512
+ mkTreasuryAddr(context.lucid, context.systemParams),
513
+ async () =>
514
+ benchmarkAndAwaitTx(
515
+ 'Stableswap - Batch a single order to mint with collateral in the pool',
516
+ await batchProcessStableswapOrders(
517
+ [stableswapOrder.utxo],
518
+ stableswapPool.utxo,
519
+ treasuryUtxo,
520
+ context.systemParams,
521
+ context.lucid,
522
+ ),
523
+ context.lucid,
524
+ context.emulator,
525
+ ),
526
+ );
527
+
528
+ ///////////////////////////////////
529
+ // Checks after last transaction //
530
+ ///////////////////////////////////
531
+
532
+ stableswapPool = await findStableswapPool(
533
+ context.lucid,
534
+ context.systemParams.validatorHashes.cdpHash,
535
+ fromSystemParamsAsset(context.systemParams.stableswapParams.cdpToken),
536
+ fromText(context.assetConfigs[0].iassetTokenNameAscii),
537
+ EXAMPLE_TOKEN_1,
538
+ );
539
+
540
+ const finalUserValue = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
541
+ addAssets(acc, utxo.assets),
542
+ )(await context.lucid.utxosAt(context.users.user.address));
543
+
544
+ const userValueDiff = addAssets(
545
+ finalUserValue,
546
+ negateAssets(initialUserValue),
547
+ );
548
+
549
+ const finalAdminValue = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
550
+ addAssets(acc, utxo.assets),
551
+ )(await context.lucid.utxosAt(context.users.admin.address));
552
+
553
+ const adminValueDiff = addAssets(
554
+ finalAdminValue,
555
+ negateAssets(initialAdminValue),
556
+ );
557
+
558
+ assert(
559
+ lovelacesAmt(adminValueDiff) >= 0n &&
560
+ lovelacesAmt(adminValueDiff) < 100_000n,
561
+ );
562
+
563
+ assert(
564
+ assetClassValueOf(treasuryValChange, iassetAc) == 50_000n,
565
+ 'Unexpected iAsset value received by the treasury',
566
+ );
567
+
568
+ assert(
569
+ assetClassValueOf(stableswapPool.utxo.assets, EXAMPLE_TOKEN_1) ==
570
+ 20_000_000n,
571
+ 'Unexpected value held by stableswap pool',
572
+ );
573
+
574
+ assert(
575
+ assetClassValueOf(userValueDiff, iassetAc) == 10_000_000n - 50_000n,
576
+ 'Unexpected value received by order owner',
577
+ );
578
+ });
579
+
392
580
  test<IndigoTestContext>('Stableswap - Batch a single order to redeem', async (context: IndigoTestContext) => {
393
581
  context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
394
582
 
@@ -498,6 +686,12 @@ test<IndigoTestContext>('Stableswap - Batch a single order to redeem', async (co
498
686
  iassetAc,
499
687
  );
500
688
 
689
+ context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
690
+
691
+ const initialAdminValue = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
692
+ addAssets(acc, utxo.assets),
693
+ )(await context.lucid.utxosAt(context.users.admin.address));
694
+
501
695
  const [__, treasuryValChange] = await getValueChangeAtAddressAfterAction(
502
696
  context.lucid,
503
697
  mkTreasuryAddr(context.lucid, context.systemParams),
@@ -537,6 +731,20 @@ test<IndigoTestContext>('Stableswap - Batch a single order to redeem', async (co
537
731
  negateAssets(initialUserValue),
538
732
  );
539
733
 
734
+ const finalAdminValue = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
735
+ addAssets(acc, utxo.assets),
736
+ )(await context.lucid.utxosAt(context.users.admin.address));
737
+
738
+ const adminValueDiff = addAssets(
739
+ finalAdminValue,
740
+ negateAssets(initialAdminValue),
741
+ );
742
+
743
+ assert(
744
+ lovelacesAmt(adminValueDiff) >= 0n &&
745
+ lovelacesAmt(adminValueDiff) < 100_000n,
746
+ );
747
+
540
748
  assert(
541
749
  assetClassValueOf(treasuryValChange, iassetAc) == 25_000n &&
542
750
  0n <= lovelacesAmt(treasuryValChange) &&
@@ -672,6 +880,12 @@ test<IndigoTestContext>('Stableswap - Batch a single order to redeem with no red
672
880
  context.systemParams,
673
881
  );
674
882
 
883
+ context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
884
+
885
+ const initialAdminValue = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
886
+ addAssets(acc, utxo.assets),
887
+ )(await context.lucid.utxosAt(context.users.admin.address));
888
+
675
889
  const [__, treasuryValChange] = await getValueChangeAtAddressAfterAction(
676
890
  context.lucid,
677
891
  mkTreasuryAddr(context.lucid, context.systemParams),
@@ -709,6 +923,18 @@ test<IndigoTestContext>('Stableswap - Batch a single order to redeem with no red
709
923
  negateAssets(initialUserValue),
710
924
  );
711
925
 
926
+ const finalAdminValue = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
927
+ addAssets(acc, utxo.assets),
928
+ )(await context.lucid.utxosAt(context.users.admin.address));
929
+
930
+ const adminValueDiff = addAssets(
931
+ finalAdminValue,
932
+ negateAssets(initialAdminValue),
933
+ );
934
+
935
+ // This one will result in a bigger gain for the admin as the tx fee is lower
936
+ assert(lovelacesAmt(adminValueDiff) >= 0n);
937
+
712
938
  assert(
713
939
  assetClassValueOf(treasuryValChange, iassetAc) == 0n,
714
940
  'Unexpected value received by the treasury',
@@ -873,6 +1099,12 @@ test<IndigoTestContext>('Stableswap - Batch a single order to redeem emptying th
873
1099
  iassetAc,
874
1100
  );
875
1101
 
1102
+ context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
1103
+
1104
+ const initialAdminValue = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
1105
+ addAssets(acc, utxo.assets),
1106
+ )(await context.lucid.utxosAt(context.users.admin.address));
1107
+
876
1108
  const [__, treasuryValChange] = await getValueChangeAtAddressAfterAction(
877
1109
  context.lucid,
878
1110
  mkTreasuryAddr(context.lucid, context.systemParams),
@@ -910,6 +1142,20 @@ test<IndigoTestContext>('Stableswap - Batch a single order to redeem emptying th
910
1142
  negateAssets(initialUserValue),
911
1143
  );
912
1144
 
1145
+ const finalAdminValue = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
1146
+ addAssets(acc, utxo.assets),
1147
+ )(await context.lucid.utxosAt(context.users.admin.address));
1148
+
1149
+ const adminValueDiff = addAssets(
1150
+ finalAdminValue,
1151
+ negateAssets(initialAdminValue),
1152
+ );
1153
+
1154
+ assert(
1155
+ lovelacesAmt(adminValueDiff) >= 0n &&
1156
+ lovelacesAmt(adminValueDiff) < 100_000n,
1157
+ );
1158
+
913
1159
  assert(
914
1160
  assetClassValueOf(treasuryValChange, iassetAc) == 50_251n &&
915
1161
  0n <= lovelacesAmt(treasuryValChange) &&
@@ -79,3 +79,52 @@ export async function benchmarkAndAwaitTx(
79
79
 
80
80
  await lucid.awaitTx(await (await signedTx.complete()).submit());
81
81
  }
82
+
83
+ // This function is used to benchmark a transaction and await it's successful execution,
84
+ // as well as to query the transaction fee.
85
+ export async function benchmarkAndAwaitTxWithTxFee(
86
+ name: string,
87
+ tx: TxBuilder,
88
+ lucid: LucidEvolution,
89
+ provider: Provider,
90
+ extraSigners: string[] = [],
91
+ ): Promise<bigint> {
92
+ // Complete the transaction
93
+ const bTx = await tx.complete();
94
+ const signatures = [await bTx.partialSign.withWallet()];
95
+ for (const signer of extraSigners) {
96
+ lucid.selectWallet.fromSeed(signer);
97
+ signatures.push(await lucid.fromTx(bTx.toCBOR()).partialSign.withWallet());
98
+ }
99
+
100
+ const signedTx = bTx.assemble(signatures);
101
+
102
+ // Calculate execution units
103
+ const exUnits = await calculateTxExUnits(provider, signedTx);
104
+
105
+ // Get protocol parameters for percentages
106
+ const protocolParams = await provider.getProtocolParameters();
107
+
108
+ if (protocolParams.maxTxExMem !== MAINNET_PROTOCOL_PARAMETERS.maxTxExMem) {
109
+ throw new Error('Protocol parameters do not match Mainnet');
110
+ }
111
+
112
+ // Calculate percentages
113
+ const result = {
114
+ ...exUnits,
115
+ memPercentage: (exUnits.mem / Number(protocolParams.maxTxExMem)) * 100,
116
+ stepsPercentage:
117
+ (exUnits.steps / Number(protocolParams.maxTxExSteps)) * 100,
118
+ sizePercentage: (exUnits.txSize / Number(protocolParams.maxTxSize)) * 100,
119
+ };
120
+
121
+ BENCHMARK_RESULTS[name] = result;
122
+
123
+ const completedTx = await signedTx.complete();
124
+
125
+ const txFee = completedTx.toTransaction().body().fee();
126
+
127
+ await lucid.awaitTx(await completedTx.submit());
128
+
129
+ return txFee;
130
+ }