@indigo-labs/indigo-sdk 0.1.19 → 0.1.21

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 +8 -3
  2. package/dist/index.d.mts +1298 -677
  3. package/dist/index.d.ts +1298 -677
  4. package/dist/index.js +4650 -2217
  5. package/dist/index.mjs +4592 -2192
  6. package/eslint.config.mjs +7 -1
  7. package/package.json +9 -4
  8. package/src/contracts/cdp/helpers.ts +167 -0
  9. package/src/contracts/cdp/scripts.ts +33 -0
  10. package/src/contracts/cdp/transactions.ts +1310 -0
  11. package/src/contracts/cdp/types.ts +161 -0
  12. package/src/contracts/cdp-creator/scripts.ts +39 -0
  13. package/src/{types/indigo/cdp-creator.ts → contracts/cdp-creator/types.ts} +6 -4
  14. package/src/contracts/collector/scripts.ts +32 -0
  15. package/src/contracts/collector/transactions.ts +44 -0
  16. package/src/contracts/execute/scripts.ts +48 -0
  17. package/src/contracts/execute/types.ts +57 -0
  18. package/src/contracts/gov/helpers.ts +157 -0
  19. package/src/contracts/gov/scripts.ts +34 -0
  20. package/src/contracts/gov/transactions.ts +1224 -0
  21. package/src/contracts/gov/types-new.ts +115 -0
  22. package/src/contracts/gov/types.ts +89 -0
  23. package/src/{helpers/interest-oracle.ts → contracts/interest-oracle/helpers.ts} +37 -9
  24. package/src/contracts/interest-oracle/scripts.ts +18 -0
  25. package/src/contracts/interest-oracle/transactions.ts +149 -0
  26. package/src/{types/indigo/interest-oracle.ts → contracts/interest-oracle/types.ts} +1 -1
  27. package/src/contracts/lrp/scripts.ts +27 -0
  28. package/src/contracts/{lrp.ts → lrp/transactions.ts} +23 -23
  29. package/src/{types/indigo/lrp.ts → contracts/lrp/types.ts} +3 -3
  30. package/src/{scripts/one-shot-policy.ts → contracts/one-shot/scripts.ts} +1 -1
  31. package/src/contracts/{one-shot.ts → one-shot/transactions.ts} +3 -3
  32. package/src/contracts/poll/helpers.ts +55 -0
  33. package/src/contracts/poll/scripts.ts +72 -0
  34. package/src/contracts/poll/types-poll-manager.ts +38 -0
  35. package/src/contracts/poll/types-poll-shard.ts +38 -0
  36. package/src/contracts/poll/types-poll.ts +88 -0
  37. package/src/{scripts/price-oracle-validator.ts → contracts/price-oracle/scripts.ts} +1 -4
  38. package/src/contracts/price-oracle/transactions.ts +112 -0
  39. package/src/{types/indigo/price-oracle.ts → contracts/price-oracle/types.ts} +16 -4
  40. package/src/{helpers/stability-pool-helpers.ts → contracts/stability-pool/helpers.ts} +110 -6
  41. package/src/contracts/stability-pool/scripts.ts +46 -0
  42. package/src/contracts/stability-pool/transactions.ts +660 -0
  43. package/src/contracts/stability-pool/types-new.ts +208 -0
  44. package/src/contracts/stability-pool/types.ts +42 -0
  45. package/src/contracts/staking/helpers.ts +116 -0
  46. package/src/contracts/staking/scripts.ts +41 -0
  47. package/src/contracts/staking/transactions.ts +268 -0
  48. package/src/contracts/staking/types-new.ts +81 -0
  49. package/src/contracts/staking/types.ts +41 -0
  50. package/src/contracts/treasury/scripts.ts +37 -0
  51. package/src/contracts/treasury/transactions.ts +44 -0
  52. package/src/contracts/treasury/types.ts +55 -0
  53. package/src/contracts/version-registry/scripts.ts +29 -0
  54. package/src/contracts/version-registry/types-new.ts +19 -0
  55. package/src/{types/indigo/version-record.ts → contracts/version-registry/types.ts} +1 -1
  56. package/src/contracts/vesting/helpers.ts +267 -0
  57. package/src/index.ts +38 -33
  58. package/src/types/evolution-schema-options.ts +16 -0
  59. package/src/types/generic.ts +78 -60
  60. package/src/types/on-chain-decimal.ts +22 -0
  61. package/src/types/system-params.ts +22 -11
  62. package/src/utils/bigint-utils.ts +7 -0
  63. package/src/{helpers → utils}/helper-txs.ts +1 -0
  64. package/src/utils/time-helpers.ts +4 -0
  65. package/src/{helpers/helpers.ts → utils/utils.ts} +0 -10
  66. package/src/{helpers → utils}/value-helpers.ts +10 -0
  67. package/src/{scripts → validators}/cdp-creator-validator.ts +4 -50
  68. package/src/{scripts → validators}/cdp-validator.ts +3 -5
  69. package/src/{scripts → validators}/collector-validator.ts +2 -3
  70. package/src/{scripts/version-registry.ts → validators/execute-validator.ts} +3 -11
  71. package/src/{scripts/gov-validator.ts → validators/governance-validator.ts} +3 -40
  72. package/src/{scripts → validators}/interest-oracle-validator.ts +4 -20
  73. package/src/validators/lrp-validator.ts +7 -0
  74. package/src/validators/poll-manager-validator.ts +7 -0
  75. package/src/{scripts → validators}/poll-shard-validator.ts +3 -43
  76. package/src/{scripts → validators}/stability-pool-validator.ts +4 -57
  77. package/src/{scripts → validators}/staking-validator.ts +2 -3
  78. package/src/{scripts → validators}/treasury-validator.ts +2 -3
  79. package/src/{scripts → validators}/version-record-policy.ts +4 -23
  80. package/src/validators/version-registry-validator.ts +7 -0
  81. package/tests/cdp.test.ts +1565 -0
  82. package/tests/datums.test.ts +125 -108
  83. package/tests/endpoints/initialize.ts +240 -338
  84. package/tests/gov.test.ts +1874 -0
  85. package/tests/hash-checks.test.ts +26 -21
  86. package/tests/indigo-test-helpers.ts +1 -55
  87. package/tests/initialize.test.ts +10 -5
  88. package/tests/interest-calculations.test.ts +18 -18
  89. package/tests/interest-oracle.test.ts +20 -18
  90. package/tests/lrp.test.ts +180 -78
  91. package/tests/mock/assets-mock.ts +59 -0
  92. package/tests/queries/cdp-queries.ts +144 -0
  93. package/tests/queries/collector-queries.ts +26 -0
  94. package/tests/queries/execute-queries.ts +46 -0
  95. package/tests/queries/governance-queries.ts +19 -16
  96. package/tests/queries/iasset-queries.ts +46 -23
  97. package/tests/queries/interest-oracle-queries.ts +3 -6
  98. package/tests/queries/lrp-queries.ts +2 -2
  99. package/tests/queries/poll-queries.ts +97 -0
  100. package/tests/queries/price-oracle-queries.ts +5 -22
  101. package/tests/queries/stability-pool-queries.ts +10 -8
  102. package/tests/queries/staking-queries.ts +28 -19
  103. package/tests/queries/treasury-queries.ts +19 -0
  104. package/tests/stability-pool.test.ts +186 -71
  105. package/tests/staking.test.ts +30 -23
  106. package/tests/test-helpers.ts +11 -2
  107. package/tests/utils/asserts.ts +13 -0
  108. package/tests/utils/index.ts +50 -0
  109. package/tsconfig.json +3 -1
  110. package/vitest.config.ts +1 -1
  111. package/.github/workflows/test.yml +0 -44
  112. package/src/contracts/cdp.ts +0 -748
  113. package/src/contracts/collector.ts +0 -98
  114. package/src/contracts/gov.ts +0 -1
  115. package/src/contracts/interest-oracle.ts +0 -149
  116. package/src/contracts/stability-pool.ts +0 -692
  117. package/src/contracts/staking.ts +0 -348
  118. package/src/contracts/treasury.ts +0 -112
  119. package/src/helpers/asset-helpers.ts +0 -57
  120. package/src/helpers/staking-helpers.ts +0 -94
  121. package/src/helpers/time-helpers.ts +0 -4
  122. package/src/scripts/execute-validator.ts +0 -52
  123. package/src/scripts/lrp-validator.ts +0 -40
  124. package/src/scripts/poll-manager-validator.ts +0 -52
  125. package/src/types/indigo/cdp.ts +0 -88
  126. package/src/types/indigo/execute.ts +0 -21
  127. package/src/types/indigo/gov.ts +0 -51
  128. package/src/types/indigo/poll-manager.ts +0 -21
  129. package/src/types/indigo/poll-shard.ts +0 -16
  130. package/src/types/indigo/stability-pool.ts +0 -233
  131. package/src/types/indigo/staking.ts +0 -99
  132. /package/src/{types/one-shot.ts → contracts/one-shot/types.ts} +0 -0
  133. /package/src/{helpers/price-oracle-helpers.ts → contracts/price-oracle/helpers.ts} +0 -0
  134. /package/src/{helpers → utils}/indigo-helpers.ts +0 -0
  135. /package/src/{helpers → utils}/lucid-utils.ts +0 -0
@@ -1,692 +0,0 @@
1
- import {
2
- Constr,
3
- fromText,
4
- LucidEvolution,
5
- TxBuilder,
6
- validatorToScriptHash,
7
- Data,
8
- UTxO,
9
- credentialToAddress,
10
- } from '@lucid-evolution/lucid';
11
- import {
12
- EpochToScaleToSum,
13
- mkSPInteger,
14
- parseAccountDatum,
15
- parseStabilityPoolDatum,
16
- serialiseStabilityPoolDatum,
17
- serialiseStabilityPoolRedeemer,
18
- spDiv,
19
- spMul,
20
- StabilityPoolDatum,
21
- StabilityPoolRedeemer,
22
- StabilityPoolSnapshot,
23
- AccountAction,
24
- AccountContent,
25
- spAdd,
26
- fromSPInteger,
27
- ActionReturnDatum,
28
- spSub,
29
- } from '../types/indigo/stability-pool';
30
- import { SystemParams } from '../types/system-params';
31
- import { addrDetails, scriptRef } from '../helpers/lucid-utils';
32
- import { mkStabilityPoolValidatorFromSP } from '../scripts/stability-pool-validator';
33
- import {
34
- adjustmentHelper,
35
- setSumInEpochToScaleToSum,
36
- updatePoolSnapshotWithdrawalFee,
37
- } from '../helpers/stability-pool-helpers';
38
- import { calculateFeeFromPercentage } from '../helpers/indigo-helpers';
39
- import { GovDatum, parseGovDatum } from '../types/indigo/gov';
40
- import { IAssetContent, parseIAssetDatum } from '../types/indigo/cdp';
41
- import { CollectorContract } from './collector';
42
- import { addressFromBech32, addressToBech32 } from '../types/generic';
43
-
44
- export class StabilityPoolContract {
45
- static async createAccount(
46
- asset: string,
47
- amount: bigint,
48
- params: SystemParams,
49
- lucid: LucidEvolution,
50
- ): Promise<TxBuilder> {
51
- const [pkh, _skh] = await addrDetails(lucid);
52
- const minLovelaces = BigInt(
53
- params.stabilityPoolParams.accountCreateFeeLovelaces +
54
- params.stabilityPoolParams.requestCollateralLovelaces,
55
- );
56
- const datum: StabilityPoolDatum = {
57
- Account: {
58
- content: {
59
- owner: pkh.hash,
60
- asset: fromText(asset),
61
- snapshot: {
62
- productVal: { value: 0n },
63
- depositVal: { value: 0n },
64
- sumVal: { value: 0n },
65
- epoch: 0n,
66
- scale: 0n,
67
- },
68
- request: 'Create',
69
- },
70
- },
71
- };
72
-
73
- return lucid
74
- .newTx()
75
- .pay.ToContract(
76
- credentialToAddress(lucid.config().network, {
77
- hash: validatorToScriptHash(
78
- mkStabilityPoolValidatorFromSP(params.stabilityPoolParams),
79
- ),
80
- type: 'Script',
81
- }),
82
- { kind: 'inline', value: serialiseStabilityPoolDatum(datum) },
83
- {
84
- lovelace: minLovelaces,
85
- [params.stabilityPoolParams.assetSymbol.unCurrencySymbol +
86
- fromText(asset)]: amount,
87
- },
88
- )
89
- .addSignerKey(pkh.hash);
90
- }
91
-
92
- static async adjustAccount(
93
- asset: string,
94
- amount: bigint,
95
- accountUtxo: UTxO,
96
- params: SystemParams,
97
- lucid: LucidEvolution,
98
- ): Promise<TxBuilder> {
99
- const [pkh, _] = await addrDetails(lucid);
100
- const myAddress = await lucid.wallet().address();
101
-
102
- const stabilityPoolScriptRef = await scriptRef(
103
- params.scriptReferences.stabilityPoolValidatorRef,
104
- lucid,
105
- );
106
-
107
- const request: AccountAction = {
108
- Adjust: {
109
- amount: amount,
110
- outputAddress: addressFromBech32(myAddress),
111
- },
112
- };
113
- const oldAccountDatum: AccountContent = parseAccountDatum(
114
- accountUtxo.datum,
115
- );
116
-
117
- const newAccountDatum: AccountContent = {
118
- ...oldAccountDatum,
119
- request,
120
- };
121
-
122
- const redeemer: StabilityPoolRedeemer = {
123
- RequestAction: {
124
- action: request,
125
- },
126
- };
127
-
128
- const value = {
129
- lovelace: BigInt(
130
- params.stabilityPoolParams.requestCollateralLovelaces +
131
- params.stabilityPoolParams.accountAdjustmentFeeLovelaces,
132
- ),
133
- [params.stabilityPoolParams.accountToken[0].unCurrencySymbol +
134
- fromText(params.stabilityPoolParams.accountToken[1].unTokenName)]: 1n,
135
- };
136
-
137
- if (amount > 0n) {
138
- value[
139
- params.stabilityPoolParams.assetSymbol.unCurrencySymbol +
140
- fromText(asset)
141
- ] = amount;
142
- }
143
-
144
- return lucid
145
- .newTx()
146
- .readFrom([stabilityPoolScriptRef])
147
- .collectFrom([accountUtxo], serialiseStabilityPoolRedeemer(redeemer))
148
- .pay.ToContract(
149
- credentialToAddress(lucid.config().network, {
150
- hash: validatorToScriptHash(
151
- mkStabilityPoolValidatorFromSP(params.stabilityPoolParams),
152
- ),
153
- type: 'Script',
154
- }),
155
- {
156
- kind: 'inline',
157
- value: serialiseStabilityPoolDatum({
158
- Account: {
159
- content: newAccountDatum,
160
- },
161
- }),
162
- },
163
- value,
164
- )
165
- .addSignerKey(pkh.hash);
166
- }
167
-
168
- static async closeAccount(
169
- accountUtxo: UTxO,
170
- params: SystemParams,
171
- lucid: LucidEvolution,
172
- ): Promise<TxBuilder> {
173
- const [pkh, _] = await addrDetails(lucid);
174
- const myAddress = await lucid.wallet().address();
175
-
176
- const stabilityPoolScriptRef = await scriptRef(
177
- params.scriptReferences.stabilityPoolValidatorRef,
178
- lucid,
179
- );
180
-
181
- const request: AccountAction = {
182
- Close: {
183
- outputAddress: addressFromBech32(myAddress),
184
- },
185
- };
186
- const oldAccountDatum: AccountContent = parseAccountDatum(
187
- accountUtxo.datum,
188
- );
189
- const newAccountDatum: AccountContent = {
190
- ...oldAccountDatum,
191
- request,
192
- };
193
-
194
- const redeemer: StabilityPoolRedeemer = {
195
- RequestAction: {
196
- action: request,
197
- },
198
- };
199
-
200
- return lucid
201
- .newTx()
202
- .readFrom([stabilityPoolScriptRef])
203
- .collectFrom([accountUtxo], serialiseStabilityPoolRedeemer(redeemer))
204
- .pay.ToContract(
205
- credentialToAddress(lucid.config().network, {
206
- hash: validatorToScriptHash(
207
- mkStabilityPoolValidatorFromSP(params.stabilityPoolParams),
208
- ),
209
- type: 'Script',
210
- }),
211
- {
212
- kind: 'inline',
213
- value: serialiseStabilityPoolDatum({
214
- Account: {
215
- content: newAccountDatum,
216
- },
217
- }),
218
- },
219
- {
220
- lovelace: BigInt(
221
- params.stabilityPoolParams.requestCollateralLovelaces +
222
- params.stabilityPoolParams.accountAdjustmentFeeLovelaces,
223
- ),
224
- [params.stabilityPoolParams.accountToken[0].unCurrencySymbol +
225
- fromText(params.stabilityPoolParams.accountToken[1].unTokenName)]: 1n,
226
- },
227
- )
228
- .addSignerKey(pkh.hash);
229
- }
230
-
231
- static async processRequest(
232
- asset: string,
233
- stabilityPoolUtxo: UTxO,
234
- accountUtxo: UTxO,
235
- govUtxo: UTxO,
236
- iAssetUtxo: UTxO,
237
- newSnapshotUtxo: UTxO | undefined,
238
- params: SystemParams,
239
- lucid: LucidEvolution,
240
- ): Promise<TxBuilder> {
241
- const redeemer: StabilityPoolRedeemer = {
242
- ProcessRequest: {
243
- requestRef: {
244
- txHash: { hash: accountUtxo.txHash },
245
- outputIndex: BigInt(accountUtxo.outputIndex),
246
- },
247
- },
248
- };
249
- const stabilityPoolScriptRef = await scriptRef(
250
- params.scriptReferences.stabilityPoolValidatorRef,
251
- lucid,
252
- );
253
-
254
- const accountDatum = parseAccountDatum(accountUtxo.datum);
255
- const stabilityPoolDatum = parseStabilityPoolDatum(stabilityPoolUtxo.datum);
256
-
257
- const tx = lucid
258
- .newTx()
259
- .collectFrom(
260
- [stabilityPoolUtxo],
261
- serialiseStabilityPoolRedeemer(redeemer),
262
- )
263
- .collectFrom([accountUtxo], serialiseStabilityPoolRedeemer(redeemer))
264
- .readFrom([iAssetUtxo, govUtxo, stabilityPoolScriptRef]);
265
-
266
- if (!accountDatum.request) throw new Error('Account Request is null');
267
- if (accountDatum.request === 'Create') {
268
- const accountTokenScriptRef = await scriptRef(
269
- params.scriptReferences.authTokenPolicies.accountTokenRef,
270
- lucid,
271
- );
272
- tx.readFrom([accountTokenScriptRef]);
273
-
274
- const iassetUnit =
275
- params.stabilityPoolParams.assetSymbol.unCurrencySymbol +
276
- fromText(asset);
277
- const reqAmount = accountUtxo.assets[iassetUnit] ?? 0n;
278
-
279
- const newAccountSnapshot: StabilityPoolSnapshot = {
280
- ...stabilityPoolDatum.snapshot,
281
- depositVal: {
282
- value: spAdd(accountDatum.snapshot.depositVal, mkSPInteger(reqAmount))
283
- .value,
284
- },
285
- };
286
-
287
- const newDeposit = spAdd(
288
- stabilityPoolDatum.snapshot.depositVal,
289
- mkSPInteger(reqAmount),
290
- );
291
- const newSum = spAdd(
292
- stabilityPoolDatum.snapshot.sumVal,
293
- spDiv(
294
- spMul(
295
- mkSPInteger(
296
- BigInt(params.stabilityPoolParams.accountCreateFeeLovelaces),
297
- ),
298
- stabilityPoolDatum.snapshot.productVal,
299
- ),
300
- newDeposit,
301
- ),
302
- );
303
- const newStabilityPoolSnapshot: StabilityPoolSnapshot = {
304
- ...stabilityPoolDatum.snapshot,
305
- depositVal: newDeposit,
306
- sumVal: newSum,
307
- };
308
-
309
- const newEpochToScaleToSum: EpochToScaleToSum = new Map(
310
- stabilityPoolDatum.epochToScaleToSum,
311
- );
312
- newEpochToScaleToSum.set(
313
- {
314
- epoch: stabilityPoolDatum.snapshot.epoch,
315
- scale: stabilityPoolDatum.snapshot.scale,
316
- },
317
- newSum,
318
- );
319
-
320
- const stabilityPoolAssetToken =
321
- stabilityPoolUtxo.assets[iassetUnit] ?? 0n;
322
- const poolOutputValue = {
323
- lovelace:
324
- stabilityPoolUtxo.assets.lovelace +
325
- BigInt(params.stabilityPoolParams.accountCreateFeeLovelaces),
326
- [params.stabilityPoolParams.stabilityPoolToken[0].unCurrencySymbol +
327
- fromText(params.stabilityPoolParams.stabilityPoolToken[1].unTokenName)]:
328
- 1n,
329
- [params.stabilityPoolParams.assetSymbol.unCurrencySymbol +
330
- fromText(asset)]: stabilityPoolAssetToken + reqAmount,
331
- };
332
-
333
- tx.mintAssets(
334
- {
335
- [params.stabilityPoolParams.accountToken[0].unCurrencySymbol +
336
- fromText(params.stabilityPoolParams.accountToken[1].unTokenName)]: 1n,
337
- },
338
- Data.to(new Constr(0, [])),
339
- );
340
-
341
- tx.pay.ToContract(
342
- stabilityPoolUtxo.address,
343
- {
344
- kind: 'inline',
345
- value: serialiseStabilityPoolDatum({
346
- StabilityPool: {
347
- content: {
348
- ...stabilityPoolDatum,
349
- snapshot: newStabilityPoolSnapshot,
350
- epochToScaleToSum: newEpochToScaleToSum,
351
- },
352
- },
353
- }),
354
- },
355
- poolOutputValue,
356
- );
357
-
358
- tx.pay.ToContract(
359
- stabilityPoolUtxo.address,
360
- {
361
- kind: 'inline',
362
- value: serialiseStabilityPoolDatum({
363
- Account: {
364
- content: {
365
- ...accountDatum,
366
- snapshot: newAccountSnapshot,
367
- request: null,
368
- },
369
- },
370
- }),
371
- },
372
- {
373
- lovelace:
374
- accountUtxo.assets.lovelace -
375
- BigInt(params.stabilityPoolParams.accountCreateFeeLovelaces),
376
- [params.stabilityPoolParams.accountToken[0].unCurrencySymbol +
377
- fromText(params.stabilityPoolParams.accountToken[1].unTokenName)]: 1n,
378
- },
379
- );
380
- } else if ('Adjust' in accountDatum.request) {
381
- const amount = accountDatum.request.Adjust.amount;
382
- const outputAddress = addressToBech32(
383
- lucid,
384
- accountDatum.request.Adjust.outputAddress,
385
- );
386
- const myAddress = await lucid.wallet().address();
387
-
388
- const [updatedAccountSnapshot, reward, refInputs] = adjustmentHelper(
389
- stabilityPoolUtxo,
390
- newSnapshotUtxo,
391
- stabilityPoolDatum.snapshot,
392
- stabilityPoolDatum.epochToScaleToSum,
393
- accountDatum.snapshot,
394
- );
395
-
396
- const govDatum: GovDatum = parseGovDatum(govUtxo.datum);
397
- const iassetDatum: IAssetContent = parseIAssetDatum(iAssetUtxo.datum);
398
- const rewardLovelacesFee = calculateFeeFromPercentage(
399
- govDatum.protocolParams.collateralFeePercentage,
400
- reward,
401
- );
402
- const isDepositOrRewardWithdrawal: boolean = amount > 0n;
403
- const bigIntMax = (...args: bigint[]): bigint =>
404
- args.reduce((m, e) => (e > m ? e : m));
405
-
406
- const balanceChange: bigint = isDepositOrRewardWithdrawal
407
- ? amount
408
- : bigIntMax(amount, -fromSPInteger(updatedAccountSnapshot.depositVal));
409
- const newAccountSnapshot: StabilityPoolSnapshot = {
410
- ...updatedAccountSnapshot,
411
- depositVal: spAdd(
412
- updatedAccountSnapshot.depositVal,
413
- mkSPInteger(balanceChange),
414
- ),
415
- };
416
-
417
- const _newPoolDepositExcludingFee = spAdd(
418
- stabilityPoolDatum.snapshot.depositVal,
419
- mkSPInteger(balanceChange),
420
- );
421
- const newPoolDepositExcludingFee =
422
- _newPoolDepositExcludingFee.value > 0n
423
- ? _newPoolDepositExcludingFee
424
- : mkSPInteger(0n);
425
-
426
- const withdrawalFee =
427
- isDepositOrRewardWithdrawal || newPoolDepositExcludingFee.value === 0n
428
- ? 0n
429
- : calculateFeeFromPercentage(
430
- iassetDatum.stabilityPoolWithdrawalFeePercentage,
431
- -balanceChange,
432
- );
433
- const newPoolDeposit = spAdd(
434
- newPoolDepositExcludingFee,
435
- mkSPInteger(withdrawalFee),
436
- );
437
- const newPoolProduct =
438
- withdrawalFee === 0n
439
- ? stabilityPoolDatum.snapshot.productVal
440
- : spMul(
441
- stabilityPoolDatum.snapshot.productVal,
442
- spAdd(
443
- mkSPInteger(1n),
444
- spDiv(mkSPInteger(withdrawalFee), newPoolDepositExcludingFee),
445
- ),
446
- );
447
- const newPoolSum = spAdd(
448
- stabilityPoolDatum.snapshot.sumVal,
449
- spDiv(
450
- spMul(
451
- mkSPInteger(
452
- BigInt(params.stabilityPoolParams.accountAdjustmentFeeLovelaces),
453
- ),
454
- newPoolProduct,
455
- ),
456
- newPoolDeposit,
457
- ),
458
- );
459
- const newPoolSnapshot: StabilityPoolSnapshot = {
460
- ...stabilityPoolDatum.snapshot,
461
- depositVal: newPoolDeposit,
462
- sumVal: newPoolSum,
463
- productVal: newPoolProduct,
464
- };
465
- const newEpochToScaleToSum: EpochToScaleToSum = setSumInEpochToScaleToSum(
466
- stabilityPoolDatum.epochToScaleToSum,
467
- stabilityPoolDatum.snapshot.epoch,
468
- stabilityPoolDatum.snapshot.scale,
469
- newPoolSum,
470
- );
471
-
472
- await CollectorContract.feeTx(rewardLovelacesFee, lucid, params, tx);
473
-
474
- tx.readFrom([govUtxo, iAssetUtxo, ...refInputs]);
475
- tx.pay.ToContract(
476
- stabilityPoolUtxo.address,
477
- {
478
- kind: 'inline',
479
- value: serialiseStabilityPoolDatum({
480
- StabilityPool: {
481
- content: {
482
- ...stabilityPoolDatum,
483
- snapshot: newPoolSnapshot,
484
- epochToScaleToSum: newEpochToScaleToSum,
485
- },
486
- },
487
- }),
488
- },
489
- {
490
- lovelace:
491
- stabilityPoolUtxo.assets.lovelace +
492
- BigInt(params.stabilityPoolParams.accountAdjustmentFeeLovelaces) -
493
- reward,
494
- [params.stabilityPoolParams.stabilityPoolToken[0].unCurrencySymbol +
495
- fromText(
496
- params.stabilityPoolParams.stabilityPoolToken[1].unTokenName,
497
- )]: 1n,
498
- [params.stabilityPoolParams.assetSymbol.unCurrencySymbol +
499
- fromText(asset)]:
500
- stabilityPoolUtxo.assets[
501
- params.stabilityPoolParams.assetSymbol.unCurrencySymbol +
502
- fromText(asset)
503
- ] +
504
- balanceChange +
505
- withdrawalFee,
506
- },
507
- );
508
-
509
- tx.pay.ToContract(
510
- stabilityPoolUtxo.address,
511
- {
512
- kind: 'inline',
513
- value: serialiseStabilityPoolDatum({
514
- Account: {
515
- content: {
516
- ...accountDatum,
517
- snapshot: newAccountSnapshot,
518
- request: null,
519
- },
520
- },
521
- }),
522
- },
523
- {
524
- lovelace:
525
- accountUtxo.assets.lovelace -
526
- BigInt(params.stabilityPoolParams.accountAdjustmentFeeLovelaces) -
527
- 2_000_000n,
528
- [params.stabilityPoolParams.accountToken[0].unCurrencySymbol +
529
- fromText(params.stabilityPoolParams.accountToken[1].unTokenName)]: 1n,
530
- },
531
- );
532
-
533
- if (myAddress !== outputAddress) {
534
- tx.pay.ToAddressWithData(
535
- outputAddress,
536
- {
537
- kind: 'inline',
538
- value: Data.to(
539
- {
540
- IndigoStabilityPoolAccountAdjustment: {
541
- spent_account: {
542
- txHash: { hash: accountUtxo.txHash },
543
- outputIndex: BigInt(accountUtxo.outputIndex),
544
- },
545
- },
546
- },
547
- ActionReturnDatum,
548
- ),
549
- },
550
- {
551
- lovelace: reward - rewardLovelacesFee + 2_000_000n,
552
- ...(amount < 0n
553
- ? {
554
- [params.stabilityPoolParams.assetSymbol.unCurrencySymbol +
555
- fromText(asset)]: -(amount - withdrawalFee),
556
- }
557
- : {}),
558
- },
559
- );
560
- } else {
561
- // TODO: User is self-handling the process request, so we will need to handle the change datum
562
- }
563
- } else if ('Close' in accountDatum.request) {
564
- const outputAddress = addressToBech32(
565
- lucid,
566
- accountDatum.request.Close.outputAddress,
567
- );
568
- const myAddress = await lucid.wallet().address();
569
-
570
- const [updatedAccountSnapshot, reward, refInputs] = adjustmentHelper(
571
- stabilityPoolUtxo,
572
- newSnapshotUtxo,
573
- stabilityPoolDatum.snapshot,
574
- stabilityPoolDatum.epochToScaleToSum,
575
- accountDatum.snapshot,
576
- );
577
-
578
- const govDatum: GovDatum = parseGovDatum(govUtxo.datum);
579
- const iassetDatum: IAssetContent = parseIAssetDatum(iAssetUtxo.datum);
580
- const rewardLovelacesFee = calculateFeeFromPercentage(
581
- govDatum.protocolParams.collateralFeePercentage,
582
- reward,
583
- );
584
-
585
- const fund = updatedAccountSnapshot.depositVal;
586
- const newPoolDepositExcludingFee = spSub(
587
- stabilityPoolDatum.snapshot.depositVal,
588
- fund,
589
- );
590
- const withdrawnAmt = fund.value < 0n ? mkSPInteger(0n) : fund;
591
- const withdrawalFeeAmount =
592
- newPoolDepositExcludingFee.value === 0n
593
- ? 0n
594
- : calculateFeeFromPercentage(
595
- iassetDatum.stabilityPoolWithdrawalFeePercentage,
596
- withdrawnAmt.value,
597
- );
598
-
599
- const [newPoolDeposit, newPoolProduct] = updatePoolSnapshotWithdrawalFee(
600
- mkSPInteger(withdrawalFeeAmount),
601
- newPoolDepositExcludingFee,
602
- stabilityPoolDatum.snapshot,
603
- );
604
- const newPoolSnapshot: StabilityPoolSnapshot = {
605
- ...stabilityPoolDatum.snapshot,
606
- depositVal: newPoolDeposit,
607
- productVal: newPoolProduct,
608
- };
609
-
610
- const accountTokenRef = await scriptRef(
611
- params.scriptReferences.authTokenPolicies.accountTokenRef,
612
- lucid,
613
- );
614
-
615
- await CollectorContract.feeTx(rewardLovelacesFee, lucid, params, tx);
616
-
617
- tx.readFrom([govUtxo, iAssetUtxo, accountTokenRef, ...refInputs]);
618
-
619
- tx.mintAssets(
620
- {
621
- [params.stabilityPoolParams.accountToken[0].unCurrencySymbol +
622
- fromText(params.stabilityPoolParams.accountToken[1].unTokenName)]:
623
- -1n,
624
- },
625
- Data.to(new Constr(0, [])),
626
- );
627
-
628
- const assetOutputAmountForSP =
629
- stabilityPoolUtxo.assets[
630
- params.stabilityPoolParams.assetSymbol.unCurrencySymbol +
631
- fromText(asset)
632
- ] -
633
- fromSPInteger(withdrawnAmt) -
634
- withdrawalFeeAmount;
635
-
636
- tx.pay.ToContract(
637
- stabilityPoolUtxo.address,
638
- {
639
- kind: 'inline',
640
- value: serialiseStabilityPoolDatum({
641
- StabilityPool: {
642
- content: {
643
- ...stabilityPoolDatum,
644
- snapshot: newPoolSnapshot,
645
- },
646
- },
647
- }),
648
- },
649
- {
650
- lovelace: stabilityPoolUtxo.assets.lovelace - reward,
651
- [params.stabilityPoolParams.stabilityPoolToken[0].unCurrencySymbol +
652
- fromText(
653
- params.stabilityPoolParams.stabilityPoolToken[1].unTokenName,
654
- )]: 1n,
655
- ...(assetOutputAmountForSP > 0n
656
- ? {
657
- [params.stabilityPoolParams.assetSymbol.unCurrencySymbol +
658
- fromText(asset)]: assetOutputAmountForSP,
659
- }
660
- : {}),
661
- },
662
- );
663
-
664
- if (myAddress !== outputAddress) {
665
- tx.pay.ToAddressWithData(
666
- outputAddress,
667
- {
668
- kind: 'inline',
669
- value: Data.to(
670
- {
671
- IndigoStabilityPoolAccountClosure: {
672
- closed_account: {
673
- txHash: { hash: accountUtxo.txHash },
674
- outputIndex: BigInt(accountUtxo.outputIndex),
675
- },
676
- },
677
- },
678
- ActionReturnDatum,
679
- ),
680
- },
681
- {
682
- lovelace: accountUtxo.assets.lovelace + reward - rewardLovelacesFee,
683
- [params.stabilityPoolParams.assetSymbol.unCurrencySymbol +
684
- fromText(asset)]: fromSPInteger(withdrawnAmt) - withdrawalFeeAmount,
685
- },
686
- );
687
- }
688
- }
689
-
690
- return tx;
691
- }
692
- }