@indigo-labs/indigo-sdk 0.3.25 → 0.3.27

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.
package/dist/index.mjs CHANGED
@@ -1050,7 +1050,7 @@ var mkStabilityPoolValidatorFromSP = (params) => {
1050
1050
  };
1051
1051
 
1052
1052
  // src/contracts/stability-pool/helpers.ts
1053
- var BASE_MAX_TX_FEE = 1000000n;
1053
+ var BASE_MAX_TX_FEE = 1250000n;
1054
1054
  var MAX_E2S2S_ENTRIES_COUNT = 5;
1055
1055
  var newScaleMultiplier = 1000000000n;
1056
1056
  var initSumVal = { value: 0n };
@@ -6957,11 +6957,11 @@ var mkGovValidatorFromSP = (params) => {
6957
6957
  import {
6958
6958
  fromText as fromText5,
6959
6959
  Data as Data32,
6960
- credentialToAddress as credentialToAddress3,
6961
6960
  fromHex as fromHex9,
6962
6961
  toHex as toHex9,
6963
6962
  addAssets as addAssets9,
6964
- slotToUnixTime as slotToUnixTime7
6963
+ slotToUnixTime as slotToUnixTime7,
6964
+ credentialToAddress as credentialToAddress3
6965
6965
  } from "@lucid-evolution/lucid";
6966
6966
  import {
6967
6967
  adaAssetClass as adaAssetClass4,
@@ -6994,10 +6994,16 @@ async function requestSpAccountCreation(assetAscii, amount, sysParams, lucid) {
6994
6994
  lastRequestProcessingTime: 0n
6995
6995
  };
6996
6996
  return lucid.newTx().pay.ToContract(
6997
- credentialToAddress3(lucid.config().network, {
6998
- hash: sysParams.validatorHashes.stabilityPoolHash,
6999
- type: "Script"
7000
- }),
6997
+ credentialToAddress3(
6998
+ lucid.config().network,
6999
+ {
7000
+ hash: sysParams.validatorHashes.stabilityPoolHash,
7001
+ type: "Script"
7002
+ },
7003
+ sysParams.stabilityPoolParams.stakeCredential != null ? fromSysParamsCredential(
7004
+ sysParams.stabilityPoolParams.stakeCredential
7005
+ ) : void 0
7006
+ ),
7001
7007
  {
7002
7008
  kind: "inline",
7003
7009
  value: serialiseStabilityPoolDatum({ Account: datum })
@@ -7006,7 +7012,9 @@ async function requestSpAccountCreation(assetAscii, amount, sysParams, lucid) {
7006
7012
  mkAssetsOf7(iassetAssetClass, amount),
7007
7013
  mkLovelacesOf4(
7008
7014
  // TODO: Calculate a more accurate amount to just cover costs.
7009
- 5000000n
7015
+ // This should cover the account creation fee,
7016
+ // the minimum ADA for the account UTxO and the transaction fee.
7017
+ BigInt(sysParams.stabilityPoolParams.accountCreateFeeLovelaces) + 3000000n
7010
7018
  )
7011
7019
  )
7012
7020
  ).addSignerKey(pkh.hash);
@@ -7054,7 +7062,9 @@ async function requestSpAccountAdjustment(amount, accountUtxo, sysParams, lucid)
7054
7062
  addAssets9(
7055
7063
  mkLovelacesOf4(
7056
7064
  // TODO: Calculate a more accurate amount to just cover costs.
7057
- 7000000n
7065
+ // This should cover the minimum ADA for 2 UTxOs (account and
7066
+ // rewards withdrawal) and the transaction fee.
7067
+ 5000000n
7058
7068
  ),
7059
7069
  mkAssetsOf7(
7060
7070
  fromSystemParamsAsset(sysParams.stabilityPoolParams.accountToken),
@@ -7107,7 +7117,8 @@ async function requestSpAccountClosure(accountUtxo, sysParams, lucid, maxTxFee =
7107
7117
  addAssets9(
7108
7118
  mkLovelacesOf4(
7109
7119
  // TODO: Calculate a more accurate amount to just cover costs.
7110
- 5000000n
7120
+ // This should cover the minimum ADA for the rewards UTxO and the transaction fee.
7121
+ 3000000n
7111
7122
  ),
7112
7123
  mkAssetsOf7(
7113
7124
  fromSystemParamsAsset(sysParams.stabilityPoolParams.accountToken),
@@ -7116,7 +7127,7 @@ async function requestSpAccountClosure(accountUtxo, sysParams, lucid, maxTxFee =
7116
7127
  )
7117
7128
  ).addSignerKey(toHex9(oldAccountDatum.owner));
7118
7129
  }
7119
- async function processSpRequest(stabilityPoolUtxo, accountUtxo, iAssetUtxo, allE2s2sSnapshotOrefs, sysParams, lucid, currentSlot) {
7130
+ async function processSpRequestAuxiliary(stabilityPoolUtxo, accountUtxo, iAssetUtxo, allE2s2sSnapshotOrefs, sysParams, lucid, currentSlot, txFee) {
7120
7131
  const network = lucid.config().network;
7121
7132
  const currentTime = BigInt(slotToUnixTime7(network, currentSlot));
7122
7133
  const stabilityPoolScriptRef = matchSingle3(
@@ -7165,7 +7176,46 @@ async function processSpRequest(stabilityPoolUtxo, accountUtxo, iAssetUtxo, allE
7165
7176
  accountUtxo.assets,
7166
7177
  iassetAssetClass
7167
7178
  );
7168
- const poolAddr = mkStabilityPoolAddr(lucid, sysParams);
7179
+ const poolAddr = credentialToAddress3(
7180
+ lucid.config().network,
7181
+ {
7182
+ hash: sysParams.validatorHashes.stabilityPoolHash,
7183
+ type: "Script"
7184
+ },
7185
+ sysParams.stabilityPoolParams.stakeCredential != null ? fromSysParamsCredential(
7186
+ sysParams.stabilityPoolParams.stakeCredential
7187
+ ) : void 0
7188
+ );
7189
+ const accountOutputAdaAmt = lovelacesAmt2(accountUtxo.assets) - BigInt(sysParams.stabilityPoolParams.accountCreateFeeLovelaces) - txFee;
7190
+ const accountTokenVal = mkAssetsOf7(
7191
+ fromSystemParamsAsset(sysParams.stabilityPoolParams.accountToken),
7192
+ 1n
7193
+ );
7194
+ const accountOutputDatum = serialiseStabilityPoolDatum({
7195
+ Account: {
7196
+ owner: accountDatum.owner,
7197
+ iasset: stabilityPoolDatum.iasset,
7198
+ state: {
7199
+ ...stabilityPoolDatum.state,
7200
+ depositVal: mkSPInteger(requestDepositAmt)
7201
+ },
7202
+ assetSums: stabilityPoolDatum.assetStates.map(([key, val]) => [
7203
+ key,
7204
+ val.currentSumVal
7205
+ ]),
7206
+ request: null,
7207
+ lastRequestProcessingTime: currentTime
7208
+ }
7209
+ });
7210
+ const accountOutMinUtxoLovelace = estimateUtxoMinLovelace3(
7211
+ lucid.config().protocolParameters,
7212
+ poolAddr,
7213
+ accountTokenVal,
7214
+ { InlineDatum: { datum: accountOutputDatum } }
7215
+ );
7216
+ if (accountOutputAdaAmt < accountOutMinUtxoLovelace) {
7217
+ throw new Error("Request doesn't have enough ADA to be processed.");
7218
+ }
7169
7219
  tx.readFrom([accountTokenScriptRef]).collectFrom([accountUtxo], {
7170
7220
  kind: "selected",
7171
7221
  inputs: [stabilityPoolUtxo, accountUtxo],
@@ -7177,13 +7227,7 @@ async function processSpRequest(stabilityPoolUtxo, accountUtxo, iAssetUtxo, allE
7177
7227
  currentTime
7178
7228
  }
7179
7229
  })
7180
- }).mintAssets(
7181
- mkAssetsOf7(
7182
- fromSystemParamsAsset(sysParams.stabilityPoolParams.accountToken),
7183
- 1n
7184
- ),
7185
- Data32.void()
7186
- ).pay.ToContract(
7230
+ }).mintAssets(accountTokenVal, Data32.void()).pay.ToContract(
7187
7231
  poolAddr,
7188
7232
  {
7189
7233
  kind: "inline",
@@ -7216,31 +7260,10 @@ async function processSpRequest(stabilityPoolUtxo, accountUtxo, iAssetUtxo, allE
7216
7260
  poolAddr,
7217
7261
  {
7218
7262
  kind: "inline",
7219
- value: serialiseStabilityPoolDatum({
7220
- Account: {
7221
- owner: accountDatum.owner,
7222
- iasset: stabilityPoolDatum.iasset,
7223
- state: {
7224
- ...stabilityPoolDatum.state,
7225
- depositVal: mkSPInteger(requestDepositAmt)
7226
- },
7227
- assetSums: stabilityPoolDatum.assetStates.map(([key, val]) => [
7228
- key,
7229
- val.currentSumVal
7230
- ]),
7231
- request: null,
7232
- lastRequestProcessingTime: currentTime
7233
- }
7234
- })
7263
+ value: accountOutputDatum
7235
7264
  },
7236
- addAssets9(
7237
- mkAssetsOf7(
7238
- fromSystemParamsAsset(sysParams.stabilityPoolParams.accountToken),
7239
- 1n
7240
- ),
7241
- mkLovelacesOf4(lovelacesAmt2(accountUtxo.assets))
7242
- )
7243
- );
7265
+ addAssets9(accountTokenVal, mkLovelacesOf4(accountOutputAdaAmt))
7266
+ ).setMinFee(txFee);
7244
7267
  }).with({ Adjust: P17.select() }, async (adjustContent) => {
7245
7268
  const iassetDatum = parseIAssetDatumOrThrow(
7246
7269
  getInlineDatumOrThrow(iAssetUtxo)
@@ -7364,7 +7387,7 @@ async function processSpRequest(stabilityPoolUtxo, accountUtxo, iAssetUtxo, allE
7364
7387
  accountOutputValWithoutAda,
7365
7388
  { InlineDatum: { datum: accountOutputDat } }
7366
7389
  );
7367
- const accountOutputAdaAmt = lovelacesAmt2(accountUtxo.assets) - extraOwnerOutputLovelacesAmt;
7390
+ const accountOutputAdaAmt = lovelacesAmt2(accountUtxo.assets) - extraOwnerOutputLovelacesAmt - txFee;
7368
7391
  if (accountOutputAdaAmt < accountOutMinUtxoLovelace) {
7369
7392
  throw new Error("Account doesn't have enough ADA to be processed.");
7370
7393
  }
@@ -7385,8 +7408,11 @@ async function processSpRequest(stabilityPoolUtxo, accountUtxo, iAssetUtxo, allE
7385
7408
  value: ownerOutputDat
7386
7409
  },
7387
7410
  actualOwnerOutputVal
7388
- );
7411
+ ).setMinFee(txFee);
7389
7412
  }).with({ Close: P17.select() }, async (closeContent) => {
7413
+ if (txFee > closeContent.maxTxFee) {
7414
+ throw new Error("Account doesn't allow current transaction fee.");
7415
+ }
7390
7416
  const accountTokenScriptRef = matchSingle3(
7391
7417
  await lucid.utxosByOutRef([
7392
7418
  fromSystemParamsScriptRef(
@@ -7483,14 +7509,38 @@ async function processSpRequest(stabilityPoolUtxo, accountUtxo, iAssetUtxo, allE
7483
7509
  })
7484
7510
  },
7485
7511
  addAssets9(
7486
- mkLovelacesOf4(lovelacesAmt2(accountUtxo.assets)),
7512
+ mkLovelacesOf4(lovelacesAmt2(accountUtxo.assets) - txFee),
7487
7513
  reward,
7488
7514
  mkAssetsOf7(iassetAssetClass, withdrawnAmt - withdrawalFeeAmt)
7489
7515
  )
7490
- );
7516
+ ).setMinFee(txFee);
7491
7517
  }).exhaustive();
7492
7518
  return tx;
7493
7519
  }
7520
+ async function processSpRequest(stabilityPoolUtxo, accountUtxo, iAssetUtxo, allE2s2sSnapshotOrefs, sysParams, lucid, currentSlot) {
7521
+ const draftTx = processSpRequestAuxiliary(
7522
+ stabilityPoolUtxo,
7523
+ accountUtxo,
7524
+ iAssetUtxo,
7525
+ allE2s2sSnapshotOrefs,
7526
+ sysParams,
7527
+ lucid,
7528
+ currentSlot,
7529
+ // Placeholder transation fee
7530
+ 1n
7531
+ );
7532
+ const fee = (await (await draftTx).complete()).toTransaction().body().fee();
7533
+ return processSpRequestAuxiliary(
7534
+ stabilityPoolUtxo,
7535
+ accountUtxo,
7536
+ iAssetUtxo,
7537
+ allE2s2sSnapshotOrefs,
7538
+ sysParams,
7539
+ lucid,
7540
+ currentSlot,
7541
+ fee
7542
+ );
7543
+ }
7494
7544
  async function createE2s2sSnapshots(stabilityPoolOref, sysParams, lucid) {
7495
7545
  const stabilityPoolRefScriptUtxo = matchSingle3(
7496
7546
  await lucid.utxosByOutRef([
@@ -11021,7 +11071,7 @@ function parseStableswapOrderRedeemerOrThrow(redeemerCborHex) {
11021
11071
 
11022
11072
  // src/contracts/stableswap/helpers.ts
11023
11073
  import { Data as Data52 } from "@evolution-sdk/evolution";
11024
- var BASE_MAX_EXECUTION_FEE = 1000000n;
11074
+ var BASE_MAX_EXECUTION_FEE = 1620000n;
11025
11075
  function createDestinationDatum(datum, outRef) {
11026
11076
  if (!datum) {
11027
11077
  return serialiseStableswapOutputDatum([
@@ -11364,7 +11414,7 @@ async function batchProcessStableswapOrders(stableswapOrderOrefs, stableswapPool
11364
11414
  stableswapPoolUtxo.assets,
11365
11415
  mkAssetsOf16(collateralAc, collateralAmtChangePool)
11366
11416
  ) : stableswapPoolUtxo.assets
11367
- ).setMinFee(2055720n);
11417
+ ).setMinFee(stableswapOrderOrefs.length > 1 ? 1498875n : 1030000n);
11368
11418
  if (amountToMint !== 0n) {
11369
11419
  tx.mintAssets(mkAssetsOf16(iassetAc, amountToMint), Data53.void());
11370
11420
  }
@@ -11761,6 +11811,7 @@ export {
11761
11811
  requestSpAccountClosure,
11762
11812
  requestSpAccountCreation,
11763
11813
  rewardSnapshotPrecision,
11814
+ rewardsPerAsset,
11764
11815
  robAmtToSpend,
11765
11816
  robBuyOrderFilledAssets,
11766
11817
  robBuyOrderSummary,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@indigo-labs/indigo-sdk",
3
- "version": "0.3.25",
3
+ "version": "0.3.27",
4
4
  "description": "Indigo SDK for interacting with Indigo endpoints via lucid-evolution",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -53,7 +53,7 @@ import {
53
53
  } from '../../types/system-params';
54
54
  import { mkStabilityPoolValidatorFromSP } from './scripts';
55
55
 
56
- export const BASE_MAX_TX_FEE = 1_000_000n;
56
+ export const BASE_MAX_TX_FEE = 1_250_000n;
57
57
 
58
58
  export const MAX_E2S2S_ENTRIES_COUNT = 5;
59
59
  const newScaleMultiplier = 1_000_000_000n;
@@ -437,7 +437,10 @@ function getE2s2sEntry(
437
437
  .exhaustive();
438
438
  }
439
439
 
440
- function rewardsPerAsset(
440
+ /**
441
+ * Sp account rewards per collateral asset.
442
+ */
443
+ export function rewardsPerAsset(
441
444
  poolAssetStates: readonly AssetState[],
442
445
  e2s2sIdxs: [FindE2S2SIdxResult, O.Option<FindE2S2SIdxResult>][],
443
446
  accountAssetSums: readonly (readonly [AssetClass, SPInteger])[],
@@ -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,
@@ -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());