@indigo-labs/indigo-sdk 0.5.5 → 0.5.6

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 (204) hide show
  1. package/package.json +11 -1
  2. package/.claude/settings.local.json +0 -7
  3. package/.github/workflows/ci.yml +0 -66
  4. package/.husky/pre-commit +0 -1
  5. package/.nvmrc +0 -1
  6. package/.prettierignore +0 -1
  7. package/.prettierrc +0 -6
  8. package/eslint.config.mjs +0 -42
  9. package/scripts/bench.sh +0 -62
  10. package/scripts/run-repeat.sh +0 -22
  11. package/src/contracts/cdp/helpers.ts +0 -173
  12. package/src/contracts/cdp/scripts.ts +0 -70
  13. package/src/contracts/cdp/transactions.ts +0 -1584
  14. package/src/contracts/cdp/types-new.ts +0 -277
  15. package/src/contracts/cdp/types.ts +0 -43
  16. package/src/contracts/cdp-creator/queries.ts +0 -30
  17. package/src/contracts/cdp-creator/scripts.ts +0 -45
  18. package/src/contracts/cdp-creator/types-new.ts +0 -50
  19. package/src/contracts/cdp-creator/types.ts +0 -24
  20. package/src/contracts/collector/scripts.ts +0 -32
  21. package/src/contracts/collector/transactions.ts +0 -50
  22. package/src/contracts/collector/types-new.ts +0 -17
  23. package/src/contracts/execute/scripts.ts +0 -57
  24. package/src/contracts/execute/types-new.ts +0 -44
  25. package/src/contracts/execute/types.ts +0 -24
  26. package/src/contracts/gov/helpers.ts +0 -351
  27. package/src/contracts/gov/scripts.ts +0 -41
  28. package/src/contracts/gov/transactions.ts +0 -1607
  29. package/src/contracts/gov/types-new.ts +0 -268
  30. package/src/contracts/gov/types.ts +0 -22
  31. package/src/contracts/iasset/helpers.ts +0 -170
  32. package/src/contracts/iasset/queries.ts +0 -102
  33. package/src/contracts/iasset/scripts.ts +0 -38
  34. package/src/contracts/iasset/types.ts +0 -154
  35. package/src/contracts/initialize/actions.ts +0 -789
  36. package/src/contracts/initialize/helpers.ts +0 -616
  37. package/src/contracts/initialize/types.ts +0 -116
  38. package/src/contracts/interest-collection/helpers.ts +0 -19
  39. package/src/contracts/interest-collection/queries.ts +0 -66
  40. package/src/contracts/interest-collection/scripts.ts +0 -44
  41. package/src/contracts/interest-collection/transactions.ts +0 -433
  42. package/src/contracts/interest-collection/types-new.ts +0 -50
  43. package/src/contracts/interest-collection/types.ts +0 -26
  44. package/src/contracts/interest-oracle/helpers.ts +0 -61
  45. package/src/contracts/interest-oracle/scripts.ts +0 -18
  46. package/src/contracts/interest-oracle/transactions.ts +0 -151
  47. package/src/contracts/interest-oracle/types-new.ts +0 -32
  48. package/src/contracts/interest-oracle/types.ts +0 -16
  49. package/src/contracts/one-shot/scripts.ts +0 -12
  50. package/src/contracts/one-shot/transactions.ts +0 -66
  51. package/src/contracts/one-shot/types.ts +0 -22
  52. package/src/contracts/poll/helpers.ts +0 -37
  53. package/src/contracts/poll/scripts.ts +0 -71
  54. package/src/contracts/poll/types-poll-manager.ts +0 -20
  55. package/src/contracts/poll/types-poll-new.ts +0 -196
  56. package/src/contracts/poll/types-poll-shard.ts +0 -16
  57. package/src/contracts/price-oracle/helpers.ts +0 -30
  58. package/src/contracts/price-oracle/scripts.ts +0 -18
  59. package/src/contracts/price-oracle/transactions.ts +0 -115
  60. package/src/contracts/price-oracle/types-new.ts +0 -50
  61. package/src/contracts/price-oracle/types.ts +0 -16
  62. package/src/contracts/pyth-feed/helpers.ts +0 -82
  63. package/src/contracts/pyth-feed/scripts.ts +0 -15
  64. package/src/contracts/pyth-feed/types.ts +0 -181
  65. package/src/contracts/rob/helpers.ts +0 -666
  66. package/src/contracts/rob/scripts.ts +0 -35
  67. package/src/contracts/rob/transactions.ts +0 -423
  68. package/src/contracts/rob/types-new.ts +0 -128
  69. package/src/contracts/rob/types.ts +0 -16
  70. package/src/contracts/rob-leverage/helpers.ts +0 -422
  71. package/src/contracts/rob-leverage/transactions.ts +0 -368
  72. package/src/contracts/stability-pool/helpers.ts +0 -798
  73. package/src/contracts/stability-pool/queries.ts +0 -132
  74. package/src/contracts/stability-pool/scripts.ts +0 -51
  75. package/src/contracts/stability-pool/transactions.ts +0 -921
  76. package/src/contracts/stability-pool/types-new.ts +0 -385
  77. package/src/contracts/stability-pool/types.ts +0 -25
  78. package/src/contracts/stableswap/helpers.ts +0 -507
  79. package/src/contracts/stableswap/scripts.ts +0 -37
  80. package/src/contracts/stableswap/transactions.ts +0 -475
  81. package/src/contracts/stableswap/types-new.ts +0 -131
  82. package/src/contracts/stableswap/types.ts +0 -17
  83. package/src/contracts/staking/helpers.ts +0 -136
  84. package/src/contracts/staking/scripts.ts +0 -41
  85. package/src/contracts/staking/transactions.ts +0 -349
  86. package/src/contracts/staking/types-new.ts +0 -137
  87. package/src/contracts/staking/types.ts +0 -17
  88. package/src/contracts/treasury/helpers.ts +0 -21
  89. package/src/contracts/treasury/queries.ts +0 -100
  90. package/src/contracts/treasury/scripts.ts +0 -27
  91. package/src/contracts/treasury/transactions.ts +0 -334
  92. package/src/contracts/treasury/types-new.ts +0 -69
  93. package/src/contracts/treasury/types.ts +0 -14
  94. package/src/contracts/version-registry/scripts.ts +0 -29
  95. package/src/contracts/version-registry/types-new.ts +0 -18
  96. package/src/contracts/version-registry/types.ts +0 -17
  97. package/src/index.ts +0 -99
  98. package/src/scripts/always-fail-validator.ts +0 -7
  99. package/src/scripts/auth-token-policy.ts +0 -24
  100. package/src/scripts/iasset-policy.ts +0 -23
  101. package/src/types/evolution-schema-options.ts +0 -16
  102. package/src/types/generic.ts +0 -52
  103. package/src/types/multisig.ts +0 -48
  104. package/src/types/on-chain-decimal.ts +0 -57
  105. package/src/types/rational.ts +0 -76
  106. package/src/types/system-params.ts +0 -463
  107. package/src/utils/array-utils.ts +0 -108
  108. package/src/utils/bigint-utils.ts +0 -35
  109. package/src/utils/helper-txs.ts +0 -31
  110. package/src/utils/indigo-helpers.ts +0 -17
  111. package/src/utils/lucid-utils.ts +0 -125
  112. package/src/utils/oracle-helpers.ts +0 -62
  113. package/src/utils/pyth/decode.ts +0 -223
  114. package/src/utils/pyth/encode.ts +0 -262
  115. package/src/utils/pyth/index.ts +0 -14
  116. package/src/utils/pyth/types.ts +0 -87
  117. package/src/utils/time-helpers.ts +0 -4
  118. package/src/utils/utils.ts +0 -25
  119. package/src/validators/always-succeed-validator.ts +0 -6
  120. package/src/validators/cdp-creator-validator.ts +0 -7
  121. package/src/validators/cdp-redeem-validator.ts +0 -7
  122. package/src/validators/cdp-validator.ts +0 -7
  123. package/src/validators/collector-validator.ts +0 -7
  124. package/src/validators/execute-validator.ts +0 -7
  125. package/src/validators/governance-validator.ts +0 -7
  126. package/src/validators/iasset-validator.ts +0 -7
  127. package/src/validators/interest-collection-validator.ts +0 -7
  128. package/src/validators/interest-oracle-validator.ts +0 -7
  129. package/src/validators/one-shot-policy.ts +0 -7
  130. package/src/validators/poll-manager-validator.ts +0 -7
  131. package/src/validators/poll-shard-validator.ts +0 -7
  132. package/src/validators/price-oracle-validator.ts +0 -7
  133. package/src/validators/pyth-feed-validator.ts +0 -7
  134. package/src/validators/rob-validator.ts +0 -7
  135. package/src/validators/stability-pool-validator.ts +0 -7
  136. package/src/validators/stableswap-validator.ts +0 -7
  137. package/src/validators/staking-validator.ts +0 -7
  138. package/src/validators/treasury-validator.ts +0 -7
  139. package/src/validators/version-record-policy.ts +0 -7
  140. package/src/validators/version-registry-validator.ts +0 -7
  141. package/tests/always-succeed/script.ts +0 -7
  142. package/tests/array-utils.test.ts +0 -92
  143. package/tests/bigint-utils.test.ts +0 -41
  144. package/tests/cdp/actions.ts +0 -680
  145. package/tests/cdp/cdp-helpers.ts +0 -55
  146. package/tests/cdp/cdp-queries.ts +0 -423
  147. package/tests/cdp/cdp.test.ts +0 -6730
  148. package/tests/cdp/transactions-mutated.ts +0 -1733
  149. package/tests/data/system-params.json +0 -1155
  150. package/tests/datums.test.ts +0 -302
  151. package/tests/endpoints/initialize.ts +0 -78
  152. package/tests/endpoints/interest-collector.ts +0 -37
  153. package/tests/endpoints/treasury.ts +0 -70
  154. package/tests/gov/actions.ts +0 -381
  155. package/tests/gov/gov.test.ts +0 -5147
  156. package/tests/gov/governance-queries.ts +0 -37
  157. package/tests/hash-checks.test.ts +0 -112
  158. package/tests/indigo-test-helpers.ts +0 -99
  159. package/tests/initialize.test.ts +0 -79
  160. package/tests/interest-calculations.test.ts +0 -120
  161. package/tests/interest-collection/interest-collection.test.ts +0 -843
  162. package/tests/interest-collection/transactions-mutated.ts +0 -260
  163. package/tests/interest-oracle.test.ts +0 -93
  164. package/tests/mock/assets-mock.ts +0 -243
  165. package/tests/mock/protocol-params-mock.ts +0 -21
  166. package/tests/price-oracle/actions.ts +0 -161
  167. package/tests/price-oracle/price-oracle-queries.ts +0 -12
  168. package/tests/price-oracle/price-oracle.test.ts +0 -228
  169. package/tests/price-oracle/transactions-mutated.ts +0 -59
  170. package/tests/pyth/endpoints.ts +0 -96
  171. package/tests/pyth/helpers.ts +0 -37
  172. package/tests/pyth/pyth-encoding.test.ts +0 -376
  173. package/tests/pyth/pyth-feeds.test.ts +0 -943
  174. package/tests/pyth/pyth-indigo.test.ts +0 -505
  175. package/tests/pyth/pyth.test.ts +0 -299
  176. package/tests/queries/collector-queries.ts +0 -27
  177. package/tests/queries/execute-queries.ts +0 -47
  178. package/tests/queries/iasset-queries.ts +0 -149
  179. package/tests/queries/interest-oracle-queries.ts +0 -12
  180. package/tests/queries/poll-queries.ts +0 -97
  181. package/tests/queries/staking-queries.ts +0 -54
  182. package/tests/rob/actions.ts +0 -56
  183. package/tests/rob/rob-leverage.test.ts +0 -2555
  184. package/tests/rob/rob-queries.ts +0 -95
  185. package/tests/rob/rob.test.ts +0 -3677
  186. package/tests/rob/transactions-mutated.ts +0 -844
  187. package/tests/script-size.test.ts +0 -240
  188. package/tests/setup.ts +0 -135
  189. package/tests/stability-pool/actions.ts +0 -229
  190. package/tests/stability-pool.test.ts +0 -6491
  191. package/tests/stableswap/stableswap-actions.ts +0 -83
  192. package/tests/stableswap/stableswap-queries.ts +0 -98
  193. package/tests/stableswap/stableswap.test.ts +0 -4471
  194. package/tests/stableswap/transactions-mutated.ts +0 -348
  195. package/tests/staking.test.ts +0 -172
  196. package/tests/test-helpers.ts +0 -94
  197. package/tests/treasury/actions.ts +0 -67
  198. package/tests/treasury/treasury.test.ts +0 -320
  199. package/tests/utils/asserts.ts +0 -87
  200. package/tests/utils/benchmark-utils.ts +0 -130
  201. package/tests/utils/index.ts +0 -168
  202. package/tsconfig.build.json +0 -4
  203. package/tsconfig.json +0 -27
  204. package/vitest.config.ts +0 -11
@@ -1,507 +0,0 @@
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';
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';
33
-
34
- export const BASE_MAX_EXECUTION_FEE = 1_620_000n;
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
-
58
- export function createDestinationDatum(
59
- datum: Data.Data | null,
60
- outRef: OutRef,
61
- ): string {
62
- if (!datum) {
63
- return serialiseStableswapOutputDatum([
64
- fromHex(fromText('IndigoStableswap')),
65
- {
66
- txHash: fromHex(outRef.txHash),
67
- outputIndex: BigInt(outRef.outputIndex),
68
- },
69
- ]);
70
- }
71
-
72
- return Data.toCBORHex(datum);
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
- }
@@ -1,37 +0,0 @@
1
- import { applyParamsToScript, SpendingValidator } from '@lucid-evolution/lucid';
2
- import { castStableswapParams, StableswapParams } from './types';
3
- import {
4
- fromSystemParamsAssetLucid,
5
- StableswapParamsSP,
6
- } from '../../types/system-params';
7
- import { _stableswapValidator } from '../../validators/stableswap-validator';
8
-
9
- export const mkStableswapValidator = (
10
- params: StableswapParams,
11
- ): SpendingValidator => {
12
- return {
13
- type: 'PlutusV3',
14
- script: applyParamsToScript(_stableswapValidator.cborHex, [
15
- castStableswapParams(params),
16
- ]),
17
- };
18
- };
19
-
20
- export const mkStableswapValidatorFromSP = (
21
- params: StableswapParamsSP,
22
- ): SpendingValidator => {
23
- return {
24
- type: 'PlutusV3',
25
- script: applyParamsToScript(_stableswapValidator.cborHex, [
26
- castStableswapParams({
27
- iassetSymbol: params.iassetSymbol.unCurrencySymbol,
28
- cdpToken: fromSystemParamsAssetLucid(params.cdpToken),
29
- versionRecordToken: fromSystemParamsAssetLucid(
30
- params.versionRecordToken,
31
- ),
32
- cdpValHash: params.cdpValHash,
33
- treasuryValHash: params.treasuryValHash,
34
- }),
35
- ]),
36
- };
37
- };