@indigo-labs/indigo-sdk 0.3.24 → 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.
@@ -4,14 +4,15 @@ import {
4
4
  TxBuilder,
5
5
  Data,
6
6
  UTxO,
7
- credentialToAddress,
8
7
  fromHex,
9
8
  toHex,
10
9
  addAssets,
11
10
  OutRef,
12
11
  slotToUnixTime,
12
+ credentialToAddress,
13
13
  } from '@lucid-evolution/lucid';
14
14
  import {
15
+ fromSysParamsCredential,
15
16
  fromSystemParamsAsset,
16
17
  fromSystemParamsScriptRef,
17
18
  SystemParams,
@@ -23,7 +24,6 @@ import {
23
24
  findRelevantE2s2sIdxs,
24
25
  initSpState,
25
26
  liquidationHelper,
26
- mkStabilityPoolAddr,
27
27
  partitionEpochToScaleToSums,
28
28
  updateAccount,
29
29
  updatePoolStateWhenWithdrawalFee,
@@ -79,7 +79,7 @@ export async function requestSpAccountCreation(
79
79
  const datum: AccountContent = {
80
80
  owner: fromHex(pkh.hash),
81
81
  iasset: iasset,
82
- state: initSpState,
82
+ state: { ...initSpState, depositVal: mkSPInteger(amount) },
83
83
  assetSums: [],
84
84
  request: 'Create',
85
85
  lastRequestProcessingTime: 0n,
@@ -88,10 +88,18 @@ export async function requestSpAccountCreation(
88
88
  return lucid
89
89
  .newTx()
90
90
  .pay.ToContract(
91
- credentialToAddress(lucid.config().network!, {
92
- hash: sysParams.validatorHashes.stabilityPoolHash,
93
- type: 'Script',
94
- }),
91
+ credentialToAddress(
92
+ lucid.config().network!,
93
+ {
94
+ hash: sysParams.validatorHashes.stabilityPoolHash,
95
+ type: 'Script',
96
+ },
97
+ sysParams.stabilityPoolParams.stakeCredential != null
98
+ ? fromSysParamsCredential(
99
+ sysParams.stabilityPoolParams.stakeCredential,
100
+ )
101
+ : undefined,
102
+ ),
95
103
  {
96
104
  kind: 'inline',
97
105
  value: serialiseStabilityPoolDatum({ Account: datum }),
@@ -100,7 +108,10 @@ export async function requestSpAccountCreation(
100
108
  mkAssetsOf(iassetAssetClass, amount),
101
109
  mkLovelacesOf(
102
110
  // TODO: Calculate a more accurate amount to just cover costs.
103
- 5_000_000n,
111
+ // This should cover the account creation fee,
112
+ // the minimum ADA for the account UTxO and the transaction fee.
113
+ BigInt(sysParams.stabilityPoolParams.accountCreateFeeLovelaces) +
114
+ 3_000_000n,
104
115
  ),
105
116
  ),
106
117
  )
@@ -163,7 +174,9 @@ export async function requestSpAccountAdjustment(
163
174
  addAssets(
164
175
  mkLovelacesOf(
165
176
  // TODO: Calculate a more accurate amount to just cover costs.
166
- 7_000_000n,
177
+ // This should cover the minimum ADA for 2 UTxOs (account and
178
+ // rewards withdrawal) and the transaction fee.
179
+ 5_000_000n,
167
180
  ),
168
181
  mkAssetsOf(
169
182
  fromSystemParamsAsset(sysParams.stabilityPoolParams.accountToken),
@@ -232,7 +245,8 @@ export async function requestSpAccountClosure(
232
245
  addAssets(
233
246
  mkLovelacesOf(
234
247
  // TODO: Calculate a more accurate amount to just cover costs.
235
- 5_000_000n,
248
+ // This should cover the minimum ADA for the rewards UTxO and the transaction fee.
249
+ 3_000_000n,
236
250
  ),
237
251
  mkAssetsOf(
238
252
  fromSystemParamsAsset(sysParams.stabilityPoolParams.accountToken),
@@ -243,7 +257,7 @@ export async function requestSpAccountClosure(
243
257
  .addSignerKey(toHex(oldAccountDatum.owner));
244
258
  }
245
259
 
246
- export async function processSpRequest(
260
+ async function processSpRequestAuxiliary(
247
261
  stabilityPoolUtxo: UTxO,
248
262
  accountUtxo: UTxO,
249
263
  iAssetUtxo: UTxO,
@@ -254,6 +268,7 @@ export async function processSpRequest(
254
268
  sysParams: SystemParams,
255
269
  lucid: LucidEvolution,
256
270
  currentSlot: number,
271
+ txFee: bigint,
257
272
  ): Promise<TxBuilder> {
258
273
  const network = lucid.config().network!;
259
274
  const currentTime = BigInt(slotToUnixTime(network, currentSlot));
@@ -320,7 +335,57 @@ export async function processSpRequest(
320
335
  iassetAssetClass,
321
336
  );
322
337
 
323
- const poolAddr = mkStabilityPoolAddr(lucid, sysParams);
338
+ const poolAddr = credentialToAddress(
339
+ lucid.config().network!,
340
+ {
341
+ hash: sysParams.validatorHashes.stabilityPoolHash,
342
+ type: 'Script',
343
+ },
344
+ sysParams.stabilityPoolParams.stakeCredential != null
345
+ ? fromSysParamsCredential(
346
+ sysParams.stabilityPoolParams.stakeCredential,
347
+ )
348
+ : undefined,
349
+ );
350
+
351
+ const accountOutputAdaAmt =
352
+ lovelacesAmt(accountUtxo.assets) -
353
+ BigInt(sysParams.stabilityPoolParams.accountCreateFeeLovelaces) -
354
+ txFee;
355
+
356
+ const accountTokenVal = mkAssetsOf(
357
+ fromSystemParamsAsset(sysParams.stabilityPoolParams.accountToken),
358
+ 1n,
359
+ );
360
+
361
+ const accountOutputDatum = serialiseStabilityPoolDatum({
362
+ Account: {
363
+ owner: accountDatum.owner,
364
+ iasset: stabilityPoolDatum.iasset,
365
+ state: {
366
+ ...stabilityPoolDatum.state,
367
+ depositVal: mkSPInteger(requestDepositAmt),
368
+ },
369
+ assetSums: stabilityPoolDatum.assetStates.map(([key, val]) => [
370
+ key,
371
+ val.currentSumVal,
372
+ ]),
373
+ request: null,
374
+ lastRequestProcessingTime: currentTime,
375
+ },
376
+ });
377
+
378
+ const accountOutMinUtxoLovelace = estimateUtxoMinLovelace(
379
+ lucid.config().protocolParameters!,
380
+ poolAddr,
381
+ accountTokenVal,
382
+ { InlineDatum: { datum: accountOutputDatum } },
383
+ );
384
+
385
+ // This is to prevent spending ADA from the wallet submitting the request processing Tx.
386
+ if (accountOutputAdaAmt < accountOutMinUtxoLovelace) {
387
+ throw new Error("Request doesn't have enough ADA to be processed.");
388
+ }
324
389
 
325
390
  tx.readFrom([accountTokenScriptRef])
326
391
  .collectFrom([accountUtxo], {
@@ -336,13 +401,7 @@ export async function processSpRequest(
336
401
  },
337
402
  }),
338
403
  })
339
- .mintAssets(
340
- mkAssetsOf(
341
- fromSystemParamsAsset(sysParams.stabilityPoolParams.accountToken),
342
- 1n,
343
- ),
344
- Data.void(),
345
- )
404
+ .mintAssets(accountTokenVal, Data.void())
346
405
  .pay.ToContract(
347
406
  poolAddr,
348
407
  {
@@ -377,31 +436,11 @@ export async function processSpRequest(
377
436
  poolAddr,
378
437
  {
379
438
  kind: 'inline',
380
- value: serialiseStabilityPoolDatum({
381
- Account: {
382
- owner: accountDatum.owner,
383
- iasset: stabilityPoolDatum.iasset,
384
- state: {
385
- ...stabilityPoolDatum.state,
386
- depositVal: mkSPInteger(requestDepositAmt),
387
- },
388
- assetSums: stabilityPoolDatum.assetStates.map(([key, val]) => [
389
- key,
390
- val.currentSumVal,
391
- ]),
392
- request: null,
393
- lastRequestProcessingTime: currentTime,
394
- },
395
- }),
439
+ value: accountOutputDatum,
396
440
  },
397
- addAssets(
398
- mkAssetsOf(
399
- fromSystemParamsAsset(sysParams.stabilityPoolParams.accountToken),
400
- 1n,
401
- ),
402
- mkLovelacesOf(lovelacesAmt(accountUtxo.assets)),
403
- ),
404
- );
441
+ addAssets(accountTokenVal, mkLovelacesOf(accountOutputAdaAmt)),
442
+ )
443
+ .setMinFee(txFee);
405
444
  })
406
445
  .with({ Adjust: P.select() }, async (adjustContent) => {
407
446
  const iassetDatum = parseIAssetDatumOrThrow(
@@ -558,9 +597,8 @@ export async function processSpRequest(
558
597
  { InlineDatum: { datum: accountOutputDat } },
559
598
  );
560
599
 
561
- // TODO: include Tx fee here.
562
600
  const accountOutputAdaAmt =
563
- lovelacesAmt(accountUtxo.assets) - extraOwnerOutputLovelacesAmt;
601
+ lovelacesAmt(accountUtxo.assets) - extraOwnerOutputLovelacesAmt - txFee;
564
602
 
565
603
  // This is to prevent spending ADA from the wallet submitting the request processing Tx.
566
604
  if (accountOutputAdaAmt < accountOutMinUtxoLovelace) {
@@ -586,9 +624,14 @@ export async function processSpRequest(
586
624
  value: ownerOutputDat,
587
625
  },
588
626
  actualOwnerOutputVal,
589
- );
627
+ )
628
+ .setMinFee(txFee);
590
629
  })
591
630
  .with({ Close: P.select() }, async (closeContent) => {
631
+ if (txFee > closeContent.maxTxFee) {
632
+ throw new Error("Account doesn't allow current transaction fee.");
633
+ }
634
+
592
635
  const accountTokenScriptRef = matchSingle(
593
636
  await lucid.utxosByOutRef([
594
637
  fromSystemParamsScriptRef(
@@ -705,17 +748,56 @@ export async function processSpRequest(
705
748
  }),
706
749
  },
707
750
  addAssets(
708
- mkLovelacesOf(lovelacesAmt(accountUtxo.assets)),
751
+ mkLovelacesOf(lovelacesAmt(accountUtxo.assets) - txFee),
709
752
  reward,
710
753
  mkAssetsOf(iassetAssetClass, withdrawnAmt - withdrawalFeeAmt),
711
754
  ),
712
- );
755
+ )
756
+ .setMinFee(txFee);
713
757
  })
714
758
  .exhaustive();
715
759
 
716
760
  return tx;
717
761
  }
718
762
 
763
+ export async function processSpRequest(
764
+ stabilityPoolUtxo: UTxO,
765
+ accountUtxo: UTxO,
766
+ iAssetUtxo: UTxO,
767
+ /**
768
+ * For performance provide only the ones related to the pool's iAsset.
769
+ */
770
+ allE2s2sSnapshotOrefs: OutRef[],
771
+ sysParams: SystemParams,
772
+ lucid: LucidEvolution,
773
+ currentSlot: number,
774
+ ): Promise<TxBuilder> {
775
+ const draftTx = processSpRequestAuxiliary(
776
+ stabilityPoolUtxo,
777
+ accountUtxo,
778
+ iAssetUtxo,
779
+ allE2s2sSnapshotOrefs,
780
+ sysParams,
781
+ lucid,
782
+ currentSlot,
783
+ // Placeholder transation fee
784
+ 1n,
785
+ );
786
+
787
+ const fee = (await (await draftTx).complete()).toTransaction().body().fee();
788
+
789
+ return processSpRequestAuxiliary(
790
+ stabilityPoolUtxo,
791
+ accountUtxo,
792
+ iAssetUtxo,
793
+ allE2s2sSnapshotOrefs,
794
+ sysParams,
795
+ lucid,
796
+ currentSlot,
797
+ fee,
798
+ );
799
+ }
800
+
719
801
  export async function createE2s2sSnapshots(
720
802
  stabilityPoolOref: OutRef,
721
803
  sysParams: SystemParams,
@@ -2,7 +2,7 @@ import { fromHex, fromText, OutRef } from '@lucid-evolution/lucid';
2
2
  import { serialiseStableswapOutputDatum } from './types-new';
3
3
  import { Data } from '@evolution-sdk/evolution';
4
4
 
5
- export const BASE_MAX_EXECUTION_FEE = 1_000_000n;
5
+ export const BASE_MAX_EXECUTION_FEE = 1_620_000n;
6
6
 
7
7
  export function createDestinationDatum(
8
8
  datum: Data.Data | null,
@@ -496,8 +496,9 @@ export async function batchProcessStableswapOrders(
496
496
  // This has to be added as otherwise there is the following error:
497
497
  // TxBuilderError: { Complete: RedeemerBuilder: Coin selection had to be updated
498
498
  // after building redeemers, possibly leading to incorrect indices. Try setting
499
- // a minimum fee of 2055720 lovelaces. }
500
- .setMinFee(2_055_720n);
499
+ // a minimum fee of 1761019 lovelaces. }
500
+ // Trying to set it as low as possible to reduce costs.
501
+ .setMinFee(stableswapOrderOrefs.length > 1 ? 1_498_875n : 1_030_000n);
501
502
 
502
503
  if (amountToMint !== 0n) {
503
504
  tx.mintAssets(mkAssetsOf(iassetAc, amountToMint), Data.void());