@indigo-labs/indigo-sdk 0.1.21 → 0.1.23

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 (135) hide show
  1. package/.github/workflows/ci.yml +3 -8
  2. package/.github/workflows/test.yml +44 -0
  3. package/dist/index.d.mts +670 -1291
  4. package/dist/index.d.ts +670 -1291
  5. package/dist/index.js +2240 -4669
  6. package/dist/index.mjs +2221 -4618
  7. package/eslint.config.mjs +1 -7
  8. package/package.json +4 -9
  9. package/src/contracts/cdp.ts +746 -0
  10. package/src/contracts/collector.ts +98 -0
  11. package/src/contracts/gov.ts +1 -0
  12. package/src/contracts/interest-oracle.ts +149 -0
  13. package/src/contracts/{lrp/transactions.ts → lrp.ts} +14 -14
  14. package/src/contracts/{one-shot/transactions.ts → one-shot.ts} +3 -3
  15. package/src/contracts/stability-pool.ts +690 -0
  16. package/src/contracts/staking.ts +348 -0
  17. package/src/contracts/treasury.ts +112 -0
  18. package/src/helpers/asset-helpers.ts +63 -0
  19. package/src/{utils → helpers}/helper-txs.ts +0 -1
  20. package/src/{utils/utils.ts → helpers/helpers.ts} +10 -0
  21. package/src/{contracts/interest-oracle/helpers.ts → helpers/interest-oracle.ts} +9 -37
  22. package/src/{contracts/stability-pool/helpers.ts → helpers/stability-pool-helpers.ts} +6 -110
  23. package/src/helpers/staking-helpers.ts +94 -0
  24. package/src/helpers/time-helpers.ts +4 -0
  25. package/src/{utils → helpers}/value-helpers.ts +0 -10
  26. package/src/index.ts +33 -38
  27. package/src/{validators → scripts}/cdp-creator-validator.ts +50 -4
  28. package/src/{validators → scripts}/cdp-validator.ts +5 -3
  29. package/src/{validators → scripts}/collector-validator.ts +3 -2
  30. package/src/scripts/execute-validator.ts +52 -0
  31. package/src/{validators/governance-validator.ts → scripts/gov-validator.ts} +40 -3
  32. package/src/{validators → scripts}/interest-oracle-validator.ts +20 -4
  33. package/src/scripts/lrp-validator.ts +40 -0
  34. package/src/{contracts/one-shot/scripts.ts → scripts/one-shot-policy.ts} +1 -1
  35. package/src/scripts/poll-manager-validator.ts +52 -0
  36. package/src/{validators → scripts}/poll-shard-validator.ts +43 -3
  37. package/src/{contracts/price-oracle/scripts.ts → scripts/price-oracle-validator.ts} +4 -1
  38. package/src/{validators → scripts}/stability-pool-validator.ts +57 -4
  39. package/src/{validators → scripts}/staking-validator.ts +3 -2
  40. package/src/{validators → scripts}/treasury-validator.ts +3 -2
  41. package/src/{validators → scripts}/version-record-policy.ts +23 -4
  42. package/src/{validators/execute-validator.ts → scripts/version-registry.ts} +11 -3
  43. package/src/types/generic.ts +60 -78
  44. package/src/{contracts/cdp-creator/types.ts → types/indigo/cdp-creator.ts} +4 -6
  45. package/src/types/indigo/cdp.ts +88 -0
  46. package/src/types/indigo/execute.ts +21 -0
  47. package/src/types/indigo/gov.ts +51 -0
  48. package/src/{contracts/interest-oracle/types.ts → types/indigo/interest-oracle.ts} +1 -1
  49. package/src/{contracts/lrp/types.ts → types/indigo/lrp.ts} +2 -2
  50. package/src/types/indigo/poll-manager.ts +21 -0
  51. package/src/types/indigo/poll-shard.ts +16 -0
  52. package/src/{contracts/price-oracle/types.ts → types/indigo/price-oracle.ts} +4 -16
  53. package/src/types/indigo/stability-pool.ts +233 -0
  54. package/src/types/indigo/staking.ts +99 -0
  55. package/src/{contracts/version-registry/types.ts → types/indigo/version-record.ts} +1 -1
  56. package/src/types/on-chain-decimal.ts +0 -22
  57. package/src/types/system-params.ts +11 -22
  58. package/tests/datums.test.ts +108 -125
  59. package/tests/endpoints/initialize.ts +338 -240
  60. package/tests/hash-checks.test.ts +21 -26
  61. package/tests/indigo-test-helpers.ts +55 -1
  62. package/tests/initialize.test.ts +5 -10
  63. package/tests/interest-calculations.test.ts +18 -18
  64. package/tests/interest-oracle.test.ts +18 -20
  65. package/tests/lrp.test.ts +65 -191
  66. package/tests/queries/governance-queries.ts +16 -19
  67. package/tests/queries/iasset-queries.ts +23 -46
  68. package/tests/queries/interest-oracle-queries.ts +6 -3
  69. package/tests/queries/lrp-queries.ts +2 -2
  70. package/tests/queries/price-oracle-queries.ts +22 -5
  71. package/tests/queries/stability-pool-queries.ts +8 -10
  72. package/tests/queries/staking-queries.ts +19 -28
  73. package/tests/stability-pool.test.ts +71 -186
  74. package/tests/staking.test.ts +23 -30
  75. package/tests/test-helpers.ts +2 -11
  76. package/tsconfig.json +1 -3
  77. package/vitest.config.ts +1 -1
  78. package/src/contracts/cdp/helpers.ts +0 -167
  79. package/src/contracts/cdp/scripts.ts +0 -33
  80. package/src/contracts/cdp/transactions.ts +0 -1310
  81. package/src/contracts/cdp/types.ts +0 -161
  82. package/src/contracts/cdp-creator/scripts.ts +0 -39
  83. package/src/contracts/collector/scripts.ts +0 -32
  84. package/src/contracts/collector/transactions.ts +0 -44
  85. package/src/contracts/execute/scripts.ts +0 -48
  86. package/src/contracts/execute/types.ts +0 -57
  87. package/src/contracts/gov/helpers.ts +0 -157
  88. package/src/contracts/gov/scripts.ts +0 -34
  89. package/src/contracts/gov/transactions.ts +0 -1224
  90. package/src/contracts/gov/types-new.ts +0 -115
  91. package/src/contracts/gov/types.ts +0 -89
  92. package/src/contracts/interest-oracle/scripts.ts +0 -18
  93. package/src/contracts/interest-oracle/transactions.ts +0 -149
  94. package/src/contracts/lrp/scripts.ts +0 -27
  95. package/src/contracts/poll/helpers.ts +0 -55
  96. package/src/contracts/poll/scripts.ts +0 -72
  97. package/src/contracts/poll/types-poll-manager.ts +0 -38
  98. package/src/contracts/poll/types-poll-shard.ts +0 -38
  99. package/src/contracts/poll/types-poll.ts +0 -88
  100. package/src/contracts/price-oracle/transactions.ts +0 -112
  101. package/src/contracts/stability-pool/scripts.ts +0 -46
  102. package/src/contracts/stability-pool/transactions.ts +0 -660
  103. package/src/contracts/stability-pool/types-new.ts +0 -208
  104. package/src/contracts/stability-pool/types.ts +0 -42
  105. package/src/contracts/staking/helpers.ts +0 -116
  106. package/src/contracts/staking/scripts.ts +0 -41
  107. package/src/contracts/staking/transactions.ts +0 -268
  108. package/src/contracts/staking/types-new.ts +0 -81
  109. package/src/contracts/staking/types.ts +0 -41
  110. package/src/contracts/treasury/scripts.ts +0 -37
  111. package/src/contracts/treasury/transactions.ts +0 -44
  112. package/src/contracts/treasury/types.ts +0 -55
  113. package/src/contracts/version-registry/scripts.ts +0 -29
  114. package/src/contracts/version-registry/types-new.ts +0 -19
  115. package/src/contracts/vesting/helpers.ts +0 -267
  116. package/src/types/evolution-schema-options.ts +0 -16
  117. package/src/utils/bigint-utils.ts +0 -7
  118. package/src/utils/time-helpers.ts +0 -4
  119. package/src/validators/lrp-validator.ts +0 -7
  120. package/src/validators/poll-manager-validator.ts +0 -7
  121. package/src/validators/version-registry-validator.ts +0 -7
  122. package/tests/cdp.test.ts +0 -1565
  123. package/tests/gov.test.ts +0 -1874
  124. package/tests/mock/assets-mock.ts +0 -59
  125. package/tests/queries/cdp-queries.ts +0 -144
  126. package/tests/queries/collector-queries.ts +0 -26
  127. package/tests/queries/execute-queries.ts +0 -46
  128. package/tests/queries/poll-queries.ts +0 -97
  129. package/tests/queries/treasury-queries.ts +0 -19
  130. package/tests/utils/asserts.ts +0 -13
  131. package/tests/utils/index.ts +0 -50
  132. /package/src/{utils → helpers}/indigo-helpers.ts +0 -0
  133. /package/src/{utils → helpers}/lucid-utils.ts +0 -0
  134. /package/src/{contracts/price-oracle/helpers.ts → helpers/price-oracle-helpers.ts} +0 -0
  135. /package/src/{contracts/one-shot/types.ts → types/one-shot.ts} +0 -0
@@ -0,0 +1,746 @@
1
+ import {
2
+ applyParamsToScript,
3
+ Assets,
4
+ Constr,
5
+ Credential,
6
+ credentialToAddress,
7
+ Data,
8
+ fromText,
9
+ LucidEvolution,
10
+ OutRef,
11
+ slotToUnixTime,
12
+ SpendingValidator,
13
+ TxBuilder,
14
+ UTxO,
15
+ validatorToAddress,
16
+ validatorToScriptHash,
17
+ } from '@lucid-evolution/lucid';
18
+ import {
19
+ CdpParams,
20
+ ScriptReferences,
21
+ SystemParams,
22
+ } from '../types/system-params';
23
+ import { IAssetHelpers, IAssetOutput } from '../helpers/asset-helpers';
24
+ import { CollectorContract } from './collector';
25
+ import { TreasuryContract } from './treasury';
26
+ import { addrDetails, scriptRef } from '../helpers/lucid-utils';
27
+ import {
28
+ calculateFeeFromPercentage,
29
+ getRandomElement,
30
+ } from '../helpers/helpers';
31
+ import {
32
+ CDPContent,
33
+ parseCDPDatum,
34
+ serialiseCDPDatum,
35
+ } from '../types/indigo/cdp';
36
+ import { _cdpValidator } from '../scripts/cdp-validator';
37
+ import { parsePriceOracleDatum } from '../types/indigo/price-oracle';
38
+ import { parseInterestOracleDatum } from '../types/indigo/interest-oracle';
39
+ import { castCDPCreatorRedeemer } from '../types/indigo/cdp-creator';
40
+ import { parseGovDatum } from '../types/indigo/gov';
41
+ import {
42
+ calculateAccruedInterest,
43
+ calculateUnitaryInterestSinceOracleLastUpdated,
44
+ } from '../helpers/interest-oracle';
45
+ import { oracleExpirationAwareValidity } from '../helpers/price-oracle-helpers';
46
+
47
+ export class CDPContract {
48
+ static async openPosition(
49
+ asset: string,
50
+ collateralAmount: bigint,
51
+ mintedAmount: bigint,
52
+ params: SystemParams,
53
+ lucid: LucidEvolution,
54
+ currentSlot: number,
55
+ assetRef?: OutRef,
56
+ priceOracleRef?: OutRef,
57
+ interestOracleRef?: OutRef,
58
+ cdpCreatorRef?: OutRef,
59
+ collectorRef?: OutRef,
60
+ ): Promise<TxBuilder> {
61
+ const network = lucid.config().network;
62
+ const currentTime = BigInt(slotToUnixTime(network, currentSlot));
63
+
64
+ const [pkh, skh] = await addrDetails(lucid);
65
+ const assetOut: IAssetOutput = await (assetRef
66
+ ? IAssetHelpers.findIAssetByRef(assetRef, lucid)
67
+ : IAssetHelpers.findIAssetByName(asset, params, lucid));
68
+ if (!assetOut || !assetOut.datum) throw new Error('Unable to find IAsset');
69
+ // Fail if delisted asset
70
+ if ('Delisted' in assetOut.datum.price)
71
+ return Promise.reject(
72
+ new Error('Trying to open CDP against delisted asset'),
73
+ );
74
+
75
+ const oracleAsset = assetOut.datum.price.Oracle.oracleNft.asset;
76
+ const oracleOut = priceOracleRef
77
+ ? (await lucid.utxosByOutRef([priceOracleRef]))[0]
78
+ : await lucid.utxoByUnit(
79
+ oracleAsset.currencySymbol + oracleAsset.tokenName,
80
+ );
81
+ if (!oracleOut.datum)
82
+ return Promise.reject(new Error('Price Oracle datum not found'));
83
+ const oracleDatum = parsePriceOracleDatum(oracleOut.datum);
84
+
85
+ const interestOracleAsset = assetOut.datum.interestOracleNft;
86
+ const interestOracleOut = interestOracleRef
87
+ ? (await lucid.utxosByOutRef([interestOracleRef]))[0]
88
+ : await lucid.utxoByUnit(
89
+ interestOracleAsset.currencySymbol + interestOracleAsset.tokenName,
90
+ );
91
+ if (!interestOracleOut.datum)
92
+ return Promise.reject(new Error('Interest Oracle datum not found'));
93
+ const interestOracleDatum = parseInterestOracleDatum(
94
+ interestOracleOut.datum,
95
+ );
96
+
97
+ const cdpCreatorOut = getRandomElement(
98
+ cdpCreatorRef
99
+ ? await lucid.utxosByOutRef([cdpCreatorRef])
100
+ : await lucid.utxosAtWithUnit(
101
+ credentialToAddress(network, {
102
+ type: 'Script',
103
+ hash: params.validatorHashes.cdpCreatorHash,
104
+ }),
105
+ params.cdpCreatorParams.cdpCreatorNft[0].unCurrencySymbol +
106
+ fromText(params.cdpCreatorParams.cdpCreatorNft[1].unTokenName),
107
+ ),
108
+ );
109
+
110
+ const cdpCreatorRedeemer = castCDPCreatorRedeemer({
111
+ CreateCDP: {
112
+ cdpOwner: pkh.hash,
113
+ minted: mintedAmount,
114
+ collateral: collateralAmount,
115
+ currentTime: currentTime,
116
+ },
117
+ });
118
+
119
+ const cdpCreatorScriptRefUtxo = await scriptRef(
120
+ params.scriptReferences.cdpCreatorValidatorRef,
121
+ lucid,
122
+ );
123
+
124
+ const cdpAddress = CDPContract.address(params.cdpParams, lucid, skh);
125
+ const cdpToken =
126
+ params.cdpParams.cdpAuthToken[0].unCurrencySymbol +
127
+ fromText(params.cdpParams.cdpAuthToken[1].unTokenName);
128
+
129
+ const cdpValue: Assets = {
130
+ lovelace: collateralAmount,
131
+ };
132
+ cdpValue[cdpToken] = 1n;
133
+ const newSnapshot =
134
+ calculateUnitaryInterestSinceOracleLastUpdated(
135
+ currentTime,
136
+ interestOracleDatum,
137
+ ) + interestOracleDatum.unitaryInterest;
138
+ const cdpDatum: CDPContent = {
139
+ cdpOwner: pkh.hash,
140
+ iasset: fromText(asset),
141
+ mintedAmt: mintedAmount,
142
+ cdpFees: {
143
+ ActiveCDPInterestTracking: {
144
+ lastSettled: currentTime,
145
+ unitaryInterestSnapshot: newSnapshot,
146
+ },
147
+ },
148
+ };
149
+
150
+ const assetToken =
151
+ params.cdpParams.cdpAssetSymbol.unCurrencySymbol + fromText(asset);
152
+ const cdpTokenMintValue: Assets = {};
153
+ cdpTokenMintValue[cdpToken] = 1n;
154
+ const iassetTokenMintValue: Assets = {};
155
+ iassetTokenMintValue[assetToken] = BigInt(mintedAmount);
156
+
157
+ const cdpAuthTokenScriptRefUtxo = await CDPContract.cdpAuthTokenRef(
158
+ params.scriptReferences,
159
+ lucid,
160
+ );
161
+ const iAssetTokenScriptRefUtxo = await CDPContract.assetTokenRef(
162
+ params.scriptReferences,
163
+ lucid,
164
+ );
165
+
166
+ const debtMintingFee = calculateFeeFromPercentage(
167
+ BigInt(assetOut.datum.debtMintingFeePercentage.getOnChainInt),
168
+ (mintedAmount * oracleDatum.price.getOnChainInt) / 1_000_000n,
169
+ );
170
+
171
+ const txValidity = oracleExpirationAwareValidity(
172
+ currentSlot,
173
+ Number(params.cdpCreatorParams.biasTime),
174
+ Number(oracleDatum.expiration),
175
+ network,
176
+ );
177
+
178
+ const tx = lucid
179
+ .newTx()
180
+ .collectFrom([cdpCreatorOut], Data.to(cdpCreatorRedeemer))
181
+ .readFrom([cdpCreatorScriptRefUtxo])
182
+ .pay.ToContract(
183
+ cdpAddress,
184
+ { kind: 'inline', value: serialiseCDPDatum(cdpDatum) },
185
+ cdpValue,
186
+ )
187
+ .pay.ToContract(
188
+ cdpCreatorOut.address,
189
+ { kind: 'inline', value: cdpCreatorOut.datum },
190
+ cdpCreatorOut.assets,
191
+ )
192
+ .readFrom([oracleOut, interestOracleOut, assetOut.utxo])
193
+ .mintAssets(cdpTokenMintValue, Data.to(new Constr(0, [])))
194
+ .readFrom([cdpAuthTokenScriptRefUtxo])
195
+ .mintAssets(iassetTokenMintValue, Data.to(new Constr(0, [])))
196
+ .readFrom([iAssetTokenScriptRefUtxo])
197
+ .addSignerKey(pkh.hash)
198
+ .validFrom(txValidity.validFrom)
199
+ .validTo(txValidity.validTo);
200
+
201
+ if (debtMintingFee > 0) {
202
+ await CollectorContract.feeTx(
203
+ debtMintingFee,
204
+ lucid,
205
+ params,
206
+ tx,
207
+ collectorRef,
208
+ );
209
+ }
210
+ return tx;
211
+ }
212
+
213
+ static async deposit(
214
+ cdpRef: OutRef,
215
+ amount: bigint,
216
+ params: SystemParams,
217
+ lucid: LucidEvolution,
218
+ currentSlot: number,
219
+ assetRef?: OutRef,
220
+ priceOracleRef?: OutRef,
221
+ interestOracleRef?: OutRef,
222
+ collectorRef?: OutRef,
223
+ govRef?: OutRef,
224
+ treasuryRef?: OutRef,
225
+ ): Promise<TxBuilder> {
226
+ return CDPContract.adjust(
227
+ cdpRef,
228
+ amount,
229
+ 0n,
230
+ params,
231
+ lucid,
232
+ currentSlot,
233
+ assetRef,
234
+ priceOracleRef,
235
+ interestOracleRef,
236
+ collectorRef,
237
+ govRef,
238
+ treasuryRef,
239
+ );
240
+ }
241
+
242
+ static async withdraw(
243
+ cdpRef: OutRef,
244
+ amount: bigint,
245
+ params: SystemParams,
246
+ lucid: LucidEvolution,
247
+ currentSlot: number,
248
+ assetRef?: OutRef,
249
+ priceOracleRef?: OutRef,
250
+ interestOracleRef?: OutRef,
251
+ collectorRef?: OutRef,
252
+ govRef?: OutRef,
253
+ treasuryRef?: OutRef,
254
+ ): Promise<TxBuilder> {
255
+ return CDPContract.adjust(
256
+ cdpRef,
257
+ -amount,
258
+ 0n,
259
+ params,
260
+ lucid,
261
+ currentSlot,
262
+ assetRef,
263
+ priceOracleRef,
264
+ interestOracleRef,
265
+ collectorRef,
266
+ govRef,
267
+ treasuryRef,
268
+ );
269
+ }
270
+
271
+ static async mint(
272
+ cdpRef: OutRef,
273
+ amount: bigint,
274
+ params: SystemParams,
275
+ lucid: LucidEvolution,
276
+ currentSlot: number,
277
+ assetRef?: OutRef,
278
+ priceOracleRef?: OutRef,
279
+ interestOracleRef?: OutRef,
280
+ collectorRef?: OutRef,
281
+ govRef?: OutRef,
282
+ treasuryRef?: OutRef,
283
+ ): Promise<TxBuilder> {
284
+ return CDPContract.adjust(
285
+ cdpRef,
286
+ 0n,
287
+ amount,
288
+ params,
289
+ lucid,
290
+ currentSlot,
291
+ assetRef,
292
+ priceOracleRef,
293
+ interestOracleRef,
294
+ collectorRef,
295
+ govRef,
296
+ treasuryRef,
297
+ );
298
+ }
299
+
300
+ static async burn(
301
+ cdpRef: OutRef,
302
+ amount: bigint,
303
+ params: SystemParams,
304
+ lucid: LucidEvolution,
305
+ currentSlot: number,
306
+ assetRef?: OutRef,
307
+ priceOracleRef?: OutRef,
308
+ interestOracleRef?: OutRef,
309
+ collectorRef?: OutRef,
310
+ govRef?: OutRef,
311
+ treasuryRef?: OutRef,
312
+ ): Promise<TxBuilder> {
313
+ return CDPContract.adjust(
314
+ cdpRef,
315
+ 0n,
316
+ -amount,
317
+ params,
318
+ lucid,
319
+ currentSlot,
320
+ assetRef,
321
+ priceOracleRef,
322
+ interestOracleRef,
323
+ collectorRef,
324
+ govRef,
325
+ treasuryRef,
326
+ );
327
+ }
328
+
329
+ static async adjust(
330
+ cdpRef: OutRef,
331
+ collateralAmount: bigint,
332
+ mintAmount: bigint,
333
+ params: SystemParams,
334
+ lucid: LucidEvolution,
335
+ currentSlot: number,
336
+ assetRef?: OutRef,
337
+ priceOracleRef?: OutRef,
338
+ interestOracleRef?: OutRef,
339
+ collectorRef?: OutRef,
340
+ govRef?: OutRef,
341
+ treasuryRef?: OutRef,
342
+ ): Promise<TxBuilder> {
343
+ const network = lucid.config().network;
344
+ // Find Pkh, Skh
345
+ const currentTime = BigInt(slotToUnixTime(network, currentSlot));
346
+
347
+ // Find Outputs: iAsset Output, CDP Output, Gov Output
348
+ const cdp = (await lucid.utxosByOutRef([cdpRef]))[0];
349
+ if (!cdp.datum) throw new Error('Unable to find CDP Datum');
350
+ const cdpDatum = parseCDPDatum(cdp.datum);
351
+ const iAsset = await (assetRef
352
+ ? IAssetHelpers.findIAssetByRef(assetRef, lucid)
353
+ : IAssetHelpers.findIAssetByName(cdpDatum.iasset, params, lucid));
354
+
355
+ const gov = govRef
356
+ ? (await lucid.utxosByOutRef([govRef]))[0]
357
+ : await lucid.utxoByUnit(
358
+ params.govParams.govNFT[0].unCurrencySymbol +
359
+ fromText(params.govParams.govNFT[1].unTokenName),
360
+ );
361
+ if (!gov.datum) throw new Error('Unable to find Gov Datum');
362
+ const govData = parseGovDatum(gov.datum);
363
+ const cdpScriptRefUtxo = await CDPContract.scriptRef(
364
+ params.scriptReferences,
365
+ lucid,
366
+ );
367
+ const cdpAssets = Object.assign({}, cdp.assets);
368
+ cdpAssets['lovelace'] = cdp.assets['lovelace'] + collateralAmount;
369
+
370
+ const interestOracleAsset = iAsset.datum.interestOracleNft;
371
+ const interestOracleOut = interestOracleRef
372
+ ? (await lucid.utxosByOutRef([interestOracleRef]))[0]
373
+ : await lucid.utxoByUnit(
374
+ interestOracleAsset.currencySymbol +
375
+ fromText(interestOracleAsset.tokenName),
376
+ );
377
+ if (!interestOracleOut.datum)
378
+ return Promise.reject(new Error('Interest Oracle datum not found'));
379
+ const interestOracleDatum = parseInterestOracleDatum(
380
+ interestOracleOut.datum,
381
+ );
382
+
383
+ const tx = lucid
384
+ .newTx()
385
+ .collectFrom(
386
+ [cdp],
387
+ Data.to(new Constr(0, [currentTime, mintAmount, collateralAmount])),
388
+ )
389
+ .readFrom([iAsset.utxo, gov, cdpScriptRefUtxo])
390
+ .addSignerKey(cdpDatum.cdpOwner);
391
+ if (!cdp.datum) throw new Error('Unable to find CDP Datum');
392
+ const cdpD = parseCDPDatum(cdp.datum);
393
+
394
+ if (!('ActiveCDPInterestTracking' in cdpD.cdpFees))
395
+ throw new Error('Invalid CDP Fees');
396
+
397
+ const newSnapshot =
398
+ calculateUnitaryInterestSinceOracleLastUpdated(
399
+ currentTime,
400
+ interestOracleDatum,
401
+ ) + interestOracleDatum.unitaryInterest;
402
+
403
+ const cdpD_: CDPContent = {
404
+ ...cdpD,
405
+ mintedAmt: cdpD.mintedAmt + mintAmount,
406
+ cdpFees: {
407
+ ActiveCDPInterestTracking: {
408
+ lastSettled: currentTime,
409
+ unitaryInterestSnapshot: newSnapshot,
410
+ },
411
+ },
412
+ };
413
+
414
+ tx.pay.ToContract(
415
+ cdp.address,
416
+ {
417
+ kind: 'inline',
418
+ value: serialiseCDPDatum(cdpD_),
419
+ },
420
+ cdpAssets,
421
+ );
422
+
423
+ // Find Oracle Ref Input
424
+ const oracleAsset = iAsset.datum.price;
425
+ if (!('Oracle' in oracleAsset)) throw new Error('Invalid oracle asset');
426
+ const oracleRefInput = priceOracleRef
427
+ ? (await lucid.utxosByOutRef([priceOracleRef]))[0]
428
+ : await lucid.utxoByUnit(
429
+ oracleAsset.Oracle.oracleNft.asset.currencySymbol +
430
+ fromText(oracleAsset.Oracle.oracleNft.asset.tokenName),
431
+ );
432
+
433
+ // Fail if delisted asset
434
+ if (!oracleRefInput.datum)
435
+ return Promise.reject(new Error('Invalid oracle input'));
436
+ const od = parsePriceOracleDatum(oracleRefInput.datum);
437
+ if (!od) return Promise.reject(new Error('Invalid oracle input'));
438
+
439
+ const txValidity = oracleExpirationAwareValidity(
440
+ currentSlot,
441
+ Number(params.cdpCreatorParams.biasTime),
442
+ Number(od.expiration),
443
+ network,
444
+ );
445
+ tx.readFrom([oracleRefInput])
446
+ .validFrom(txValidity.validFrom)
447
+ .validTo(txValidity.validTo);
448
+
449
+ let fee = 0n;
450
+ if (collateralAmount < 0) {
451
+ fee += calculateFeeFromPercentage(
452
+ govData.protocolParams.collateralFeePercentage.getOnChainInt,
453
+ collateralAmount,
454
+ );
455
+ }
456
+
457
+ if (mintAmount > 0) {
458
+ fee += calculateFeeFromPercentage(
459
+ iAsset.datum.debtMintingFeePercentage.getOnChainInt,
460
+ (mintAmount * od.price.getOnChainInt) / 1_000_000n,
461
+ );
462
+ }
463
+
464
+ // Interest payment
465
+
466
+ const interestPaymentAsset = calculateAccruedInterest(
467
+ currentTime,
468
+ cdpD.cdpFees.ActiveCDPInterestTracking.unitaryInterestSnapshot,
469
+ cdpD.mintedAmt,
470
+ cdpD.cdpFees.ActiveCDPInterestTracking.lastSettled,
471
+ interestOracleDatum,
472
+ );
473
+ const interestPayment =
474
+ (interestPaymentAsset * od.price.getOnChainInt) / 1_000_000n;
475
+ const interestCollectorPayment = calculateFeeFromPercentage(
476
+ iAsset.datum.interestCollectorPortionPercentage.getOnChainInt,
477
+ interestPayment,
478
+ );
479
+ const interestTreasuryPayment = interestPayment - interestCollectorPayment;
480
+
481
+ if (interestTreasuryPayment > 0) {
482
+ await TreasuryContract.feeTx(
483
+ interestTreasuryPayment,
484
+ lucid,
485
+ params,
486
+ tx,
487
+ treasuryRef,
488
+ );
489
+ }
490
+
491
+ fee += interestCollectorPayment;
492
+ tx.readFrom([interestOracleOut]);
493
+
494
+ if (mintAmount !== 0n) {
495
+ const iAssetTokenScriptRefUtxo = await CDPContract.assetTokenRef(
496
+ params.scriptReferences,
497
+ lucid,
498
+ );
499
+ const iassetToken =
500
+ params.cdpParams.cdpAssetSymbol.unCurrencySymbol + cdpD.iasset;
501
+ const mintValue = {} as Assets;
502
+ mintValue[iassetToken] = mintAmount;
503
+
504
+ tx.readFrom([iAssetTokenScriptRefUtxo]).mintAssets(
505
+ mintValue,
506
+ Data.to(new Constr(0, [])),
507
+ );
508
+ }
509
+
510
+ if (fee > 0n) {
511
+ await CollectorContract.feeTx(fee, lucid, params, tx, collectorRef);
512
+ }
513
+
514
+ return tx;
515
+ }
516
+
517
+ static async close(
518
+ cdpRef: OutRef,
519
+ params: SystemParams,
520
+ lucid: LucidEvolution,
521
+ currentSlot: number,
522
+ assetRef?: OutRef,
523
+ priceOracleRef?: OutRef,
524
+ interestOracleRef?: OutRef,
525
+ collectorRef?: OutRef,
526
+ govRef?: OutRef,
527
+ treasuryRef?: OutRef,
528
+ ): Promise<TxBuilder> {
529
+ const network = lucid.config().network;
530
+ // Find Pkh, Skh
531
+ const currentTime = BigInt(slotToUnixTime(network, currentSlot));
532
+
533
+ // Find Outputs: iAsset Output, CDP Output, Gov Output
534
+ const cdp = (await lucid.utxosByOutRef([cdpRef]))[0];
535
+ if (!cdp.datum) throw new Error('Unable to find CDP Datum');
536
+ const cdpDatum = parseCDPDatum(cdp.datum);
537
+ const iAsset = await (assetRef
538
+ ? IAssetHelpers.findIAssetByRef(assetRef, lucid)
539
+ : IAssetHelpers.findIAssetByName(cdpDatum.iasset, params, lucid));
540
+
541
+ const gov = govRef
542
+ ? (await lucid.utxosByOutRef([govRef]))[0]
543
+ : await lucid.utxoByUnit(
544
+ params.govParams.govNFT[0].unCurrencySymbol +
545
+ fromText(params.govParams.govNFT[1].unTokenName),
546
+ );
547
+
548
+ if (!gov.datum) throw new Error('Unable to find Gov Datum');
549
+ const cdpScriptRefUtxo = await CDPContract.scriptRef(
550
+ params.scriptReferences,
551
+ lucid,
552
+ );
553
+
554
+ const interestOracleAsset = iAsset.datum.interestOracleNft;
555
+ const interestOracleOut = interestOracleRef
556
+ ? (await lucid.utxosByOutRef([interestOracleRef]))[0]
557
+ : await lucid.utxoByUnit(
558
+ interestOracleAsset.currencySymbol +
559
+ fromText(interestOracleAsset.tokenName),
560
+ );
561
+ if (!interestOracleOut.datum)
562
+ return Promise.reject(new Error('Interest Oracle datum not found'));
563
+ const interestOracleDatum = parseInterestOracleDatum(
564
+ interestOracleOut.datum,
565
+ );
566
+
567
+ const tx = lucid
568
+ .newTx()
569
+ .collectFrom([cdp], Data.to(new Constr(1, [currentTime])))
570
+ .readFrom([iAsset.utxo, gov, cdpScriptRefUtxo])
571
+ .addSignerKey(cdpDatum.cdpOwner);
572
+ if (!cdp.datum) throw new Error('Unable to find CDP Datum');
573
+ const cdpD = parseCDPDatum(cdp.datum);
574
+ if (!('ActiveCDPInterestTracking' in cdpD.cdpFees))
575
+ throw new Error('Invalid CDP Fees');
576
+
577
+ // Find Oracle Ref Input
578
+ if (!('Oracle' in iAsset.datum.price))
579
+ throw new Error('iAsset is delisted');
580
+ const oracleAsset = iAsset.datum.price.Oracle.oracleNft.asset;
581
+ const oracleRefInput = priceOracleRef
582
+ ? (await lucid.utxosByOutRef([priceOracleRef]))[0]
583
+ : await lucid.utxoByUnit(
584
+ oracleAsset.currencySymbol + fromText(oracleAsset.tokenName),
585
+ );
586
+
587
+ // Fail if delisted asset
588
+ if (!oracleRefInput.datum)
589
+ return Promise.reject(new Error('Invalid oracle input'));
590
+ const od = parsePriceOracleDatum(oracleRefInput.datum);
591
+
592
+ const txValidity = oracleExpirationAwareValidity(
593
+ currentSlot,
594
+ Number(params.cdpCreatorParams.biasTime),
595
+ Number(od.expiration),
596
+ network,
597
+ );
598
+ tx.readFrom([oracleRefInput])
599
+ .validFrom(txValidity.validFrom)
600
+ .validTo(txValidity.validTo);
601
+
602
+ let fee = 0n;
603
+
604
+ // Interest payment
605
+ const interestPaymentAsset = calculateAccruedInterest(
606
+ currentTime,
607
+ cdpD.cdpFees.ActiveCDPInterestTracking.unitaryInterestSnapshot,
608
+ cdpD.mintedAmt,
609
+ cdpD.cdpFees.ActiveCDPInterestTracking.lastSettled,
610
+ interestOracleDatum,
611
+ );
612
+ const interestPayment =
613
+ (interestPaymentAsset * od.price.getOnChainInt) / 1_000_000n;
614
+ const interestCollectorPayment = calculateFeeFromPercentage(
615
+ iAsset.datum.interestCollectorPortionPercentage.getOnChainInt,
616
+ interestPayment,
617
+ );
618
+ const interestTreasuryPayment = interestPayment - interestCollectorPayment;
619
+
620
+ if (interestTreasuryPayment > 0) {
621
+ await TreasuryContract.feeTx(
622
+ interestTreasuryPayment,
623
+ lucid,
624
+ params,
625
+ tx,
626
+ treasuryRef,
627
+ );
628
+ }
629
+
630
+ fee += interestCollectorPayment;
631
+ tx.readFrom([interestOracleOut]);
632
+
633
+ const iAssetTokenScriptRefUtxo = await CDPContract.assetTokenRef(
634
+ params.scriptReferences,
635
+ lucid,
636
+ );
637
+ const iassetToken =
638
+ params.cdpParams.cdpAssetSymbol.unCurrencySymbol + cdpD.iasset;
639
+ const assetBurnValue = {} as Assets;
640
+ assetBurnValue[iassetToken] = -BigInt(cdpD.mintedAmt);
641
+ const cdpTokenBurnValue = {} as Assets;
642
+ cdpTokenBurnValue[
643
+ params.cdpParams.cdpAuthToken[0].unCurrencySymbol +
644
+ fromText(params.cdpParams.cdpAuthToken[1].unTokenName)
645
+ ] = -1n;
646
+ const cdpAuthTokenScriptRefUtxo = await CDPContract.cdpAuthTokenRef(
647
+ params.scriptReferences,
648
+ lucid,
649
+ );
650
+ tx.readFrom([iAssetTokenScriptRefUtxo])
651
+ .mintAssets(assetBurnValue, Data.to(new Constr(0, [])))
652
+ .readFrom([cdpAuthTokenScriptRefUtxo])
653
+ .mintAssets(cdpTokenBurnValue, Data.to(new Constr(0, [])));
654
+
655
+ if (fee > 0n) {
656
+ await CollectorContract.feeTx(fee, lucid, params, tx, collectorRef);
657
+ }
658
+
659
+ return tx;
660
+ }
661
+
662
+ static validator(params: CdpParams): SpendingValidator {
663
+ return {
664
+ type: _cdpValidator.type,
665
+ script: applyParamsToScript(_cdpValidator.cborHex, [
666
+ new Constr(0, [
667
+ new Constr(0, [
668
+ params.cdpAuthToken[0].unCurrencySymbol,
669
+ fromText(params.cdpAuthToken[1].unTokenName),
670
+ ]),
671
+ params.cdpAssetSymbol.unCurrencySymbol,
672
+ new Constr(0, [
673
+ params.iAssetAuthToken[0].unCurrencySymbol,
674
+ fromText(params.iAssetAuthToken[1].unTokenName),
675
+ ]),
676
+ new Constr(0, [
677
+ params.stabilityPoolAuthToken[0].unCurrencySymbol,
678
+ fromText(params.stabilityPoolAuthToken[1].unTokenName),
679
+ ]),
680
+ new Constr(0, [
681
+ params.versionRecordToken[0].unCurrencySymbol,
682
+ fromText(params.versionRecordToken[1].unTokenName),
683
+ ]),
684
+ new Constr(0, [
685
+ params.upgradeToken[0].unCurrencySymbol,
686
+ fromText(params.upgradeToken[1].unTokenName),
687
+ ]),
688
+ params.collectorValHash,
689
+ params.spValHash,
690
+ new Constr(0, [
691
+ params.govNFT[0].unCurrencySymbol,
692
+ fromText(params.govNFT[1].unTokenName),
693
+ ]),
694
+ BigInt(params.minCollateralInLovelace),
695
+ BigInt(params.partialRedemptionExtraFeeLovelace),
696
+ BigInt(params.biasTime),
697
+ params.treasuryValHash,
698
+ ]),
699
+ ]),
700
+ };
701
+ }
702
+
703
+ static validatorHash(params: CdpParams): string {
704
+ return validatorToScriptHash(CDPContract.validator(params));
705
+ }
706
+
707
+ static address(
708
+ cdpParams: CdpParams,
709
+ lucid: LucidEvolution,
710
+ skh?: Credential,
711
+ ) {
712
+ const network = lucid.config().network;
713
+ if (!network) {
714
+ throw new Error('Network configuration is undefined');
715
+ }
716
+ return validatorToAddress(network, CDPContract.validator(cdpParams), skh);
717
+ }
718
+
719
+ static scriptRef(
720
+ params: ScriptReferences,
721
+ lucid: LucidEvolution,
722
+ ): Promise<UTxO> {
723
+ return scriptRef(params.cdpValidatorRef, lucid);
724
+ }
725
+
726
+ static cdpAuthTokenRef(
727
+ params: ScriptReferences,
728
+ lucid: LucidEvolution,
729
+ ): Promise<UTxO> {
730
+ return scriptRef(params.authTokenPolicies.cdpAuthTokenRef, lucid);
731
+ }
732
+
733
+ static assetTokenRef(
734
+ params: ScriptReferences,
735
+ lucid: LucidEvolution,
736
+ ): Promise<UTxO> {
737
+ return scriptRef(params.iAssetTokenPolicyRef, lucid);
738
+ }
739
+
740
+ static assetAuthTokenRef(
741
+ params: ScriptReferences,
742
+ lucid: LucidEvolution,
743
+ ): Promise<UTxO> {
744
+ return scriptRef(params.authTokenPolicies.iAssetTokenRef, lucid);
745
+ }
746
+ }