@indigo-labs/indigo-sdk 0.4.4 → 0.5.0

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.
Files changed (49) hide show
  1. package/dist/index.d.mts +102 -40
  2. package/dist/index.d.ts +102 -40
  3. package/dist/index.js +900 -801
  4. package/dist/index.mjs +339 -256
  5. package/package.json +2 -2
  6. package/src/contracts/cdp/helpers.ts +7 -6
  7. package/src/contracts/cdp/transactions.ts +8 -29
  8. package/src/contracts/gov/transactions.ts +12 -16
  9. package/src/contracts/iasset/helpers.ts +2 -7
  10. package/src/contracts/initialize/actions.ts +0 -2
  11. package/src/contracts/initialize/helpers.ts +0 -4
  12. package/src/contracts/interest-collection/transactions.ts +2 -5
  13. package/src/contracts/interest-oracle/helpers.ts +4 -0
  14. package/src/contracts/interest-oracle/transactions.ts +1 -4
  15. package/src/contracts/price-oracle/helpers.ts +5 -11
  16. package/src/contracts/price-oracle/transactions.ts +2 -7
  17. package/src/contracts/rob/transactions.ts +0 -2
  18. package/src/contracts/rob-leverage/transactions.ts +1 -4
  19. package/src/contracts/stability-pool/transactions.ts +2 -8
  20. package/src/contracts/stableswap/helpers.ts +369 -1
  21. package/src/contracts/stableswap/transactions.ts +43 -191
  22. package/src/contracts/staking/transactions.ts +2 -7
  23. package/tests/cdp/actions.ts +0 -11
  24. package/tests/cdp/cdp-helpers.ts +2 -2
  25. package/tests/cdp/cdp-queries.ts +1 -3
  26. package/tests/cdp/cdp.test.ts +409 -620
  27. package/tests/cdp/transactions-mutated.ts +30 -25
  28. package/tests/endpoints/initialize.ts +0 -2
  29. package/tests/gov/actions.ts +7 -32
  30. package/tests/gov/gov.test.ts +169 -401
  31. package/tests/indigo-test-helpers.ts +0 -1
  32. package/tests/initialize.test.ts +15 -20
  33. package/tests/interest-collection/interest-collection.test.ts +0 -4
  34. package/tests/interest-oracle.test.ts +1 -8
  35. package/tests/price-oracle/actions.ts +6 -8
  36. package/tests/price-oracle/price-oracle.test.ts +1 -13
  37. package/tests/price-oracle/transactions-mutated.ts +1 -4
  38. package/tests/pyth/pyth-indigo.test.ts +8 -12
  39. package/tests/pyth/pyth.test.ts +1 -2
  40. package/tests/rob/actions.ts +0 -2
  41. package/tests/rob/rob-leverage.test.ts +164 -220
  42. package/tests/rob/rob.test.ts +158 -263
  43. package/tests/rob/transactions-mutated.ts +7 -18
  44. package/tests/stability-pool/actions.ts +4 -8
  45. package/tests/stability-pool.test.ts +164 -255
  46. package/tests/stableswap/stableswap-actions.ts +0 -1
  47. package/tests/stableswap/stableswap.test.ts +226 -4
  48. package/tests/staking.test.ts +2 -13
  49. package/tests/treasury/treasury.test.ts +6 -30
@@ -1,9 +1,41 @@
1
1
  import { fromHex, fromText, OutRef } from '@lucid-evolution/lucid';
2
- import { serialiseStableswapOutputDatum } from './types-new';
2
+ import { array as A, ord as Ord, function as F } from 'fp-ts';
3
+ import {
4
+ serialiseStableswapOutputDatum,
5
+ StableswapOrderDatum,
6
+ } from './types-new';
3
7
  import { Data } from '@evolution-sdk/evolution';
8
+ import { ParsedOutput } from '../../types/generic';
9
+ import { assetClassValueOf } from '@3rd-eye-labs/cardano-offchain-common';
10
+ import { SystemParams } from '../../types/system-params';
11
+ import { StableswapPoolContent } from '../cdp/types-new';
12
+ import { calculateFeeFromRatio } from '../../utils/indigo-helpers';
13
+ import {
14
+ rationalDiv,
15
+ rationalFloor,
16
+ rationalFromInt,
17
+ rationalMul,
18
+ rationalToFloat,
19
+ } from '../../types/rational';
20
+ import { BigIntOrd } from '../../utils/bigint-utils';
4
21
 
5
22
  export const BASE_MAX_EXECUTION_FEE = 1_620_000n;
6
23
 
24
+ export type OrderProcessConfig = {
25
+ /**
26
+ * Whether to ignore that an order has max execution fee set lower
27
+ * than the threshold (i.e. the processor might be paying the remaining cardano tx fee).
28
+ *
29
+ * The default should be `false`. In case, owner wants to process
30
+ * such order himself, he can ignore the constraint.
31
+ */
32
+ ignoreMaxExecutionFeeConstraint: boolean;
33
+ };
34
+
35
+ export const defaultOrderProcessingConfig: OrderProcessConfig = {
36
+ ignoreMaxExecutionFeeConstraint: false,
37
+ };
38
+
7
39
  export function createDestinationDatum(
8
40
  datum: Data.Data | null,
9
41
  outRef: OutRef,
@@ -20,3 +52,339 @@ export function createDestinationDatum(
20
52
 
21
53
  return Data.toCBORHex(datum);
22
54
  }
55
+
56
+ export type StableswapInfo = {
57
+ suppliedIasset: bigint;
58
+ suppliedCollateralAsset: bigint;
59
+ owedIasset: bigint;
60
+ owedCollateralAsset: bigint;
61
+ redemptionFee: bigint;
62
+ mintingFee: bigint;
63
+ };
64
+
65
+ export type StableswapOrderInfo = {
66
+ order: ParsedOutput<StableswapOrderDatum>;
67
+ orderType: 'MINTING' | 'REDEEMING';
68
+ swapInfo: StableswapInfo;
69
+ };
70
+
71
+ /**
72
+ * Determine the PSM order type.
73
+ */
74
+ export function psmOrderType(
75
+ order: ParsedOutput<StableswapOrderDatum>,
76
+ sysParams: SystemParams,
77
+ ):
78
+ | { type: 'MINTING'; suppliedCollateral: bigint }
79
+ | { type: 'REDEEMING'; suppliedIassets: bigint }
80
+ | { type: 'ERROR'; reason: string } {
81
+ const iassetAc = {
82
+ currencySymbol: fromHex(
83
+ sysParams.stableswapParams.iassetSymbol.unCurrencySymbol,
84
+ ),
85
+ tokenName: order.datum.iasset,
86
+ };
87
+
88
+ const suppliedIasset = assetClassValueOf(order.utxo.assets, iassetAc);
89
+ const suppliedCollateralAsset = assetClassValueOf(
90
+ order.utxo.assets,
91
+ order.datum.collateralAsset,
92
+ );
93
+
94
+ if (
95
+ (suppliedIasset != 0n && suppliedCollateralAsset != 0n) ||
96
+ (suppliedIasset == 0n && suppliedCollateralAsset == 0n)
97
+ ) {
98
+ return {
99
+ type: 'ERROR',
100
+ reason: 'An order must supply either iAsset or collateral asset',
101
+ };
102
+ }
103
+
104
+ const isMinting = suppliedCollateralAsset > 0n;
105
+
106
+ return isMinting
107
+ ? { type: 'MINTING', suppliedCollateral: suppliedCollateralAsset }
108
+ : { type: 'REDEEMING', suppliedIassets: suppliedIasset };
109
+ }
110
+
111
+ type SummaryResult =
112
+ | { _tag: 'SUCCESS'; result: StableswapOrderInfo }
113
+ | { _tag: 'ERROR'; reason: string };
114
+
115
+ /**
116
+ * Summarise the order in detail
117
+ */
118
+ export function summariseOrder(
119
+ order: ParsedOutput<StableswapOrderDatum>,
120
+ psmPool: ParsedOutput<StableswapPoolContent>,
121
+ sysParams: SystemParams,
122
+ orderProcessingConfig: OrderProcessConfig,
123
+ ): SummaryResult {
124
+ const orderType = psmOrderType(order, sysParams);
125
+
126
+ if (orderType.type === 'ERROR') {
127
+ return { _tag: 'ERROR', reason: orderType.reason };
128
+ }
129
+
130
+ const isOneToOne =
131
+ psmPool.datum.collateralToIassetRatio.numerator ===
132
+ psmPool.datum.collateralToIassetRatio.denominator;
133
+
134
+ if (
135
+ !orderProcessingConfig.ignoreMaxExecutionFeeConstraint &&
136
+ order.datum.maxExecutionFee < BASE_MAX_EXECUTION_FEE
137
+ ) {
138
+ return {
139
+ _tag: 'ERROR',
140
+ reason: `Max execution fee is too low. Minimum: ${BASE_MAX_EXECUTION_FEE}`,
141
+ };
142
+ }
143
+
144
+ if (orderType.type === 'MINTING') {
145
+ const suppliedCollateralAsset = orderType.suppliedCollateral;
146
+
147
+ if (
148
+ rationalToFloat(order.datum.maxFeeRatio) <
149
+ rationalToFloat(psmPool.datum.mintingFeeRatio)
150
+ ) {
151
+ return { _tag: 'ERROR', reason: 'Max fee ratio not satisfied' };
152
+ }
153
+
154
+ // Mint order with one to one ratio case.
155
+ if (isOneToOne) {
156
+ const fee = calculateFeeFromRatio(
157
+ psmPool.datum.mintingFeeRatio,
158
+ suppliedCollateralAsset,
159
+ );
160
+
161
+ return {
162
+ _tag: 'SUCCESS',
163
+ result: {
164
+ order: order,
165
+ orderType: 'MINTING',
166
+ swapInfo: {
167
+ suppliedCollateralAsset: suppliedCollateralAsset,
168
+ suppliedIasset: 0n,
169
+ owedCollateralAsset: 0n,
170
+ owedIasset: suppliedCollateralAsset - fee,
171
+ mintingFee: fee,
172
+ redemptionFee: 0n,
173
+ },
174
+ },
175
+ };
176
+ // Mint order with any ratio case.
177
+ } else {
178
+ const iAssetConversion = rationalFloor(
179
+ rationalDiv(
180
+ rationalFromInt(suppliedCollateralAsset),
181
+ psmPool.datum.collateralToIassetRatio,
182
+ ),
183
+ );
184
+
185
+ const attemptedNormalizedCollateral = rationalFloor(
186
+ rationalMul(
187
+ rationalFromInt(iAssetConversion),
188
+ psmPool.datum.collateralToIassetRatio,
189
+ ),
190
+ );
191
+
192
+ const normalizedCollateralSupplied =
193
+ rationalFloor(
194
+ rationalDiv(
195
+ rationalFromInt(attemptedNormalizedCollateral),
196
+ psmPool.datum.collateralToIassetRatio,
197
+ ),
198
+ ) != iAssetConversion
199
+ ? suppliedCollateralAsset
200
+ : attemptedNormalizedCollateral;
201
+
202
+ const fee = calculateFeeFromRatio(
203
+ psmPool.datum.mintingFeeRatio,
204
+ iAssetConversion,
205
+ );
206
+
207
+ return {
208
+ _tag: 'SUCCESS',
209
+ result: {
210
+ order: order,
211
+ orderType: 'MINTING',
212
+ swapInfo: {
213
+ suppliedCollateralAsset: normalizedCollateralSupplied,
214
+ suppliedIasset: 0n,
215
+ owedIasset: iAssetConversion - fee,
216
+ owedCollateralAsset: 0n,
217
+ mintingFee: fee,
218
+ redemptionFee: 0n,
219
+ },
220
+ },
221
+ };
222
+ }
223
+ // Redeem order case
224
+ } else {
225
+ const suppliedIasset = orderType.suppliedIassets;
226
+
227
+ if (
228
+ rationalToFloat(order.datum.maxFeeRatio) <
229
+ rationalToFloat(psmPool.datum.redemptionFeeRatio)
230
+ ) {
231
+ return { _tag: 'ERROR', reason: 'Max fee ratio not satisfied' };
232
+ }
233
+
234
+ const fee = calculateFeeFromRatio(
235
+ psmPool.datum.redemptionFeeRatio,
236
+ suppliedIasset,
237
+ );
238
+
239
+ const effectiveSuppliedIasset = suppliedIasset - fee;
240
+
241
+ // Redeem order with one to one ratio case
242
+ if (isOneToOne) {
243
+ return {
244
+ _tag: 'SUCCESS',
245
+ result: {
246
+ order: order,
247
+ orderType: 'REDEEMING',
248
+ swapInfo: {
249
+ suppliedIasset: effectiveSuppliedIasset,
250
+ suppliedCollateralAsset: 0n,
251
+ owedCollateralAsset: effectiveSuppliedIasset,
252
+ owedIasset: 0n,
253
+ redemptionFee: fee,
254
+ mintingFee: 0n,
255
+ },
256
+ },
257
+ };
258
+ // Redeem order with any ratio case
259
+ } else {
260
+ const collateralConversion = rationalFloor(
261
+ rationalMul(
262
+ rationalFromInt(effectiveSuppliedIasset),
263
+ psmPool.datum.collateralToIassetRatio,
264
+ ),
265
+ );
266
+
267
+ const attemptedNormalizedEffectiveIasset = rationalFloor(
268
+ rationalDiv(
269
+ rationalFromInt(collateralConversion),
270
+ psmPool.datum.collateralToIassetRatio,
271
+ ),
272
+ );
273
+
274
+ const normalizedEffectiveIasset =
275
+ rationalFloor(
276
+ rationalMul(
277
+ rationalFromInt(attemptedNormalizedEffectiveIasset),
278
+ psmPool.datum.collateralToIassetRatio,
279
+ ),
280
+ ) != collateralConversion
281
+ ? effectiveSuppliedIasset
282
+ : attemptedNormalizedEffectiveIasset;
283
+
284
+ return {
285
+ _tag: 'SUCCESS',
286
+ result: {
287
+ order: order,
288
+ orderType: 'REDEEMING',
289
+ swapInfo: {
290
+ suppliedIasset: normalizedEffectiveIasset,
291
+ suppliedCollateralAsset: 0n,
292
+ owedCollateralAsset: rationalFloor(
293
+ rationalMul(
294
+ rationalFromInt(effectiveSuppliedIasset),
295
+ psmPool.datum.collateralToIassetRatio,
296
+ ),
297
+ ),
298
+ owedIasset: 0n,
299
+ redemptionFee: fee,
300
+ mintingFee: 0n,
301
+ },
302
+ },
303
+ };
304
+ }
305
+ }
306
+ }
307
+
308
+ /**
309
+ * Pick only orders that are valid and can be processed based on available liquidity.
310
+ */
311
+ export function pickValidSatisfiableOrders(
312
+ orders: ParsedOutput<StableswapOrderDatum>[],
313
+ pool: ParsedOutput<StableswapPoolContent>,
314
+ sysParams: SystemParams,
315
+ orderProcessingConfig: OrderProcessConfig,
316
+ ): ParsedOutput<StableswapOrderDatum>[] {
317
+ const psmAvailableLiq = assetClassValueOf(
318
+ pool.utxo.assets,
319
+ pool.datum.collateralAsset,
320
+ );
321
+
322
+ const validOrders = orders.flatMap((order) => {
323
+ try {
324
+ const res = summariseOrder(order, pool, sysParams, orderProcessingConfig);
325
+ return res._tag === 'SUCCESS' ? [res.result] : [];
326
+ } catch (_) {
327
+ return [];
328
+ }
329
+ });
330
+
331
+ const { left: mintingOrders, right: redeemingOrdersSortedDesc } = F.pipe(
332
+ validOrders,
333
+ A.partition((o) => o.orderType === 'REDEEMING'),
334
+ ({ left, right }) => ({
335
+ left,
336
+ // Sort the redeeming orders descending.
337
+ right: F.pipe(
338
+ right,
339
+ A.sort(
340
+ Ord.contramap(
341
+ (o: StableswapOrderInfo) => o.swapInfo.owedCollateralAsset,
342
+ )(Ord.reverse(BigIntOrd)),
343
+ ),
344
+ ),
345
+ }),
346
+ );
347
+
348
+ const totalCollateralDiff = F.pipe(
349
+ validOrders,
350
+ A.reduce<StableswapOrderInfo, bigint>(
351
+ 0n,
352
+ (acc, order) =>
353
+ acc +
354
+ order.swapInfo.suppliedCollateralAsset -
355
+ order.swapInfo.owedCollateralAsset,
356
+ ),
357
+ );
358
+
359
+ // When there's enough liquidity for processing all the orders,
360
+ // no need for addtional filtering.
361
+ if (psmAvailableLiq + totalCollateralDiff >= 0n) {
362
+ return validOrders.map((o) => o.order);
363
+ }
364
+
365
+ const collateralFromMinting = F.pipe(
366
+ mintingOrders,
367
+ A.reduce(0n, (acc, o) => acc + o.swapInfo.suppliedCollateralAsset),
368
+ );
369
+
370
+ // Take the redeeming orders from largest until there's enough liquidity.
371
+ const { selected } = F.pipe(
372
+ redeemingOrdersSortedDesc,
373
+ A.reduce(
374
+ {
375
+ intermediateCollateralAmt: psmAvailableLiq + collateralFromMinting,
376
+ selected: [] as StableswapOrderInfo[],
377
+ },
378
+ ({ intermediateCollateralAmt, selected }, order) =>
379
+ intermediateCollateralAmt >= order.swapInfo.owedCollateralAsset
380
+ ? {
381
+ intermediateCollateralAmt:
382
+ intermediateCollateralAmt - order.swapInfo.owedCollateralAsset,
383
+ selected: [...selected, order],
384
+ }
385
+ : { intermediateCollateralAmt, selected },
386
+ ),
387
+ );
388
+
389
+ return [...mintingOrders, ...selected].map((o) => o.order);
390
+ }
@@ -11,7 +11,6 @@ import {
11
11
  sortUTxOs,
12
12
  toHex,
13
13
  TxBuilder,
14
- UTxO,
15
14
  } from '@lucid-evolution/lucid';
16
15
  import {
17
16
  fromSystemParamsScriptRef,
@@ -42,34 +41,20 @@ import {
42
41
  serialiseStableswapPoolDatum,
43
42
  StableswapPoolContent,
44
43
  } from '../cdp/types-new';
45
- import { calculateFeeFromRatio } from '../../utils/indigo-helpers';
46
44
  import { array as A, function as F } from 'fp-ts';
47
45
  import { isEmpty } from 'fp-ts/lib/Array';
48
- import { BASE_MAX_EXECUTION_FEE, createDestinationDatum } from './helpers';
46
+ import {
47
+ BASE_MAX_EXECUTION_FEE,
48
+ createDestinationDatum,
49
+ defaultOrderProcessingConfig,
50
+ OrderProcessConfig,
51
+ StableswapInfo,
52
+ StableswapOrderInfo,
53
+ summariseOrder,
54
+ } from './helpers';
49
55
  import * as Core from '@evolution-sdk/evolution';
50
56
  import { treasuryFeeTx } from '../treasury/transactions';
51
- import {
52
- Rational,
53
- rationalDiv,
54
- rationalFloor,
55
- rationalFromInt,
56
- rationalMul,
57
- } from '../../types/rational';
58
-
59
- type StableswapInfo = {
60
- suppliedCollateralAsset: bigint;
61
- suppliedIasset: bigint;
62
- owedCollateralAsset: bigint;
63
- owedIasset: bigint;
64
- mintingFee: bigint;
65
- redemptionFee: bigint;
66
- };
67
-
68
- type StableswapOrderInfo = {
69
- utxo: UTxO;
70
- datum: StableswapOrderDatum;
71
- swapInfo: StableswapInfo;
72
- };
57
+ import { Rational } from '../../types/rational';
73
58
 
74
59
  export async function createStableswapOrder(
75
60
  iasset: string,
@@ -188,6 +173,7 @@ export async function batchProcessStableswapOrders(
188
173
  treasuryOref: OutRef,
189
174
  sysParams: SystemParams,
190
175
  lucid: LucidEvolution,
176
+ orderProcessingConfig: OrderProcessConfig = defaultOrderProcessingConfig,
191
177
  ): Promise<TxBuilder> {
192
178
  const stableswapScriptRefUtxo = matchSingle(
193
179
  await lucid.utxosByOutRef([
@@ -269,159 +255,18 @@ export async function batchProcessStableswapOrders(
269
255
  throw new Error('Wrong batch of orders');
270
256
  }
271
257
 
272
- const suppliedIasset = assetClassValueOf(orderUtxo.assets, iassetAc);
273
- const suppliedCollateralAsset = assetClassValueOf(
274
- orderUtxo.assets,
275
- collateralAc,
258
+ const res = summariseOrder(
259
+ { utxo: orderUtxo, datum: orderDatum },
260
+ { utxo: stableswapPoolUtxo, datum: stableswapPoolDatum },
261
+ sysParams,
262
+ orderProcessingConfig,
276
263
  );
277
264
 
278
- if (
279
- (suppliedIasset != 0n && suppliedCollateralAsset != 0n) ||
280
- (suppliedIasset == 0n && suppliedCollateralAsset == 0n)
281
- ) {
282
- throw new Error(
283
- 'An order must supply either iAsset or collateral asset',
284
- );
265
+ if (res._tag === 'ERROR') {
266
+ throw new Error(res.reason);
285
267
  }
286
268
 
287
- const isMinting = suppliedCollateralAsset > 0n;
288
-
289
- const isOneToOne =
290
- stableswapPoolDatum.collateralToIassetRatio.numerator ===
291
- stableswapPoolDatum.collateralToIassetRatio.denominator;
292
-
293
- if (isMinting) {
294
- // Mint order with one to one ratio case.
295
- if (isOneToOne) {
296
- const fee = calculateFeeFromRatio(
297
- stableswapPoolDatum.mintingFeeRatio,
298
- suppliedCollateralAsset,
299
- );
300
-
301
- return {
302
- utxo: orderUtxo,
303
- datum: orderDatum,
304
- swapInfo: {
305
- suppliedCollateralAsset: suppliedCollateralAsset,
306
- suppliedIasset: 0n,
307
- owedCollateralAsset: 0n,
308
- owedIasset: suppliedCollateralAsset - fee,
309
- mintingFee: fee,
310
- redemptionFee: 0n,
311
- },
312
- };
313
- // Mint order with any ratio case.
314
- } else {
315
- const iAssetConversion = rationalFloor(
316
- rationalDiv(
317
- rationalFromInt(suppliedCollateralAsset),
318
- stableswapPoolDatum.collateralToIassetRatio,
319
- ),
320
- );
321
-
322
- const attemptedNormalizedCollateral = rationalFloor(
323
- rationalMul(
324
- rationalFromInt(iAssetConversion),
325
- stableswapPoolDatum.collateralToIassetRatio,
326
- ),
327
- );
328
-
329
- const normalizedCollateralSupplied =
330
- rationalFloor(
331
- rationalDiv(
332
- rationalFromInt(attemptedNormalizedCollateral),
333
- stableswapPoolDatum.collateralToIassetRatio,
334
- ),
335
- ) != iAssetConversion
336
- ? suppliedCollateralAsset
337
- : attemptedNormalizedCollateral;
338
-
339
- const fee = calculateFeeFromRatio(
340
- stableswapPoolDatum.mintingFeeRatio,
341
- iAssetConversion,
342
- );
343
-
344
- return {
345
- utxo: orderUtxo,
346
- datum: orderDatum,
347
- swapInfo: {
348
- suppliedCollateralAsset: normalizedCollateralSupplied,
349
- suppliedIasset: 0n,
350
- owedCollateralAsset: 0n,
351
- owedIasset: iAssetConversion - fee,
352
- mintingFee: fee,
353
- redemptionFee: 0n,
354
- },
355
- };
356
- }
357
- // Redeem order case
358
- } else {
359
- const fee = calculateFeeFromRatio(
360
- stableswapPoolDatum.redemptionFeeRatio,
361
- suppliedIasset,
362
- );
363
-
364
- const effectiveSuppliedIasset = suppliedIasset - fee;
365
-
366
- // Redeem order with one to one ratio case
367
- if (isOneToOne) {
368
- return {
369
- utxo: orderUtxo,
370
- datum: orderDatum,
371
- swapInfo: {
372
- suppliedCollateralAsset: 0n,
373
- suppliedIasset: effectiveSuppliedIasset,
374
- owedCollateralAsset: effectiveSuppliedIasset,
375
- owedIasset: 0n,
376
- mintingFee: 0n,
377
- redemptionFee: fee,
378
- },
379
- };
380
- // Redeem order with any ratio case
381
- } else {
382
- const collateralConversion = rationalFloor(
383
- rationalMul(
384
- rationalFromInt(effectiveSuppliedIasset),
385
- stableswapPoolDatum.collateralToIassetRatio,
386
- ),
387
- );
388
-
389
- const attemptedNormalizedEffectiveIasset = rationalFloor(
390
- rationalDiv(
391
- rationalFromInt(collateralConversion),
392
- stableswapPoolDatum.collateralToIassetRatio,
393
- ),
394
- );
395
-
396
- const normalizedEffectiveIasset =
397
- rationalFloor(
398
- rationalMul(
399
- rationalFromInt(attemptedNormalizedEffectiveIasset),
400
- stableswapPoolDatum.collateralToIassetRatio,
401
- ),
402
- ) != collateralConversion
403
- ? effectiveSuppliedIasset
404
- : attemptedNormalizedEffectiveIasset;
405
-
406
- return {
407
- utxo: orderUtxo,
408
- datum: orderDatum,
409
- swapInfo: {
410
- suppliedCollateralAsset: 0n,
411
- suppliedIasset: normalizedEffectiveIasset,
412
- owedCollateralAsset: rationalFloor(
413
- rationalMul(
414
- rationalFromInt(effectiveSuppliedIasset),
415
- stableswapPoolDatum.collateralToIassetRatio,
416
- ),
417
- ),
418
- owedIasset: 0n,
419
- mintingFee: 0n,
420
- redemptionFee: fee,
421
- },
422
- };
423
- }
424
- }
269
+ return res.result;
425
270
  },
426
271
  );
427
272
 
@@ -448,7 +293,7 @@ export async function batchProcessStableswapOrders(
448
293
  owedIasset: acc.owedIasset + orderInfo.swapInfo.owedIasset,
449
294
  mintingFee: acc.mintingFee + orderInfo.swapInfo.mintingFee,
450
295
  redemptionFee: acc.redemptionFee + orderInfo.swapInfo.redemptionFee,
451
- };
296
+ } satisfies StableswapInfo;
452
297
  },
453
298
  ),
454
299
  );
@@ -456,6 +301,15 @@ export async function batchProcessStableswapOrders(
456
301
  const collateralAmtChangePool =
457
302
  totalSwapInfo.suppliedCollateralAsset - totalSwapInfo.owedCollateralAsset;
458
303
 
304
+ const newPsmPoolValue = addAssets(
305
+ stableswapPoolUtxo.assets,
306
+ mkAssetsOf(collateralAc, collateralAmtChangePool),
307
+ );
308
+
309
+ if (assetClassValueOf(newPsmPoolValue, collateralAc) < 0n) {
310
+ throw new Error('Cannot batch more than liquidity allows');
311
+ }
312
+
459
313
  const amountToMint =
460
314
  totalSwapInfo.owedIasset -
461
315
  totalSwapInfo.suppliedIasset +
@@ -486,12 +340,7 @@ export async function batchProcessStableswapOrders(
486
340
  kind: 'inline',
487
341
  value: serialiseStableswapPoolDatum(stableswapPoolDatum),
488
342
  },
489
- collateralAmtChangePool != 0n
490
- ? addAssets(
491
- stableswapPoolUtxo.assets,
492
- mkAssetsOf(collateralAc, collateralAmtChangePool),
493
- )
494
- : stableswapPoolUtxo.assets,
343
+ newPsmPoolValue,
495
344
  )
496
345
  // This has to be added as otherwise there is the following error:
497
346
  // TxBuilderError: { Complete: RedeemerBuilder: Coin selection had to be updated
@@ -509,8 +358,8 @@ export async function batchProcessStableswapOrders(
509
358
  A.reduce<StableswapOrderInfo, TxBuilder>(tx, (acc, orderInfo) => {
510
359
  return acc
511
360
  .collectFrom(
512
- [orderInfo.utxo],
513
- isSameOutRef(orderInfo.utxo, mainOrderUtxo)
361
+ [orderInfo.order.utxo],
362
+ isSameOutRef(orderInfo.order.utxo, mainOrderUtxo)
514
363
  ? serialiseStableswapOrderRedeemer('BatchProcessStableswapOrders')
515
364
  : {
516
365
  kind: 'selected',
@@ -522,26 +371,29 @@ export async function batchProcessStableswapOrders(
522
371
  },
523
372
  });
524
373
  },
525
- inputs: [orderInfo.utxo, mainOrderUtxo],
374
+ inputs: [orderInfo.order.utxo, mainOrderUtxo],
526
375
  },
527
376
  )
528
377
  .pay.ToAddressWithData(
529
- addressToBech32(orderInfo.datum.destination, lucid.config().network!),
378
+ addressToBech32(
379
+ orderInfo.order.datum.destination,
380
+ lucid.config().network!,
381
+ ),
530
382
  {
531
383
  kind: 'inline',
532
384
  value: createDestinationDatum(
533
- orderInfo.datum.destinationInlineDatum ?? null,
534
- orderInfo.utxo,
385
+ orderInfo.order.datum.destinationInlineDatum ?? null,
386
+ orderInfo.order.utxo,
535
387
  ),
536
388
  },
537
389
  addAssets(
538
- // Currently, we always take the max execution fee from the order utxo.
390
+ // NOTICE: Currently, we always take the max execution fee from the order utxo.
539
391
  // This can be improved so that we take the actual execution fee.
540
392
  mkLovelacesOf(
541
- lovelacesAmt(orderInfo.utxo.assets) -
542
- orderInfo.datum.maxExecutionFee,
393
+ lovelacesAmt(orderInfo.order.utxo.assets) -
394
+ orderInfo.order.datum.maxExecutionFee,
543
395
  ),
544
- orderInfo.swapInfo.owedIasset > 0
396
+ orderInfo.orderType === 'MINTING'
545
397
  ? mkAssetsOf(iassetAc, orderInfo.swapInfo.owedIasset)
546
398
  : mkAssetsOf(
547
399
  collateralAc,