@indigo-labs/indigo-sdk 0.1.19 → 0.1.20

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 +1004 -681
  3. package/dist/index.d.ts +1004 -681
  4. package/dist/index.js +4652 -2255
  5. package/dist/index.mjs +4592 -2210
  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 +37 -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
@@ -0,0 +1,1224 @@
1
+ // import { OutRef } from '@lucid-evolution/lucid';
2
+ import {
3
+ addAssets,
4
+ Assets,
5
+ fromHex,
6
+ fromText,
7
+ LucidEvolution,
8
+ OutRef,
9
+ paymentCredentialOf,
10
+ slotToUnixTime,
11
+ toText,
12
+ TxBuilder,
13
+ UTxO,
14
+ } from '@lucid-evolution/lucid';
15
+ import {
16
+ parseGovDatumOrThrow,
17
+ serialiseGovDatum,
18
+ serialiseGovRedeemer,
19
+ TreasuryWithdrawal,
20
+ } from '../gov/types';
21
+ import { matchSingle } from '../../utils/utils';
22
+ import {
23
+ createScriptAddress,
24
+ getInlineDatumOrThrow,
25
+ } from '../../utils/lucid-utils';
26
+ import {
27
+ parsePollManagerOrThrow,
28
+ parsePollShardOrThrow,
29
+ PollShardContent,
30
+ PollStatus,
31
+ serialisePollDatum,
32
+ } from '../poll/types-poll';
33
+ import {
34
+ assetClassValueOf,
35
+ isAssetsZero,
36
+ lovelacesAmt,
37
+ mkAssetsOf,
38
+ mkLovelacesOf,
39
+ negateAssets,
40
+ } from '../../utils/value-helpers';
41
+ import { Data } from '@lucid-evolution/lucid';
42
+ import { pipe } from 'fp-ts/lib/function';
43
+ import { array as A, option as O, function as F } from 'fp-ts';
44
+ import { match, P } from 'ts-pattern';
45
+ import {
46
+ fromSysParamsScriptCredential,
47
+ fromSystemParamsAsset,
48
+ fromSystemParamsScriptRef,
49
+ SystemParams,
50
+ } from '../../types/system-params';
51
+ import { serialisePollManagerRedeemer } from '../poll/types-poll-manager';
52
+ import { ONE_SECOND } from '../../utils/time-helpers';
53
+ import { addressFromBech32, addressToBech32 } from '../../types/generic';
54
+ import { serialiseStakingRedeemer } from '../staking/types';
55
+ import {
56
+ serialisePollShardRedeemer,
57
+ VoteOption,
58
+ } from '../poll/types-poll-shard';
59
+ import {
60
+ parseStakingPositionOrThrow,
61
+ serialiseStakingDatum,
62
+ StakingPosLockedAmt,
63
+ } from '../staking/types-new';
64
+ import { updateStakingLockedAmount } from '../staking/helpers';
65
+ import { pollPassQuorum } from '../poll/helpers';
66
+ import {
67
+ parseExecuteDatumOrThrow,
68
+ serialiseExecuteDatum,
69
+ } from '../execute/types';
70
+ import {
71
+ serialiseTreasuryRedeemer,
72
+ serialiseWithdrawalOutputDatum,
73
+ } from '../treasury/types';
74
+ import { bigintMin } from '../../utils/bigint-utils';
75
+ import { OCD_DECIMAL_UNIT } from '../../types/on-chain-decimal';
76
+ import {
77
+ parseIAssetDatumOrThrow,
78
+ serialiseCdpRedeemer,
79
+ serialiseIAssetDatum,
80
+ } from '../cdp/types';
81
+ import {
82
+ createValueFromWithdrawal,
83
+ findRelativeIAssetForInsertion,
84
+ iassetCreationDatumHelper,
85
+ proposalDeposit,
86
+ } from './helpers';
87
+ import {
88
+ initEpochToScaleToSumMap,
89
+ initSpSnapshot,
90
+ } from '../stability-pool/helpers';
91
+ import { serialiseVersionRecordDatum } from '../version-registry/types-new';
92
+ import { parseUpgradePaths, ProposalContent } from './types-new';
93
+ import { serialiseStabilityPoolDatum } from '../stability-pool/types-new';
94
+
95
+ /**
96
+ * Returns the new PollId.
97
+ */
98
+ export async function createProposal(
99
+ proposalContent: ProposalContent,
100
+ treasuryWithdrawal: TreasuryWithdrawal | null,
101
+ sysParams: SystemParams,
102
+ lucid: LucidEvolution,
103
+ currentSlot: number,
104
+ govOref: OutRef,
105
+ /**
106
+ * This has to be passed only in case of createAsset proposal
107
+ */
108
+ allIAssetOrefs: OutRef[],
109
+ ): Promise<[TxBuilder, bigint]> {
110
+ const network = lucid.config().network!;
111
+ const currentTime = BigInt(slotToUnixTime(network, currentSlot));
112
+
113
+ const ownAddr = await lucid.wallet().address();
114
+ const pkh = paymentCredentialOf(ownAddr);
115
+
116
+ const govRefScriptUtxo = matchSingle(
117
+ await lucid.utxosByOutRef([
118
+ fromSystemParamsScriptRef(
119
+ sysParams.scriptReferences.governanceValidatorRef,
120
+ ),
121
+ ]),
122
+ (_) => new Error('Expected a single Gov Ref Script UTXO'),
123
+ );
124
+ const pollAuthTokenPolicyRefScriptUtxo = matchSingle(
125
+ await lucid.utxosByOutRef([
126
+ fromSystemParamsScriptRef(
127
+ sysParams.scriptReferences.authTokenPolicies.pollManagerTokenRef,
128
+ ),
129
+ ]),
130
+ (_) =>
131
+ new Error('Expected a single poll auth token policy ref Script UTXO'),
132
+ );
133
+ const govUtxo = matchSingle(
134
+ await lucid.utxosByOutRef([govOref]),
135
+ (_) => new Error('Expected a single Gov UTXO'),
136
+ );
137
+
138
+ const govDatum = parseGovDatumOrThrow(getInlineDatumOrThrow(govUtxo));
139
+
140
+ const votingEndTime = currentTime + govDatum.protocolParams.votingPeriod;
141
+ const expirationTime =
142
+ votingEndTime + govDatum.protocolParams.expirationPeriod;
143
+ const proposingEndTime =
144
+ currentTime + govDatum.protocolParams.proposingPeriod;
145
+
146
+ const pollNftValue = mkAssetsOf(
147
+ fromSystemParamsAsset(sysParams.govParams.pollToken),
148
+ 1n,
149
+ );
150
+
151
+ const newPollId = govDatum.currentProposal + 1n;
152
+
153
+ const tx = lucid.newTx();
154
+
155
+ // Add iAsset ref input when Propose asset proposal
156
+ await match(proposalContent)
157
+ .with({ ProposeAsset: { asset: P.select() } }, async (newAsset) => {
158
+ const relativeIAsset = await findRelativeIAssetForInsertion(
159
+ toText(newAsset),
160
+ allIAssetOrefs,
161
+ lucid,
162
+ );
163
+
164
+ pipe(
165
+ relativeIAsset,
166
+ O.match(
167
+ () => {
168
+ if (govDatum.iassetsCount !== 0n) {
169
+ throw new Error(
170
+ 'Has to find relative iAsset when there are iAssets.',
171
+ );
172
+ }
173
+ },
174
+ (relative) => {
175
+ tx.readFrom([relative.utxo]);
176
+ },
177
+ ),
178
+ );
179
+ })
180
+ .otherwise(() => {});
181
+
182
+ return [
183
+ tx
184
+ .mintAssets(pollNftValue, Data.void())
185
+ // Ref scripts
186
+ .readFrom([govRefScriptUtxo, pollAuthTokenPolicyRefScriptUtxo])
187
+ .collectFrom(
188
+ [govUtxo],
189
+ serialiseGovRedeemer({
190
+ CreatePoll: {
191
+ content: proposalContent,
192
+ currentTime: currentTime,
193
+ proposalOwner: pkh.hash,
194
+ treasuryWithdrawal: treasuryWithdrawal,
195
+ },
196
+ }),
197
+ )
198
+ .pay.ToContract(
199
+ govUtxo.address,
200
+ {
201
+ kind: 'inline',
202
+ value: serialiseGovDatum({
203
+ ...govDatum,
204
+ currentProposal: govDatum.currentProposal + 1n,
205
+ activeProposals: govDatum.activeProposals + 1n,
206
+ }),
207
+ },
208
+ govUtxo.assets,
209
+ )
210
+ .pay.ToContract(
211
+ createScriptAddress(network, sysParams.validatorHashes.pollManagerHash),
212
+ {
213
+ kind: 'inline',
214
+ value: serialisePollDatum({
215
+ PollManager: {
216
+ content: {
217
+ pollId: newPollId,
218
+ pollOwner: pkh.hash,
219
+ content: proposalContent,
220
+ treasuryWithdrawal: treasuryWithdrawal,
221
+ status: { yesVotes: 0n, noVotes: 0n },
222
+ votingEndTime: votingEndTime,
223
+ createdShardsCount: 0n,
224
+ talliedShardsCount: 0n,
225
+ totalShardsCount: govDatum.protocolParams.totalShards,
226
+ proposingEndTime: proposingEndTime,
227
+ expirationTime: expirationTime,
228
+ protocolVersion: govDatum.currentVersion,
229
+ minimumQuorum: govDatum.protocolParams.minimumQuorum,
230
+ },
231
+ },
232
+ }),
233
+ },
234
+ addAssets(
235
+ pollNftValue,
236
+ mkAssetsOf(
237
+ fromSystemParamsAsset(sysParams.govParams.indyAsset),
238
+ proposalDeposit(
239
+ govDatum.protocolParams.proposalDeposit,
240
+ govDatum.activeProposals,
241
+ ),
242
+ ),
243
+ ),
244
+ )
245
+ .validFrom(Number(currentTime) - ONE_SECOND)
246
+ .validTo(Number(currentTime + sysParams.govParams.gBiasTime) - ONE_SECOND)
247
+ .addSigner(ownAddr),
248
+ newPollId,
249
+ ];
250
+ }
251
+
252
+ /**
253
+ * Builds transaction creating shards of count chunk size.
254
+ */
255
+ export async function createShardsChunks(
256
+ /**
257
+ * This gets automatically capped to total shards count.
258
+ */
259
+ chunkSize: bigint,
260
+ pollManagerOref: OutRef,
261
+ sysParams: SystemParams,
262
+ currentSlot: number,
263
+ lucid: LucidEvolution,
264
+ ): Promise<TxBuilder> {
265
+ const network = lucid.config().network!;
266
+ const currentTime = BigInt(slotToUnixTime(network, currentSlot));
267
+
268
+ const ownAddr = await lucid.wallet().address();
269
+
270
+ const pollManagerUtxo = matchSingle(
271
+ await lucid.utxosByOutRef([pollManagerOref]),
272
+ (_) => new Error('Expected a single Poll manager UTXO'),
273
+ );
274
+
275
+ const pollManager = parsePollManagerOrThrow(
276
+ getInlineDatumOrThrow(pollManagerUtxo),
277
+ );
278
+
279
+ if (pollManager.createdShardsCount >= pollManager.totalShardsCount) {
280
+ throw new Error('All shards already created.');
281
+ }
282
+
283
+ const pollAuthTokenPolicyRefScriptUtxo = matchSingle(
284
+ await lucid.utxosByOutRef([
285
+ fromSystemParamsScriptRef(
286
+ sysParams.scriptReferences.authTokenPolicies.pollManagerTokenRef,
287
+ ),
288
+ ]),
289
+ (_) =>
290
+ new Error('Expected a single poll auth token policy ref Script UTXO'),
291
+ );
292
+ const pollManagerRefScriptUtxo = matchSingle(
293
+ await lucid.utxosByOutRef([
294
+ fromSystemParamsScriptRef(
295
+ sysParams.scriptReferences.pollManagerValidatorRef,
296
+ ),
297
+ ]),
298
+ (_) =>
299
+ new Error('Expected a single poll auth token policy ref Script UTXO'),
300
+ );
301
+
302
+ const shardsCount = BigInt(
303
+ Math.min(
304
+ Number(chunkSize),
305
+ Number(pollManager.totalShardsCount - pollManager.createdShardsCount),
306
+ ),
307
+ );
308
+
309
+ const pollNft = fromSystemParamsAsset(sysParams.govParams.pollToken);
310
+
311
+ const tx = lucid
312
+ .newTx()
313
+ .validFrom(Number(currentTime) - ONE_SECOND)
314
+ .validTo(
315
+ Number(currentTime + sysParams.pollManagerParams.pBiasTime) - ONE_SECOND,
316
+ )
317
+ .mintAssets(mkAssetsOf(pollNft, shardsCount), Data.void())
318
+ // Ref scripts
319
+ .readFrom([pollAuthTokenPolicyRefScriptUtxo, pollManagerRefScriptUtxo])
320
+ .collectFrom(
321
+ [pollManagerUtxo],
322
+ serialisePollManagerRedeemer({ CreateShards: { currentTime } }),
323
+ )
324
+ .pay.ToContract(
325
+ pollManagerUtxo.address,
326
+ {
327
+ kind: 'inline',
328
+ value: serialisePollDatum({
329
+ PollManager: {
330
+ content: {
331
+ ...pollManager,
332
+ createdShardsCount: pollManager.createdShardsCount + shardsCount,
333
+ },
334
+ },
335
+ }),
336
+ },
337
+ pollManagerUtxo.assets,
338
+ )
339
+ .addSigner(ownAddr);
340
+
341
+ for (let idx = 0; idx < shardsCount; idx++) {
342
+ tx.pay.ToContract(
343
+ createScriptAddress(network, sysParams.validatorHashes.pollShardHash),
344
+ {
345
+ kind: 'inline',
346
+ value: serialisePollDatum({
347
+ PollShard: {
348
+ content: {
349
+ pollId: pollManager.pollId,
350
+ status: { yesVotes: 0n, noVotes: 0n },
351
+ votingEndTime: pollManager.votingEndTime,
352
+ managerAddress: addressFromBech32(pollManagerUtxo.address),
353
+ },
354
+ },
355
+ }),
356
+ },
357
+ mkAssetsOf(pollNft, 1n),
358
+ );
359
+ }
360
+
361
+ return tx;
362
+ }
363
+
364
+ /**
365
+ * Updates both locked amount and poll status based on the vote.
366
+ */
367
+ function voteHelper(
368
+ stakingPosLockedAmt: StakingPosLockedAmt,
369
+ pollShard: PollShardContent,
370
+ voteOption: VoteOption,
371
+ currentTime: bigint,
372
+ indyStakedAmt: bigint,
373
+ ): [StakingPosLockedAmt, PollStatus] {
374
+ const newPollStatus = match(voteOption)
375
+ .returnType<PollStatus>()
376
+ .with('Yes', () => ({
377
+ ...pollShard.status,
378
+ yesVotes: pollShard.status.yesVotes + indyStakedAmt,
379
+ }))
380
+ .with('No', () => ({
381
+ ...pollShard.status,
382
+ noVotes: pollShard.status.noVotes + indyStakedAmt,
383
+ }))
384
+ .exhaustive();
385
+
386
+ const newLockedAmt: [
387
+ bigint,
388
+ { readonly voteAmt: bigint; readonly votingEnd: bigint },
389
+ ][] = [
390
+ ...updateStakingLockedAmount(stakingPosLockedAmt, currentTime).entries(),
391
+ [
392
+ pollShard.pollId,
393
+ { voteAmt: indyStakedAmt, votingEnd: pollShard.votingEndTime },
394
+ ],
395
+ ];
396
+
397
+ return [new Map(newLockedAmt), newPollStatus];
398
+ }
399
+
400
+ export async function vote(
401
+ voteOption: VoteOption,
402
+ pollShardOref: OutRef,
403
+ stakingPositionOref: OutRef,
404
+ sysParams: SystemParams,
405
+ lucid: LucidEvolution,
406
+ currentSlot: number,
407
+ ): Promise<TxBuilder> {
408
+ const network = lucid.config().network!;
409
+ const currentTime = BigInt(slotToUnixTime(network, currentSlot));
410
+
411
+ const ownAddr = await lucid.wallet().address();
412
+
413
+ const pollShardRefScriptUtxo = matchSingle(
414
+ await lucid.utxosByOutRef([
415
+ fromSystemParamsScriptRef(
416
+ sysParams.scriptReferences.pollShardValidatorRef,
417
+ ),
418
+ ]),
419
+ (_) => new Error('Expected a single poll shard ref Script UTXO'),
420
+ );
421
+ const stakingRefScriptUtxo = matchSingle(
422
+ await lucid.utxosByOutRef([
423
+ fromSystemParamsScriptRef(sysParams.scriptReferences.stakingValidatorRef),
424
+ ]),
425
+ (_) => new Error('Expected a single staking ref Script UTXO'),
426
+ );
427
+
428
+ const pollShardUtxo = matchSingle(
429
+ await lucid.utxosByOutRef([pollShardOref]),
430
+ (_) => new Error('Expected a single Poll shard UTXO'),
431
+ );
432
+ const pollShardDatum = parsePollShardOrThrow(
433
+ getInlineDatumOrThrow(pollShardUtxo),
434
+ );
435
+
436
+ const stakingPosUtxo = matchSingle(
437
+ await lucid.utxosByOutRef([stakingPositionOref]),
438
+ (_) => new Error('Expected a single staking position UTXO'),
439
+ );
440
+ const stakingPosDatum = parseStakingPositionOrThrow(
441
+ getInlineDatumOrThrow(stakingPosUtxo),
442
+ );
443
+
444
+ const indyStakedAmt = assetClassValueOf(
445
+ stakingPosUtxo.assets,
446
+ fromSystemParamsAsset(sysParams.govParams.indyAsset),
447
+ );
448
+
449
+ const validityFrom = Number(currentTime) - ONE_SECOND;
450
+
451
+ if (stakingPosDatum.lockedAmount.has(pollShardDatum.pollId)) {
452
+ throw new Error('Already voted for that proposal.');
453
+ }
454
+
455
+ const [newLockedAmt, newVoteStatus] = voteHelper(
456
+ stakingPosDatum.lockedAmount,
457
+ pollShardDatum,
458
+ voteOption,
459
+ BigInt(validityFrom),
460
+ indyStakedAmt,
461
+ );
462
+
463
+ return lucid
464
+ .newTx()
465
+ .validFrom(validityFrom)
466
+ .validTo(Number(pollShardDatum.votingEndTime) - ONE_SECOND)
467
+ .readFrom([stakingRefScriptUtxo, pollShardRefScriptUtxo])
468
+ .collectFrom([stakingPosUtxo], serialiseStakingRedeemer('Lock'))
469
+ .collectFrom(
470
+ [pollShardUtxo],
471
+ serialisePollShardRedeemer({ Vote: { content: voteOption } }),
472
+ )
473
+ .pay.ToContract(
474
+ pollShardUtxo.address,
475
+ {
476
+ kind: 'inline',
477
+ value: serialisePollDatum({
478
+ PollShard: {
479
+ content: {
480
+ ...pollShardDatum,
481
+ status: newVoteStatus,
482
+ },
483
+ },
484
+ }),
485
+ },
486
+ pollShardUtxo.assets,
487
+ )
488
+ .pay.ToContract(
489
+ stakingPosUtxo.address,
490
+ {
491
+ kind: 'inline',
492
+ value: serialiseStakingDatum({
493
+ ...stakingPosDatum,
494
+ lockedAmount: newLockedAmt,
495
+ }),
496
+ },
497
+ stakingPosUtxo.assets,
498
+ )
499
+ .addSigner(ownAddr);
500
+ }
501
+
502
+ export async function mergeShards(
503
+ pollManagerOref: OutRef,
504
+ shardsOutRefs: OutRef[],
505
+ sysParams: SystemParams,
506
+ lucid: LucidEvolution,
507
+ currentSlot: number,
508
+ ): Promise<TxBuilder> {
509
+ const network = lucid.config().network!;
510
+ const currentTime = BigInt(slotToUnixTime(network, currentSlot));
511
+
512
+ const ownAddr = await lucid.wallet().address();
513
+
514
+ const pollShardRefScriptUtxo = matchSingle(
515
+ await lucid.utxosByOutRef([
516
+ fromSystemParamsScriptRef(
517
+ sysParams.scriptReferences.pollShardValidatorRef,
518
+ ),
519
+ ]),
520
+ (_) => new Error('Expected a single poll shard ref Script UTXO'),
521
+ );
522
+ const pollManagerRefScriptUtxo = matchSingle(
523
+ await lucid.utxosByOutRef([
524
+ fromSystemParamsScriptRef(
525
+ sysParams.scriptReferences.pollManagerValidatorRef,
526
+ ),
527
+ ]),
528
+ (_) => new Error('Expected a single poll shard ref Script UTXO'),
529
+ );
530
+
531
+ const pollAuthTokenPolicyRefScriptUtxo = matchSingle(
532
+ await lucid.utxosByOutRef([
533
+ fromSystemParamsScriptRef(
534
+ sysParams.scriptReferences.authTokenPolicies.pollManagerTokenRef,
535
+ ),
536
+ ]),
537
+ (_) =>
538
+ new Error('Expected a single poll auth token policy ref Script UTXO'),
539
+ );
540
+
541
+ const pollManagerUtxo = matchSingle(
542
+ await lucid.utxosByOutRef([pollManagerOref]),
543
+ (_) => new Error('Expected a single Poll manager UTXO'),
544
+ );
545
+
546
+ const pollManagerDatum = parsePollManagerOrThrow(
547
+ getInlineDatumOrThrow(pollManagerUtxo),
548
+ );
549
+
550
+ const shardUtxos = await lucid.utxosByOutRef(shardsOutRefs);
551
+
552
+ const aggregatedStatus: PollStatus = F.pipe(
553
+ shardUtxos,
554
+ A.map((utxo) => parsePollShardOrThrow(getInlineDatumOrThrow(utxo))),
555
+ A.reduce<PollShardContent, PollStatus>(
556
+ pollManagerDatum.status,
557
+ (acc, shard) => ({
558
+ yesVotes: acc.yesVotes + shard.status.yesVotes,
559
+ noVotes: acc.noVotes + shard.status.noVotes,
560
+ }),
561
+ ),
562
+ );
563
+
564
+ const shardsAggregatedAda = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
565
+ addAssets(acc, mkLovelacesOf(lovelacesAmt(utxo.assets))),
566
+ )(shardUtxos);
567
+
568
+ const pollNft = fromSystemParamsAsset(sysParams.govParams.pollToken);
569
+
570
+ return lucid
571
+ .newTx()
572
+ .validFrom(Number(currentTime) - ONE_SECOND)
573
+ .validTo(
574
+ Number(currentTime + sysParams.pollManagerParams.pBiasTime) - ONE_SECOND,
575
+ )
576
+ .readFrom([
577
+ pollShardRefScriptUtxo,
578
+ pollManagerRefScriptUtxo,
579
+ pollAuthTokenPolicyRefScriptUtxo,
580
+ ])
581
+ .mintAssets(mkAssetsOf(pollNft, -BigInt(shardsOutRefs.length)), Data.void())
582
+ .collectFrom(
583
+ [pollManagerUtxo],
584
+ serialisePollManagerRedeemer({
585
+ MergeShardsManager: { currentTime: currentTime },
586
+ }),
587
+ )
588
+ .collectFrom(
589
+ shardUtxos,
590
+ serialisePollShardRedeemer({
591
+ MergeShards: {
592
+ currentTime: currentTime,
593
+ pollManagerRef: {
594
+ outputIndex: BigInt(pollManagerUtxo.outputIndex),
595
+ txHash: { hash: pollManagerUtxo.txHash },
596
+ },
597
+ },
598
+ }),
599
+ )
600
+ .pay.ToContract(
601
+ pollManagerUtxo.address,
602
+ {
603
+ kind: 'inline',
604
+ value: serialisePollDatum({
605
+ PollManager: {
606
+ content: {
607
+ ...pollManagerDatum,
608
+ talliedShardsCount:
609
+ pollManagerDatum.talliedShardsCount +
610
+ BigInt(shardsOutRefs.length),
611
+ status: aggregatedStatus,
612
+ },
613
+ },
614
+ }),
615
+ },
616
+ addAssets(pollManagerUtxo.assets, shardsAggregatedAda),
617
+ )
618
+ .addSigner(ownAddr);
619
+ }
620
+
621
+ export async function endProposal(
622
+ pollManagerOref: OutRef,
623
+ govOref: OutRef,
624
+ sysParams: SystemParams,
625
+ lucid: LucidEvolution,
626
+ currentSlot: number,
627
+ ): Promise<TxBuilder> {
628
+ const network = lucid.config().network!;
629
+ const currentTime = BigInt(slotToUnixTime(network, currentSlot));
630
+
631
+ const ownAddr = await lucid.wallet().address();
632
+
633
+ const pollManagerRefScriptUtxo = matchSingle(
634
+ await lucid.utxosByOutRef([
635
+ fromSystemParamsScriptRef(
636
+ sysParams.scriptReferences.pollManagerValidatorRef,
637
+ ),
638
+ ]),
639
+ (_) => new Error('Expected a single poll shard ref Script UTXO'),
640
+ );
641
+ const govRefScriptUtxo = matchSingle(
642
+ await lucid.utxosByOutRef([
643
+ fromSystemParamsScriptRef(
644
+ sysParams.scriptReferences.governanceValidatorRef,
645
+ ),
646
+ ]),
647
+ (_) => new Error('Expected a single Gov Ref Script UTXO'),
648
+ );
649
+ const pollAuthTokenPolicyRefScriptUtxo = matchSingle(
650
+ await lucid.utxosByOutRef([
651
+ fromSystemParamsScriptRef(
652
+ sysParams.scriptReferences.authTokenPolicies.pollManagerTokenRef,
653
+ ),
654
+ ]),
655
+ (_) =>
656
+ new Error('Expected a single poll auth token policy ref Script UTXO'),
657
+ );
658
+ const upgradeTokenPolicyRefScriptUtxo = matchSingle(
659
+ await lucid.utxosByOutRef([
660
+ fromSystemParamsScriptRef(
661
+ sysParams.scriptReferences.authTokenPolicies.upgradeTokenRef,
662
+ ),
663
+ ]),
664
+ (_) =>
665
+ new Error('Expected a single upgrade auth token policy ref Script UTXO'),
666
+ );
667
+
668
+ const pollManagerUtxo = matchSingle(
669
+ await lucid.utxosByOutRef([pollManagerOref]),
670
+ (_) => new Error('Expected a single Poll manager UTXO'),
671
+ );
672
+ const pollManager = parsePollManagerOrThrow(
673
+ getInlineDatumOrThrow(pollManagerUtxo),
674
+ );
675
+
676
+ const govUtxo = matchSingle(
677
+ await lucid.utxosByOutRef([govOref]),
678
+ (_) => new Error('Expected a single Gov UTXO'),
679
+ );
680
+ const govDatum = parseGovDatumOrThrow(getInlineDatumOrThrow(govUtxo));
681
+
682
+ const pollNft = fromSystemParamsAsset(sysParams.govParams.pollToken);
683
+ const indyAsset = fromSystemParamsAsset(
684
+ sysParams.pollManagerParams.indyAsset,
685
+ );
686
+
687
+ const proposalExpired = currentTime > pollManager.expirationTime;
688
+ const proposalPassed =
689
+ !proposalExpired &&
690
+ pollPassQuorum(
691
+ sysParams.pollManagerParams.initialIndyDistribution,
692
+ pollManager.status,
693
+ currentTime,
694
+ pollManager.minimumQuorum,
695
+ govDatum.treasuryIndyWithdrawnAmt,
696
+ );
697
+
698
+ const upgradeTokenVal = mkAssetsOf(
699
+ fromSystemParamsAsset(sysParams.govParams.upgradeToken),
700
+ 1n,
701
+ );
702
+
703
+ const tx = lucid
704
+ .newTx()
705
+ .validFrom(Number(currentTime) - ONE_SECOND)
706
+ .validTo(
707
+ Number(currentTime + sysParams.pollManagerParams.pBiasTime) - ONE_SECOND,
708
+ )
709
+ .readFrom([
710
+ pollManagerRefScriptUtxo,
711
+ govRefScriptUtxo,
712
+ pollAuthTokenPolicyRefScriptUtxo,
713
+ upgradeTokenPolicyRefScriptUtxo,
714
+ ])
715
+ .collectFrom(
716
+ [pollManagerUtxo],
717
+ serialisePollManagerRedeemer({ EndPoll: { currentTime: currentTime } }),
718
+ )
719
+ .collectFrom(
720
+ [govUtxo],
721
+ serialiseGovRedeemer({ WitnessEndPoll: { currentTime: currentTime } }),
722
+ )
723
+ .mintAssets(mkAssetsOf(pollNft, -1n), Data.void())
724
+ .pay.ToContract(
725
+ govUtxo.address,
726
+ {
727
+ kind: 'inline',
728
+ value: serialiseGovDatum({
729
+ ...govDatum,
730
+ activeProposals: govDatum.activeProposals - 1n,
731
+ }),
732
+ },
733
+ govUtxo.assets,
734
+ )
735
+ .addSigner(ownAddr);
736
+
737
+ if (proposalPassed) {
738
+ tx.pay
739
+ .ToContract(
740
+ createScriptAddress(network, sysParams.validatorHashes.executeHash),
741
+ {
742
+ kind: 'inline',
743
+ value: serialiseExecuteDatum({
744
+ id: pollManager.pollId,
745
+ content: pollManager.content,
746
+ passedTime: currentTime,
747
+ votingEndTime: pollManager.votingEndTime,
748
+ protocolVersion: pollManager.protocolVersion,
749
+ treasuryWithdrawal: pollManager.treasuryWithdrawal,
750
+ }),
751
+ },
752
+ upgradeTokenVal,
753
+ )
754
+ .mintAssets(upgradeTokenVal, Data.void());
755
+ } else {
756
+ tx.pay.ToContract(
757
+ createScriptAddress(network, sysParams.validatorHashes.treasuryHash),
758
+ { kind: 'inline', value: Data.void() },
759
+ mkAssetsOf(
760
+ indyAsset,
761
+ assetClassValueOf(pollManagerUtxo.assets, indyAsset),
762
+ ),
763
+ );
764
+ }
765
+
766
+ return tx;
767
+ }
768
+
769
+ export async function executeProposal(
770
+ executeOref: OutRef,
771
+ govOref: OutRef,
772
+ treasuryWithdrawalOref: OutRef | null,
773
+ allIAssetOrefs: OutRef[] | null,
774
+ modifyIAssetOref: OutRef | null,
775
+ sysParams: SystemParams,
776
+ lucid: LucidEvolution,
777
+ currentSlot: number,
778
+ ): Promise<TxBuilder> {
779
+ const network = lucid.config().network!;
780
+ const currentTime = BigInt(slotToUnixTime(network, currentSlot));
781
+
782
+ const ownAddr = await lucid.wallet().address();
783
+
784
+ const govUtxo = matchSingle(
785
+ await lucid.utxosByOutRef([govOref]),
786
+ (_) => new Error('Expected a single gov UTXO'),
787
+ );
788
+
789
+ const govDatum = parseGovDatumOrThrow(getInlineDatumOrThrow(govUtxo));
790
+
791
+ const govRefScriptUtxo = matchSingle(
792
+ await lucid.utxosByOutRef([
793
+ fromSystemParamsScriptRef(
794
+ sysParams.scriptReferences.governanceValidatorRef,
795
+ ),
796
+ ]),
797
+ (_) => new Error('Expected a single Gov Ref Script UTXO'),
798
+ );
799
+ const executeRefScriptUtxo = matchSingle(
800
+ await lucid.utxosByOutRef([
801
+ fromSystemParamsScriptRef(sysParams.scriptReferences.executeValidatorRef),
802
+ ]),
803
+ (_) => new Error('Expected a single execute Ref Script UTXO'),
804
+ );
805
+
806
+ const upgradeTokenPolicyRefScriptUtxo = matchSingle(
807
+ await lucid.utxosByOutRef([
808
+ fromSystemParamsScriptRef(
809
+ sysParams.scriptReferences.authTokenPolicies.upgradeTokenRef,
810
+ ),
811
+ ]),
812
+ (_) =>
813
+ new Error('Expected a single upgrade auth token policy ref Script UTXO'),
814
+ );
815
+
816
+ const executeUtxo = matchSingle(
817
+ await lucid.utxosByOutRef([executeOref]),
818
+ (_) => new Error('Expected a single execute UTXO'),
819
+ );
820
+
821
+ const executeDatum = parseExecuteDatumOrThrow(
822
+ getInlineDatumOrThrow(executeUtxo),
823
+ );
824
+
825
+ const indyWithdrawalAmt = assetClassValueOf(
826
+ executeDatum.treasuryWithdrawal
827
+ ? createValueFromWithdrawal(executeDatum.treasuryWithdrawal)
828
+ : {},
829
+ fromSystemParamsAsset(sysParams.govParams.indyAsset),
830
+ );
831
+ const newTreasuryWithdrawnIndyAmtCapped = bigintMin(
832
+ 4_822_081n * OCD_DECIMAL_UNIT,
833
+ govDatum.treasuryIndyWithdrawnAmt + indyWithdrawalAmt,
834
+ );
835
+
836
+ const tx = lucid.newTx();
837
+
838
+ // Handle treasury withdrawal
839
+ await pipe(
840
+ O.fromNullable(executeDatum.treasuryWithdrawal),
841
+ O.match(
842
+ () => {
843
+ if (treasuryWithdrawalOref) {
844
+ throw new Error('Cannot provide withdrawal oref when no withdrawal.');
845
+ }
846
+ return Promise.resolve();
847
+ },
848
+ async (withdrawal) => {
849
+ if (!treasuryWithdrawalOref) {
850
+ throw new Error('Have to provide withdrawal oref when withdrawal.');
851
+ }
852
+
853
+ const treasuryRefScriptUtxo = matchSingle(
854
+ await lucid.utxosByOutRef([
855
+ fromSystemParamsScriptRef(
856
+ sysParams.scriptReferences.treasuryValidatorRef,
857
+ ),
858
+ ]),
859
+ (_) => new Error('Expected a single Treasury Ref Script UTXO'),
860
+ );
861
+
862
+ const treasuryWithdrawalUtxo = matchSingle(
863
+ await lucid.utxosByOutRef([treasuryWithdrawalOref]),
864
+ (_) => new Error('Expected a single withdrawal UTXO'),
865
+ );
866
+
867
+ const withdrawalVal = createValueFromWithdrawal(withdrawal);
868
+ const withdrawalChangeVal = addAssets(
869
+ treasuryWithdrawalUtxo.assets,
870
+ negateAssets(withdrawalVal),
871
+ );
872
+
873
+ if (!isAssetsZero(withdrawalChangeVal)) {
874
+ tx.pay.ToContract(
875
+ createScriptAddress(
876
+ network,
877
+ sysParams.validatorHashes.treasuryHash,
878
+ sysParams.treasuryParams.treasuryUtxosStakeCredential
879
+ ? fromSysParamsScriptCredential(
880
+ sysParams.treasuryParams.treasuryUtxosStakeCredential,
881
+ )
882
+ : undefined,
883
+ ),
884
+ { kind: 'inline', value: Data.void() },
885
+ withdrawalChangeVal,
886
+ );
887
+ }
888
+
889
+ tx.readFrom([treasuryRefScriptUtxo])
890
+ .collectFrom(
891
+ [treasuryWithdrawalUtxo],
892
+ serialiseTreasuryRedeemer('Withdraw'),
893
+ )
894
+ .pay.ToAddressWithData(
895
+ addressToBech32(withdrawal.destination, lucid.config().network!),
896
+ {
897
+ kind: 'inline',
898
+ value: serialiseWithdrawalOutputDatum([
899
+ fromText('IndigoTreasuryWithdrawal'),
900
+ {
901
+ txHash: { hash: executeUtxo.txHash },
902
+ outputIndex: BigInt(executeUtxo.outputIndex),
903
+ },
904
+ ]),
905
+ },
906
+ withdrawalVal,
907
+ );
908
+ },
909
+ ),
910
+ );
911
+
912
+ await match(executeDatum.content)
913
+ .with({ ProposeAsset: P.select() }, async (proposeContent) => {
914
+ const iassetTokenPolicyRefScriptUtxo = matchSingle(
915
+ await lucid.utxosByOutRef([
916
+ fromSystemParamsScriptRef(
917
+ sysParams.scriptReferences.authTokenPolicies.iAssetAuthTokenRef,
918
+ ),
919
+ ]),
920
+ (_) =>
921
+ new Error(
922
+ 'Expected a single iasset auth token policy ref Script UTXO',
923
+ ),
924
+ );
925
+ const stabilityPoolTokenPolicyRefScriptUtxo = matchSingle(
926
+ await lucid.utxosByOutRef([
927
+ fromSystemParamsScriptRef(
928
+ sysParams.scriptReferences.authTokenPolicies
929
+ .stabilityPoolAuthTokenRef,
930
+ ),
931
+ ]),
932
+ (_) =>
933
+ new Error('Expected a single SP auth token policy ref Script UTXO'),
934
+ );
935
+
936
+ const cdpRefScriptUtxo = matchSingle(
937
+ await lucid.utxosByOutRef([
938
+ fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
939
+ ]),
940
+ (_) => new Error('Expected a single CDP Ref Script UTXO'),
941
+ );
942
+
943
+ if (!allIAssetOrefs) {
944
+ throw new Error('Have to provide all iasset orefs when propose asset.');
945
+ }
946
+
947
+ const iassetToReference = await findRelativeIAssetForInsertion(
948
+ toText(proposeContent.asset),
949
+ allIAssetOrefs,
950
+ lucid,
951
+ );
952
+
953
+ const { newIAsset, newReferencedIAsset } = iassetCreationDatumHelper(
954
+ proposeContent,
955
+ F.pipe(
956
+ iassetToReference,
957
+ O.map((i) => i.datum),
958
+ ),
959
+ );
960
+
961
+ const iassetAuthVal = mkAssetsOf(
962
+ fromSystemParamsAsset(sysParams.executeParams.iAssetToken),
963
+ 1n,
964
+ );
965
+ const spAuthVal = mkAssetsOf(
966
+ fromSystemParamsAsset(sysParams.executeParams.stabilityPoolToken),
967
+ 1n,
968
+ );
969
+
970
+ tx.readFrom([
971
+ govRefScriptUtxo,
972
+ cdpRefScriptUtxo,
973
+ iassetTokenPolicyRefScriptUtxo,
974
+ stabilityPoolTokenPolicyRefScriptUtxo,
975
+ ])
976
+ .mintAssets(spAuthVal, Data.void())
977
+ .mintAssets(iassetAuthVal, Data.void())
978
+ .collectFrom([govUtxo], serialiseGovRedeemer('UpgradeGov'))
979
+ .pay.ToContract(
980
+ govUtxo.address,
981
+ {
982
+ kind: 'inline',
983
+ value: serialiseGovDatum({
984
+ ...govDatum,
985
+ treasuryIndyWithdrawnAmt: newTreasuryWithdrawnIndyAmtCapped,
986
+ iassetsCount: govDatum.iassetsCount + 1n,
987
+ }),
988
+ },
989
+ govUtxo.assets,
990
+ )
991
+ .pay.ToContract(
992
+ createScriptAddress(network, sysParams.validatorHashes.cdpHash),
993
+ { kind: 'inline', value: serialiseIAssetDatum(newIAsset) },
994
+ iassetAuthVal,
995
+ )
996
+ .pay.ToContract(
997
+ createScriptAddress(
998
+ network,
999
+ sysParams.validatorHashes.stabilityPoolHash,
1000
+ ),
1001
+ {
1002
+ kind: 'inline',
1003
+ value: serialiseStabilityPoolDatum(
1004
+ {
1005
+ StabilityPool: {
1006
+ asset: fromHex(proposeContent.asset),
1007
+ poolSnapshot: initSpSnapshot,
1008
+ epochToScaleToSum: initEpochToScaleToSumMap(),
1009
+ },
1010
+ },
1011
+ true,
1012
+ ),
1013
+ },
1014
+ spAuthVal,
1015
+ );
1016
+
1017
+ F.pipe(
1018
+ iassetToReference,
1019
+ O.match(
1020
+ () => {
1021
+ // no action
1022
+ },
1023
+ (i) =>
1024
+ F.pipe(
1025
+ newReferencedIAsset,
1026
+ O.match(
1027
+ () => {
1028
+ throw new Error('Expected some referenced iasset.');
1029
+ },
1030
+ (newRefIAsset) => {
1031
+ tx.collectFrom(
1032
+ [i.utxo],
1033
+ serialiseCdpRedeemer('UpdateOrInsertAsset'),
1034
+ ).pay.ToContract(
1035
+ i.utxo.address,
1036
+ {
1037
+ kind: 'inline',
1038
+ value: serialiseIAssetDatum(newRefIAsset),
1039
+ },
1040
+ i.utxo.assets,
1041
+ );
1042
+ },
1043
+ ),
1044
+ ),
1045
+ ),
1046
+ );
1047
+ })
1048
+ .with({ ModifyAsset: P.select() }, async (modifyContent) => {
1049
+ const cdpRefScriptUtxo = matchSingle(
1050
+ await lucid.utxosByOutRef([
1051
+ fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
1052
+ ]),
1053
+ (_) => new Error('Expected a single CDP Ref Script UTXO'),
1054
+ );
1055
+
1056
+ if (!modifyIAssetOref) {
1057
+ throw new Error('Have to provide iasset oref when modify asset.');
1058
+ }
1059
+
1060
+ const iassetUtxo = matchSingle(
1061
+ await lucid.utxosByOutRef([modifyIAssetOref]),
1062
+ (_) => new Error('Expected a single iasset UTXO'),
1063
+ );
1064
+
1065
+ const iassetDatum = parseIAssetDatumOrThrow(
1066
+ getInlineDatumOrThrow(iassetUtxo),
1067
+ );
1068
+
1069
+ tx.readFrom([cdpRefScriptUtxo])
1070
+ .collectFrom([iassetUtxo], serialiseCdpRedeemer('UpdateOrInsertAsset'))
1071
+ .pay.ToContract(
1072
+ createScriptAddress(network, sysParams.validatorHashes.cdpHash),
1073
+ {
1074
+ kind: 'inline',
1075
+ value: serialiseIAssetDatum({
1076
+ assetName: iassetDatum.assetName,
1077
+ price: modifyContent.newAssetPriceInfo,
1078
+ interestOracleNft: modifyContent.newInterestOracleNft,
1079
+ redemptionRatio: modifyContent.newRedemptionRatioPercentage,
1080
+ maintenanceRatio: modifyContent.newMaintenanceRatioPercentage,
1081
+ liquidationRatio: modifyContent.newLiquidationRatioPercentage,
1082
+ debtMintingFeePercentage:
1083
+ modifyContent.newDebtMintingFeePercentage,
1084
+ liquidationProcessingFeePercentage:
1085
+ modifyContent.newLiquidationProcessingFeePercentage,
1086
+ stabilityPoolWithdrawalFeePercentage:
1087
+ modifyContent.newStabilityPoolWithdrawalFeePercentage,
1088
+ redemptionReimbursementPercentage:
1089
+ modifyContent.newRedemptionReimbursementPercentage,
1090
+ redemptionProcessingFeePercentage:
1091
+ modifyContent.newRedemptionProcessingFeePercentage,
1092
+ interestCollectorPortionPercentage:
1093
+ modifyContent.newInterestCollectorPortionPercentage,
1094
+ firstIAsset: iassetDatum.firstIAsset,
1095
+ nextIAsset: iassetDatum.nextIAsset,
1096
+ }),
1097
+ },
1098
+ iassetUtxo.assets,
1099
+ );
1100
+
1101
+ if (indyWithdrawalAmt > 0) {
1102
+ tx.readFrom([govRefScriptUtxo])
1103
+ .collectFrom([govUtxo], serialiseGovRedeemer('UpgradeGov'))
1104
+ .pay.ToContract(
1105
+ govUtxo.address,
1106
+ {
1107
+ kind: 'inline',
1108
+ value: serialiseGovDatum({
1109
+ ...govDatum,
1110
+ treasuryIndyWithdrawnAmt: newTreasuryWithdrawnIndyAmtCapped,
1111
+ }),
1112
+ },
1113
+ govUtxo.assets,
1114
+ );
1115
+ } else {
1116
+ tx.readFrom([govUtxo]);
1117
+ }
1118
+ })
1119
+ .with({ TextProposal: P.any }, () => {
1120
+ if (indyWithdrawalAmt > 0) {
1121
+ tx.readFrom([govRefScriptUtxo])
1122
+ .collectFrom([govUtxo], serialiseGovRedeemer('UpgradeGov'))
1123
+ .pay.ToContract(
1124
+ govUtxo.address,
1125
+ {
1126
+ kind: 'inline',
1127
+ value: serialiseGovDatum({
1128
+ ...govDatum,
1129
+ treasuryIndyWithdrawnAmt: newTreasuryWithdrawnIndyAmtCapped,
1130
+ }),
1131
+ },
1132
+ govUtxo.assets,
1133
+ );
1134
+ } else {
1135
+ tx.readFrom([govUtxo]);
1136
+ }
1137
+ })
1138
+ .with(
1139
+ { ModifyProtocolParams: { newParams: P.select() } },
1140
+ (newProtocolParams) => {
1141
+ tx.readFrom([govRefScriptUtxo])
1142
+ .collectFrom([govUtxo], serialiseGovRedeemer('UpgradeGov'))
1143
+ .pay.ToContract(
1144
+ govUtxo.address,
1145
+ {
1146
+ kind: 'inline',
1147
+ value: serialiseGovDatum({
1148
+ ...govDatum,
1149
+ protocolParams: newProtocolParams,
1150
+ treasuryIndyWithdrawnAmt: newTreasuryWithdrawnIndyAmtCapped,
1151
+ }),
1152
+ },
1153
+ govUtxo.assets,
1154
+ );
1155
+ },
1156
+ )
1157
+ .with({ UpgradeProtocol: P.select() }, async (d) => {
1158
+ const upgradeDetails = parseUpgradePaths(d.content);
1159
+
1160
+ const versionRecordTokenPolicyRefScriptUtxo = matchSingle(
1161
+ await lucid.utxosByOutRef([
1162
+ fromSystemParamsScriptRef(
1163
+ sysParams.scriptReferences.versionRecordTokenPolicyRef,
1164
+ ),
1165
+ ]),
1166
+ (_) =>
1167
+ new Error(
1168
+ 'Expected a single version record token policy ref Script UTXO',
1169
+ ),
1170
+ );
1171
+
1172
+ const versionRecordNftVal = mkAssetsOf(
1173
+ fromSystemParamsAsset(sysParams.executeParams.versionRecordToken),
1174
+ 1n,
1175
+ );
1176
+
1177
+ tx.readFrom([govRefScriptUtxo, versionRecordTokenPolicyRefScriptUtxo])
1178
+ .mintAssets(versionRecordNftVal, Data.void())
1179
+ .pay.ToContract(
1180
+ createScriptAddress(
1181
+ network,
1182
+ sysParams.validatorHashes.versionRegistryHash,
1183
+ ),
1184
+ {
1185
+ kind: 'inline',
1186
+ value: serialiseVersionRecordDatum({
1187
+ upgradeId: upgradeDetails.upgradeId,
1188
+ upgradePaths: new Map(
1189
+ upgradeDetails.upgradePaths
1190
+ .entries()
1191
+ .map(([h1, h2]) => [h1, h2.upgradeSymbol]),
1192
+ ),
1193
+ }),
1194
+ },
1195
+ versionRecordNftVal,
1196
+ )
1197
+ .collectFrom([govUtxo], serialiseGovRedeemer('UpgradeGov'))
1198
+ .pay.ToContract(
1199
+ govUtxo.address,
1200
+ {
1201
+ kind: 'inline',
1202
+ value: serialiseGovDatum({
1203
+ ...govDatum,
1204
+ currentVersion: govDatum.currentVersion + 1n,
1205
+ treasuryIndyWithdrawnAmt: newTreasuryWithdrawnIndyAmtCapped,
1206
+ }),
1207
+ },
1208
+ govUtxo.assets,
1209
+ );
1210
+ })
1211
+ .exhaustive();
1212
+
1213
+ tx.readFrom([upgradeTokenPolicyRefScriptUtxo, executeRefScriptUtxo])
1214
+ .validFrom(Number(currentTime) - ONE_SECOND)
1215
+ .validTo(Number(currentTime + sysParams.govParams.gBiasTime) - ONE_SECOND)
1216
+ .collectFrom([executeUtxo], Data.void())
1217
+ .mintAssets(
1218
+ mkAssetsOf(fromSystemParamsAsset(sysParams.govParams.upgradeToken), -1n),
1219
+ Data.void(),
1220
+ )
1221
+ .addSigner(ownAddr);
1222
+
1223
+ return tx;
1224
+ }