@indigo-labs/indigo-sdk 0.2.42 → 0.3.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 (201) hide show
  1. package/.github/workflows/ci.yml +4 -2
  2. package/dist/index.d.mts +3008 -2194
  3. package/dist/index.d.ts +3008 -2194
  4. package/dist/index.js +9827 -6194
  5. package/dist/index.mjs +8591 -4809
  6. package/package.json +14 -3
  7. package/src/contracts/cdp/helpers.ts +68 -72
  8. package/src/contracts/cdp/scripts.ts +50 -13
  9. package/src/contracts/cdp/transactions.ts +831 -545
  10. package/src/contracts/cdp/types-new.ts +256 -0
  11. package/src/contracts/cdp/types.ts +26 -144
  12. package/src/contracts/cdp-creator/scripts.ts +15 -9
  13. package/src/contracts/cdp-creator/types-new.ts +50 -0
  14. package/src/contracts/cdp-creator/types.ts +5 -31
  15. package/src/contracts/collector/scripts.ts +1 -1
  16. package/src/contracts/collector/transactions.ts +23 -13
  17. package/src/contracts/collector/types-new.ts +17 -0
  18. package/src/contracts/execute/scripts.ts +19 -10
  19. package/src/contracts/execute/types-new.ts +44 -0
  20. package/src/contracts/execute/types.ts +5 -38
  21. package/src/contracts/gov/helpers.ts +187 -51
  22. package/src/contracts/gov/scripts.ts +17 -10
  23. package/src/contracts/gov/transactions.ts +599 -271
  24. package/src/contracts/gov/types-new.ts +253 -100
  25. package/src/contracts/gov/types.ts +4 -71
  26. package/src/contracts/iasset/helpers.ts +172 -0
  27. package/src/contracts/iasset/scripts.ts +38 -0
  28. package/src/contracts/iasset/types.ts +154 -0
  29. package/src/contracts/initialize/actions.ts +768 -0
  30. package/src/contracts/initialize/helpers.ts +611 -36
  31. package/src/contracts/initialize/types.ts +102 -28
  32. package/src/contracts/interest-collection/helpers.ts +19 -0
  33. package/src/contracts/interest-collection/scripts.ts +44 -0
  34. package/src/contracts/interest-collection/transactions.ts +436 -0
  35. package/src/contracts/interest-collection/types-new.ts +50 -0
  36. package/src/contracts/interest-collection/types.ts +26 -0
  37. package/src/contracts/interest-oracle/helpers.ts +2 -30
  38. package/src/contracts/interest-oracle/scripts.ts +1 -1
  39. package/src/contracts/interest-oracle/transactions.ts +21 -16
  40. package/src/contracts/interest-oracle/types-new.ts +32 -0
  41. package/src/contracts/interest-oracle/types.ts +1 -40
  42. package/src/contracts/one-shot/transactions.ts +1 -2
  43. package/src/contracts/poll/helpers.ts +5 -23
  44. package/src/contracts/poll/scripts.ts +12 -13
  45. package/src/contracts/poll/types-poll-manager.ts +1 -19
  46. package/src/contracts/poll/types-poll-new.ts +170 -0
  47. package/src/contracts/poll/types-poll-shard.ts +2 -24
  48. package/src/contracts/price-oracle/helpers.ts +1 -4
  49. package/src/contracts/price-oracle/scripts.ts +3 -8
  50. package/src/contracts/price-oracle/transactions.ts +32 -25
  51. package/src/contracts/price-oracle/types-new.ts +50 -0
  52. package/src/contracts/price-oracle/types.ts +2 -36
  53. package/src/contracts/pyth-feed/helpers.ts +58 -0
  54. package/src/contracts/pyth-feed/scripts.ts +15 -0
  55. package/src/contracts/pyth-feed/types.ts +181 -0
  56. package/src/contracts/rob/helpers.ts +405 -0
  57. package/src/contracts/rob/scripts.ts +35 -0
  58. package/src/contracts/rob/transactions.ts +410 -0
  59. package/src/contracts/rob/types-new.ts +128 -0
  60. package/src/contracts/rob/types.ts +16 -0
  61. package/src/contracts/rob-leverage/helpers.ts +424 -0
  62. package/src/contracts/{leverage → rob-leverage}/transactions.ts +68 -48
  63. package/src/contracts/stability-pool/helpers.ts +714 -230
  64. package/src/contracts/stability-pool/scripts.ts +20 -15
  65. package/src/contracts/stability-pool/transactions.ts +625 -493
  66. package/src/contracts/stability-pool/types-new.ts +237 -100
  67. package/src/contracts/stability-pool/types.ts +5 -22
  68. package/src/contracts/stableswap/helpers.ts +22 -0
  69. package/src/contracts/stableswap/scripts.ts +37 -0
  70. package/src/contracts/stableswap/transactions.ts +647 -0
  71. package/src/contracts/stableswap/types-new.ts +131 -0
  72. package/src/contracts/stableswap/types.ts +17 -0
  73. package/src/contracts/staking/helpers.ts +49 -34
  74. package/src/contracts/staking/scripts.ts +1 -1
  75. package/src/contracts/staking/transactions.ts +85 -130
  76. package/src/contracts/staking/types-new.ts +60 -28
  77. package/src/contracts/staking/types.ts +1 -28
  78. package/src/contracts/treasury/helpers.ts +21 -0
  79. package/src/contracts/treasury/scripts.ts +16 -26
  80. package/src/contracts/treasury/transactions.ts +256 -27
  81. package/src/contracts/treasury/types-new.ts +69 -0
  82. package/src/contracts/treasury/types.ts +2 -43
  83. package/src/contracts/version-registry/scripts.ts +2 -2
  84. package/src/contracts/version-registry/types-new.ts +6 -7
  85. package/src/index.ts +37 -20
  86. package/src/scripts/auth-token-policy.ts +3 -2
  87. package/src/scripts/iasset-policy.ts +3 -2
  88. package/src/types/evolution-schema-options.ts +3 -3
  89. package/src/types/generic.ts +17 -89
  90. package/src/types/multisig.ts +48 -0
  91. package/src/types/on-chain-decimal.ts +14 -7
  92. package/src/types/rational.ts +61 -0
  93. package/src/types/system-params.ts +237 -41
  94. package/src/utils/array-utils.ts +70 -1
  95. package/src/utils/bigint-utils.ts +12 -0
  96. package/src/utils/indigo-helpers.ts +8 -10
  97. package/src/utils/lucid-utils.ts +47 -40
  98. package/src/utils/oracle-helpers.ts +62 -0
  99. package/src/utils/pyth/decode.ts +223 -0
  100. package/src/utils/pyth/encode.ts +262 -0
  101. package/src/utils/pyth/index.ts +14 -0
  102. package/src/utils/pyth/types.ts +87 -0
  103. package/src/validators/always-succeed-validator.ts +6 -0
  104. package/src/validators/cdp-creator-validator.ts +2 -2
  105. package/src/validators/cdp-redeem-validator.ts +7 -0
  106. package/src/validators/cdp-validator.ts +2 -2
  107. package/src/validators/collector-validator.ts +2 -2
  108. package/src/validators/execute-validator.ts +2 -2
  109. package/src/validators/governance-validator.ts +2 -2
  110. package/src/validators/iasset-validator.ts +7 -0
  111. package/src/validators/interest-collection-validator.ts +7 -0
  112. package/src/validators/interest-oracle-validator.ts +2 -2
  113. package/src/validators/poll-manager-validator.ts +2 -2
  114. package/src/validators/poll-shard-validator.ts +2 -2
  115. package/src/validators/price-oracle-validator.ts +7 -0
  116. package/src/validators/pyth-feed-validator.ts +7 -0
  117. package/src/validators/rob-validator.ts +7 -0
  118. package/src/validators/stability-pool-validator.ts +2 -2
  119. package/src/validators/stableswap-validator.ts +7 -0
  120. package/src/validators/staking-validator.ts +2 -2
  121. package/src/validators/treasury-validator.ts +2 -2
  122. package/src/validators/version-record-policy.ts +2 -2
  123. package/src/validators/version-registry-validator.ts +2 -2
  124. package/tests/always-succeed/script.ts +7 -0
  125. package/tests/bigint-utils.test.ts +41 -0
  126. package/tests/cdp/actions.ts +611 -0
  127. package/tests/cdp/cdp-helpers.ts +55 -0
  128. package/tests/cdp/cdp-queries.ts +440 -0
  129. package/tests/cdp/cdp.test.ts +6087 -0
  130. package/tests/cdp/transactions-mutated.ts +1729 -0
  131. package/tests/data/system-params.json +177 -34
  132. package/tests/datums.test.ts +209 -210
  133. package/tests/endpoints/initialize.ts +68 -0
  134. package/tests/endpoints/interest-collector.ts +37 -0
  135. package/tests/endpoints/treasury.ts +70 -0
  136. package/tests/gov/actions.ts +406 -0
  137. package/tests/gov/gov.test.ts +4450 -0
  138. package/tests/{queries → gov}/governance-queries.ts +6 -3
  139. package/tests/hash-checks.test.ts +38 -11
  140. package/tests/indigo-test-helpers.ts +100 -0
  141. package/tests/initialize.test.ts +61 -9
  142. package/tests/interest-collection/interest-collection.test.ts +892 -0
  143. package/tests/interest-collection/interest-collector-queries.ts +49 -0
  144. package/tests/interest-collection/transactions-mutated.ts +260 -0
  145. package/tests/interest-oracle.test.ts +43 -35
  146. package/tests/mock/assets-mock.ts +234 -23
  147. package/tests/mock/protocol-params-mock.ts +21 -0
  148. package/tests/price-oracle/actions.ts +163 -0
  149. package/tests/price-oracle/price-oracle-queries.ts +12 -0
  150. package/tests/price-oracle/price-oracle.test.ts +240 -0
  151. package/tests/price-oracle/transactions-mutated.ts +62 -0
  152. package/tests/pyth/endpoints.ts +96 -0
  153. package/tests/pyth/helpers.ts +37 -0
  154. package/tests/pyth/pyth-encoding.test.ts +376 -0
  155. package/tests/pyth/pyth-indigo.test.ts +509 -0
  156. package/tests/pyth/pyth.test.ts +300 -0
  157. package/tests/queries/execute-queries.ts +6 -5
  158. package/tests/queries/iasset-queries.ts +175 -5
  159. package/tests/queries/interest-oracle-queries.ts +4 -2
  160. package/tests/queries/poll-queries.ts +8 -9
  161. package/tests/queries/stability-pool-queries.ts +95 -48
  162. package/tests/queries/staking-queries.ts +4 -2
  163. package/tests/queries/treasury-queries.ts +80 -5
  164. package/tests/rob/actions.ts +58 -0
  165. package/tests/{lrp-leverage.test.ts → rob/rob-leverage.test.ts} +393 -296
  166. package/tests/rob/rob-queries.ts +95 -0
  167. package/tests/rob/rob.test.ts +3762 -0
  168. package/tests/rob/transactions-mutated.ts +853 -0
  169. package/tests/script-size.test.ts +240 -0
  170. package/tests/setup.ts +135 -0
  171. package/tests/stability-pool/actions.ts +210 -0
  172. package/tests/stability-pool.test.ts +5469 -666
  173. package/tests/stableswap/stableswap-actions.ts +84 -0
  174. package/tests/stableswap/stableswap-queries.ts +89 -0
  175. package/tests/stableswap/stableswap.test.ts +3891 -0
  176. package/tests/stableswap/transactions-mutated.ts +348 -0
  177. package/tests/staking.test.ts +82 -99
  178. package/tests/test-helpers.ts +58 -11
  179. package/tests/treasury.test.ts +242 -0
  180. package/tests/utils/asserts.ts +74 -0
  181. package/tests/utils/benchmark-utils.ts +81 -0
  182. package/tests/utils/index.ts +122 -4
  183. package/tsconfig.json +9 -1
  184. package/vitest.config.ts +3 -1
  185. package/src/contracts/collector/types.ts +0 -16
  186. package/src/contracts/initialize/transactions.ts +0 -891
  187. package/src/contracts/leverage/helpers.ts +0 -424
  188. package/src/contracts/lrp/helpers.ts +0 -294
  189. package/src/contracts/lrp/scripts.ts +0 -27
  190. package/src/contracts/lrp/transactions.ts +0 -250
  191. package/src/contracts/lrp/types.ts +0 -131
  192. package/src/contracts/poll/types-poll.ts +0 -88
  193. package/src/contracts/vesting/helpers.ts +0 -218
  194. package/src/utils/value-helpers.ts +0 -37
  195. package/src/validators/lrp-validator.ts +0 -7
  196. package/tests/cdp.test.ts +0 -1528
  197. package/tests/gov.test.ts +0 -2011
  198. package/tests/lrp.test.ts +0 -673
  199. package/tests/queries/cdp-queries.ts +0 -220
  200. package/tests/queries/lrp-queries.ts +0 -76
  201. package/tests/queries/price-oracle-queries.ts +0 -10
@@ -0,0 +1,647 @@
1
+ import {
2
+ addAssets,
3
+ Address,
4
+ credentialToAddress,
5
+ Data,
6
+ fromHex,
7
+ fromText,
8
+ LucidEvolution,
9
+ OutRef,
10
+ paymentCredentialOf,
11
+ sortUTxOs,
12
+ toHex,
13
+ TxBuilder,
14
+ UTxO,
15
+ validatorToScriptHash,
16
+ } from '@lucid-evolution/lucid';
17
+ import {
18
+ fromSystemParamsScriptRef,
19
+ SystemParams,
20
+ } from '../../types/system-params';
21
+ import { mkStableswapValidatorFromSP } from './scripts';
22
+ import { estimateUtxoMinLovelace } from '../../utils/lucid-utils';
23
+ import {
24
+ parseStableswapOrderDatumOrThrow,
25
+ serialiseStableswapOrderDatum,
26
+ serialiseStableswapOrderRedeemer,
27
+ StableswapOrderDatum,
28
+ } from './types-new';
29
+ import {
30
+ AssetClass,
31
+ addressFromBech32,
32
+ addressToBech32,
33
+ getInlineDatumOrThrow,
34
+ matchSingle,
35
+ mkAssetsOf,
36
+ mkLovelacesOf,
37
+ lovelacesAmt,
38
+ isSameOutRef,
39
+ assetClassValueOf,
40
+ } from '@3rd-eye-labs/cardano-offchain-common';
41
+ import {
42
+ parseStableswapPoolDatumOrThrow,
43
+ serialiseCdpRedeemer,
44
+ serialiseStableswapPoolDatum,
45
+ StableswapPoolContent,
46
+ } from '../cdp/types-new';
47
+ import { calculateFeeFromRatio } from '../../utils/indigo-helpers';
48
+ import { array as A, function as F } from 'fp-ts';
49
+ import { isEmpty } from 'fp-ts/lib/Array';
50
+ import { BASE_MAX_EXECUTION_FEE, createDestinationDatum } from './helpers';
51
+ import * as Core from '@evolution-sdk/evolution';
52
+ import { treasuryFeeTx } from '../treasury/transactions';
53
+ import {
54
+ Rational,
55
+ rationalDiv,
56
+ rationalFloor,
57
+ rationalFromInt,
58
+ rationalMul,
59
+ } from '../../types/rational';
60
+
61
+ type StableswapInfo = {
62
+ suppliedCollateralAsset: bigint;
63
+ suppliedIasset: bigint;
64
+ owedCollateralAsset: bigint;
65
+ owedIasset: bigint;
66
+ mintingFee: bigint;
67
+ redemptionFee: bigint;
68
+ };
69
+
70
+ type StableswapOrderInfo = {
71
+ utxo: UTxO;
72
+ datum: StableswapOrderDatum;
73
+ swapInfo: StableswapInfo;
74
+ };
75
+
76
+ export async function createStableswapOrder(
77
+ iasset: string,
78
+ collateralAsset: AssetClass,
79
+ amount: bigint,
80
+ minting: boolean,
81
+ poolDatum: StableswapPoolContent,
82
+ params: SystemParams,
83
+ lucid: LucidEvolution,
84
+ destinationAddress?: Address,
85
+ destinationInlineDatum?: Core.Data.Data,
86
+ maxExecutionFee: bigint = BASE_MAX_EXECUTION_FEE,
87
+ additionalLovelaces: bigint = 0n,
88
+ maxFeeRatio?: Rational,
89
+ ): Promise<TxBuilder> {
90
+ const myAddress = await lucid.wallet().address();
91
+
92
+ const pkh = paymentCredentialOf(myAddress);
93
+
94
+ const datum: StableswapOrderDatum = {
95
+ iasset: fromHex(fromText(iasset)),
96
+ collateralAsset: collateralAsset,
97
+ owner: fromHex(pkh.hash),
98
+ destination: addressFromBech32(destinationAddress ?? myAddress),
99
+ destinationInlineDatum: destinationInlineDatum ?? null,
100
+ maxExecutionFee: maxExecutionFee,
101
+ maxFeeRatio:
102
+ maxFeeRatio ??
103
+ (minting ? poolDatum.mintingFeeRatio : poolDatum.redemptionFeeRatio),
104
+ };
105
+
106
+ const assetsToSwap = minting
107
+ ? mkAssetsOf(collateralAsset, amount)
108
+ : mkAssetsOf(
109
+ {
110
+ currencySymbol: fromHex(
111
+ params.stableswapParams.iassetSymbol.unCurrencySymbol,
112
+ ),
113
+ tokenName: fromHex(fromText(iasset)),
114
+ },
115
+ amount,
116
+ );
117
+
118
+ // This is an approximation of the amount of lovelace that will be needed to pay for the output.
119
+ const expectedOutputLovelaces = estimateUtxoMinLovelace(
120
+ lucid.config().protocolParameters!,
121
+ myAddress,
122
+ assetsToSwap,
123
+ {
124
+ kind: 'inline',
125
+ value: createDestinationDatum(destinationInlineDatum ?? null, {
126
+ txHash:
127
+ '0000000000000000000000000000000000000000000000000000000000000000',
128
+ outputIndex: 0,
129
+ }),
130
+ },
131
+ );
132
+
133
+ const roundedExpectedOutputLovelaces =
134
+ (expectedOutputLovelaces / 1_000_000n + 1n) * 1_000_000n;
135
+
136
+ return lucid.newTx().pay.ToContract(
137
+ credentialToAddress(lucid.config().network!, {
138
+ hash: validatorToScriptHash(
139
+ mkStableswapValidatorFromSP(params.stableswapParams),
140
+ ),
141
+ type: 'Script',
142
+ }),
143
+ {
144
+ kind: 'inline',
145
+ value: serialiseStableswapOrderDatum(datum),
146
+ },
147
+ addAssets(
148
+ assetsToSwap,
149
+ mkLovelacesOf(
150
+ roundedExpectedOutputLovelaces + maxExecutionFee + additionalLovelaces,
151
+ ),
152
+ ),
153
+ );
154
+ }
155
+
156
+ export async function cancelStableswapOrder(
157
+ stableswapOrderOref: OutRef,
158
+ sysParams: SystemParams,
159
+ lucid: LucidEvolution,
160
+ ): Promise<TxBuilder> {
161
+ const stableswapScriptRefUtxo = matchSingle(
162
+ await lucid.utxosByOutRef([
163
+ fromSystemParamsScriptRef(
164
+ sysParams.scriptReferences.stableswapValidatorRef,
165
+ ),
166
+ ]),
167
+ (_) => new Error('Expected a single Stableswap Ref Script UTXO'),
168
+ );
169
+
170
+ const stableswapOrderUtxo = matchSingle(
171
+ await lucid.utxosByOutRef([stableswapOrderOref]),
172
+ (_) => new Error('Expected a single Stableswap Order UTXO.'),
173
+ );
174
+
175
+ const stableswapOrderDatum = parseStableswapOrderDatumOrThrow(
176
+ getInlineDatumOrThrow(stableswapOrderUtxo),
177
+ );
178
+
179
+ return lucid
180
+ .newTx()
181
+ .readFrom([stableswapScriptRefUtxo])
182
+ .collectFrom(
183
+ [stableswapOrderUtxo],
184
+ serialiseStableswapOrderRedeemer('CancelStableswapOrder'),
185
+ )
186
+ .addSignerKey(toHex(stableswapOrderDatum.owner));
187
+ }
188
+
189
+ export async function batchProcessStableswapOrders(
190
+ stableswapOrderOrefs: OutRef[],
191
+ stableswapPoolOref: OutRef,
192
+ treasuryOref: OutRef,
193
+ sysParams: SystemParams,
194
+ lucid: LucidEvolution,
195
+ ): Promise<TxBuilder> {
196
+ const stableswapScriptRefUtxo = matchSingle(
197
+ await lucid.utxosByOutRef([
198
+ fromSystemParamsScriptRef(
199
+ sysParams.scriptReferences.stableswapValidatorRef,
200
+ ),
201
+ ]),
202
+ (_) => new Error('Expected a single Stableswap Ref Script UTXO'),
203
+ );
204
+
205
+ const cdpScriptRefUtxo = matchSingle(
206
+ await lucid.utxosByOutRef([
207
+ fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
208
+ ]),
209
+ (_) => new Error('Expected a single CDP Ref Script UTXO'),
210
+ );
211
+
212
+ const iAssetTokenPolicyRefScriptUtxo = matchSingle(
213
+ await lucid.utxosByOutRef([
214
+ fromSystemParamsScriptRef(
215
+ sysParams.scriptReferences.iAssetTokenPolicyRef,
216
+ ),
217
+ ]),
218
+ (_) => new Error('Expected a single iasset token policy Ref Script UTXO'),
219
+ );
220
+
221
+ if (isEmpty(stableswapOrderOrefs)) {
222
+ throw new Error('At least one order must be provided.');
223
+ }
224
+
225
+ const stableswapOrderUtxos = await lucid.utxosByOutRef(stableswapOrderOrefs);
226
+
227
+ const sortedStableswapOrderUtxos = sortUTxOs(
228
+ stableswapOrderUtxos,
229
+ 'Canonical',
230
+ );
231
+
232
+ if (sortedStableswapOrderUtxos.length !== stableswapOrderOrefs.length) {
233
+ throw new Error('Expected certain number of orders');
234
+ }
235
+
236
+ const mainOrderUtxo = sortedStableswapOrderUtxos[0];
237
+
238
+ const mainOrderDatum = parseStableswapOrderDatumOrThrow(
239
+ getInlineDatumOrThrow(mainOrderUtxo),
240
+ );
241
+
242
+ const iassetAc = {
243
+ currencySymbol: fromHex(
244
+ sysParams.stableswapParams.iassetSymbol.unCurrencySymbol,
245
+ ),
246
+ tokenName: mainOrderDatum.iasset,
247
+ };
248
+
249
+ const collateralAc = mainOrderDatum.collateralAsset;
250
+
251
+ const stableswapPoolUtxo = matchSingle(
252
+ await lucid.utxosByOutRef([stableswapPoolOref]),
253
+ (_) => new Error('Expected a single cdp UTXO'),
254
+ );
255
+
256
+ const stableswapPoolDatum = parseStableswapPoolDatumOrThrow(
257
+ getInlineDatumOrThrow(stableswapPoolUtxo),
258
+ );
259
+
260
+ const ordersInfo: StableswapOrderInfo[] = sortedStableswapOrderUtxos.map(
261
+ (orderUtxo) => {
262
+ const orderDatum = parseStableswapOrderDatumOrThrow(
263
+ getInlineDatumOrThrow(orderUtxo),
264
+ );
265
+
266
+ if (
267
+ toHex(orderDatum.iasset) != toHex(mainOrderDatum.iasset) ||
268
+ toHex(orderDatum.collateralAsset.currencySymbol) !=
269
+ toHex(mainOrderDatum.collateralAsset.currencySymbol) ||
270
+ toHex(orderDatum.collateralAsset.tokenName) !=
271
+ toHex(mainOrderDatum.collateralAsset.tokenName)
272
+ ) {
273
+ throw new Error('Wrong batch of orders');
274
+ }
275
+
276
+ const suppliedIasset = assetClassValueOf(orderUtxo.assets, iassetAc);
277
+ const suppliedCollateralAsset = assetClassValueOf(
278
+ orderUtxo.assets,
279
+ collateralAc,
280
+ );
281
+
282
+ if (
283
+ (suppliedIasset != 0n && suppliedCollateralAsset != 0n) ||
284
+ (suppliedIasset == 0n && suppliedCollateralAsset == 0n)
285
+ ) {
286
+ throw new Error(
287
+ 'An order must supply either iAsset or collateral asset',
288
+ );
289
+ }
290
+
291
+ const isMinting = suppliedCollateralAsset > 0n;
292
+
293
+ const isOneToOne =
294
+ stableswapPoolDatum.collateralToIassetRatio.numerator ===
295
+ stableswapPoolDatum.collateralToIassetRatio.denominator;
296
+
297
+ if (isMinting) {
298
+ // Mint order with one to one ratio case.
299
+ if (isOneToOne) {
300
+ const fee = calculateFeeFromRatio(
301
+ stableswapPoolDatum.mintingFeeRatio,
302
+ suppliedCollateralAsset,
303
+ );
304
+
305
+ return {
306
+ utxo: orderUtxo,
307
+ datum: orderDatum,
308
+ swapInfo: {
309
+ suppliedCollateralAsset: suppliedCollateralAsset,
310
+ suppliedIasset: 0n,
311
+ owedCollateralAsset: 0n,
312
+ owedIasset: suppliedCollateralAsset - fee,
313
+ mintingFee: fee,
314
+ redemptionFee: 0n,
315
+ },
316
+ };
317
+ // Mint order with any ratio case.
318
+ } else {
319
+ const iAssetConversion = rationalFloor(
320
+ rationalDiv(
321
+ rationalFromInt(suppliedCollateralAsset),
322
+ stableswapPoolDatum.collateralToIassetRatio,
323
+ ),
324
+ );
325
+
326
+ const attemptedNormalizedCollateral = rationalFloor(
327
+ rationalMul(
328
+ rationalFromInt(iAssetConversion),
329
+ stableswapPoolDatum.collateralToIassetRatio,
330
+ ),
331
+ );
332
+
333
+ const normalizedCollateralSupplied =
334
+ rationalFloor(
335
+ rationalDiv(
336
+ rationalFromInt(attemptedNormalizedCollateral),
337
+ stableswapPoolDatum.collateralToIassetRatio,
338
+ ),
339
+ ) != iAssetConversion
340
+ ? suppliedCollateralAsset
341
+ : attemptedNormalizedCollateral;
342
+
343
+ const fee = calculateFeeFromRatio(
344
+ stableswapPoolDatum.mintingFeeRatio,
345
+ iAssetConversion,
346
+ );
347
+
348
+ return {
349
+ utxo: orderUtxo,
350
+ datum: orderDatum,
351
+ swapInfo: {
352
+ suppliedCollateralAsset: normalizedCollateralSupplied,
353
+ suppliedIasset: 0n,
354
+ owedCollateralAsset: 0n,
355
+ owedIasset: iAssetConversion - fee,
356
+ mintingFee: fee,
357
+ redemptionFee: 0n,
358
+ },
359
+ };
360
+ }
361
+ // Redeem order case
362
+ } else {
363
+ const fee = calculateFeeFromRatio(
364
+ stableswapPoolDatum.redemptionFeeRatio,
365
+ suppliedIasset,
366
+ );
367
+
368
+ const effectiveSuppliedIasset = suppliedIasset - fee;
369
+
370
+ // Redeem order with one to one ratio case
371
+ if (isOneToOne) {
372
+ return {
373
+ utxo: orderUtxo,
374
+ datum: orderDatum,
375
+ swapInfo: {
376
+ suppliedCollateralAsset: 0n,
377
+ suppliedIasset: effectiveSuppliedIasset,
378
+ owedCollateralAsset: effectiveSuppliedIasset,
379
+ owedIasset: 0n,
380
+ mintingFee: 0n,
381
+ redemptionFee: fee,
382
+ },
383
+ };
384
+ // Redeem order with any ratio case
385
+ } else {
386
+ const collateralConversion = rationalFloor(
387
+ rationalMul(
388
+ rationalFromInt(effectiveSuppliedIasset),
389
+ stableswapPoolDatum.collateralToIassetRatio,
390
+ ),
391
+ );
392
+
393
+ const attemptedNormalizedEffectiveIasset = rationalFloor(
394
+ rationalDiv(
395
+ rationalFromInt(collateralConversion),
396
+ stableswapPoolDatum.collateralToIassetRatio,
397
+ ),
398
+ );
399
+
400
+ const normalizedEffectiveIasset =
401
+ rationalFloor(
402
+ rationalMul(
403
+ rationalFromInt(attemptedNormalizedEffectiveIasset),
404
+ stableswapPoolDatum.collateralToIassetRatio,
405
+ ),
406
+ ) != collateralConversion
407
+ ? effectiveSuppliedIasset
408
+ : attemptedNormalizedEffectiveIasset;
409
+
410
+ return {
411
+ utxo: orderUtxo,
412
+ datum: orderDatum,
413
+ swapInfo: {
414
+ suppliedCollateralAsset: 0n,
415
+ suppliedIasset: normalizedEffectiveIasset,
416
+ owedCollateralAsset: rationalFloor(
417
+ rationalMul(
418
+ rationalFromInt(effectiveSuppliedIasset),
419
+ stableswapPoolDatum.collateralToIassetRatio,
420
+ ),
421
+ ),
422
+ owedIasset: 0n,
423
+ mintingFee: 0n,
424
+ redemptionFee: fee,
425
+ },
426
+ };
427
+ }
428
+ }
429
+ },
430
+ );
431
+
432
+ const totalSwapInfo = F.pipe(
433
+ ordersInfo,
434
+ A.reduce<StableswapOrderInfo, StableswapInfo>(
435
+ {
436
+ suppliedCollateralAsset: 0n,
437
+ suppliedIasset: 0n,
438
+ owedCollateralAsset: 0n,
439
+ owedIasset: 0n,
440
+ mintingFee: 0n,
441
+ redemptionFee: 0n,
442
+ },
443
+ (acc, orderInfo) => {
444
+ return {
445
+ suppliedCollateralAsset:
446
+ acc.suppliedCollateralAsset +
447
+ orderInfo.swapInfo.suppliedCollateralAsset,
448
+ suppliedIasset:
449
+ acc.suppliedIasset + orderInfo.swapInfo.suppliedIasset,
450
+ owedCollateralAsset:
451
+ acc.owedCollateralAsset + orderInfo.swapInfo.owedCollateralAsset,
452
+ owedIasset: acc.owedIasset + orderInfo.swapInfo.owedIasset,
453
+ mintingFee: acc.mintingFee + orderInfo.swapInfo.mintingFee,
454
+ redemptionFee: acc.redemptionFee + orderInfo.swapInfo.redemptionFee,
455
+ };
456
+ },
457
+ ),
458
+ );
459
+
460
+ const collateralAmtChangePool =
461
+ totalSwapInfo.suppliedCollateralAsset - totalSwapInfo.owedCollateralAsset;
462
+
463
+ const amountToMint =
464
+ totalSwapInfo.owedIasset -
465
+ totalSwapInfo.suppliedIasset +
466
+ totalSwapInfo.mintingFee;
467
+
468
+ const fee = totalSwapInfo.mintingFee + totalSwapInfo.redemptionFee;
469
+
470
+ const tx = lucid
471
+ .newTx()
472
+ .readFrom([
473
+ stableswapScriptRefUtxo,
474
+ cdpScriptRefUtxo,
475
+ iAssetTokenPolicyRefScriptUtxo,
476
+ ])
477
+ .collectFrom([stableswapPoolUtxo], {
478
+ kind: 'selected',
479
+ makeRedeemer: (inputIndices) =>
480
+ serialiseCdpRedeemer({
481
+ Stableswap: {
482
+ forwardingInputIndex: inputIndices[0],
483
+ },
484
+ }),
485
+ inputs: [mainOrderUtxo],
486
+ })
487
+ .pay.ToContract(
488
+ stableswapPoolUtxo.address,
489
+ {
490
+ kind: 'inline',
491
+ value: serialiseStableswapPoolDatum(stableswapPoolDatum),
492
+ },
493
+ collateralAmtChangePool != 0n
494
+ ? addAssets(
495
+ stableswapPoolUtxo.assets,
496
+ mkAssetsOf(collateralAc, collateralAmtChangePool),
497
+ )
498
+ : stableswapPoolUtxo.assets,
499
+ )
500
+ // This has to be added as otherwise there is the following error:
501
+ // TxBuilderError: { Complete: RedeemerBuilder: Coin selection had to be updated
502
+ // after building redeemers, possibly leading to incorrect indices. Try setting
503
+ // a minimum fee of 2055720 lovelaces. }
504
+ .setMinFee(2_055_720n);
505
+
506
+ if (amountToMint !== 0n) {
507
+ tx.mintAssets(mkAssetsOf(iassetAc, amountToMint), Data.void());
508
+ }
509
+
510
+ F.pipe(
511
+ ordersInfo,
512
+ A.reduce<StableswapOrderInfo, TxBuilder>(tx, (acc, orderInfo) => {
513
+ return acc
514
+ .collectFrom(
515
+ [orderInfo.utxo],
516
+ isSameOutRef(orderInfo.utxo, mainOrderUtxo)
517
+ ? serialiseStableswapOrderRedeemer('BatchProcessStableswapOrders')
518
+ : {
519
+ kind: 'selected',
520
+ makeRedeemer: (inputIndices: bigint[]) => {
521
+ return serialiseStableswapOrderRedeemer({
522
+ BatchAuxiliary: {
523
+ ownInputIndex: inputIndices[0],
524
+ mainOrderInputIndex: inputIndices[1],
525
+ },
526
+ });
527
+ },
528
+ inputs: [orderInfo.utxo, mainOrderUtxo],
529
+ },
530
+ )
531
+ .pay.ToAddressWithData(
532
+ addressToBech32(orderInfo.datum.destination, lucid.config().network!),
533
+ {
534
+ kind: 'inline',
535
+ value: createDestinationDatum(
536
+ orderInfo.datum.destinationInlineDatum ?? null,
537
+ orderInfo.utxo,
538
+ ),
539
+ },
540
+ addAssets(
541
+ // Currently, we always take the max execution fee from the order utxo.
542
+ // This can be improved so that we take the actual execution fee.
543
+ mkLovelacesOf(
544
+ lovelacesAmt(orderInfo.utxo.assets) -
545
+ orderInfo.datum.maxExecutionFee,
546
+ ),
547
+ orderInfo.swapInfo.owedIasset > 0
548
+ ? mkAssetsOf(iassetAc, orderInfo.swapInfo.owedIasset)
549
+ : mkAssetsOf(
550
+ collateralAc,
551
+ orderInfo.swapInfo.owedCollateralAsset,
552
+ ),
553
+ ),
554
+ );
555
+ }),
556
+ );
557
+ if (fee > 0) {
558
+ await treasuryFeeTx(
559
+ iassetAc,
560
+ fee,
561
+ 0n,
562
+ lucid,
563
+ sysParams,
564
+ tx,
565
+ stableswapPoolOref,
566
+ treasuryOref,
567
+ );
568
+ }
569
+ return tx;
570
+ }
571
+
572
+ export async function updateStableswapPoolFees(
573
+ stableswapPoolOutRef: OutRef,
574
+ stableswapFeeOutRef: OutRef,
575
+ newMintingFeeRatio: Rational | null,
576
+ newRedemptionFeeRatio: Rational | null,
577
+ sysParams: SystemParams,
578
+ lucid: LucidEvolution,
579
+ ): Promise<TxBuilder> {
580
+ const stableswapScriptRefUtxo = matchSingle(
581
+ await lucid.utxosByOutRef([
582
+ fromSystemParamsScriptRef(
583
+ sysParams.scriptReferences.stableswapValidatorRef,
584
+ ),
585
+ ]),
586
+ (_) => new Error('Expected a single Stableswap Ref Script UTXO'),
587
+ );
588
+
589
+ const cdpScriptRefUtxo = matchSingle(
590
+ await lucid.utxosByOutRef([
591
+ fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
592
+ ]),
593
+ (_) => new Error('Expected a single CDP Ref Script UTXO'),
594
+ );
595
+
596
+ const stableswapPool = matchSingle(
597
+ await lucid.utxosByOutRef([stableswapPoolOutRef]),
598
+ (_) => new Error('Expected a single Stableswap Pool UTXO.'),
599
+ );
600
+
601
+ const stableswapFeeUtxo = matchSingle(
602
+ await lucid.utxosByOutRef([stableswapFeeOutRef]),
603
+ (_) => new Error('Expected a single Stableswap Fee UTXO.'),
604
+ );
605
+
606
+ const stableswapPoolDatum = parseStableswapPoolDatumOrThrow(
607
+ getInlineDatumOrThrow(stableswapPool),
608
+ );
609
+ const newStableswapPoolDatum = {
610
+ ...stableswapPoolDatum,
611
+ mintingFeeRatio: newMintingFeeRatio ?? stableswapPoolDatum.mintingFeeRatio,
612
+ redemptionFeeRatio:
613
+ newRedemptionFeeRatio ?? stableswapPoolDatum.redemptionFeeRatio,
614
+ };
615
+
616
+ if (!stableswapPoolDatum.feeManager) {
617
+ throw new Error('Stableswap pool fee manager is not set');
618
+ }
619
+
620
+ return lucid
621
+ .newTx()
622
+ .readFrom([stableswapScriptRefUtxo, cdpScriptRefUtxo])
623
+ .collectFrom([stableswapPool], {
624
+ kind: 'selected',
625
+ makeRedeemer: (inputIndices) =>
626
+ serialiseCdpRedeemer({
627
+ Stableswap: {
628
+ forwardingInputIndex: inputIndices[0],
629
+ },
630
+ }),
631
+ inputs: [stableswapFeeUtxo],
632
+ })
633
+ .collectFrom(
634
+ [stableswapFeeUtxo],
635
+ serialiseStableswapOrderRedeemer('UpdateFees'),
636
+ )
637
+ .pay.ToContract(
638
+ stableswapPool.address,
639
+ {
640
+ kind: 'inline',
641
+ value: serialiseStableswapPoolDatum(newStableswapPoolDatum),
642
+ },
643
+ stableswapPool.assets,
644
+ )
645
+ .addSignerKey(toHex(stableswapPoolDatum.feeManager))
646
+ .setMinFee(1038402n);
647
+ }