@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,405 @@
1
+ import {
2
+ addAssets,
3
+ Assets,
4
+ fromHex,
5
+ toHex,
6
+ TxBuilder,
7
+ UTxO,
8
+ } from '@lucid-evolution/lucid';
9
+ import {
10
+ RobDatum,
11
+ parseRobDatumOrThrow,
12
+ serialiseRobDatum,
13
+ serialiseRobRedeemer,
14
+ } from './types-new';
15
+ import { calculateFeeFromRatio } from '../../utils/indigo-helpers';
16
+ import { zeroNegatives } from '../../utils/bigint-utils';
17
+ import {
18
+ readonlyArray as RA,
19
+ array as A,
20
+ function as F,
21
+ option as O,
22
+ } from 'fp-ts';
23
+ import { SystemParams } from '../../types/system-params';
24
+ import { match, P } from 'ts-pattern';
25
+ import { getInlineDatumOrThrow } from '../../utils/lucid-utils';
26
+ import {
27
+ adaAssetClass,
28
+ AssetClass,
29
+ assetClassValueOf,
30
+ isSameAssetClass,
31
+ lovelacesAmt,
32
+ mkAssetsOf,
33
+ } from '@3rd-eye-labs/cardano-offchain-common';
34
+ import {
35
+ Rational,
36
+ rationalDiv,
37
+ rationalFloor,
38
+ rationalFromInt,
39
+ rationalMul,
40
+ } from '../../types/rational';
41
+
42
+ export const MIN_ROB_COLLATERAL_AMT = 3_000_000n;
43
+
44
+ export function robAmountToSpend(
45
+ utxo: UTxO,
46
+ datum: RobDatum,
47
+ sysParams: SystemParams,
48
+ ): bigint {
49
+ return match(datum.orderType)
50
+ .returnType<bigint>()
51
+ .with({ BuyIAssetOrder: P.select() }, (content) => {
52
+ if (isSameAssetClass(adaAssetClass, content.collateralAsset)) {
53
+ return zeroNegatives(
54
+ lovelacesAmt(utxo.assets) - MIN_ROB_COLLATERAL_AMT,
55
+ );
56
+ } else {
57
+ return assetClassValueOf(utxo.assets, content.collateralAsset);
58
+ }
59
+ })
60
+ .with({ SellIAssetOrder: P.any }, (_) => {
61
+ return assetClassValueOf(utxo.assets, {
62
+ currencySymbol: fromHex(
63
+ sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
64
+ ),
65
+ tokenName: datum.iasset,
66
+ });
67
+ })
68
+ .exhaustive();
69
+ }
70
+
71
+ export function isFullyRedeemed(
72
+ utxo: UTxO,
73
+ datum: RobDatum,
74
+ sysParams: SystemParams,
75
+ ): boolean {
76
+ // TODO: or when the price doesn't allow single token redemption
77
+ return robAmountToSpend(utxo, datum, sysParams) === 0n;
78
+ }
79
+
80
+ // /**
81
+ // * Calculate the actually redeemable lovelaces taking into account:
82
+ // * - LRP datum
83
+ // * - UTXO's value
84
+ // * - min redemption
85
+ // *
86
+ // * This helps to handle incorrectly initialised LRPs, too.
87
+ // */
88
+ // export function lrpRedeemableLovelacesInclReimb(
89
+ // lrp: [UTxO, RobDatum],
90
+ // lrpParams: RobParamsSP,
91
+ // ): bigint {
92
+ // const datum = lrp[1];
93
+ // const utxo = lrp[0];
94
+
95
+ // let res = 0n;
96
+ // // When incorrectly initialised
97
+ // if (datum.lovelacesToSpend > lovelacesAmt(utxo.assets)) {
98
+ // res = bigintMax(lovelacesAmt(utxo.assets) - MIN_ROB_COLLATERAL_AMT, 0n);
99
+ // } else {
100
+ // res = datum.lovelacesToSpend;
101
+ // }
102
+
103
+ // if (res < lrpParams.minRedemptionLovelacesAmt) {
104
+ // return 0n;
105
+ // }
106
+
107
+ // return res;
108
+ // }
109
+
110
+ /**
111
+ * Right now we allow multi redemptions when the collateral asset, iasset pair is the same.
112
+ * The on-chain however should allow even other combinations.
113
+ */
114
+ export function buildRedemptionsTx(
115
+ /**
116
+ * The tuple represents the ROB UTXO and the amount to payout for a redemption. In case of buy order,
117
+ * it's denominated in iAssets, in case of sell order, it's denominated in collateral asset.
118
+ */
119
+ redemptions: [UTxO, bigint][],
120
+ iasset: Uint8Array<ArrayBufferLike>,
121
+ collateralAsset: AssetClass,
122
+ price: Rational,
123
+ redemptionReimbursementRatio: Rational,
124
+ sysParams: SystemParams,
125
+ tx: TxBuilder,
126
+ /**
127
+ * The number of Tx outputs before these new ones.
128
+ */
129
+ txOutputsBeforeCount: bigint,
130
+ collateralAssetRefInputIdx: bigint,
131
+ iassetRefInputIdx: bigint,
132
+ oracleIdx: bigint | null,
133
+ ): TxBuilder {
134
+ return F.pipe(
135
+ redemptions,
136
+ A.reduceWithIndex<[UTxO, bigint], TxBuilder>(
137
+ tx,
138
+ (idx, acc, [robUtxo, spendAmt]) => {
139
+ const robDatum = parseRobDatumOrThrow(getInlineDatumOrThrow(robUtxo));
140
+
141
+ if (toHex(robDatum.iasset) !== toHex(iasset)) {
142
+ throw new Error('Only same iAsset');
143
+ }
144
+
145
+ const [robOutputVal, sellOrderAllowedAssetsIdx] = match(
146
+ robDatum.orderType,
147
+ )
148
+ .returnType<[Assets, bigint]>()
149
+ .with({ BuyIAssetOrder: P.select() }, (content) => {
150
+ if (!isSameAssetClass(content.collateralAsset, collateralAsset)) {
151
+ throw new Error('Only same collateral asset');
152
+ }
153
+
154
+ const payoutIAssetAmt = spendAmt;
155
+
156
+ const reimburstmentIAsset = calculateFeeFromRatio(
157
+ redemptionReimbursementRatio,
158
+ payoutIAssetAmt,
159
+ );
160
+
161
+ const collateralForRedemption = rationalFloor(
162
+ rationalMul(
163
+ rationalFromInt(payoutIAssetAmt - reimburstmentIAsset),
164
+ price,
165
+ ),
166
+ );
167
+
168
+ const resultVal = addAssets(
169
+ robUtxo.assets,
170
+ mkAssetsOf(collateralAsset, -collateralForRedemption),
171
+ mkAssetsOf(
172
+ {
173
+ currencySymbol: fromHex(
174
+ sysParams.robParams.iassetPolicyId.unCurrencySymbol,
175
+ ),
176
+ tokenName: robDatum.iasset,
177
+ },
178
+ payoutIAssetAmt,
179
+ ),
180
+ );
181
+
182
+ return [resultVal, 0n];
183
+ })
184
+ .with({ SellIAssetOrder: P.select() }, (content) => {
185
+ const allowedAssetIdx = F.pipe(
186
+ content.allowedCollateralAssets,
187
+ RA.findIndex(([asset, _]) =>
188
+ isSameAssetClass(asset, collateralAsset),
189
+ ),
190
+ O.getOrElse<number>(() => {
191
+ throw new Error("Doesn't allow required collateral asset.");
192
+ }),
193
+ );
194
+
195
+ const payoutCollateralAmt = spendAmt;
196
+
197
+ const reimbursementCollateral = calculateFeeFromRatio(
198
+ redemptionReimbursementRatio,
199
+ payoutCollateralAmt,
200
+ );
201
+
202
+ const redeemedIAssetAmt = rationalFloor(
203
+ rationalDiv(
204
+ rationalFromInt(payoutCollateralAmt - reimbursementCollateral),
205
+ price,
206
+ ),
207
+ );
208
+
209
+ const resultVal = addAssets(
210
+ robUtxo.assets,
211
+ mkAssetsOf(collateralAsset, payoutCollateralAmt),
212
+ mkAssetsOf(
213
+ {
214
+ currencySymbol: fromHex(
215
+ sysParams.robParams.iassetPolicyId.unCurrencySymbol,
216
+ ),
217
+ tokenName: robDatum.iasset,
218
+ },
219
+ -redeemedIAssetAmt,
220
+ ),
221
+ );
222
+
223
+ return [resultVal, BigInt(allowedAssetIdx)];
224
+ })
225
+ .exhaustive();
226
+
227
+ if (lovelacesAmt(robOutputVal) < MIN_ROB_COLLATERAL_AMT) {
228
+ throw new Error('ROB was incorrectly initialised.');
229
+ }
230
+
231
+ return acc
232
+ .collectFrom([robUtxo], {
233
+ kind: 'self',
234
+ makeRedeemer: (ownIdx) =>
235
+ serialiseRobRedeemer({
236
+ Redeem: {
237
+ ownInputIdx: ownIdx,
238
+ collateralAssetRefInputIdx: collateralAssetRefInputIdx,
239
+ iassetRefInputIdx: iassetRefInputIdx,
240
+ continuingOutputIdx: txOutputsBeforeCount + BigInt(idx),
241
+ sellOrderAllowedAssetsIdx: sellOrderAllowedAssetsIdx,
242
+ priceOracleIdx:
243
+ oracleIdx != null
244
+ ? { OracleRefInputIdx: oracleIdx }
245
+ : 'OracleVoid',
246
+ },
247
+ }),
248
+ })
249
+ .pay.ToContract(
250
+ robUtxo.address,
251
+ {
252
+ kind: 'inline',
253
+ value: serialiseRobDatum({
254
+ ...robDatum,
255
+ robRefInput: {
256
+ outputIndex: BigInt(robUtxo.outputIndex),
257
+ txHash: fromHex(robUtxo.txHash),
258
+ },
259
+ }),
260
+ },
261
+ robOutputVal,
262
+ );
263
+ },
264
+ ),
265
+ );
266
+ }
267
+
268
+ // /**
269
+ // * Given all available LRP UTXOs, calculate total available ADA that can be redeemed (including reimbursement fee).
270
+ // * Taking into account ncorrectly initialised LRPs (without base collateral).
271
+ // */
272
+ // export function calculateTotalAdaForRedemption(
273
+ // iasset: Uint8Array<ArrayBufferLike>,
274
+ // iassetPrice: OnChainDecimal,
275
+ // lrpParams: RobParamsSP,
276
+ // allLrps: [UTxO, RobDatum][],
277
+ // /**
278
+ // * How many LRPs can be redeemed in a single Tx.
279
+ // */
280
+ // maxLrpsInTx: number,
281
+ // ): bigint {
282
+ // return F.pipe(
283
+ // allLrps,
284
+ // A.filterMap(([utxo, datum]) => {
285
+ // if (
286
+ // toHex(datum.iasset) !== toHex(iasset) ||
287
+ // datum.maxPrice.getOnChainInt < iassetPrice.getOnChainInt
288
+ // ) {
289
+ // return O.none;
290
+ // }
291
+
292
+ // const lovelacesToSpend = lrpRedeemableLovelacesInclReimb(
293
+ // [utxo, datum],
294
+ // lrpParams,
295
+ // );
296
+
297
+ // if (lovelacesToSpend === 0n) {
298
+ // return O.none;
299
+ // }
300
+
301
+ // // Subtract the reimbursement fee here on each iteration to simulate real redemptions.
302
+ // return O.some(lovelacesToSpend);
303
+ // }),
304
+ // // From largest to smallest
305
+ // A.sort(Ord.reverse(BigIntOrd)),
306
+ // // We can fit only this number of redemptions with CDP open into a single Tx.
307
+ // A.takeLeft(maxLrpsInTx),
308
+ // sum,
309
+ // );
310
+ // }
311
+
312
+ // export function randomLrpsSubsetSatisfyingTargetLovelaces(
313
+ // iasset: Uint8Array<ArrayBufferLike>,
314
+ // // Including the reimbursement percentage
315
+ // targetLovelacesToSpend: bigint,
316
+ // iassetPrice: OnChainDecimal,
317
+ // allLrps: [UTxO, RobDatum][],
318
+ // lrpParams: RobParamsSP,
319
+ // /**
320
+ // * How many LRPs can be redeemed in a single Tx.
321
+ // */
322
+ // maxLrpsInTx: number,
323
+ // randomiseFn: (arr: [UTxO, RobDatum][]) => [UTxO, RobDatum][] = shuffle,
324
+ // ): [UTxO, RobDatum][] {
325
+ // if (targetLovelacesToSpend < lrpParams.minRedemptionLovelacesAmt) {
326
+ // throw new Error("Can't redeem less than the minimum.");
327
+ // }
328
+
329
+ // const shuffled = randomiseFn(
330
+ // F.pipe(
331
+ // allLrps,
332
+ // A.filter(
333
+ // ([_, datum]) =>
334
+ // toHex(datum.iasset) === toHex(iasset) &&
335
+ // datum.maxPrice.getOnChainInt >= iassetPrice.getOnChainInt,
336
+ // ),
337
+ // ),
338
+ // );
339
+
340
+ // // Sorted from highest to lowest by lovelaces to spend
341
+ // let result: [UTxO, RobDatum][] = [];
342
+ // let runningSum = 0n;
343
+
344
+ // for (let i = 0; i < shuffled.length; i++) {
345
+ // const element = shuffled[i];
346
+
347
+ // const lovelacesToSpend = lrpRedeemableLovelacesInclReimb(
348
+ // element,
349
+ // lrpParams,
350
+ // );
351
+
352
+ // // Do not add LRPs with smaller lovelacesToSpend than the minRedemption
353
+ // // to the random subset.
354
+ // if (lovelacesToSpend < lrpParams.minRedemptionLovelacesAmt) {
355
+ // continue;
356
+ // }
357
+
358
+ // // When we can't add a new redemption because otherwise the min redemption
359
+ // // wouldn't be satisfied.
360
+ // // Try to replace the smallest collected with a following larger one when available.
361
+ // if (
362
+ // result.length > 0 &&
363
+ // targetLovelacesToSpend - runningSum < lrpParams.minRedemptionLovelacesAmt
364
+ // ) {
365
+ // const last = result[result.length - 1];
366
+
367
+ // // Pop the smallest collected when the current is larger.
368
+ // if (lrpRedeemableLovelacesInclReimb(last, lrpParams) < lovelacesToSpend) {
369
+ // const popped = result.pop()!;
370
+ // runningSum -= lrpRedeemableLovelacesInclReimb(popped, lrpParams);
371
+ // } else {
372
+ // continue;
373
+ // }
374
+ // }
375
+
376
+ // result = insertSorted(
377
+ // result,
378
+ // element,
379
+ // Ord.contramap<bigint, [UTxO, RobDatum]>(
380
+ // ([_, dat]) => dat.lovelacesToSpend,
381
+ // // From highest to lowest
382
+ // )(Ord.reverse(BigIntOrd)),
383
+ // );
384
+ // runningSum += lovelacesToSpend;
385
+
386
+ // // When more items than max allowed, pop the one with smallest value
387
+ // if (result.length > maxLrpsInTx) {
388
+ // const popped = result.pop()!;
389
+ // runningSum -= lrpRedeemableLovelacesInclReimb(popped, lrpParams);
390
+ // }
391
+
392
+ // if (runningSum >= targetLovelacesToSpend) {
393
+ // return result;
394
+ // }
395
+ // }
396
+
397
+ // if (
398
+ // targetLovelacesToSpend - runningSum >=
399
+ // lrpParams.minRedemptionLovelacesAmt
400
+ // ) {
401
+ // throw new Error("Couldn't achieve target lovelaces");
402
+ // }
403
+
404
+ // return result;
405
+ // }
@@ -0,0 +1,35 @@
1
+ import { applyParamsToScript, SpendingValidator } from '@lucid-evolution/lucid';
2
+ import { castRobParams, RobParams } from './types';
3
+ import {
4
+ fromSystemParamsAssetLucid,
5
+ RobParamsSP,
6
+ } from '../../types/system-params';
7
+ import { _robValidator } from '../../validators/rob-validator';
8
+
9
+ export const mkRobValidator = (params: RobParams): SpendingValidator => {
10
+ return {
11
+ type: 'PlutusV3',
12
+ script: applyParamsToScript(_robValidator.cborHex, [castRobParams(params)]),
13
+ };
14
+ };
15
+
16
+ export const mkRobValidatorFromSP = (
17
+ params: RobParamsSP,
18
+ ): SpendingValidator => {
19
+ return {
20
+ type: 'PlutusV3',
21
+ script: applyParamsToScript(_robValidator.cborHex, [
22
+ castRobParams({
23
+ versionRecordToken: fromSystemParamsAssetLucid(
24
+ params.versionRecordToken,
25
+ ),
26
+ iassetValHash: params.iassetValHash,
27
+ iassetAuthToken: fromSystemParamsAssetLucid(params.iassetAuthToken),
28
+ collateralAssetAuthToken: fromSystemParamsAssetLucid(
29
+ params.collateralAssetAuthToken,
30
+ ),
31
+ iassetPolicyId: params.iassetPolicyId.unCurrencySymbol,
32
+ }),
33
+ ]),
34
+ };
35
+ };