@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
package/tests/cdp.test.ts DELETED
@@ -1,1528 +0,0 @@
1
- import {
2
- addAssets,
3
- Emulator,
4
- EmulatorAccount,
5
- fromText,
6
- generateEmulatorAccount,
7
- Lucid,
8
- paymentCredentialOf,
9
- stakeCredentialOf,
10
- UTxO,
11
- } from '@lucid-evolution/lucid';
12
- import { assert, beforeEach, describe, expect, test } from 'vitest';
13
- import { LucidContext, repeat, runAndAwaitTx } from './test-helpers';
14
- import {
15
- assetClassValueOf,
16
- lovelacesAmt,
17
- mkLovelacesOf,
18
- } from '../src/utils/value-helpers';
19
- import { AssetInfo, init } from '../src/contracts/initialize/transactions';
20
- import {
21
- addrDetails,
22
- burnCdp,
23
- CDPContent,
24
- closeCdp,
25
- createScriptAddress,
26
- createSpAccount,
27
- depositCdp,
28
- freezeCdp,
29
- fromSystemParamsAsset,
30
- getInlineDatumOrThrow,
31
- InterestOracleDatum,
32
- liquidateCdp,
33
- matchSingle,
34
- mergeCdps,
35
- mintCdp,
36
- openCdp,
37
- parseInterestOracleDatum,
38
- parsePriceOracleDatum,
39
- processSpRequest,
40
- redeemCdp,
41
- SystemParams,
42
- withdrawCdp,
43
- } from '../src';
44
- import {
45
- findAllActiveCdps,
46
- findAllNecessaryOrefs,
47
- findCdp,
48
- findFrozenCDPs,
49
- } from './queries/cdp-queries';
50
- import { getValueChangeAtAddressAfterAction } from './utils';
51
- import { cdpCollateralRatioPercentage } from '../src/contracts/cdp/helpers';
52
- import { OnChainDecimal } from '../src/types/on-chain-decimal';
53
- import { feedPriceOracleTx } from '../src/contracts/price-oracle/transactions';
54
- import { iusdInitialAssetCfg } from './mock/assets-mock';
55
- import { assertValueInRange } from './utils/asserts';
56
- import { findStabilityPoolAccount } from './queries/stability-pool-queries';
57
-
58
- type MyContext = LucidContext<{
59
- admin: EmulatorAccount;
60
- user: EmulatorAccount;
61
- }>;
62
-
63
- async function findPrice(
64
- context: MyContext,
65
- sysParams: SystemParams,
66
- asset: string,
67
- ): Promise<OnChainDecimal> {
68
- const orefs = await findAllNecessaryOrefs(context.lucid, sysParams, asset);
69
-
70
- const priceOracleUtxo = matchSingle(
71
- await context.lucid.utxosByOutRef([orefs.priceOracleUtxo]),
72
- (_) => new Error('Expected a single price oracle UTXO'),
73
- );
74
- const priceOracleDatum = parsePriceOracleDatum(
75
- getInlineDatumOrThrow(priceOracleUtxo),
76
- );
77
-
78
- return priceOracleDatum.price;
79
- }
80
-
81
- async function findInterestDatum(
82
- context: MyContext,
83
- sysParams: SystemParams,
84
- asset: string,
85
- ): Promise<InterestOracleDatum> {
86
- const orefs = await findAllNecessaryOrefs(context.lucid, sysParams, asset);
87
-
88
- const interestOracleUtxo = matchSingle(
89
- await context.lucid.utxosByOutRef([orefs.interestOracleUtxo]),
90
- (_) => new Error('Expected a single interest oracle UTXO'),
91
- );
92
- return parseInterestOracleDatum(getInlineDatumOrThrow(interestOracleUtxo));
93
- }
94
-
95
- async function findCdpCR(
96
- context: MyContext,
97
- sysParams: SystemParams,
98
- tokenAssetInfo: AssetInfo,
99
- cdp: { utxo: UTxO; datum: CDPContent },
100
- ): Promise<number> {
101
- return cdpCollateralRatioPercentage(
102
- context.emulator.slot,
103
- await findPrice(context, sysParams, tokenAssetInfo.iassetTokenNameAscii),
104
- cdp.utxo,
105
- cdp.datum,
106
- await findInterestDatum(
107
- context,
108
- sysParams,
109
- tokenAssetInfo.iassetTokenNameAscii,
110
- ),
111
- context.lucid.config().network!,
112
- );
113
- }
114
-
115
- describe('CDP', () => {
116
- beforeEach<MyContext>(async (context: MyContext) => {
117
- context.users = {
118
- admin: generateEmulatorAccount({
119
- lovelace: BigInt(100_000_000_000_000),
120
- }),
121
- user: generateEmulatorAccount(addAssets(mkLovelacesOf(150_000_000n))),
122
- };
123
-
124
- context.emulator = new Emulator([context.users.admin, context.users.user]);
125
- context.lucid = await Lucid(context.emulator, 'Custom');
126
- });
127
-
128
- test<MyContext>('Open CDP', async (context: MyContext) => {
129
- context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
130
-
131
- const [sysParams, _] = await init(context.lucid, [iusdInitialAssetCfg]);
132
-
133
- const asset = 'iUSD';
134
-
135
- const orefs = await findAllNecessaryOrefs(context.lucid, sysParams, asset);
136
-
137
- await runAndAwaitTx(
138
- context.lucid,
139
- openCdp(
140
- 10_000_000n,
141
- 500_000n,
142
- sysParams,
143
- orefs.cdpCreatorUtxo,
144
- orefs.iasset.utxo,
145
- orefs.priceOracleUtxo,
146
- orefs.interestOracleUtxo,
147
- orefs.collectorUtxo,
148
- context.lucid,
149
- context.emulator.slot,
150
- ),
151
- );
152
- });
153
-
154
- test<MyContext>('Deposit CDP', async (context: MyContext) => {
155
- context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
156
-
157
- const [pkh, skh] = await addrDetails(context.lucid);
158
-
159
- const [sysParams, _] = await init(context.lucid, [iusdInitialAssetCfg]);
160
-
161
- const asset = 'iUSD';
162
-
163
- const initialMint = 500_000n;
164
- const initialCollateral = 10_000_000n;
165
-
166
- {
167
- const orefs = await findAllNecessaryOrefs(
168
- context.lucid,
169
- sysParams,
170
- asset,
171
- );
172
-
173
- await runAndAwaitTx(
174
- context.lucid,
175
- openCdp(
176
- initialCollateral,
177
- initialMint,
178
- sysParams,
179
- orefs.cdpCreatorUtxo,
180
- orefs.iasset.utxo,
181
- orefs.priceOracleUtxo,
182
- orefs.interestOracleUtxo,
183
- orefs.collectorUtxo,
184
- context.lucid,
185
- context.emulator.slot,
186
- ),
187
- );
188
- }
189
-
190
- context.emulator.awaitSlot(1000);
191
-
192
- {
193
- const cdp = await findCdp(
194
- context.lucid,
195
- sysParams.validatorHashes.cdpHash,
196
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
197
- pkh.hash,
198
- skh,
199
- );
200
- const orefs = await findAllNecessaryOrefs(
201
- context.lucid,
202
- sysParams,
203
- asset,
204
- );
205
-
206
- const [_, treasuryValChange] = await getValueChangeAtAddressAfterAction(
207
- context.lucid,
208
- createScriptAddress(
209
- context.lucid.config().network!,
210
- sysParams.validatorHashes.treasuryHash,
211
- ),
212
- () =>
213
- runAndAwaitTx(
214
- context.lucid,
215
- depositCdp(
216
- 1_000_000n,
217
- cdp.utxo,
218
- orefs.iasset.utxo,
219
- orefs.priceOracleUtxo,
220
- orefs.interestOracleUtxo,
221
- orefs.collectorUtxo,
222
- orefs.govUtxo,
223
- orefs.treasuryUtxo,
224
- sysParams,
225
- context.lucid,
226
- context.emulator.slot,
227
- ),
228
- ),
229
- );
230
-
231
- assert(
232
- lovelacesAmt(treasuryValChange) > 0,
233
- 'Expected some interest paid to treasury',
234
- );
235
- }
236
-
237
- const cdp = await findCdp(
238
- context.lucid,
239
- sysParams.validatorHashes.cdpHash,
240
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
241
- pkh.hash,
242
- skh,
243
- );
244
-
245
- expect(cdp.datum.mintedAmt).toBe(initialMint);
246
- expect(lovelacesAmt(cdp.utxo.assets)).toBe(initialCollateral + 1_000_000n);
247
- });
248
-
249
- test<MyContext>('Withdraw CDP', async (context: MyContext) => {
250
- context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
251
-
252
- const [pkh, skh] = await addrDetails(context.lucid);
253
-
254
- const [sysParams, _] = await init(context.lucid, [iusdInitialAssetCfg]);
255
-
256
- const asset = 'iUSD';
257
-
258
- const initialMint = 500_000n;
259
- const initialCollateral = 15_000_000n;
260
-
261
- {
262
- const orefs = await findAllNecessaryOrefs(
263
- context.lucid,
264
- sysParams,
265
- asset,
266
- );
267
-
268
- await runAndAwaitTx(
269
- context.lucid,
270
- openCdp(
271
- initialCollateral,
272
- initialMint,
273
- sysParams,
274
- orefs.cdpCreatorUtxo,
275
- orefs.iasset.utxo,
276
- orefs.priceOracleUtxo,
277
- orefs.interestOracleUtxo,
278
- orefs.collectorUtxo,
279
- context.lucid,
280
- context.emulator.slot,
281
- ),
282
- );
283
- }
284
-
285
- context.emulator.awaitSlot(100);
286
-
287
- {
288
- const cdp = await findCdp(
289
- context.lucid,
290
- sysParams.validatorHashes.cdpHash,
291
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
292
- pkh.hash,
293
- skh,
294
- );
295
- const orefs = await findAllNecessaryOrefs(
296
- context.lucid,
297
- sysParams,
298
- asset,
299
- );
300
-
301
- const [_, treasuryValChange] = await getValueChangeAtAddressAfterAction(
302
- context.lucid,
303
- createScriptAddress(
304
- context.lucid.config().network!,
305
- sysParams.validatorHashes.treasuryHash,
306
- ),
307
- () =>
308
- runAndAwaitTx(
309
- context.lucid,
310
- withdrawCdp(
311
- 4_000_000n,
312
- cdp.utxo,
313
- orefs.iasset.utxo,
314
- orefs.priceOracleUtxo,
315
- orefs.interestOracleUtxo,
316
- orefs.collectorUtxo,
317
- orefs.govUtxo,
318
- orefs.treasuryUtxo,
319
- sysParams,
320
- context.lucid,
321
- context.emulator.slot,
322
- ),
323
- ),
324
- );
325
-
326
- assert(
327
- lovelacesAmt(treasuryValChange) > 0,
328
- 'Expected some interest paid to treasury',
329
- );
330
- }
331
-
332
- const cdp = await findCdp(
333
- context.lucid,
334
- sysParams.validatorHashes.cdpHash,
335
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
336
- pkh.hash,
337
- skh,
338
- );
339
-
340
- expect(cdp.datum.mintedAmt).toBe(initialMint);
341
- expect(lovelacesAmt(cdp.utxo.assets)).toBe(initialCollateral - 4_000_000n);
342
- });
343
-
344
- test<MyContext>('Mint CDP', async (context: MyContext) => {
345
- context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
346
-
347
- const [pkh, skh] = await addrDetails(context.lucid);
348
-
349
- const [sysParams, _] = await init(context.lucid, [iusdInitialAssetCfg]);
350
-
351
- const asset = 'iUSD';
352
-
353
- const initialMint = 500_000n;
354
- const initialCollateral = 12_000_000n;
355
-
356
- {
357
- const orefs = await findAllNecessaryOrefs(
358
- context.lucid,
359
- sysParams,
360
- asset,
361
- );
362
-
363
- await runAndAwaitTx(
364
- context.lucid,
365
- openCdp(
366
- initialCollateral,
367
- initialMint,
368
- sysParams,
369
- orefs.cdpCreatorUtxo,
370
- orefs.iasset.utxo,
371
- orefs.priceOracleUtxo,
372
- orefs.interestOracleUtxo,
373
- orefs.collectorUtxo,
374
- context.lucid,
375
- context.emulator.slot,
376
- ),
377
- );
378
- }
379
-
380
- context.emulator.awaitSlot(100);
381
-
382
- {
383
- const cdp = await findCdp(
384
- context.lucid,
385
- sysParams.validatorHashes.cdpHash,
386
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
387
- pkh.hash,
388
- skh,
389
- );
390
- const orefs = await findAllNecessaryOrefs(
391
- context.lucid,
392
- sysParams,
393
- asset,
394
- );
395
-
396
- const [_, treasuryValChange] = await getValueChangeAtAddressAfterAction(
397
- context.lucid,
398
- createScriptAddress(
399
- context.lucid.config().network!,
400
- sysParams.validatorHashes.treasuryHash,
401
- ),
402
- () =>
403
- runAndAwaitTx(
404
- context.lucid,
405
- mintCdp(
406
- 1_000n,
407
- cdp.utxo,
408
- orefs.iasset.utxo,
409
- orefs.priceOracleUtxo,
410
- orefs.interestOracleUtxo,
411
- orefs.collectorUtxo,
412
- orefs.govUtxo,
413
- orefs.treasuryUtxo,
414
- sysParams,
415
- context.lucid,
416
- context.emulator.slot,
417
- ),
418
- ),
419
- );
420
-
421
- assert(
422
- lovelacesAmt(treasuryValChange) > 0,
423
- 'Expected some interest paid to treasury',
424
- );
425
- }
426
-
427
- const cdp = await findCdp(
428
- context.lucid,
429
- sysParams.validatorHashes.cdpHash,
430
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
431
- pkh.hash,
432
- skh,
433
- );
434
-
435
- expect(cdp.datum.mintedAmt).toBe(initialMint + 1_000n);
436
- expect(lovelacesAmt(cdp.utxo.assets)).toBe(initialCollateral);
437
- });
438
-
439
- test<MyContext>('Burn CDP', async (context: MyContext) => {
440
- context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
441
-
442
- const [pkh, skh] = await addrDetails(context.lucid);
443
-
444
- const [sysParams, _] = await init(context.lucid, [iusdInitialAssetCfg]);
445
-
446
- const asset = 'iUSD';
447
-
448
- const initialMint = 500_000n;
449
- const initialCollateral = 12_000_000n;
450
-
451
- {
452
- const orefs = await findAllNecessaryOrefs(
453
- context.lucid,
454
- sysParams,
455
- asset,
456
- );
457
-
458
- await runAndAwaitTx(
459
- context.lucid,
460
- openCdp(
461
- initialCollateral,
462
- initialMint,
463
- sysParams,
464
- orefs.cdpCreatorUtxo,
465
- orefs.iasset.utxo,
466
- orefs.priceOracleUtxo,
467
- orefs.interestOracleUtxo,
468
- orefs.collectorUtxo,
469
- context.lucid,
470
- context.emulator.slot,
471
- ),
472
- );
473
- }
474
-
475
- context.emulator.awaitSlot(1000);
476
-
477
- {
478
- const cdp = await findCdp(
479
- context.lucid,
480
- sysParams.validatorHashes.cdpHash,
481
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
482
- pkh.hash,
483
- skh,
484
- );
485
- const orefs = await findAllNecessaryOrefs(
486
- context.lucid,
487
- sysParams,
488
- asset,
489
- );
490
-
491
- const [_, treasuryValChange] = await getValueChangeAtAddressAfterAction(
492
- context.lucid,
493
- createScriptAddress(
494
- context.lucid.config().network!,
495
- sysParams.validatorHashes.treasuryHash,
496
- ),
497
- () =>
498
- runAndAwaitTx(
499
- context.lucid,
500
- burnCdp(
501
- 1_000n,
502
- cdp.utxo,
503
- orefs.iasset.utxo,
504
- orefs.priceOracleUtxo,
505
- orefs.interestOracleUtxo,
506
- orefs.collectorUtxo,
507
- orefs.govUtxo,
508
- orefs.treasuryUtxo,
509
- sysParams,
510
- context.lucid,
511
- context.emulator.slot,
512
- ),
513
- ),
514
- );
515
-
516
- assert(
517
- lovelacesAmt(treasuryValChange) > 0,
518
- 'Expected some interest paid to treasury',
519
- );
520
- }
521
-
522
- const cdp = await findCdp(
523
- context.lucid,
524
- sysParams.validatorHashes.cdpHash,
525
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
526
- pkh.hash,
527
- skh,
528
- );
529
-
530
- expect(cdp.datum.mintedAmt).toBe(initialMint - 1_000n);
531
- expect(lovelacesAmt(cdp.utxo.assets)).toBe(initialCollateral);
532
- });
533
-
534
- test<MyContext>('Close CDP', async (context: MyContext) => {
535
- context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
536
-
537
- const [pkh, skh] = await addrDetails(context.lucid);
538
-
539
- const [sysParams, _] = await init(context.lucid, [iusdInitialAssetCfg]);
540
-
541
- const asset = 'iUSD';
542
-
543
- {
544
- const orefs = await findAllNecessaryOrefs(
545
- context.lucid,
546
- sysParams,
547
- asset,
548
- );
549
-
550
- await runAndAwaitTx(
551
- context.lucid,
552
- openCdp(
553
- 10_000_000n,
554
- 500_000n,
555
- sysParams,
556
- orefs.cdpCreatorUtxo,
557
- orefs.iasset.utxo,
558
- orefs.priceOracleUtxo,
559
- orefs.interestOracleUtxo,
560
- orefs.collectorUtxo,
561
- context.lucid,
562
- context.emulator.slot,
563
- ),
564
- );
565
- }
566
-
567
- context.emulator.awaitSlot(1000);
568
-
569
- {
570
- const cdp = await findCdp(
571
- context.lucid,
572
- sysParams.validatorHashes.cdpHash,
573
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
574
- pkh.hash,
575
- skh,
576
- );
577
-
578
- const orefs = await findAllNecessaryOrefs(
579
- context.lucid,
580
- sysParams,
581
- asset,
582
- );
583
-
584
- await runAndAwaitTx(
585
- context.lucid,
586
- closeCdp(
587
- cdp.utxo,
588
- orefs.iasset.utxo,
589
- orefs.priceOracleUtxo,
590
- orefs.interestOracleUtxo,
591
- orefs.collectorUtxo,
592
- orefs.govUtxo,
593
- orefs.treasuryUtxo,
594
- sysParams,
595
- context.lucid,
596
- context.emulator.slot,
597
- ),
598
- );
599
- }
600
- });
601
-
602
- test<MyContext>('Redeem CDP', async (context: MyContext) => {
603
- context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
604
-
605
- const [pkh, skh] = await addrDetails(context.lucid);
606
-
607
- const [sysParams, [iusdAssetInfo]] = await init(context.lucid, [
608
- iusdInitialAssetCfg,
609
- ]);
610
-
611
- {
612
- const orefs = await findAllNecessaryOrefs(
613
- context.lucid,
614
- sysParams,
615
- iusdAssetInfo.iassetTokenNameAscii,
616
- );
617
-
618
- await runAndAwaitTx(
619
- context.lucid,
620
- openCdp(
621
- 20_000_000n,
622
- 10_000_000n,
623
- sysParams,
624
- orefs.cdpCreatorUtxo,
625
- orefs.iasset.utxo,
626
- orefs.priceOracleUtxo,
627
- orefs.interestOracleUtxo,
628
- orefs.collectorUtxo,
629
- context.lucid,
630
- context.emulator.slot,
631
- ),
632
- );
633
- }
634
-
635
- // Add iAssets to user's wallet
636
- {
637
- context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
638
-
639
- const orefs = await findAllNecessaryOrefs(
640
- context.lucid,
641
- sysParams,
642
- iusdAssetInfo.iassetTokenNameAscii,
643
- );
644
-
645
- await runAndAwaitTx(
646
- context.lucid,
647
- openCdp(
648
- 50_000_000n,
649
- 10_000_000n,
650
- sysParams,
651
- orefs.cdpCreatorUtxo,
652
- orefs.iasset.utxo,
653
- orefs.priceOracleUtxo,
654
- orefs.interestOracleUtxo,
655
- orefs.collectorUtxo,
656
- context.lucid,
657
- context.emulator.slot,
658
- ),
659
- );
660
-
661
- context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
662
- }
663
-
664
- {
665
- const cdp = await findCdp(
666
- context.lucid,
667
- sysParams.validatorHashes.cdpHash,
668
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
669
- pkh.hash,
670
- skh,
671
- );
672
-
673
- assertValueInRange(
674
- await findCdpCR(context, sysParams, iusdAssetInfo, cdp),
675
- { min: 199, max: 200 },
676
- );
677
-
678
- const orefs = await findAllNecessaryOrefs(
679
- context.lucid,
680
- sysParams,
681
- iusdAssetInfo.iassetTokenNameAscii,
682
- );
683
-
684
- await runAndAwaitTx(
685
- context.lucid,
686
- feedPriceOracleTx(
687
- context.lucid,
688
- orefs.priceOracleUtxo,
689
- {
690
- getOnChainInt: 1_250_000n,
691
- },
692
- iusdAssetInfo.oracleParams,
693
- context.emulator.slot,
694
- ),
695
- );
696
-
697
- assertValueInRange(
698
- await findCdpCR(context, sysParams, iusdAssetInfo, cdp),
699
- { min: 159, max: 160 },
700
- );
701
- }
702
-
703
- {
704
- // Let user do the redemption (i.e. not the CDP's owner)
705
- context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
706
-
707
- const cdp = await findCdp(
708
- context.lucid,
709
- sysParams.validatorHashes.cdpHash,
710
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
711
- pkh.hash,
712
- skh,
713
- );
714
-
715
- const orefs = await findAllNecessaryOrefs(
716
- context.lucid,
717
- sysParams,
718
- iusdAssetInfo.iassetTokenNameAscii,
719
- );
720
-
721
- await runAndAwaitTx(
722
- context.lucid,
723
- redeemCdp(
724
- cdp.datum.mintedAmt,
725
- cdp.utxo,
726
- orefs.iasset.utxo,
727
- orefs.priceOracleUtxo,
728
- orefs.interestOracleUtxo,
729
- orefs.collectorUtxo,
730
- orefs.treasuryUtxo,
731
- sysParams,
732
- context.lucid,
733
- context.emulator.slot,
734
- ),
735
- );
736
-
737
- context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
738
- }
739
-
740
- {
741
- const cdp = await findCdp(
742
- context.lucid,
743
- sysParams.validatorHashes.cdpHash,
744
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
745
- pkh.hash,
746
- skh,
747
- );
748
-
749
- assertValueInRange(
750
- await findCdpCR(context, sysParams, iusdAssetInfo, cdp),
751
- { min: 199, max: 201 },
752
- );
753
- }
754
- });
755
-
756
- test<MyContext>('Freeze CDP', async (context: MyContext) => {
757
- context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
758
-
759
- const [pkh, skh] = await addrDetails(context.lucid);
760
-
761
- const [sysParams, [iusdAssetInfo]] = await init(context.lucid, [
762
- iusdInitialAssetCfg,
763
- ]);
764
-
765
- {
766
- const orefs = await findAllNecessaryOrefs(
767
- context.lucid,
768
- sysParams,
769
- iusdAssetInfo.iassetTokenNameAscii,
770
- );
771
-
772
- await runAndAwaitTx(
773
- context.lucid,
774
- openCdp(
775
- 20_000_000n,
776
- 10_000_000n,
777
- sysParams,
778
- orefs.cdpCreatorUtxo,
779
- orefs.iasset.utxo,
780
- orefs.priceOracleUtxo,
781
- orefs.interestOracleUtxo,
782
- orefs.collectorUtxo,
783
- context.lucid,
784
- context.emulator.slot,
785
- ),
786
- );
787
- }
788
-
789
- context.emulator.awaitSlot(1000);
790
-
791
- {
792
- const cdp = await findCdp(
793
- context.lucid,
794
- sysParams.validatorHashes.cdpHash,
795
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
796
- pkh.hash,
797
- skh,
798
- );
799
-
800
- const orefs = await findAllNecessaryOrefs(
801
- context.lucid,
802
- sysParams,
803
- iusdAssetInfo.iassetTokenNameAscii,
804
- );
805
-
806
- await runAndAwaitTx(
807
- context.lucid,
808
- feedPriceOracleTx(
809
- context.lucid,
810
- orefs.priceOracleUtxo,
811
- {
812
- getOnChainInt: 1_800_000n,
813
- },
814
- iusdAssetInfo.oracleParams,
815
- context.emulator.slot,
816
- ),
817
- );
818
-
819
- assertValueInRange(
820
- await findCdpCR(context, sysParams, iusdAssetInfo, cdp),
821
- { min: 111, max: 112 },
822
- );
823
- }
824
-
825
- {
826
- const cdp = await findCdp(
827
- context.lucid,
828
- sysParams.validatorHashes.cdpHash,
829
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
830
- pkh.hash,
831
- skh,
832
- );
833
-
834
- const orefs = await findAllNecessaryOrefs(
835
- context.lucid,
836
- sysParams,
837
- iusdAssetInfo.iassetTokenNameAscii,
838
- );
839
-
840
- await runAndAwaitTx(
841
- context.lucid,
842
- freezeCdp(
843
- cdp.utxo,
844
- orefs.iasset.utxo,
845
- orefs.priceOracleUtxo,
846
- orefs.interestOracleUtxo,
847
- sysParams,
848
- context.lucid,
849
- context.emulator.slot,
850
- ),
851
- );
852
- }
853
-
854
- {
855
- const frozenCdp = matchSingle(
856
- await findFrozenCDPs(
857
- context.lucid,
858
- sysParams.validatorHashes.cdpHash,
859
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
860
- iusdAssetInfo.iassetTokenNameAscii,
861
- ),
862
- (_) => new Error('Expected only single frozen CDP'),
863
- );
864
-
865
- expect(
866
- frozenCdp.datum.mintedAmt === 10_000_000n &&
867
- frozenCdp.datum.cdpOwner == null,
868
- 'Expected frozen certain frozen CDP',
869
- ).toBeTruthy();
870
- }
871
- });
872
-
873
- test<MyContext>('Liquidate CDP', async (context: MyContext) => {
874
- context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
875
-
876
- const [sysParams, [iusdAssetInfo]] = await init(context.lucid, [
877
- iusdInitialAssetCfg,
878
- ]);
879
-
880
- {
881
- const orefs = await findAllNecessaryOrefs(
882
- context.lucid,
883
- sysParams,
884
- iusdAssetInfo.iassetTokenNameAscii,
885
- );
886
-
887
- // This is the position that will get liquidated.
888
- await runAndAwaitTx(
889
- context.lucid,
890
- openCdp(
891
- 10_000_000n,
892
- 5_000_000n,
893
- sysParams,
894
- orefs.cdpCreatorUtxo,
895
- orefs.iasset.utxo,
896
- orefs.priceOracleUtxo,
897
- orefs.interestOracleUtxo,
898
- orefs.collectorUtxo,
899
- context.lucid,
900
- context.emulator.slot,
901
- ),
902
- );
903
- }
904
-
905
- context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
906
-
907
- {
908
- const orefs = await findAllNecessaryOrefs(
909
- context.lucid,
910
- sysParams,
911
- iusdAssetInfo.iassetTokenNameAscii,
912
- );
913
-
914
- await runAndAwaitTx(
915
- context.lucid,
916
- openCdp(
917
- 20_000_000n,
918
- 10_000_000n,
919
- sysParams,
920
- orefs.cdpCreatorUtxo,
921
- orefs.iasset.utxo,
922
- orefs.priceOracleUtxo,
923
- orefs.interestOracleUtxo,
924
- orefs.collectorUtxo,
925
- context.lucid,
926
- context.emulator.slot,
927
- ),
928
- );
929
-
930
- await runAndAwaitTx(
931
- context.lucid,
932
- createSpAccount(
933
- iusdAssetInfo.iassetTokenNameAscii,
934
- 10_000_000n,
935
- sysParams,
936
- context.lucid,
937
- ),
938
- );
939
- }
940
-
941
- // Process the create account request
942
- {
943
- const orefs = await findAllNecessaryOrefs(
944
- context.lucid,
945
- sysParams,
946
- iusdAssetInfo.iassetTokenNameAscii,
947
- );
948
-
949
- const accountUtxo = await findStabilityPoolAccount(
950
- context.lucid,
951
- sysParams.validatorHashes.stabilityPoolHash,
952
- paymentCredentialOf(context.users.user.address).hash,
953
- iusdAssetInfo.iassetTokenNameAscii,
954
- );
955
-
956
- await runAndAwaitTx(
957
- context.lucid,
958
- processSpRequest(
959
- iusdAssetInfo.iassetTokenNameAscii,
960
- orefs.stabilityPoolUtxo,
961
- accountUtxo,
962
- orefs.govUtxo,
963
- orefs.iasset.utxo,
964
- undefined,
965
- sysParams,
966
- context.lucid,
967
- orefs.collectorUtxo,
968
- ),
969
- );
970
- }
971
-
972
- context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
973
-
974
- {
975
- const cdp = await findCdp(
976
- context.lucid,
977
- sysParams.validatorHashes.cdpHash,
978
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
979
- paymentCredentialOf(context.users.admin.address).hash,
980
- stakeCredentialOf(context.users.admin.address),
981
- );
982
-
983
- const orefs = await findAllNecessaryOrefs(
984
- context.lucid,
985
- sysParams,
986
- iusdAssetInfo.iassetTokenNameAscii,
987
- );
988
-
989
- await runAndAwaitTx(
990
- context.lucid,
991
- feedPriceOracleTx(
992
- context.lucid,
993
- orefs.priceOracleUtxo,
994
- {
995
- getOnChainInt: 1_800_000n,
996
- },
997
- iusdAssetInfo.oracleParams,
998
- context.emulator.slot,
999
- ),
1000
- );
1001
-
1002
- assertValueInRange(
1003
- await findCdpCR(context, sysParams, iusdAssetInfo, cdp),
1004
- { min: 111, max: 112 },
1005
- );
1006
- }
1007
-
1008
- // We want user to do the freeze of admin's CDP
1009
- context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
1010
-
1011
- {
1012
- const cdp = await findCdp(
1013
- context.lucid,
1014
- sysParams.validatorHashes.cdpHash,
1015
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
1016
- paymentCredentialOf(context.users.admin.address).hash,
1017
- stakeCredentialOf(context.users.admin.address),
1018
- );
1019
-
1020
- const orefs = await findAllNecessaryOrefs(
1021
- context.lucid,
1022
- sysParams,
1023
- iusdAssetInfo.iassetTokenNameAscii,
1024
- );
1025
-
1026
- await runAndAwaitTx(
1027
- context.lucid,
1028
- freezeCdp(
1029
- cdp.utxo,
1030
- orefs.iasset.utxo,
1031
- orefs.priceOracleUtxo,
1032
- orefs.interestOracleUtxo,
1033
- sysParams,
1034
- context.lucid,
1035
- context.emulator.slot,
1036
- ),
1037
- );
1038
- }
1039
-
1040
- {
1041
- const frozenCdp = matchSingle(
1042
- await findFrozenCDPs(
1043
- context.lucid,
1044
- sysParams.validatorHashes.cdpHash,
1045
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
1046
- iusdAssetInfo.iassetTokenNameAscii,
1047
- ),
1048
- (_) => new Error('Expected only single frozen CDP'),
1049
- );
1050
-
1051
- const orefs = await findAllNecessaryOrefs(
1052
- context.lucid,
1053
- sysParams,
1054
- iusdAssetInfo.iassetTokenNameAscii,
1055
- );
1056
-
1057
- await runAndAwaitTx(
1058
- context.lucid,
1059
- liquidateCdp(
1060
- frozenCdp.utxo,
1061
- orefs.stabilityPoolUtxo,
1062
- orefs.collectorUtxo,
1063
- orefs.treasuryUtxo,
1064
- sysParams,
1065
- context.lucid,
1066
- ),
1067
- );
1068
- }
1069
-
1070
- {
1071
- const orefs = await findAllNecessaryOrefs(
1072
- context.lucid,
1073
- sysParams,
1074
- iusdAssetInfo.iassetTokenNameAscii,
1075
- );
1076
-
1077
- expect(
1078
- assetClassValueOf(orefs.stabilityPoolUtxo.assets, {
1079
- currencySymbol: sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
1080
- tokenName: fromText(iusdAssetInfo.iassetTokenNameAscii),
1081
- }) === 5_000_000n,
1082
- 'Expected different stability pool iassets amount',
1083
- ).toBeTruthy();
1084
- }
1085
- });
1086
-
1087
- test<MyContext>('Partialy liquidate CDP', async (context: MyContext) => {
1088
- context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
1089
-
1090
- const [sysParams, [iusdAssetInfo]] = await init(context.lucid, [
1091
- iusdInitialAssetCfg,
1092
- ]);
1093
-
1094
- {
1095
- const orefs = await findAllNecessaryOrefs(
1096
- context.lucid,
1097
- sysParams,
1098
- iusdAssetInfo.iassetTokenNameAscii,
1099
- );
1100
-
1101
- // This is the position that will get liquidated.
1102
- await runAndAwaitTx(
1103
- context.lucid,
1104
- openCdp(
1105
- 10_000_000n,
1106
- 5_000_000n,
1107
- sysParams,
1108
- orefs.cdpCreatorUtxo,
1109
- orefs.iasset.utxo,
1110
- orefs.priceOracleUtxo,
1111
- orefs.interestOracleUtxo,
1112
- orefs.collectorUtxo,
1113
- context.lucid,
1114
- context.emulator.slot,
1115
- ),
1116
- );
1117
- }
1118
-
1119
- context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
1120
-
1121
- {
1122
- const orefs = await findAllNecessaryOrefs(
1123
- context.lucid,
1124
- sysParams,
1125
- iusdAssetInfo.iassetTokenNameAscii,
1126
- );
1127
-
1128
- await runAndAwaitTx(
1129
- context.lucid,
1130
- openCdp(
1131
- 20_000_000n,
1132
- 3_000_000n,
1133
- sysParams,
1134
- orefs.cdpCreatorUtxo,
1135
- orefs.iasset.utxo,
1136
- orefs.priceOracleUtxo,
1137
- orefs.interestOracleUtxo,
1138
- orefs.collectorUtxo,
1139
- context.lucid,
1140
- context.emulator.slot,
1141
- ),
1142
- );
1143
-
1144
- await runAndAwaitTx(
1145
- context.lucid,
1146
- createSpAccount(
1147
- iusdAssetInfo.iassetTokenNameAscii,
1148
- 3_000_000n,
1149
- sysParams,
1150
- context.lucid,
1151
- ),
1152
- );
1153
- }
1154
-
1155
- // Process the create account request
1156
- {
1157
- const orefs = await findAllNecessaryOrefs(
1158
- context.lucid,
1159
- sysParams,
1160
- iusdAssetInfo.iassetTokenNameAscii,
1161
- );
1162
-
1163
- const accountUtxo = await findStabilityPoolAccount(
1164
- context.lucid,
1165
- sysParams.validatorHashes.stabilityPoolHash,
1166
- paymentCredentialOf(context.users.user.address).hash,
1167
- iusdAssetInfo.iassetTokenNameAscii,
1168
- );
1169
-
1170
- await runAndAwaitTx(
1171
- context.lucid,
1172
- processSpRequest(
1173
- iusdAssetInfo.iassetTokenNameAscii,
1174
- orefs.stabilityPoolUtxo,
1175
- accountUtxo,
1176
- orefs.govUtxo,
1177
- orefs.iasset.utxo,
1178
- undefined,
1179
- sysParams,
1180
- context.lucid,
1181
- orefs.collectorUtxo,
1182
- ),
1183
- );
1184
- }
1185
-
1186
- context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
1187
-
1188
- {
1189
- const cdp = await findCdp(
1190
- context.lucid,
1191
- sysParams.validatorHashes.cdpHash,
1192
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
1193
- paymentCredentialOf(context.users.admin.address).hash,
1194
- stakeCredentialOf(context.users.admin.address),
1195
- );
1196
-
1197
- const orefs = await findAllNecessaryOrefs(
1198
- context.lucid,
1199
- sysParams,
1200
- iusdAssetInfo.iassetTokenNameAscii,
1201
- );
1202
-
1203
- await runAndAwaitTx(
1204
- context.lucid,
1205
- feedPriceOracleTx(
1206
- context.lucid,
1207
- orefs.priceOracleUtxo,
1208
- {
1209
- getOnChainInt: 1_800_000n,
1210
- },
1211
- iusdAssetInfo.oracleParams,
1212
- context.emulator.slot,
1213
- ),
1214
- );
1215
-
1216
- assertValueInRange(
1217
- await findCdpCR(context, sysParams, iusdAssetInfo, cdp),
1218
- { min: 111, max: 112 },
1219
- );
1220
- }
1221
-
1222
- // We want user to do the freeze of admin's CDP
1223
- context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
1224
-
1225
- {
1226
- const cdp = await findCdp(
1227
- context.lucid,
1228
- sysParams.validatorHashes.cdpHash,
1229
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
1230
- paymentCredentialOf(context.users.admin.address).hash,
1231
- stakeCredentialOf(context.users.admin.address),
1232
- );
1233
-
1234
- const orefs = await findAllNecessaryOrefs(
1235
- context.lucid,
1236
- sysParams,
1237
- iusdAssetInfo.iassetTokenNameAscii,
1238
- );
1239
-
1240
- await runAndAwaitTx(
1241
- context.lucid,
1242
- freezeCdp(
1243
- cdp.utxo,
1244
- orefs.iasset.utxo,
1245
- orefs.priceOracleUtxo,
1246
- orefs.interestOracleUtxo,
1247
- sysParams,
1248
- context.lucid,
1249
- context.emulator.slot,
1250
- ),
1251
- );
1252
- }
1253
-
1254
- {
1255
- const frozenCdp = matchSingle(
1256
- await findFrozenCDPs(
1257
- context.lucid,
1258
- sysParams.validatorHashes.cdpHash,
1259
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
1260
- iusdAssetInfo.iassetTokenNameAscii,
1261
- ),
1262
- (_) => new Error('Expected only single frozen CDP'),
1263
- );
1264
-
1265
- const orefs = await findAllNecessaryOrefs(
1266
- context.lucid,
1267
- sysParams,
1268
- iusdAssetInfo.iassetTokenNameAscii,
1269
- );
1270
-
1271
- await runAndAwaitTx(
1272
- context.lucid,
1273
- liquidateCdp(
1274
- frozenCdp.utxo,
1275
- orefs.stabilityPoolUtxo,
1276
- orefs.collectorUtxo,
1277
- orefs.treasuryUtxo,
1278
- sysParams,
1279
- context.lucid,
1280
- ),
1281
- );
1282
- }
1283
-
1284
- {
1285
- const orefs = await findAllNecessaryOrefs(
1286
- context.lucid,
1287
- sysParams,
1288
- iusdAssetInfo.iassetTokenNameAscii,
1289
- );
1290
-
1291
- expect(
1292
- assetClassValueOf(orefs.stabilityPoolUtxo.assets, {
1293
- currencySymbol: sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
1294
- tokenName: fromText(iusdAssetInfo.iassetTokenNameAscii),
1295
- }) === 0n,
1296
- 'Expected different stability pool iassets amount',
1297
- ).toBeTruthy();
1298
- }
1299
- });
1300
-
1301
- test<MyContext>('Merge CDPs and liquidate merged', async (context: MyContext) => {
1302
- context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
1303
-
1304
- const [sysParams, [iusdAssetInfo]] = await init(context.lucid, [
1305
- iusdInitialAssetCfg,
1306
- ]);
1307
-
1308
- await repeat(3, async () => {
1309
- const orefs = await findAllNecessaryOrefs(
1310
- context.lucid,
1311
- sysParams,
1312
- iusdAssetInfo.iassetTokenNameAscii,
1313
- );
1314
-
1315
- await runAndAwaitTx(
1316
- context.lucid,
1317
- openCdp(
1318
- 12_000_000n,
1319
- 6_000_000n,
1320
- sysParams,
1321
- orefs.cdpCreatorUtxo,
1322
- orefs.iasset.utxo,
1323
- orefs.priceOracleUtxo,
1324
- orefs.interestOracleUtxo,
1325
- orefs.collectorUtxo,
1326
- context.lucid,
1327
- context.emulator.slot,
1328
- ),
1329
- );
1330
- });
1331
-
1332
- {
1333
- const orefs = await findAllNecessaryOrefs(
1334
- context.lucid,
1335
- sysParams,
1336
- iusdAssetInfo.iassetTokenNameAscii,
1337
- );
1338
-
1339
- await runAndAwaitTx(
1340
- context.lucid,
1341
- feedPriceOracleTx(
1342
- context.lucid,
1343
- orefs.priceOracleUtxo,
1344
- {
1345
- getOnChainInt: 1_800_000n,
1346
- },
1347
- iusdAssetInfo.oracleParams,
1348
- context.emulator.slot,
1349
- ),
1350
- );
1351
- }
1352
-
1353
- {
1354
- const activeCdps = await findAllActiveCdps(
1355
- context.lucid,
1356
- sysParams,
1357
- iusdAssetInfo.iassetTokenNameAscii,
1358
- stakeCredentialOf(context.users.admin.address),
1359
- );
1360
-
1361
- expect(activeCdps.length === 3, 'Expected 3 cdps').toBeTruthy();
1362
-
1363
- for (const cdp of activeCdps) {
1364
- const orefs = await findAllNecessaryOrefs(
1365
- context.lucid,
1366
- sysParams,
1367
- iusdAssetInfo.iassetTokenNameAscii,
1368
- );
1369
-
1370
- await runAndAwaitTx(
1371
- context.lucid,
1372
- freezeCdp(
1373
- cdp.utxo,
1374
- orefs.iasset.utxo,
1375
- orefs.priceOracleUtxo,
1376
- orefs.interestOracleUtxo,
1377
- sysParams,
1378
- context.lucid,
1379
- context.emulator.slot,
1380
- ),
1381
- );
1382
- }
1383
- }
1384
-
1385
- {
1386
- const frozenCdps = await findFrozenCDPs(
1387
- context.lucid,
1388
- sysParams.validatorHashes.cdpHash,
1389
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
1390
- iusdAssetInfo.iassetTokenNameAscii,
1391
- );
1392
-
1393
- expect(frozenCdps.length === 3, 'Expected 3 frozen cdps').toBeTruthy();
1394
-
1395
- await runAndAwaitTx(
1396
- context.lucid,
1397
- mergeCdps(
1398
- frozenCdps.map((cdp) => cdp.utxo),
1399
- sysParams,
1400
- context.lucid,
1401
- ),
1402
- );
1403
- }
1404
-
1405
- ///////////////////////////
1406
- // Liquidation
1407
- ///////////////////////////
1408
-
1409
- context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
1410
-
1411
- {
1412
- const orefs = await findAllNecessaryOrefs(
1413
- context.lucid,
1414
- sysParams,
1415
- iusdAssetInfo.iassetTokenNameAscii,
1416
- );
1417
-
1418
- await runAndAwaitTx(
1419
- context.lucid,
1420
- openCdp(
1421
- 100_000_000n,
1422
- 20_000_000n,
1423
- sysParams,
1424
- orefs.cdpCreatorUtxo,
1425
- orefs.iasset.utxo,
1426
- orefs.priceOracleUtxo,
1427
- orefs.interestOracleUtxo,
1428
- orefs.collectorUtxo,
1429
- context.lucid,
1430
- context.emulator.slot,
1431
- ),
1432
- );
1433
-
1434
- await runAndAwaitTx(
1435
- context.lucid,
1436
- createSpAccount(
1437
- iusdAssetInfo.iassetTokenNameAscii,
1438
- 20_000_000n,
1439
- sysParams,
1440
- context.lucid,
1441
- ),
1442
- );
1443
- }
1444
-
1445
- // Process the create account request
1446
- {
1447
- const orefs = await findAllNecessaryOrefs(
1448
- context.lucid,
1449
- sysParams,
1450
- iusdAssetInfo.iassetTokenNameAscii,
1451
- );
1452
-
1453
- const accountUtxo = await findStabilityPoolAccount(
1454
- context.lucid,
1455
- sysParams.validatorHashes.stabilityPoolHash,
1456
- paymentCredentialOf(context.users.user.address).hash,
1457
- iusdAssetInfo.iassetTokenNameAscii,
1458
- );
1459
-
1460
- await runAndAwaitTx(
1461
- context.lucid,
1462
- processSpRequest(
1463
- iusdAssetInfo.iassetTokenNameAscii,
1464
- orefs.stabilityPoolUtxo,
1465
- accountUtxo,
1466
- orefs.govUtxo,
1467
- orefs.iasset.utxo,
1468
- undefined,
1469
- sysParams,
1470
- context.lucid,
1471
- orefs.collectorUtxo,
1472
- ),
1473
- );
1474
- }
1475
-
1476
- {
1477
- const frozenCdp = matchSingle(
1478
- await findFrozenCDPs(
1479
- context.lucid,
1480
- sysParams.validatorHashes.cdpHash,
1481
- fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
1482
- iusdAssetInfo.iassetTokenNameAscii,
1483
- ),
1484
- (_) => new Error('Expected only single frozen CDP'),
1485
- );
1486
-
1487
- expect(
1488
- frozenCdp.datum.mintedAmt === 18_000_000n &&
1489
- frozenCdp.datum.cdpOwner == null,
1490
- 'Expected frozen certain frozen CDP',
1491
- ).toBeTruthy();
1492
-
1493
- const orefs = await findAllNecessaryOrefs(
1494
- context.lucid,
1495
- sysParams,
1496
- iusdAssetInfo.iassetTokenNameAscii,
1497
- );
1498
-
1499
- await runAndAwaitTx(
1500
- context.lucid,
1501
- liquidateCdp(
1502
- frozenCdp.utxo,
1503
- orefs.stabilityPoolUtxo,
1504
- orefs.collectorUtxo,
1505
- orefs.treasuryUtxo,
1506
- sysParams,
1507
- context.lucid,
1508
- ),
1509
- );
1510
- }
1511
-
1512
- {
1513
- const orefs = await findAllNecessaryOrefs(
1514
- context.lucid,
1515
- sysParams,
1516
- iusdAssetInfo.iassetTokenNameAscii,
1517
- );
1518
-
1519
- expect(
1520
- assetClassValueOf(orefs.stabilityPoolUtxo.assets, {
1521
- currencySymbol: sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
1522
- tokenName: fromText(iusdAssetInfo.iassetTokenNameAscii),
1523
- }) === 2_000_000n,
1524
- 'Expected different stability pool iassets amount',
1525
- ).toBeTruthy();
1526
- }
1527
- });
1528
- });