@indigo-labs/indigo-sdk 0.4.4 → 0.5.1
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.d.mts +121 -40
- package/dist/index.d.ts +121 -40
- package/dist/index.js +962 -818
- package/dist/index.mjs +418 -282
- package/package.json +2 -2
- package/scripts/run-repeat.sh +22 -0
- package/src/contracts/cdp/helpers.ts +7 -6
- package/src/contracts/cdp/transactions.ts +8 -29
- package/src/contracts/gov/transactions.ts +12 -16
- package/src/contracts/iasset/helpers.ts +2 -7
- package/src/contracts/initialize/actions.ts +0 -2
- package/src/contracts/initialize/helpers.ts +0 -4
- package/src/contracts/interest-collection/transactions.ts +2 -5
- package/src/contracts/interest-oracle/helpers.ts +4 -0
- package/src/contracts/interest-oracle/transactions.ts +1 -4
- package/src/contracts/price-oracle/helpers.ts +5 -11
- package/src/contracts/price-oracle/transactions.ts +2 -7
- package/src/contracts/rob/transactions.ts +0 -2
- package/src/contracts/rob-leverage/transactions.ts +1 -4
- package/src/contracts/stability-pool/transactions.ts +2 -8
- package/src/contracts/stableswap/helpers.ts +487 -2
- package/src/contracts/stableswap/transactions.ts +53 -222
- package/src/contracts/staking/transactions.ts +2 -7
- package/tests/cdp/actions.ts +0 -11
- package/tests/cdp/cdp-helpers.ts +2 -2
- package/tests/cdp/cdp-queries.ts +1 -3
- package/tests/cdp/cdp.test.ts +407 -619
- package/tests/cdp/transactions-mutated.ts +30 -25
- package/tests/endpoints/initialize.ts +0 -2
- package/tests/gov/actions.ts +7 -32
- package/tests/gov/gov.test.ts +169 -401
- package/tests/indigo-test-helpers.ts +2 -3
- package/tests/initialize.test.ts +15 -20
- package/tests/interest-collection/interest-collection.test.ts +0 -4
- package/tests/interest-oracle.test.ts +1 -8
- package/tests/price-oracle/actions.ts +6 -8
- package/tests/price-oracle/price-oracle.test.ts +1 -13
- package/tests/price-oracle/transactions-mutated.ts +1 -4
- package/tests/pyth/pyth-indigo.test.ts +8 -12
- package/tests/pyth/pyth.test.ts +1 -2
- package/tests/rob/actions.ts +0 -2
- package/tests/rob/rob-leverage.test.ts +164 -220
- package/tests/rob/rob.test.ts +158 -263
- package/tests/rob/transactions-mutated.ts +7 -18
- package/tests/stability-pool/actions.ts +4 -8
- package/tests/stability-pool.test.ts +164 -255
- package/tests/stableswap/stableswap-actions.ts +0 -1
- package/tests/stableswap/stableswap-queries.ts +26 -17
- package/tests/stableswap/stableswap.test.ts +338 -4
- package/tests/staking.test.ts +2 -13
- package/tests/treasury/treasury.test.ts +6 -30
|
@@ -1,9 +1,60 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
Assets,
|
|
3
|
+
fromHex,
|
|
4
|
+
fromText,
|
|
5
|
+
LucidEvolution,
|
|
6
|
+
OutRef,
|
|
7
|
+
toHex,
|
|
8
|
+
} from '@lucid-evolution/lucid';
|
|
9
|
+
import { array as A, ord as Ord, function as F } from 'fp-ts';
|
|
10
|
+
import {
|
|
11
|
+
serialiseStableswapOutputDatum,
|
|
12
|
+
StableswapOrderDatum,
|
|
13
|
+
} from './types-new';
|
|
3
14
|
import { Data } from '@evolution-sdk/evolution';
|
|
15
|
+
import { ParsedOutput } from '../../types/generic';
|
|
16
|
+
import {
|
|
17
|
+
addressToBech32,
|
|
18
|
+
assetClassValueOf,
|
|
19
|
+
lovelacesAmt,
|
|
20
|
+
} from '@3rd-eye-labs/cardano-offchain-common';
|
|
21
|
+
import { SystemParams } from '../../types/system-params';
|
|
22
|
+
import { StableswapPoolContent } from '../cdp/types-new';
|
|
23
|
+
import { calculateFeeFromRatio } from '../../utils/indigo-helpers';
|
|
24
|
+
import {
|
|
25
|
+
rationalDiv,
|
|
26
|
+
rationalFloor,
|
|
27
|
+
rationalFromInt,
|
|
28
|
+
rationalMul,
|
|
29
|
+
rationalToFloat,
|
|
30
|
+
} from '../../types/rational';
|
|
31
|
+
import { BigIntOrd } from '../../utils/bigint-utils';
|
|
32
|
+
import { estimateUtxoMinLovelace } from '../../utils/lucid-utils';
|
|
4
33
|
|
|
5
34
|
export const BASE_MAX_EXECUTION_FEE = 1_620_000n;
|
|
6
35
|
|
|
36
|
+
export type PSMProcessingConfig = {
|
|
37
|
+
/**
|
|
38
|
+
* Whether to enforce that an order has max execution fee satisfying
|
|
39
|
+
* min threshold (i.e. the processor might be paying the remaining cardano tx fee).
|
|
40
|
+
*
|
|
41
|
+
* In case, owner wants to process such order himself, he can ignore the constraint.
|
|
42
|
+
* @default true
|
|
43
|
+
*/
|
|
44
|
+
enforceMaxExecutionFeeConstraint?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Whether to enforce the minimum lovelace constraint. If not enforced,
|
|
47
|
+
* the processor might need to pay the min lovelace for the order output.
|
|
48
|
+
* @default true
|
|
49
|
+
*/
|
|
50
|
+
enforceMinLovelaceConstraint?: boolean;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const defaultPSMProcessingConfig: PSMProcessingConfig = {
|
|
54
|
+
enforceMaxExecutionFeeConstraint: true,
|
|
55
|
+
enforceMinLovelaceConstraint: true,
|
|
56
|
+
};
|
|
57
|
+
|
|
7
58
|
export function createDestinationDatum(
|
|
8
59
|
datum: Data.Data | null,
|
|
9
60
|
outRef: OutRef,
|
|
@@ -20,3 +71,437 @@ export function createDestinationDatum(
|
|
|
20
71
|
|
|
21
72
|
return Data.toCBORHex(datum);
|
|
22
73
|
}
|
|
74
|
+
|
|
75
|
+
export type StableswapInfo = {
|
|
76
|
+
suppliedIasset: bigint;
|
|
77
|
+
suppliedCollateralAsset: bigint;
|
|
78
|
+
owedIasset: bigint;
|
|
79
|
+
owedCollateralAsset: bigint;
|
|
80
|
+
redemptionFee: bigint;
|
|
81
|
+
mintingFee: bigint;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export type StableswapOrderInfo = {
|
|
85
|
+
order: ParsedOutput<StableswapOrderDatum>;
|
|
86
|
+
orderType: 'MINTING' | 'REDEEMING';
|
|
87
|
+
swapInfo: StableswapInfo;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* This is an approximation of the amount of lovelace that will be needed to pay for the output.
|
|
92
|
+
*/
|
|
93
|
+
export function estimateMinOrderOutputLovelaces(
|
|
94
|
+
lucid: LucidEvolution,
|
|
95
|
+
orderDatum: StableswapOrderDatum,
|
|
96
|
+
/**
|
|
97
|
+
* Including execution fee as well.
|
|
98
|
+
*/
|
|
99
|
+
orderAssets: Assets,
|
|
100
|
+
): bigint {
|
|
101
|
+
const expectedOutputLovelaces = estimateUtxoMinLovelace(
|
|
102
|
+
lucid.config().protocolParameters!,
|
|
103
|
+
addressToBech32(orderDatum.destination, lucid.config().network!),
|
|
104
|
+
orderAssets,
|
|
105
|
+
{
|
|
106
|
+
kind: 'inline',
|
|
107
|
+
value: createDestinationDatum(orderDatum.destinationInlineDatum ?? null, {
|
|
108
|
+
txHash:
|
|
109
|
+
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
110
|
+
outputIndex: 0,
|
|
111
|
+
}),
|
|
112
|
+
},
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const roundedExpectedOutputLovelaces =
|
|
116
|
+
(expectedOutputLovelaces / 1_000_000n + 1n) * 1_000_000n;
|
|
117
|
+
|
|
118
|
+
return roundedExpectedOutputLovelaces;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
type OrderTypeError = 'VALUE_ERROR';
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Determine the PSM order type.
|
|
125
|
+
*/
|
|
126
|
+
export function psmOrderType(
|
|
127
|
+
order: ParsedOutput<StableswapOrderDatum>,
|
|
128
|
+
sysParams: SystemParams,
|
|
129
|
+
):
|
|
130
|
+
| { type: 'MINTING'; suppliedCollateral: bigint }
|
|
131
|
+
| { type: 'REDEEMING'; suppliedIassets: bigint }
|
|
132
|
+
| { type: 'ERROR'; errorTag: OrderTypeError; reason: string } {
|
|
133
|
+
const iassetAc = {
|
|
134
|
+
currencySymbol: fromHex(
|
|
135
|
+
sysParams.stableswapParams.iassetSymbol.unCurrencySymbol,
|
|
136
|
+
),
|
|
137
|
+
tokenName: order.datum.iasset,
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const suppliedIasset = assetClassValueOf(order.utxo.assets, iassetAc);
|
|
141
|
+
const suppliedCollateralAsset = assetClassValueOf(
|
|
142
|
+
order.utxo.assets,
|
|
143
|
+
order.datum.collateralAsset,
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
if (
|
|
147
|
+
(suppliedIasset != 0n && suppliedCollateralAsset != 0n) ||
|
|
148
|
+
(suppliedIasset == 0n && suppliedCollateralAsset == 0n)
|
|
149
|
+
) {
|
|
150
|
+
return {
|
|
151
|
+
type: 'ERROR',
|
|
152
|
+
errorTag: 'VALUE_ERROR',
|
|
153
|
+
reason: 'An order must supply either iAsset or collateral asset',
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const isMinting = suppliedCollateralAsset > 0n;
|
|
158
|
+
|
|
159
|
+
return isMinting
|
|
160
|
+
? { type: 'MINTING', suppliedCollateral: suppliedCollateralAsset }
|
|
161
|
+
: { type: 'REDEEMING', suppliedIassets: suppliedIasset };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
type SummaryError =
|
|
165
|
+
| OrderTypeError
|
|
166
|
+
| 'ASSET_MISMATCH'
|
|
167
|
+
| 'EXECUTION_FEE'
|
|
168
|
+
| 'MAX_FEE'
|
|
169
|
+
| 'INSUFFICIENT_LOVELACES';
|
|
170
|
+
|
|
171
|
+
type SummaryResult =
|
|
172
|
+
| { _tag: 'SUCCESS'; result: StableswapOrderInfo }
|
|
173
|
+
| { _tag: 'ERROR'; errorTag: SummaryError; description: string };
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Summarise the order. Validate all the necessary constraints
|
|
177
|
+
* for this order to be processed.
|
|
178
|
+
*/
|
|
179
|
+
export function summariseOrder(
|
|
180
|
+
lucid: LucidEvolution,
|
|
181
|
+
order: ParsedOutput<StableswapOrderDatum>,
|
|
182
|
+
psmPool: ParsedOutput<StableswapPoolContent>,
|
|
183
|
+
sysParams: SystemParams,
|
|
184
|
+
psmProcessingConfig?: PSMProcessingConfig,
|
|
185
|
+
): SummaryResult {
|
|
186
|
+
const config = {
|
|
187
|
+
...defaultPSMProcessingConfig,
|
|
188
|
+
...psmProcessingConfig,
|
|
189
|
+
} satisfies PSMProcessingConfig;
|
|
190
|
+
|
|
191
|
+
const orderType = psmOrderType(order, sysParams);
|
|
192
|
+
|
|
193
|
+
if (orderType.type === 'ERROR') {
|
|
194
|
+
return {
|
|
195
|
+
_tag: 'ERROR',
|
|
196
|
+
errorTag: orderType.errorTag,
|
|
197
|
+
description: orderType.reason,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (
|
|
202
|
+
toHex(order.datum.iasset) !== toHex(psmPool.datum.iasset) ||
|
|
203
|
+
toHex(order.datum.collateralAsset.currencySymbol) !==
|
|
204
|
+
toHex(psmPool.datum.collateralAsset.currencySymbol) ||
|
|
205
|
+
toHex(order.datum.collateralAsset.tokenName) !==
|
|
206
|
+
toHex(psmPool.datum.collateralAsset.tokenName)
|
|
207
|
+
) {
|
|
208
|
+
return {
|
|
209
|
+
_tag: 'ERROR',
|
|
210
|
+
errorTag: 'ASSET_MISMATCH',
|
|
211
|
+
description: 'Order - PSM assets mismatch',
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const isOneToOne =
|
|
216
|
+
psmPool.datum.collateralToIassetRatio.numerator ===
|
|
217
|
+
psmPool.datum.collateralToIassetRatio.denominator;
|
|
218
|
+
|
|
219
|
+
// TODO: check that the order has enough funds.
|
|
220
|
+
if (
|
|
221
|
+
config.enforceMaxExecutionFeeConstraint &&
|
|
222
|
+
order.datum.maxExecutionFee < BASE_MAX_EXECUTION_FEE
|
|
223
|
+
) {
|
|
224
|
+
return {
|
|
225
|
+
_tag: 'ERROR',
|
|
226
|
+
errorTag: 'EXECUTION_FEE',
|
|
227
|
+
description: `Max execution fee is too low. Minimum: ${BASE_MAX_EXECUTION_FEE}`,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (
|
|
232
|
+
config.enforceMinLovelaceConstraint &&
|
|
233
|
+
lovelacesAmt(order.utxo.assets) <
|
|
234
|
+
estimateMinOrderOutputLovelaces(lucid, order.datum, order.utxo.assets) +
|
|
235
|
+
order.datum.maxExecutionFee
|
|
236
|
+
) {
|
|
237
|
+
return {
|
|
238
|
+
_tag: 'ERROR',
|
|
239
|
+
errorTag: 'INSUFFICIENT_LOVELACES',
|
|
240
|
+
description:
|
|
241
|
+
'Order has insufficient lovelaces to cover execution fee and order output.',
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (orderType.type === 'MINTING') {
|
|
246
|
+
const suppliedCollateralAsset = orderType.suppliedCollateral;
|
|
247
|
+
|
|
248
|
+
if (
|
|
249
|
+
rationalToFloat(order.datum.maxFeeRatio) <
|
|
250
|
+
rationalToFloat(psmPool.datum.mintingFeeRatio)
|
|
251
|
+
) {
|
|
252
|
+
return {
|
|
253
|
+
_tag: 'ERROR',
|
|
254
|
+
errorTag: 'MAX_FEE',
|
|
255
|
+
description: 'Max fee ratio not satisfied',
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// Mint order with one to one ratio case.
|
|
260
|
+
if (isOneToOne) {
|
|
261
|
+
const fee = calculateFeeFromRatio(
|
|
262
|
+
psmPool.datum.mintingFeeRatio,
|
|
263
|
+
suppliedCollateralAsset,
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
return {
|
|
267
|
+
_tag: 'SUCCESS',
|
|
268
|
+
result: {
|
|
269
|
+
order: order,
|
|
270
|
+
orderType: 'MINTING',
|
|
271
|
+
swapInfo: {
|
|
272
|
+
suppliedCollateralAsset: suppliedCollateralAsset,
|
|
273
|
+
suppliedIasset: 0n,
|
|
274
|
+
owedCollateralAsset: 0n,
|
|
275
|
+
owedIasset: suppliedCollateralAsset - fee,
|
|
276
|
+
mintingFee: fee,
|
|
277
|
+
redemptionFee: 0n,
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
};
|
|
281
|
+
// Mint order with any ratio case.
|
|
282
|
+
} else {
|
|
283
|
+
const iAssetConversion = rationalFloor(
|
|
284
|
+
rationalDiv(
|
|
285
|
+
rationalFromInt(suppliedCollateralAsset),
|
|
286
|
+
psmPool.datum.collateralToIassetRatio,
|
|
287
|
+
),
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
const attemptedNormalizedCollateral = rationalFloor(
|
|
291
|
+
rationalMul(
|
|
292
|
+
rationalFromInt(iAssetConversion),
|
|
293
|
+
psmPool.datum.collateralToIassetRatio,
|
|
294
|
+
),
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
const normalizedCollateralSupplied =
|
|
298
|
+
rationalFloor(
|
|
299
|
+
rationalDiv(
|
|
300
|
+
rationalFromInt(attemptedNormalizedCollateral),
|
|
301
|
+
psmPool.datum.collateralToIassetRatio,
|
|
302
|
+
),
|
|
303
|
+
) != iAssetConversion
|
|
304
|
+
? suppliedCollateralAsset
|
|
305
|
+
: attemptedNormalizedCollateral;
|
|
306
|
+
|
|
307
|
+
const fee = calculateFeeFromRatio(
|
|
308
|
+
psmPool.datum.mintingFeeRatio,
|
|
309
|
+
iAssetConversion,
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
return {
|
|
313
|
+
_tag: 'SUCCESS',
|
|
314
|
+
result: {
|
|
315
|
+
order: order,
|
|
316
|
+
orderType: 'MINTING',
|
|
317
|
+
swapInfo: {
|
|
318
|
+
suppliedCollateralAsset: normalizedCollateralSupplied,
|
|
319
|
+
suppliedIasset: 0n,
|
|
320
|
+
owedIasset: iAssetConversion - fee,
|
|
321
|
+
owedCollateralAsset: 0n,
|
|
322
|
+
mintingFee: fee,
|
|
323
|
+
redemptionFee: 0n,
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
// Redeem order case
|
|
329
|
+
} else {
|
|
330
|
+
const suppliedIasset = orderType.suppliedIassets;
|
|
331
|
+
|
|
332
|
+
if (
|
|
333
|
+
rationalToFloat(order.datum.maxFeeRatio) <
|
|
334
|
+
rationalToFloat(psmPool.datum.redemptionFeeRatio)
|
|
335
|
+
) {
|
|
336
|
+
return {
|
|
337
|
+
_tag: 'ERROR',
|
|
338
|
+
errorTag: 'MAX_FEE',
|
|
339
|
+
description: 'Max fee ratio not satisfied',
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const fee = calculateFeeFromRatio(
|
|
344
|
+
psmPool.datum.redemptionFeeRatio,
|
|
345
|
+
suppliedIasset,
|
|
346
|
+
);
|
|
347
|
+
|
|
348
|
+
const effectiveSuppliedIasset = suppliedIasset - fee;
|
|
349
|
+
|
|
350
|
+
// Redeem order with one to one ratio case
|
|
351
|
+
if (isOneToOne) {
|
|
352
|
+
return {
|
|
353
|
+
_tag: 'SUCCESS',
|
|
354
|
+
result: {
|
|
355
|
+
order: order,
|
|
356
|
+
orderType: 'REDEEMING',
|
|
357
|
+
swapInfo: {
|
|
358
|
+
suppliedIasset: effectiveSuppliedIasset,
|
|
359
|
+
suppliedCollateralAsset: 0n,
|
|
360
|
+
owedCollateralAsset: effectiveSuppliedIasset,
|
|
361
|
+
owedIasset: 0n,
|
|
362
|
+
redemptionFee: fee,
|
|
363
|
+
mintingFee: 0n,
|
|
364
|
+
},
|
|
365
|
+
},
|
|
366
|
+
};
|
|
367
|
+
// Redeem order with any ratio case
|
|
368
|
+
} else {
|
|
369
|
+
const collateralConversion = rationalFloor(
|
|
370
|
+
rationalMul(
|
|
371
|
+
rationalFromInt(effectiveSuppliedIasset),
|
|
372
|
+
psmPool.datum.collateralToIassetRatio,
|
|
373
|
+
),
|
|
374
|
+
);
|
|
375
|
+
|
|
376
|
+
const attemptedNormalizedEffectiveIasset = rationalFloor(
|
|
377
|
+
rationalDiv(
|
|
378
|
+
rationalFromInt(collateralConversion),
|
|
379
|
+
psmPool.datum.collateralToIassetRatio,
|
|
380
|
+
),
|
|
381
|
+
);
|
|
382
|
+
|
|
383
|
+
const normalizedEffectiveIasset =
|
|
384
|
+
rationalFloor(
|
|
385
|
+
rationalMul(
|
|
386
|
+
rationalFromInt(attemptedNormalizedEffectiveIasset),
|
|
387
|
+
psmPool.datum.collateralToIassetRatio,
|
|
388
|
+
),
|
|
389
|
+
) != collateralConversion
|
|
390
|
+
? effectiveSuppliedIasset
|
|
391
|
+
: attemptedNormalizedEffectiveIasset;
|
|
392
|
+
|
|
393
|
+
return {
|
|
394
|
+
_tag: 'SUCCESS',
|
|
395
|
+
result: {
|
|
396
|
+
order: order,
|
|
397
|
+
orderType: 'REDEEMING',
|
|
398
|
+
swapInfo: {
|
|
399
|
+
suppliedIasset: normalizedEffectiveIasset,
|
|
400
|
+
suppliedCollateralAsset: 0n,
|
|
401
|
+
owedCollateralAsset: rationalFloor(
|
|
402
|
+
rationalMul(
|
|
403
|
+
rationalFromInt(effectiveSuppliedIasset),
|
|
404
|
+
psmPool.datum.collateralToIassetRatio,
|
|
405
|
+
),
|
|
406
|
+
),
|
|
407
|
+
owedIasset: 0n,
|
|
408
|
+
redemptionFee: fee,
|
|
409
|
+
mintingFee: 0n,
|
|
410
|
+
},
|
|
411
|
+
},
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Pick only orders that are valid, belong to the PSM pool,
|
|
419
|
+
* and can be processed based on available liquidity.
|
|
420
|
+
*/
|
|
421
|
+
export function pickValidSatisfiableOrders(
|
|
422
|
+
lucid: LucidEvolution,
|
|
423
|
+
orders: ParsedOutput<StableswapOrderDatum>[],
|
|
424
|
+
pool: ParsedOutput<StableswapPoolContent>,
|
|
425
|
+
sysParams: SystemParams,
|
|
426
|
+
psmProcessingConfig?: PSMProcessingConfig,
|
|
427
|
+
): ParsedOutput<StableswapOrderDatum>[] {
|
|
428
|
+
const psmAvailableLiq = assetClassValueOf(
|
|
429
|
+
pool.utxo.assets,
|
|
430
|
+
pool.datum.collateralAsset,
|
|
431
|
+
);
|
|
432
|
+
|
|
433
|
+
const validOrders = orders.flatMap((order) => {
|
|
434
|
+
try {
|
|
435
|
+
const res = summariseOrder(
|
|
436
|
+
lucid,
|
|
437
|
+
order,
|
|
438
|
+
pool,
|
|
439
|
+
sysParams,
|
|
440
|
+
psmProcessingConfig,
|
|
441
|
+
);
|
|
442
|
+
return res._tag === 'SUCCESS' ? [res.result] : [];
|
|
443
|
+
} catch (_) {
|
|
444
|
+
return [];
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
const { left: mintingOrders, right: redeemingOrdersSortedDesc } = F.pipe(
|
|
449
|
+
validOrders,
|
|
450
|
+
A.partition((o) => o.orderType === 'REDEEMING'),
|
|
451
|
+
({ left, right }) => ({
|
|
452
|
+
left,
|
|
453
|
+
// Sort the redeeming orders descending.
|
|
454
|
+
right: F.pipe(
|
|
455
|
+
right,
|
|
456
|
+
A.sort(
|
|
457
|
+
Ord.contramap(
|
|
458
|
+
(o: StableswapOrderInfo) => o.swapInfo.owedCollateralAsset,
|
|
459
|
+
)(Ord.reverse(BigIntOrd)),
|
|
460
|
+
),
|
|
461
|
+
),
|
|
462
|
+
}),
|
|
463
|
+
);
|
|
464
|
+
|
|
465
|
+
const totalCollateralDiff = F.pipe(
|
|
466
|
+
validOrders,
|
|
467
|
+
A.reduce<StableswapOrderInfo, bigint>(
|
|
468
|
+
0n,
|
|
469
|
+
(acc, order) =>
|
|
470
|
+
acc +
|
|
471
|
+
order.swapInfo.suppliedCollateralAsset -
|
|
472
|
+
order.swapInfo.owedCollateralAsset,
|
|
473
|
+
),
|
|
474
|
+
);
|
|
475
|
+
|
|
476
|
+
// When there's enough liquidity for processing all the orders,
|
|
477
|
+
// no need for addtional filtering.
|
|
478
|
+
if (psmAvailableLiq + totalCollateralDiff >= 0n) {
|
|
479
|
+
return validOrders.map((o) => o.order);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const collateralFromMinting = F.pipe(
|
|
483
|
+
mintingOrders,
|
|
484
|
+
A.reduce(0n, (acc, o) => acc + o.swapInfo.suppliedCollateralAsset),
|
|
485
|
+
);
|
|
486
|
+
|
|
487
|
+
// Take the redeeming orders from largest until there's enough liquidity.
|
|
488
|
+
const { selected } = F.pipe(
|
|
489
|
+
redeemingOrdersSortedDesc,
|
|
490
|
+
A.reduce(
|
|
491
|
+
{
|
|
492
|
+
intermediateCollateralAmt: psmAvailableLiq + collateralFromMinting,
|
|
493
|
+
selected: [] as StableswapOrderInfo[],
|
|
494
|
+
},
|
|
495
|
+
({ intermediateCollateralAmt, selected }, order) =>
|
|
496
|
+
intermediateCollateralAmt >= order.swapInfo.owedCollateralAsset
|
|
497
|
+
? {
|
|
498
|
+
intermediateCollateralAmt:
|
|
499
|
+
intermediateCollateralAmt - order.swapInfo.owedCollateralAsset,
|
|
500
|
+
selected: [...selected, order],
|
|
501
|
+
}
|
|
502
|
+
: { intermediateCollateralAmt, selected },
|
|
503
|
+
),
|
|
504
|
+
);
|
|
505
|
+
|
|
506
|
+
return [...mintingOrders, ...selected].map((o) => o.order);
|
|
507
|
+
}
|