@indigo-labs/indigo-sdk 0.5.4 → 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 (208) hide show
  1. package/dist/index.d.mts +11 -1
  2. package/dist/index.d.ts +11 -1
  3. package/dist/index.js +17 -0
  4. package/dist/index.mjs +15 -0
  5. package/package.json +11 -1
  6. package/.claude/settings.local.json +0 -7
  7. package/.github/workflows/ci.yml +0 -66
  8. package/.husky/pre-commit +0 -1
  9. package/.nvmrc +0 -1
  10. package/.prettierignore +0 -1
  11. package/.prettierrc +0 -6
  12. package/eslint.config.mjs +0 -42
  13. package/scripts/bench.sh +0 -62
  14. package/scripts/run-repeat.sh +0 -22
  15. package/src/contracts/cdp/helpers.ts +0 -173
  16. package/src/contracts/cdp/scripts.ts +0 -70
  17. package/src/contracts/cdp/transactions.ts +0 -1584
  18. package/src/contracts/cdp/types-new.ts +0 -277
  19. package/src/contracts/cdp/types.ts +0 -43
  20. package/src/contracts/cdp-creator/queries.ts +0 -30
  21. package/src/contracts/cdp-creator/scripts.ts +0 -45
  22. package/src/contracts/cdp-creator/types-new.ts +0 -50
  23. package/src/contracts/cdp-creator/types.ts +0 -24
  24. package/src/contracts/collector/scripts.ts +0 -32
  25. package/src/contracts/collector/transactions.ts +0 -50
  26. package/src/contracts/collector/types-new.ts +0 -17
  27. package/src/contracts/execute/scripts.ts +0 -57
  28. package/src/contracts/execute/types-new.ts +0 -44
  29. package/src/contracts/execute/types.ts +0 -24
  30. package/src/contracts/gov/helpers.ts +0 -351
  31. package/src/contracts/gov/scripts.ts +0 -41
  32. package/src/contracts/gov/transactions.ts +0 -1607
  33. package/src/contracts/gov/types-new.ts +0 -268
  34. package/src/contracts/gov/types.ts +0 -22
  35. package/src/contracts/iasset/helpers.ts +0 -170
  36. package/src/contracts/iasset/queries.ts +0 -102
  37. package/src/contracts/iasset/scripts.ts +0 -38
  38. package/src/contracts/iasset/types.ts +0 -154
  39. package/src/contracts/initialize/actions.ts +0 -789
  40. package/src/contracts/initialize/helpers.ts +0 -616
  41. package/src/contracts/initialize/types.ts +0 -116
  42. package/src/contracts/interest-collection/helpers.ts +0 -19
  43. package/src/contracts/interest-collection/queries.ts +0 -66
  44. package/src/contracts/interest-collection/scripts.ts +0 -44
  45. package/src/contracts/interest-collection/transactions.ts +0 -433
  46. package/src/contracts/interest-collection/types-new.ts +0 -50
  47. package/src/contracts/interest-collection/types.ts +0 -26
  48. package/src/contracts/interest-oracle/helpers.ts +0 -61
  49. package/src/contracts/interest-oracle/scripts.ts +0 -18
  50. package/src/contracts/interest-oracle/transactions.ts +0 -151
  51. package/src/contracts/interest-oracle/types-new.ts +0 -32
  52. package/src/contracts/interest-oracle/types.ts +0 -16
  53. package/src/contracts/one-shot/scripts.ts +0 -12
  54. package/src/contracts/one-shot/transactions.ts +0 -66
  55. package/src/contracts/one-shot/types.ts +0 -22
  56. package/src/contracts/poll/helpers.ts +0 -37
  57. package/src/contracts/poll/scripts.ts +0 -71
  58. package/src/contracts/poll/types-poll-manager.ts +0 -20
  59. package/src/contracts/poll/types-poll-new.ts +0 -196
  60. package/src/contracts/poll/types-poll-shard.ts +0 -16
  61. package/src/contracts/price-oracle/helpers.ts +0 -30
  62. package/src/contracts/price-oracle/scripts.ts +0 -18
  63. package/src/contracts/price-oracle/transactions.ts +0 -115
  64. package/src/contracts/price-oracle/types-new.ts +0 -50
  65. package/src/contracts/price-oracle/types.ts +0 -16
  66. package/src/contracts/pyth-feed/helpers.ts +0 -82
  67. package/src/contracts/pyth-feed/scripts.ts +0 -15
  68. package/src/contracts/pyth-feed/types.ts +0 -181
  69. package/src/contracts/rob/helpers.ts +0 -666
  70. package/src/contracts/rob/scripts.ts +0 -35
  71. package/src/contracts/rob/transactions.ts +0 -423
  72. package/src/contracts/rob/types-new.ts +0 -128
  73. package/src/contracts/rob/types.ts +0 -16
  74. package/src/contracts/rob-leverage/helpers.ts +0 -422
  75. package/src/contracts/rob-leverage/transactions.ts +0 -368
  76. package/src/contracts/stability-pool/helpers.ts +0 -798
  77. package/src/contracts/stability-pool/queries.ts +0 -132
  78. package/src/contracts/stability-pool/scripts.ts +0 -51
  79. package/src/contracts/stability-pool/transactions.ts +0 -921
  80. package/src/contracts/stability-pool/types-new.ts +0 -385
  81. package/src/contracts/stability-pool/types.ts +0 -25
  82. package/src/contracts/stableswap/helpers.ts +0 -507
  83. package/src/contracts/stableswap/scripts.ts +0 -37
  84. package/src/contracts/stableswap/transactions.ts +0 -475
  85. package/src/contracts/stableswap/types-new.ts +0 -131
  86. package/src/contracts/stableswap/types.ts +0 -17
  87. package/src/contracts/staking/helpers.ts +0 -136
  88. package/src/contracts/staking/scripts.ts +0 -41
  89. package/src/contracts/staking/transactions.ts +0 -349
  90. package/src/contracts/staking/types-new.ts +0 -137
  91. package/src/contracts/staking/types.ts +0 -17
  92. package/src/contracts/treasury/helpers.ts +0 -21
  93. package/src/contracts/treasury/queries.ts +0 -100
  94. package/src/contracts/treasury/scripts.ts +0 -27
  95. package/src/contracts/treasury/transactions.ts +0 -334
  96. package/src/contracts/treasury/types-new.ts +0 -69
  97. package/src/contracts/treasury/types.ts +0 -14
  98. package/src/contracts/version-registry/scripts.ts +0 -29
  99. package/src/contracts/version-registry/types-new.ts +0 -18
  100. package/src/contracts/version-registry/types.ts +0 -17
  101. package/src/index.ts +0 -93
  102. package/src/scripts/always-fail-validator.ts +0 -7
  103. package/src/scripts/auth-token-policy.ts +0 -24
  104. package/src/scripts/iasset-policy.ts +0 -23
  105. package/src/types/evolution-schema-options.ts +0 -16
  106. package/src/types/generic.ts +0 -52
  107. package/src/types/multisig.ts +0 -48
  108. package/src/types/on-chain-decimal.ts +0 -57
  109. package/src/types/rational.ts +0 -61
  110. package/src/types/system-params.ts +0 -463
  111. package/src/utils/array-utils.ts +0 -108
  112. package/src/utils/bigint-utils.ts +0 -35
  113. package/src/utils/helper-txs.ts +0 -31
  114. package/src/utils/indigo-helpers.ts +0 -17
  115. package/src/utils/lucid-utils.ts +0 -125
  116. package/src/utils/oracle-helpers.ts +0 -62
  117. package/src/utils/pyth/decode.ts +0 -223
  118. package/src/utils/pyth/encode.ts +0 -262
  119. package/src/utils/pyth/index.ts +0 -14
  120. package/src/utils/pyth/types.ts +0 -87
  121. package/src/utils/time-helpers.ts +0 -4
  122. package/src/utils/utils.ts +0 -25
  123. package/src/validators/always-succeed-validator.ts +0 -6
  124. package/src/validators/cdp-creator-validator.ts +0 -7
  125. package/src/validators/cdp-redeem-validator.ts +0 -7
  126. package/src/validators/cdp-validator.ts +0 -7
  127. package/src/validators/collector-validator.ts +0 -7
  128. package/src/validators/execute-validator.ts +0 -7
  129. package/src/validators/governance-validator.ts +0 -7
  130. package/src/validators/iasset-validator.ts +0 -7
  131. package/src/validators/interest-collection-validator.ts +0 -7
  132. package/src/validators/interest-oracle-validator.ts +0 -7
  133. package/src/validators/one-shot-policy.ts +0 -7
  134. package/src/validators/poll-manager-validator.ts +0 -7
  135. package/src/validators/poll-shard-validator.ts +0 -7
  136. package/src/validators/price-oracle-validator.ts +0 -7
  137. package/src/validators/pyth-feed-validator.ts +0 -7
  138. package/src/validators/rob-validator.ts +0 -7
  139. package/src/validators/stability-pool-validator.ts +0 -7
  140. package/src/validators/stableswap-validator.ts +0 -7
  141. package/src/validators/staking-validator.ts +0 -7
  142. package/src/validators/treasury-validator.ts +0 -7
  143. package/src/validators/version-record-policy.ts +0 -7
  144. package/src/validators/version-registry-validator.ts +0 -7
  145. package/tests/always-succeed/script.ts +0 -7
  146. package/tests/array-utils.test.ts +0 -92
  147. package/tests/bigint-utils.test.ts +0 -41
  148. package/tests/cdp/actions.ts +0 -680
  149. package/tests/cdp/cdp-helpers.ts +0 -55
  150. package/tests/cdp/cdp-queries.ts +0 -423
  151. package/tests/cdp/cdp.test.ts +0 -6730
  152. package/tests/cdp/transactions-mutated.ts +0 -1733
  153. package/tests/data/system-params.json +0 -1155
  154. package/tests/datums.test.ts +0 -302
  155. package/tests/endpoints/initialize.ts +0 -78
  156. package/tests/endpoints/interest-collector.ts +0 -37
  157. package/tests/endpoints/treasury.ts +0 -70
  158. package/tests/gov/actions.ts +0 -381
  159. package/tests/gov/gov.test.ts +0 -5147
  160. package/tests/gov/governance-queries.ts +0 -37
  161. package/tests/hash-checks.test.ts +0 -112
  162. package/tests/indigo-test-helpers.ts +0 -99
  163. package/tests/initialize.test.ts +0 -79
  164. package/tests/interest-calculations.test.ts +0 -120
  165. package/tests/interest-collection/interest-collection.test.ts +0 -843
  166. package/tests/interest-collection/transactions-mutated.ts +0 -260
  167. package/tests/interest-oracle.test.ts +0 -93
  168. package/tests/mock/assets-mock.ts +0 -243
  169. package/tests/mock/protocol-params-mock.ts +0 -21
  170. package/tests/price-oracle/actions.ts +0 -161
  171. package/tests/price-oracle/price-oracle-queries.ts +0 -12
  172. package/tests/price-oracle/price-oracle.test.ts +0 -228
  173. package/tests/price-oracle/transactions-mutated.ts +0 -59
  174. package/tests/pyth/endpoints.ts +0 -96
  175. package/tests/pyth/helpers.ts +0 -37
  176. package/tests/pyth/pyth-encoding.test.ts +0 -376
  177. package/tests/pyth/pyth-feeds.test.ts +0 -943
  178. package/tests/pyth/pyth-indigo.test.ts +0 -505
  179. package/tests/pyth/pyth.test.ts +0 -299
  180. package/tests/queries/collector-queries.ts +0 -27
  181. package/tests/queries/execute-queries.ts +0 -47
  182. package/tests/queries/iasset-queries.ts +0 -149
  183. package/tests/queries/interest-oracle-queries.ts +0 -12
  184. package/tests/queries/poll-queries.ts +0 -97
  185. package/tests/queries/staking-queries.ts +0 -54
  186. package/tests/rob/actions.ts +0 -56
  187. package/tests/rob/rob-leverage.test.ts +0 -2555
  188. package/tests/rob/rob-queries.ts +0 -95
  189. package/tests/rob/rob.test.ts +0 -3677
  190. package/tests/rob/transactions-mutated.ts +0 -844
  191. package/tests/script-size.test.ts +0 -240
  192. package/tests/setup.ts +0 -135
  193. package/tests/stability-pool/actions.ts +0 -229
  194. package/tests/stability-pool.test.ts +0 -6491
  195. package/tests/stableswap/stableswap-actions.ts +0 -83
  196. package/tests/stableswap/stableswap-queries.ts +0 -98
  197. package/tests/stableswap/stableswap.test.ts +0 -4471
  198. package/tests/stableswap/transactions-mutated.ts +0 -348
  199. package/tests/staking.test.ts +0 -172
  200. package/tests/test-helpers.ts +0 -94
  201. package/tests/treasury/actions.ts +0 -67
  202. package/tests/treasury/treasury.test.ts +0 -320
  203. package/tests/utils/asserts.ts +0 -87
  204. package/tests/utils/benchmark-utils.ts +0 -130
  205. package/tests/utils/index.ts +0 -168
  206. package/tsconfig.build.json +0 -4
  207. package/tsconfig.json +0 -27
  208. package/vitest.config.ts +0 -11
@@ -1,844 +0,0 @@
1
- import {
2
- addAssets,
3
- Data,
4
- fromHex,
5
- getInputIndices,
6
- LucidEvolution,
7
- OutRef,
8
- toHex,
9
- TxBuilder,
10
- } from '@lucid-evolution/lucid';
11
- import {
12
- addrDetails,
13
- buildRedemptionsTx,
14
- collectInterestTx,
15
- createScriptAddress,
16
- fromSystemParamsAsset,
17
- fromSystemParamsScriptRef,
18
- getInlineDatumOrThrow,
19
- matchSingle,
20
- SystemParams,
21
- treasuryFeeTx,
22
- } from '../../src';
23
- import {
24
- parseCollateralAssetDatumOrThrow,
25
- parseIAssetDatumOrThrow,
26
- } from '../../src/contracts/iasset/types';
27
- import { match, P } from 'ts-pattern';
28
- import { parsePriceOracleDatum } from '../../src/contracts/price-oracle/types-new';
29
- import { unzip, zip } from 'fp-ts/lib/Array';
30
- import {
31
- parseCdpDatumOrThrow,
32
- serialiseCdpDatum,
33
- serialiseCdpRedeemer,
34
- } from '../../src/contracts/cdp/types-new';
35
- import { parseInterestOracleDatum } from '../../src/contracts/interest-oracle/types-new';
36
- import {
37
- calculateAccruedInterest,
38
- calculateUnitaryInterestSinceOracleLastUpdated,
39
- } from '../../src/contracts/interest-oracle/helpers';
40
- import { mkAssetsOf } from '@3rd-eye-labs/cardano-offchain-common';
41
- import { oracleExpirationAwareValidity } from '../../src/contracts/price-oracle/helpers';
42
- import { calculateFeeFromRatio } from '../../src/utils/indigo-helpers';
43
- import {
44
- serialiseCDPCreatorDatum,
45
- serialiseCDPCreatorRedeemer,
46
- } from '../../src/contracts/cdp-creator/types-new';
47
- import { retrieveAdjustedPrice } from '../../src/utils/oracle-helpers';
48
-
49
- // Unlike the real redeemRob function, this one allows building a transaction to redeem
50
- // using a delisted iAsset. Therefore, no price oracle is required.
51
- export async function testRedeemRob(
52
- redemptionRobsData: [OutRef, bigint][],
53
- priceOracleOutRef: OutRef | undefined,
54
- iassetOutRef: OutRef,
55
- collateralAssetOutRef: OutRef,
56
- lucid: LucidEvolution,
57
- sysParams: SystemParams,
58
- pythMessage?: string,
59
- ): Promise<TxBuilder> {
60
- const robScriptRefUtxo = matchSingle(
61
- await lucid.utxosByOutRef([
62
- fromSystemParamsScriptRef(sysParams.scriptReferences.robValidatorRef),
63
- ]),
64
- (_) => new Error('Expected a single ROB Ref Script UTXO'),
65
- );
66
-
67
- const iassetUtxo = matchSingle(
68
- await lucid.utxosByOutRef([iassetOutRef]),
69
- (_) => new Error('Expected a single IAsset UTXO'),
70
- );
71
-
72
- const iassetDatum = parseIAssetDatumOrThrow(
73
- getInlineDatumOrThrow(iassetUtxo),
74
- );
75
-
76
- const collateralAssetUtxo = matchSingle(
77
- await lucid.utxosByOutRef([collateralAssetOutRef]),
78
- (_) => new Error('Expected a single collateral asset UTXO'),
79
- );
80
-
81
- const collateralAssetDatum = parseCollateralAssetDatumOrThrow(
82
- getInlineDatumOrThrow(collateralAssetUtxo),
83
- );
84
-
85
- const [adjustedPrice, priceOracleUtxo] = await retrieveAdjustedPrice(
86
- iassetDatum.assetName,
87
- collateralAssetDatum.collateralAsset,
88
- collateralAssetDatum.priceInfo,
89
- collateralAssetDatum.extraDecimals,
90
- priceOracleOutRef,
91
- pythMessage,
92
- sysParams.pythConfig,
93
- lucid,
94
- );
95
-
96
- const [robsToRedeemOutRefs, robRedemptionIAssetAmt] =
97
- unzip(redemptionRobsData);
98
-
99
- const redemptionRobs = await lucid
100
- .utxosByOutRef(robsToRedeemOutRefs)
101
- .then((val) => zip(val, robRedemptionIAssetAmt));
102
-
103
- const allRefInputs = [
104
- robScriptRefUtxo,
105
- iassetUtxo,
106
- collateralAssetUtxo,
107
- ...(priceOracleUtxo == null ? [] : [priceOracleUtxo]),
108
- ];
109
-
110
- const refInputIdxs = getInputIndices(allRefInputs, allRefInputs);
111
-
112
- const tx = buildRedemptionsTx(
113
- redemptionRobs,
114
- iassetDatum.assetName,
115
- collateralAssetDatum.collateralAsset,
116
- adjustedPrice,
117
- iassetDatum.redemptionReimbursementRatio,
118
- sysParams,
119
- lucid.newTx(),
120
- 0n,
121
- refInputIdxs[2],
122
- refInputIdxs[1],
123
- priceOracleOutRef !== undefined
124
- ? { OracleRefInputIdx: refInputIdxs[3] }
125
- : 'OracleVoid',
126
- );
127
-
128
- const transaction = lucid
129
- .newTx()
130
- .validTo(lucid.slotToUnixTime(lucid.currentSlot()))
131
- .readFrom(allRefInputs)
132
- .compose(tx);
133
-
134
- if (priceOracleUtxo != null) {
135
- transaction.readFrom([priceOracleUtxo]);
136
- }
137
-
138
- return transaction;
139
- }
140
-
141
- export async function redeemWithCdpCreate(
142
- redemptionRobsData: [OutRef, bigint][],
143
- collateralAmount: bigint,
144
- mintedAmount: bigint,
145
- sysParams: SystemParams,
146
- cdpCreatorOref: OutRef,
147
- iassetOref: OutRef,
148
- collateralAssetOref: OutRef,
149
- priceOracleOref: OutRef,
150
- interestOracleOref: OutRef,
151
- treasuryOref: OutRef,
152
- lucid: LucidEvolution,
153
- ): Promise<TxBuilder> {
154
- const network = lucid.config().network!;
155
- const currentTime = BigInt(lucid.slotToUnixTime(lucid.currentSlot()));
156
-
157
- const [pkh, skh] = await addrDetails(lucid);
158
-
159
- const treasuryRefScriptUtxo = matchSingle(
160
- await lucid.utxosByOutRef([
161
- fromSystemParamsScriptRef(
162
- sysParams.scriptReferences.treasuryValidatorRef,
163
- ),
164
- ]),
165
- (_) => new Error('Expected a single treasury Ref Script UTXO'),
166
- );
167
-
168
- const robScriptRefUtxo = matchSingle(
169
- await lucid.utxosByOutRef([
170
- fromSystemParamsScriptRef(sysParams.scriptReferences.robValidatorRef),
171
- ]),
172
- (_) => new Error('Expected a single ROB Ref Script UTXO'),
173
- );
174
-
175
- const cdpCreatorRefScriptUtxo = matchSingle(
176
- await lucid.utxosByOutRef([
177
- fromSystemParamsScriptRef(
178
- sysParams.scriptReferences.cdpCreatorValidatorRef,
179
- ),
180
- ]),
181
- (_) => new Error('Expected a single cdp creator Ref Script UTXO'),
182
- );
183
- const cdpAuthTokenPolicyRefScriptUtxo = matchSingle(
184
- await lucid.utxosByOutRef([
185
- fromSystemParamsScriptRef(
186
- sysParams.scriptReferences.authTokenPolicies.cdpAuthTokenRef,
187
- ),
188
- ]),
189
- (_) => new Error('Expected a single cdp auth token policy Ref Script UTXO'),
190
- );
191
- const iAssetTokenPolicyRefScriptUtxo = matchSingle(
192
- await lucid.utxosByOutRef([
193
- fromSystemParamsScriptRef(
194
- sysParams.scriptReferences.iAssetTokenPolicyRef,
195
- ),
196
- ]),
197
- (_) => new Error('Expected a single iasset token policy Ref Script UTXO'),
198
- );
199
-
200
- const iassetUtxo = matchSingle(
201
- await lucid.utxosByOutRef([iassetOref]),
202
- (_) => new Error('Expected a single iasset UTXO'),
203
- );
204
- const iassetDatum = parseIAssetDatumOrThrow(
205
- getInlineDatumOrThrow(iassetUtxo),
206
- );
207
-
208
- const collateralAssetUtxo = matchSingle(
209
- await lucid.utxosByOutRef([collateralAssetOref]),
210
- (_) => new Error('Expected a single collateral asset UTXO'),
211
- );
212
- const collateralAssetDatum = parseCollateralAssetDatumOrThrow(
213
- getInlineDatumOrThrow(collateralAssetUtxo),
214
- );
215
-
216
- const priceOracleUtxo = matchSingle(
217
- await lucid.utxosByOutRef([priceOracleOref]),
218
- (_) => new Error('Expected a single price oracle UTXO'),
219
- );
220
- const priceOracleDatum = parsePriceOracleDatum(
221
- getInlineDatumOrThrow(priceOracleUtxo),
222
- );
223
-
224
- const interestOracleUtxo = matchSingle(
225
- await lucid.utxosByOutRef([interestOracleOref]),
226
- (_) => new Error('Expected a single interest oracle UTXO'),
227
- );
228
- const interestOracleDatum = parseInterestOracleDatum(
229
- getInlineDatumOrThrow(interestOracleUtxo),
230
- );
231
-
232
- const cdpCreatorUtxo = matchSingle(
233
- await lucid.utxosByOutRef([cdpCreatorOref]),
234
- (_) => new Error('Expected a single CDP creator UTXO'),
235
- );
236
-
237
- match(collateralAssetDatum.priceInfo)
238
- .with({ Delisted: P.any }, () => {
239
- throw new Error("Can't open CDP of delisted asset");
240
- })
241
- .otherwise(() => {});
242
-
243
- const cdpNftVal = mkAssetsOf(
244
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
245
- 1n,
246
- );
247
-
248
- const iassetClass = {
249
- currencySymbol: fromHex(
250
- sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
251
- ),
252
- tokenName: iassetDatum.assetName,
253
- };
254
-
255
- const debtMintingFee = calculateFeeFromRatio(
256
- iassetDatum.debtMintingFeeRatio,
257
- mintedAmount,
258
- );
259
-
260
- const iassetTokensVal = mkAssetsOf(iassetClass, mintedAmount);
261
-
262
- const [robsToRedeemOutRefs, robRedemptionIAssetAmt] =
263
- unzip(redemptionRobsData);
264
-
265
- const redemptionRobs = await lucid
266
- .utxosByOutRef(robsToRedeemOutRefs)
267
- .then((val) => zip(val, robRedemptionIAssetAmt));
268
-
269
- const allRefInputs = [
270
- collateralAssetUtxo,
271
- iassetUtxo,
272
- priceOracleUtxo,
273
- interestOracleUtxo,
274
- // ref scripts
275
- cdpCreatorRefScriptUtxo,
276
- robScriptRefUtxo,
277
- iAssetTokenPolicyRefScriptUtxo,
278
- cdpAuthTokenPolicyRefScriptUtxo,
279
- ...(debtMintingFee > 0n ? [treasuryRefScriptUtxo] : []),
280
- ];
281
-
282
- const refInputIdxs = getInputIndices(allRefInputs, allRefInputs);
283
-
284
- const tx = buildRedemptionsTx(
285
- redemptionRobs,
286
- iassetDatum.assetName,
287
- collateralAssetDatum.collateralAsset,
288
- priceOracleDatum.price,
289
- iassetDatum.redemptionReimbursementRatio,
290
- sysParams,
291
- lucid.newTx(),
292
- 0n,
293
- refInputIdxs[0],
294
- refInputIdxs[1],
295
- { OracleRefInputIdx: refInputIdxs[2] },
296
- );
297
-
298
- const txValidity = oracleExpirationAwareValidity(
299
- lucid,
300
- Number(sysParams.cdpCreatorParams.biasTime),
301
- Number(priceOracleDatum.expirationTime),
302
- );
303
-
304
- tx.validFrom(txValidity.validFrom)
305
- .validTo(txValidity.validTo)
306
- // Ref scripts
307
- .readFrom(allRefInputs)
308
- .mintAssets(cdpNftVal, Data.void())
309
- .mintAssets(iassetTokensVal, Data.void())
310
- .pay.ToContract(
311
- createScriptAddress(network, sysParams.validatorHashes.cdpHash, skh),
312
- {
313
- kind: 'inline',
314
- value: serialiseCdpDatum({
315
- cdpOwner: fromHex(pkh.hash),
316
- iasset: iassetDatum.assetName,
317
- collateralAsset: collateralAssetDatum.collateralAsset,
318
- mintedAmt: mintedAmount,
319
- cdpFees: {
320
- ActiveCDPInterestTracking: {
321
- lastSettled: currentTime,
322
- unitaryInterestSnapshot:
323
- calculateUnitaryInterestSinceOracleLastUpdated(
324
- currentTime,
325
- interestOracleDatum,
326
- ) + interestOracleDatum.unitaryInterest,
327
- },
328
- },
329
- }),
330
- },
331
- addAssets(
332
- cdpNftVal,
333
- mkAssetsOf(collateralAssetDatum.collateralAsset, collateralAmount),
334
- ),
335
- )
336
- .pay.ToContract(
337
- cdpCreatorUtxo.address,
338
- {
339
- kind: 'inline',
340
- value: serialiseCDPCreatorDatum({
341
- creatorInputOref: {
342
- outputIndex: BigInt(cdpCreatorUtxo.outputIndex),
343
- txHash: fromHex(cdpCreatorUtxo.txHash),
344
- },
345
- }),
346
- },
347
- cdpCreatorUtxo.assets,
348
- );
349
-
350
- if (debtMintingFee > 0) {
351
- await treasuryFeeTx(
352
- iassetClass,
353
- debtMintingFee,
354
- 0n,
355
- lucid,
356
- sysParams,
357
- tx,
358
- cdpCreatorUtxo,
359
- treasuryOref,
360
- );
361
- }
362
-
363
- tx.collectFrom([cdpCreatorUtxo], {
364
- kind: 'self',
365
- makeRedeemer: (inputIdx) => {
366
- return serialiseCDPCreatorRedeemer({
367
- CreateCDP: {
368
- cdpOwner: fromHex(pkh.hash),
369
- minted: mintedAmount,
370
- collateralAmt: collateralAmount,
371
- currentTime: currentTime,
372
- creatorInputIdx: inputIdx,
373
- creatorOutputIdx: BigInt(redemptionRobs.length) + 1n,
374
- cdpOutputIdx: BigInt(redemptionRobs.length),
375
- iassetRefInputIdx: refInputIdxs[1],
376
- collateralAssetRefInputIdx: refInputIdxs[0],
377
- interestOracleRefInputIdx: refInputIdxs[3],
378
- priceOracleIdx: { OracleRefInputIdx: refInputIdxs[2] },
379
- },
380
- });
381
- },
382
- });
383
-
384
- return tx;
385
- }
386
-
387
- export async function redeemWithCdpClose(
388
- redemptionRobsData: [OutRef, bigint][],
389
- cdpOref: OutRef,
390
- iassetOref: OutRef,
391
- collateralAssetOref: OutRef,
392
- priceOracleOref: OutRef,
393
- interestOracleOref: OutRef,
394
- interestCollectorOref: OutRef,
395
- sysParams: SystemParams,
396
- lucid: LucidEvolution,
397
- ): Promise<TxBuilder> {
398
- const currentTime = BigInt(lucid.slotToUnixTime(lucid.currentSlot()));
399
-
400
- const robScriptRefUtxo = matchSingle(
401
- await lucid.utxosByOutRef([
402
- fromSystemParamsScriptRef(sysParams.scriptReferences.robValidatorRef),
403
- ]),
404
- (_) => new Error('Expected a single ROB Ref Script UTXO'),
405
- );
406
-
407
- const cdpRefScriptUtxo = matchSingle(
408
- await lucid.utxosByOutRef([
409
- fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
410
- ]),
411
- (_) => new Error('Expected a single cdp Ref Script UTXO'),
412
- );
413
-
414
- const interestCollectorRefScriptUtxo = matchSingle(
415
- await lucid.utxosByOutRef([
416
- fromSystemParamsScriptRef(
417
- sysParams.scriptReferences.interestCollectionValidatorRef,
418
- ),
419
- ]),
420
- (_) => new Error('Expected a single interest collector Ref Script UTXO'),
421
- );
422
-
423
- const cdpAuthTokenPolicyRefScriptUtxo = matchSingle(
424
- await lucid.utxosByOutRef([
425
- fromSystemParamsScriptRef(
426
- sysParams.scriptReferences.authTokenPolicies.cdpAuthTokenRef,
427
- ),
428
- ]),
429
- (_) => new Error('Expected a single cdp auth token policy Ref Script UTXO'),
430
- );
431
-
432
- const iAssetTokenPolicyRefScriptUtxo = matchSingle(
433
- await lucid.utxosByOutRef([
434
- fromSystemParamsScriptRef(
435
- sysParams.scriptReferences.iAssetTokenPolicyRef,
436
- ),
437
- ]),
438
- (_) => new Error('Expected a single iasset token policy Ref Script UTXO'),
439
- );
440
-
441
- const cdpUtxo = matchSingle(
442
- await lucid.utxosByOutRef([cdpOref]),
443
- (_) => new Error('Expected a single cdp UTXO'),
444
- );
445
- const cdpDatum = parseCdpDatumOrThrow(getInlineDatumOrThrow(cdpUtxo));
446
-
447
- const collateralAssetUtxo = matchSingle(
448
- await lucid.utxosByOutRef([collateralAssetOref]),
449
- (_) => new Error('Expected a single collateral asset UTXO'),
450
- );
451
- const collateralDatum = parseCollateralAssetDatumOrThrow(
452
- getInlineDatumOrThrow(collateralAssetUtxo),
453
- );
454
-
455
- const iassetUtxo = matchSingle(
456
- await lucid.utxosByOutRef([iassetOref]),
457
- (_) => new Error('Expected a single iasset UTXO'),
458
- );
459
-
460
- const iassetDatum = parseIAssetDatumOrThrow(
461
- getInlineDatumOrThrow(iassetUtxo),
462
- );
463
-
464
- const priceOracleUtxo = matchSingle(
465
- await lucid.utxosByOutRef([priceOracleOref]),
466
- (_) => new Error('Expected a single price oracle UTXO'),
467
- );
468
- const priceOracleDatum = parsePriceOracleDatum(
469
- getInlineDatumOrThrow(priceOracleUtxo),
470
- );
471
-
472
- const interestOracleUtxo = matchSingle(
473
- await lucid.utxosByOutRef([interestOracleOref]),
474
- (_) => new Error('Expected a single interest oracle UTXO'),
475
- );
476
- const interestOracleDatum = parseInterestOracleDatum(
477
- getInlineDatumOrThrow(interestOracleUtxo),
478
- );
479
-
480
- const interestAmt = match(cdpDatum.cdpFees)
481
- .with({ FrozenCDPAccumulatedFees: P.any }, () => {
482
- throw new Error('CDP fees wrong');
483
- })
484
- .with({ ActiveCDPInterestTracking: P.select() }, (interest) => {
485
- return calculateAccruedInterest(
486
- currentTime,
487
- interest.unitaryInterestSnapshot,
488
- cdpDatum.mintedAmt,
489
- interest.lastSettled,
490
- interestOracleDatum,
491
- );
492
- })
493
- .exhaustive();
494
-
495
- const [robsToRedeemOutRefs, robRedemptionIAssetAmt] =
496
- unzip(redemptionRobsData);
497
-
498
- const redemptionRobs = await lucid
499
- .utxosByOutRef(robsToRedeemOutRefs)
500
- .then((val) => zip(val, robRedemptionIAssetAmt));
501
-
502
- const allRefInputs = [
503
- collateralAssetUtxo,
504
- iassetUtxo,
505
- priceOracleUtxo,
506
- interestOracleUtxo,
507
- // ref scripts
508
- robScriptRefUtxo,
509
- cdpRefScriptUtxo,
510
- iAssetTokenPolicyRefScriptUtxo,
511
- cdpAuthTokenPolicyRefScriptUtxo,
512
- ];
513
-
514
- const allAll = [
515
- ...allRefInputs,
516
- ...(interestAmt > 0n ? [interestCollectorRefScriptUtxo] : []),
517
- ];
518
- const refInputIdxs = getInputIndices(allRefInputs, allAll);
519
-
520
- const tx = buildRedemptionsTx(
521
- redemptionRobs,
522
- iassetDatum.assetName,
523
- collateralDatum.collateralAsset,
524
- priceOracleDatum.price,
525
- iassetDatum.redemptionReimbursementRatio,
526
- sysParams,
527
- lucid.newTx(),
528
- 0n,
529
- refInputIdxs[0],
530
- refInputIdxs[1],
531
- { OracleRefInputIdx: refInputIdxs[2] },
532
- );
533
-
534
- const validateFrom = lucid.slotToUnixTime(lucid.currentSlot() - 1);
535
- const validateTo = Math.min(
536
- Number(BigInt(validateFrom) + sysParams.cdpParams.biasTime / 2n),
537
- Number(priceOracleDatum.expirationTime - 1n * 1000n),
538
- );
539
-
540
- tx.validFrom(validateFrom)
541
- .validTo(validateTo)
542
- .readFrom(allRefInputs)
543
- .mintAssets(
544
- mkAssetsOf(
545
- {
546
- currencySymbol: fromHex(
547
- sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
548
- ),
549
- tokenName: collateralDatum.iasset,
550
- },
551
- -cdpDatum.mintedAmt,
552
- ),
553
- Data.void(),
554
- )
555
- .mintAssets(
556
- mkAssetsOf(fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken), -1n),
557
- Data.void(),
558
- )
559
- .collectFrom(
560
- [cdpUtxo],
561
- serialiseCdpRedeemer({ CloseCdp: { currentTime: currentTime } }),
562
- )
563
- // TODO: this is just a placeholder
564
- .setMinFee(3_000_000n);
565
-
566
- if (!cdpDatum.cdpOwner) {
567
- throw new Error('Expected active CDP');
568
- }
569
-
570
- tx.addSignerKey(toHex(cdpDatum.cdpOwner));
571
-
572
- if (interestAmt > 0n) {
573
- await collectInterestTx(
574
- mkAssetsOf(
575
- {
576
- currencySymbol: fromHex(
577
- sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
578
- ),
579
- tokenName: collateralDatum.iasset,
580
- },
581
- interestAmt,
582
- ),
583
- lucid,
584
- sysParams,
585
- tx,
586
- interestCollectorOref,
587
- );
588
- }
589
-
590
- return tx;
591
- }
592
-
593
- export async function redeemWithCdpAdjust(
594
- redemptionRobsData: [OutRef, bigint][],
595
- /// cdp adjust params
596
- collateralAdjustment: bigint,
597
- debtAdjustment: bigint,
598
- cdpOref: OutRef,
599
- iassetOref: OutRef,
600
- collateralAssetOref: OutRef,
601
- priceOracleOref: OutRef,
602
- interestOracleOref: OutRef,
603
- treasuryOref: OutRef,
604
- interestCollectorOref: OutRef,
605
- lucid: LucidEvolution,
606
- sysParams: SystemParams,
607
- ): Promise<TxBuilder> {
608
- const currentTime = BigInt(lucid.slotToUnixTime(lucid.currentSlot()));
609
-
610
- const robScriptRefUtxo = matchSingle(
611
- await lucid.utxosByOutRef([
612
- fromSystemParamsScriptRef(sysParams.scriptReferences.robValidatorRef),
613
- ]),
614
- (_) => new Error('Expected a single ROB Ref Script UTXO'),
615
- );
616
-
617
- const cdpRefScriptUtxo = matchSingle(
618
- await lucid.utxosByOutRef([
619
- fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
620
- ]),
621
- (_) => new Error('Expected a single cdp Ref Script UTXO'),
622
- );
623
-
624
- const iAssetTokenPolicyRefScriptUtxo = matchSingle(
625
- await lucid.utxosByOutRef([
626
- fromSystemParamsScriptRef(
627
- sysParams.scriptReferences.iAssetTokenPolicyRef,
628
- ),
629
- ]),
630
- (_) => new Error('Expected a single iasset token policy Ref Script UTXO'),
631
- );
632
-
633
- const interestCollectorRefScriptUtxo = matchSingle(
634
- await lucid.utxosByOutRef([
635
- fromSystemParamsScriptRef(
636
- sysParams.scriptReferences.interestCollectionValidatorRef,
637
- ),
638
- ]),
639
- (_) => new Error('Expected a single interest collector Ref Script UTXO'),
640
- );
641
-
642
- const treasuryRefScriptUtxo = matchSingle(
643
- await lucid.utxosByOutRef([
644
- fromSystemParamsScriptRef(
645
- sysParams.scriptReferences.treasuryValidatorRef,
646
- ),
647
- ]),
648
- (_) => new Error('Expected a single treasury Ref Script UTXO'),
649
- );
650
-
651
- const cdpUtxo = matchSingle(
652
- await lucid.utxosByOutRef([cdpOref]),
653
- (_) => new Error('Expected a single cdp UTXO'),
654
- );
655
- const cdpDatum = parseCdpDatumOrThrow(getInlineDatumOrThrow(cdpUtxo));
656
-
657
- const iassetUtxo = matchSingle(
658
- await lucid.utxosByOutRef([iassetOref]),
659
- (_) => new Error('Expected a single iasset UTXO'),
660
- );
661
-
662
- const iassetDatum = parseIAssetDatumOrThrow(
663
- getInlineDatumOrThrow(iassetUtxo),
664
- );
665
-
666
- const collateralAssetUtxo = matchSingle(
667
- await lucid.utxosByOutRef([collateralAssetOref]),
668
- (_) => new Error('Expected a single collateral asset UTXO'),
669
- );
670
- const collateralAssetDatum = parseCollateralAssetDatumOrThrow(
671
- getInlineDatumOrThrow(collateralAssetUtxo),
672
- );
673
-
674
- const priceOracleUtxo = matchSingle(
675
- await lucid.utxosByOutRef([priceOracleOref]),
676
- (_) => new Error('Expected a single price oracle UTXO'),
677
- );
678
- const priceOracleDatum = parsePriceOracleDatum(
679
- getInlineDatumOrThrow(priceOracleUtxo),
680
- );
681
-
682
- const interestOracleUtxo = matchSingle(
683
- await lucid.utxosByOutRef([interestOracleOref]),
684
- (_) => new Error('Expected a single interest oracle UTXO'),
685
- );
686
- const interestOracleDatum = parseInterestOracleDatum(
687
- getInlineDatumOrThrow(interestOracleUtxo),
688
- );
689
-
690
- const interestAmt = match(cdpDatum.cdpFees)
691
- .with({ FrozenCDPAccumulatedFees: P.any }, () => {
692
- throw new Error('CDP fees wrong');
693
- })
694
- .with({ ActiveCDPInterestTracking: P.select() }, (interest) => {
695
- return calculateAccruedInterest(
696
- currentTime,
697
- interest.unitaryInterestSnapshot,
698
- cdpDatum.mintedAmt,
699
- interest.lastSettled,
700
- interestOracleDatum,
701
- );
702
- })
703
- .exhaustive();
704
-
705
- const mintedAmountChange = debtAdjustment + interestAmt;
706
-
707
- let treasuryFee = 0n;
708
-
709
- // when mint
710
- if (debtAdjustment > 0n) {
711
- treasuryFee += calculateFeeFromRatio(
712
- iassetDatum.debtMintingFeeRatio,
713
- debtAdjustment,
714
- );
715
- }
716
-
717
- const [robsToRedeemOutRefs, robRedemptionIAssetAmt] =
718
- unzip(redemptionRobsData);
719
-
720
- const redemptionRobs = await lucid
721
- .utxosByOutRef(robsToRedeemOutRefs)
722
- .then((val) => zip(val, robRedemptionIAssetAmt));
723
-
724
- const allRefInputs = [
725
- collateralAssetUtxo,
726
- iassetUtxo,
727
- priceOracleUtxo,
728
- interestOracleUtxo,
729
- // ref scripts
730
- robScriptRefUtxo,
731
- cdpRefScriptUtxo,
732
- iAssetTokenPolicyRefScriptUtxo,
733
- ];
734
-
735
- const allAll = [
736
- ...allRefInputs,
737
- ...(interestAmt > 0n ? [interestCollectorRefScriptUtxo] : []),
738
- ...(treasuryFee > 0n ? [treasuryRefScriptUtxo] : []),
739
- ];
740
- const refInputIdxs = getInputIndices(allRefInputs, allAll);
741
-
742
- const tx = buildRedemptionsTx(
743
- redemptionRobs,
744
- iassetDatum.assetName,
745
- collateralAssetDatum.collateralAsset,
746
- priceOracleDatum.price,
747
- iassetDatum.redemptionReimbursementRatio,
748
- sysParams,
749
- lucid.newTx(),
750
- 0n,
751
- refInputIdxs[0],
752
- refInputIdxs[1],
753
- { OracleRefInputIdx: refInputIdxs[2] },
754
- );
755
-
756
- const txValidity = oracleExpirationAwareValidity(
757
- lucid,
758
- Number(sysParams.cdpCreatorParams.biasTime),
759
- Number(priceOracleDatum.expirationTime),
760
- );
761
-
762
- tx.validFrom(txValidity.validFrom)
763
- .validTo(txValidity.validTo)
764
- .readFrom(allRefInputs)
765
- .collectFrom(
766
- [cdpUtxo],
767
- serialiseCdpRedeemer({
768
- AdjustCdp: {
769
- currentTime: currentTime,
770
- debtAdjustment: debtAdjustment,
771
- collateralAdjustment,
772
- priceOracleIdx: { OracleRefInputIdx: refInputIdxs[2] },
773
- },
774
- }),
775
- )
776
- .pay.ToContract(
777
- cdpUtxo.address,
778
- {
779
- kind: 'inline',
780
- value: serialiseCdpDatum({
781
- ...cdpDatum,
782
- mintedAmt: cdpDatum.mintedAmt + mintedAmountChange,
783
- cdpFees: {
784
- ActiveCDPInterestTracking: {
785
- lastSettled: currentTime,
786
- unitaryInterestSnapshot:
787
- calculateUnitaryInterestSinceOracleLastUpdated(
788
- currentTime,
789
- interestOracleDatum,
790
- ) + interestOracleDatum.unitaryInterest,
791
- },
792
- },
793
- }),
794
- },
795
- addAssets(
796
- cdpUtxo.assets,
797
- mkAssetsOf(cdpDatum.collateralAsset, collateralAdjustment),
798
- ),
799
- );
800
-
801
- if (!cdpDatum.cdpOwner) {
802
- throw new Error('Expected active CDP');
803
- }
804
-
805
- tx.addSignerKey(toHex(cdpDatum.cdpOwner));
806
-
807
- const iAssetAc = {
808
- currencySymbol: fromHex(
809
- sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
810
- ),
811
- tokenName: iassetDatum.assetName,
812
- };
813
-
814
- if (mintedAmountChange !== 0n) {
815
- const iassetTokensVal = mkAssetsOf(iAssetAc, mintedAmountChange);
816
-
817
- tx.mintAssets(iassetTokensVal, Data.void());
818
- }
819
-
820
- if (interestAmt > 0n) {
821
- await collectInterestTx(
822
- mkAssetsOf(iAssetAc, interestAmt),
823
- lucid,
824
- sysParams,
825
- tx,
826
- interestCollectorOref,
827
- );
828
- }
829
-
830
- if (treasuryFee > 0n) {
831
- await treasuryFeeTx(
832
- iAssetAc,
833
- treasuryFee,
834
- 0n,
835
- lucid,
836
- sysParams,
837
- tx,
838
- cdpOref,
839
- treasuryOref,
840
- );
841
- }
842
-
843
- return tx;
844
- }