@indigo-labs/indigo-sdk 0.2.41 → 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 -495
  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
@@ -1,3 +1,4 @@
1
+ // import { OutRef } from '@lucid-evolution/lucid';
1
2
  import {
2
3
  addAssets,
3
4
  Assets,
@@ -7,91 +8,96 @@ import {
7
8
  OutRef,
8
9
  paymentCredentialOf,
9
10
  slotToUnixTime,
10
- toText,
11
+ toHex,
11
12
  TxBuilder,
12
13
  UTxO,
13
14
  } from '@lucid-evolution/lucid';
14
- import {
15
- parseGovDatumOrThrow,
16
- serialiseGovDatum,
17
- serialiseGovRedeemer,
18
- TreasuryWithdrawal,
19
- } from '../gov/types';
15
+
20
16
  import { matchSingle } from '../../utils/utils';
21
17
  import {
22
18
  createScriptAddress,
23
19
  getInlineDatumOrThrow,
24
- resolveUtxo,
25
- UtxoOrOutRef,
26
20
  } from '../../utils/lucid-utils';
27
- import {
28
- parsePollManagerOrThrow,
29
- parsePollShardOrThrow,
30
- PollShardContent,
31
- PollStatus,
32
- serialisePollDatum,
33
- } from '../poll/types-poll';
21
+
34
22
  import {
35
23
  assetClassValueOf,
36
24
  isAssetsZero,
25
+ isSameAssetClass,
37
26
  lovelacesAmt,
38
27
  mkAssetsOf,
39
28
  mkLovelacesOf,
40
29
  negateAssets,
41
- } from '../../utils/value-helpers';
30
+ } from '@3rd-eye-labs/cardano-offchain-common';
42
31
  import { Data } from '@lucid-evolution/lucid';
43
32
  import { pipe } from 'fp-ts/lib/function';
44
33
  import { array as A, option as O, function as F } from 'fp-ts';
45
34
  import { match, P } from 'ts-pattern';
46
35
  import {
47
- fromSysParamsScriptCredential,
36
+ fromSysParamsCredential,
48
37
  fromSystemParamsAsset,
49
38
  fromSystemParamsScriptRef,
50
39
  SystemParams,
51
40
  } from '../../types/system-params';
52
- import { serialisePollManagerRedeemer } from '../poll/types-poll-manager';
53
41
  import { ONE_SECOND } from '../../utils/time-helpers';
54
- import { addressFromBech32, addressToBech32 } from '../../types/generic';
55
- import { serialiseStakingRedeemer } from '../staking/types';
56
- import {
57
- serialisePollShardRedeemer,
58
- VoteOption,
59
- } from '../poll/types-poll-shard';
60
42
  import {
61
43
  parseStakingPositionOrThrow,
62
44
  serialiseStakingDatum,
45
+ serialiseStakingRedeemer,
63
46
  StakingPosLockedAmt,
64
47
  } from '../staking/types-new';
65
48
  import { updateStakingLockedAmount } from '../staking/helpers';
66
49
  import { pollPassQuorum } from '../poll/helpers';
50
+ import {
51
+ collateralAssetCreationDatumHelper,
52
+ createValueFromWithdrawal,
53
+ findRelativeCollateralAssetForInsertion,
54
+ findRelativeIAssetForInsertion,
55
+ iassetCreationDatumHelper,
56
+ proposalDeposit,
57
+ } from './helpers';
58
+ import { initSpState } from '../stability-pool/helpers';
59
+ import { serialiseVersionRecordDatum } from '../version-registry/types-new';
60
+ import {
61
+ parseGovDatumOrThrow,
62
+ ProposalContent,
63
+ serialiseGovDatum,
64
+ serialiseGovRedeemer,
65
+ TreasuryWithdrawal,
66
+ } from './types-new';
67
+ import { serialiseStabilityPoolDatum } from '../stability-pool/types-new';
68
+ import {
69
+ parsePollManagerOrThrow,
70
+ parsePollShardOrThrow,
71
+ PollShardContent,
72
+ PollStatus,
73
+ serialisePollDatum,
74
+ serialisePollManagerRedeemer,
75
+ serialisePollShardRedeemer,
76
+ VoteOption,
77
+ } from '../poll/types-poll-new';
67
78
  import {
68
79
  parseExecuteDatumOrThrow,
69
80
  serialiseExecuteDatum,
70
- } from '../execute/types';
81
+ } from '../execute/types-new';
82
+ import {
83
+ addressFromBech32,
84
+ addressToBech32,
85
+ } from '@3rd-eye-labs/cardano-offchain-common';
71
86
  import {
72
87
  serialiseTreasuryRedeemer,
73
88
  serialiseWithdrawalOutputDatum,
74
- } from '../treasury/types';
75
- import { bigintMin } from '../../utils/bigint-utils';
76
- import { OCD_DECIMAL_UNIT } from '../../types/on-chain-decimal';
89
+ } from '../treasury/types-new';
77
90
  import {
91
+ parseCollateralAssetDatumOrThrow,
78
92
  parseIAssetDatumOrThrow,
79
- serialiseCdpRedeemer,
80
93
  serialiseIAssetDatum,
81
- } from '../cdp/types';
82
- import {
83
- createValueFromWithdrawal,
84
- findRelativeIAssetForInsertion,
85
- iassetCreationDatumHelper,
86
- proposalDeposit,
87
- } from './helpers';
94
+ serialiseIAssetRedeemer,
95
+ } from '../iasset/types';
96
+ import { serialiseCDPCreatorRedeemer } from '../cdp-creator/types-new';
88
97
  import {
89
- initEpochToScaleToSumMap,
90
- initSpSnapshot,
91
- } from '../stability-pool/helpers';
92
- import { serialiseVersionRecordDatum } from '../version-registry/types-new';
93
- import { parseUpgradePaths, ProposalContent } from './types-new';
94
- import { serialiseStabilityPoolDatum } from '../stability-pool/types-new';
98
+ serialiseCdpRedeemer,
99
+ serialiseStableswapPoolDatum,
100
+ } from '../cdp/types-new';
95
101
 
96
102
  /**
97
103
  * Returns the new PollId.
@@ -102,11 +108,11 @@ export async function createProposal(
102
108
  sysParams: SystemParams,
103
109
  lucid: LucidEvolution,
104
110
  currentSlot: number,
105
- gov: UtxoOrOutRef,
111
+ govOref: OutRef,
106
112
  /**
107
113
  * This has to be passed only in case of createAsset proposal
108
114
  */
109
- allIAssetOrefs: OutRef[],
115
+ allIAssetOrefs: OutRef[] | undefined,
110
116
  ): Promise<[TxBuilder, bigint]> {
111
117
  const network = lucid.config().network!;
112
118
  const currentTime = BigInt(slotToUnixTime(network, currentSlot));
@@ -131,7 +137,10 @@ export async function createProposal(
131
137
  (_) =>
132
138
  new Error('Expected a single poll auth token policy ref Script UTXO'),
133
139
  );
134
- const govUtxo = await resolveUtxo(gov, lucid, 'Expected a single Gov UTXO');
140
+ const govUtxo = matchSingle(
141
+ await lucid.utxosByOutRef([govOref]),
142
+ (_) => new Error('Expected a single Gov UTXO'),
143
+ );
135
144
 
136
145
  const govDatum = parseGovDatumOrThrow(getInlineDatumOrThrow(govUtxo));
137
146
 
@@ -152,9 +161,13 @@ export async function createProposal(
152
161
 
153
162
  // Add iAsset ref input when Propose asset proposal
154
163
  await match(proposalContent)
155
- .with({ ProposeAsset: { asset: P.select() } }, async (newAsset) => {
164
+ .with({ ProposeIAsset: { asset: P.select() } }, async (newAsset) => {
165
+ if (allIAssetOrefs === undefined) {
166
+ throw new Error('Missing iAsset orefs');
167
+ }
168
+
156
169
  const relativeIAsset = await findRelativeIAssetForInsertion(
157
- toText(newAsset),
170
+ newAsset,
158
171
  allIAssetOrefs,
159
172
  lucid,
160
173
  );
@@ -188,7 +201,7 @@ export async function createProposal(
188
201
  CreatePoll: {
189
202
  content: proposalContent,
190
203
  currentTime: currentTime,
191
- proposalOwner: pkh.hash,
204
+ proposalOwner: fromHex(pkh.hash),
192
205
  treasuryWithdrawal: treasuryWithdrawal,
193
206
  },
194
207
  }),
@@ -211,21 +224,19 @@ export async function createProposal(
211
224
  kind: 'inline',
212
225
  value: serialisePollDatum({
213
226
  PollManager: {
214
- content: {
215
- pollId: newPollId,
216
- pollOwner: pkh.hash,
217
- content: proposalContent,
218
- treasuryWithdrawal: treasuryWithdrawal,
219
- status: { yesVotes: 0n, noVotes: 0n },
220
- votingEndTime: votingEndTime,
221
- createdShardsCount: 0n,
222
- talliedShardsCount: 0n,
223
- totalShardsCount: govDatum.protocolParams.totalShards,
224
- proposingEndTime: proposingEndTime,
225
- expirationTime: expirationTime,
226
- protocolVersion: govDatum.currentVersion,
227
- minimumQuorum: govDatum.protocolParams.minimumQuorum,
228
- },
227
+ pollId: newPollId,
228
+ pollOwner: fromHex(pkh.hash),
229
+ content: proposalContent,
230
+ treasuryWithdrawal: treasuryWithdrawal,
231
+ status: { yesVotes: 0n, noVotes: 0n },
232
+ votingEndTime: votingEndTime,
233
+ createdShardsCount: 0n,
234
+ talliedShardsCount: 0n,
235
+ totalShardsCount: govDatum.protocolParams.totalShards,
236
+ proposingEndTime: proposingEndTime,
237
+ expirationTime: expirationTime,
238
+ protocolVersion: govDatum.currentVersion,
239
+ minimumQuorum: govDatum.protocolParams.minimumQuorum,
229
240
  },
230
241
  }),
231
242
  },
@@ -255,7 +266,7 @@ export async function createShardsChunks(
255
266
  * This gets automatically capped to total shards count.
256
267
  */
257
268
  chunkSize: bigint,
258
- pollManager: UtxoOrOutRef,
269
+ pollManagerOref: OutRef,
259
270
  sysParams: SystemParams,
260
271
  currentSlot: number,
261
272
  lucid: LucidEvolution,
@@ -265,19 +276,16 @@ export async function createShardsChunks(
265
276
 
266
277
  const ownAddr = await lucid.wallet().address();
267
278
 
268
- const pollManagerUtxo = await resolveUtxo(
269
- pollManager,
270
- lucid,
271
- 'Expected a single Poll manager UTXO',
279
+ const pollManagerUtxo = matchSingle(
280
+ await lucid.utxosByOutRef([pollManagerOref]),
281
+ (_) => new Error('Expected a single Poll manager UTXO'),
272
282
  );
273
283
 
274
- const pollManagerDatum = parsePollManagerOrThrow(
284
+ const pollManager = parsePollManagerOrThrow(
275
285
  getInlineDatumOrThrow(pollManagerUtxo),
276
286
  );
277
287
 
278
- if (
279
- pollManagerDatum.createdShardsCount >= pollManagerDatum.totalShardsCount
280
- ) {
288
+ if (pollManager.createdShardsCount >= pollManager.totalShardsCount) {
281
289
  throw new Error('All shards already created.');
282
290
  }
283
291
 
@@ -303,9 +311,7 @@ export async function createShardsChunks(
303
311
  const shardsCount = BigInt(
304
312
  Math.min(
305
313
  Number(chunkSize),
306
- Number(
307
- pollManagerDatum.totalShardsCount - pollManagerDatum.createdShardsCount,
308
- ),
314
+ Number(pollManager.totalShardsCount - pollManager.createdShardsCount),
309
315
  ),
310
316
  );
311
317
 
@@ -330,11 +336,8 @@ export async function createShardsChunks(
330
336
  kind: 'inline',
331
337
  value: serialisePollDatum({
332
338
  PollManager: {
333
- content: {
334
- ...pollManagerDatum,
335
- createdShardsCount:
336
- pollManagerDatum.createdShardsCount + shardsCount,
337
- },
339
+ ...pollManager,
340
+ createdShardsCount: pollManager.createdShardsCount + shardsCount,
338
341
  },
339
342
  }),
340
343
  },
@@ -349,12 +352,10 @@ export async function createShardsChunks(
349
352
  kind: 'inline',
350
353
  value: serialisePollDatum({
351
354
  PollShard: {
352
- content: {
353
- pollId: pollManagerDatum.pollId,
354
- status: { yesVotes: 0n, noVotes: 0n },
355
- votingEndTime: pollManagerDatum.votingEndTime,
356
- managerAddress: addressFromBech32(pollManagerUtxo.address),
357
- },
355
+ pollId: pollManager.pollId,
356
+ status: { yesVotes: 0n, noVotes: 0n },
357
+ votingEndTime: pollManager.votingEndTime,
358
+ managerAddress: addressFromBech32(pollManagerUtxo.address),
358
359
  },
359
360
  }),
360
361
  },
@@ -387,24 +388,21 @@ function voteHelper(
387
388
  }))
388
389
  .exhaustive();
389
390
 
390
- const newLockedAmt: [
391
- bigint,
392
- { readonly voteAmt: bigint; readonly votingEnd: bigint },
393
- ][] = [
394
- ...updateStakingLockedAmount(stakingPosLockedAmt, currentTime).entries(),
391
+ const newLockedAmt: StakingPosLockedAmt = [
392
+ ...updateStakingLockedAmount(stakingPosLockedAmt, currentTime),
395
393
  [
396
- pollShard.pollId,
394
+ BigInt(pollShard.pollId),
397
395
  { voteAmt: indyStakedAmt, votingEnd: pollShard.votingEndTime },
398
396
  ],
399
397
  ];
400
398
 
401
- return [new Map(newLockedAmt), newPollStatus];
399
+ return [newLockedAmt, newPollStatus];
402
400
  }
403
401
 
404
402
  export async function vote(
405
403
  voteOption: VoteOption,
406
- pollShard: UtxoOrOutRef,
407
- stakingPosition: UtxoOrOutRef,
404
+ pollShardOref: OutRef,
405
+ stakingPositionOref: OutRef,
408
406
  sysParams: SystemParams,
409
407
  lucid: LucidEvolution,
410
408
  currentSlot: number,
@@ -429,19 +427,17 @@ export async function vote(
429
427
  (_) => new Error('Expected a single staking ref Script UTXO'),
430
428
  );
431
429
 
432
- const pollShardUtxo = await resolveUtxo(
433
- pollShard,
434
- lucid,
435
- 'Expected a single Poll shard UTXO',
430
+ const pollShardUtxo = matchSingle(
431
+ await lucid.utxosByOutRef([pollShardOref]),
432
+ (_) => new Error('Expected a single Poll shard UTXO'),
436
433
  );
437
434
  const pollShardDatum = parsePollShardOrThrow(
438
435
  getInlineDatumOrThrow(pollShardUtxo),
439
436
  );
440
437
 
441
- const stakingPosUtxo = await resolveUtxo(
442
- stakingPosition,
443
- lucid,
444
- 'Expected a single staking position UTXO',
438
+ const stakingPosUtxo = matchSingle(
439
+ await lucid.utxosByOutRef([stakingPositionOref]),
440
+ (_) => new Error('Expected a single staking position UTXO'),
445
441
  );
446
442
  const stakingPosDatum = parseStakingPositionOrThrow(
447
443
  getInlineDatumOrThrow(stakingPosUtxo),
@@ -452,11 +448,12 @@ export async function vote(
452
448
  fromSystemParamsAsset(sysParams.govParams.indyAsset),
453
449
  );
454
450
 
455
- const validityFrom =
456
- Number(currentTime) -
457
- Math.min(120 * ONE_SECOND, Number(sysParams.govParams.gBiasTime));
451
+ const validityFrom = Number(currentTime) - ONE_SECOND;
458
452
 
459
- if (stakingPosDatum.lockedAmount.has(pollShardDatum.pollId)) {
453
+ if (
454
+ stakingPosDatum.lockedAmount.find((x) => x[0] === pollShardDatum.pollId) !==
455
+ undefined
456
+ ) {
460
457
  throw new Error('Already voted for that proposal.');
461
458
  }
462
459
 
@@ -471,19 +468,12 @@ export async function vote(
471
468
  return lucid
472
469
  .newTx()
473
470
  .validFrom(validityFrom)
474
- .validTo(
475
- Math.min(
476
- Number(pollShardDatum.votingEndTime) - ONE_SECOND,
477
- Number(currentTime) +
478
- Number(sysParams.govParams.gBiasTime) -
479
- ONE_SECOND,
480
- ),
481
- )
471
+ .validTo(Number(pollShardDatum.votingEndTime) - ONE_SECOND)
482
472
  .readFrom([stakingRefScriptUtxo, pollShardRefScriptUtxo])
483
473
  .collectFrom([stakingPosUtxo], serialiseStakingRedeemer('Lock'))
484
474
  .collectFrom(
485
475
  [pollShardUtxo],
486
- serialisePollShardRedeemer({ Vote: { content: voteOption } }),
476
+ serialisePollShardRedeemer({ Vote: voteOption }),
487
477
  )
488
478
  .pay.ToContract(
489
479
  pollShardUtxo.address,
@@ -491,10 +481,8 @@ export async function vote(
491
481
  kind: 'inline',
492
482
  value: serialisePollDatum({
493
483
  PollShard: {
494
- content: {
495
- ...pollShardDatum,
496
- status: newVoteStatus,
497
- },
484
+ ...pollShardDatum,
485
+ status: newVoteStatus,
498
486
  },
499
487
  }),
500
488
  },
@@ -515,7 +503,7 @@ export async function vote(
515
503
  }
516
504
 
517
505
  export async function mergeShards(
518
- pollManager: UtxoOrOutRef,
506
+ pollManagerOref: OutRef,
519
507
  shardsOutRefs: OutRef[],
520
508
  sysParams: SystemParams,
521
509
  lucid: LucidEvolution,
@@ -553,10 +541,9 @@ export async function mergeShards(
553
541
  new Error('Expected a single poll auth token policy ref Script UTXO'),
554
542
  );
555
543
 
556
- const pollManagerUtxo = await resolveUtxo(
557
- pollManager,
558
- lucid,
559
- 'Expected a single Poll manager UTXO',
544
+ const pollManagerUtxo = matchSingle(
545
+ await lucid.utxosByOutRef([pollManagerOref]),
546
+ (_) => new Error('Expected a single Poll manager UTXO'),
560
547
  );
561
548
 
562
549
  const pollManagerDatum = parsePollManagerOrThrow(
@@ -608,7 +595,7 @@ export async function mergeShards(
608
595
  currentTime: currentTime,
609
596
  pollManagerRef: {
610
597
  outputIndex: BigInt(pollManagerUtxo.outputIndex),
611
- txHash: { hash: pollManagerUtxo.txHash },
598
+ txHash: fromHex(pollManagerUtxo.txHash),
612
599
  },
613
600
  },
614
601
  }),
@@ -619,13 +606,11 @@ export async function mergeShards(
619
606
  kind: 'inline',
620
607
  value: serialisePollDatum({
621
608
  PollManager: {
622
- content: {
623
- ...pollManagerDatum,
624
- talliedShardsCount:
625
- pollManagerDatum.talliedShardsCount +
626
- BigInt(shardsOutRefs.length),
627
- status: aggregatedStatus,
628
- },
609
+ ...pollManagerDatum,
610
+ talliedShardsCount:
611
+ pollManagerDatum.talliedShardsCount +
612
+ BigInt(shardsOutRefs.length),
613
+ status: aggregatedStatus,
629
614
  },
630
615
  }),
631
616
  },
@@ -635,8 +620,8 @@ export async function mergeShards(
635
620
  }
636
621
 
637
622
  export async function endProposal(
638
- pollManager: UtxoOrOutRef,
639
- gov: UtxoOrOutRef,
623
+ pollManagerOref: OutRef,
624
+ govOref: OutRef,
640
625
  sysParams: SystemParams,
641
626
  lucid: LucidEvolution,
642
627
  currentSlot: number,
@@ -681,16 +666,18 @@ export async function endProposal(
681
666
  new Error('Expected a single upgrade auth token policy ref Script UTXO'),
682
667
  );
683
668
 
684
- const pollManagerUtxo = await resolveUtxo(
685
- pollManager,
686
- lucid,
687
- 'Expected a single Poll manager UTXO',
669
+ const pollManagerUtxo = matchSingle(
670
+ await lucid.utxosByOutRef([pollManagerOref]),
671
+ (_) => new Error('Expected a single Poll manager UTXO'),
688
672
  );
689
- const pollManagerDatum = parsePollManagerOrThrow(
673
+ const pollManager = parsePollManagerOrThrow(
690
674
  getInlineDatumOrThrow(pollManagerUtxo),
691
675
  );
692
676
 
693
- const govUtxo = await resolveUtxo(gov, lucid, 'Expected a single Gov UTXO');
677
+ const govUtxo = matchSingle(
678
+ await lucid.utxosByOutRef([govOref]),
679
+ (_) => new Error('Expected a single Gov UTXO'),
680
+ );
694
681
  const govDatum = parseGovDatumOrThrow(getInlineDatumOrThrow(govUtxo));
695
682
 
696
683
  const pollNft = fromSystemParamsAsset(sysParams.govParams.pollToken);
@@ -698,15 +685,13 @@ export async function endProposal(
698
685
  sysParams.pollManagerParams.indyAsset,
699
686
  );
700
687
 
701
- const proposalExpired = currentTime > pollManagerDatum.expirationTime;
688
+ const proposalExpired = currentTime > pollManager.expirationTime;
702
689
  const proposalPassed =
703
690
  !proposalExpired &&
704
691
  pollPassQuorum(
705
- sysParams.pollManagerParams.initialIndyDistribution,
706
- pollManagerDatum.status,
707
- currentTime,
708
- pollManagerDatum.minimumQuorum,
709
- govDatum.treasuryIndyWithdrawnAmt,
692
+ pollManager.status,
693
+ govDatum.protocolParams.electorate,
694
+ pollManager.minimumQuorum,
710
695
  );
711
696
 
712
697
  const upgradeTokenVal = mkAssetsOf(
@@ -755,12 +740,12 @@ export async function endProposal(
755
740
  {
756
741
  kind: 'inline',
757
742
  value: serialiseExecuteDatum({
758
- id: pollManagerDatum.pollId,
759
- content: pollManagerDatum.content,
743
+ id: pollManager.pollId,
744
+ content: pollManager.content,
760
745
  passedTime: currentTime,
761
- votingEndTime: pollManagerDatum.votingEndTime,
762
- protocolVersion: pollManagerDatum.protocolVersion,
763
- treasuryWithdrawal: pollManagerDatum.treasuryWithdrawal,
746
+ votingEndTime: pollManager.votingEndTime,
747
+ protocolVersion: pollManager.protocolVersion,
748
+ treasuryWithdrawal: pollManager.treasuryWithdrawal,
764
749
  }),
765
750
  },
766
751
  upgradeTokenVal,
@@ -781,11 +766,21 @@ export async function endProposal(
781
766
  }
782
767
 
783
768
  export async function executeProposal(
784
- execute: UtxoOrOutRef,
785
- gov: UtxoOrOutRef,
786
- treasuryWithdrawal: UtxoOrOutRef | null,
769
+ executeOref: OutRef,
770
+ govOref: OutRef,
771
+ treasuryWithdrawalOref: OutRef | null,
787
772
  allIAssetOrefs: OutRef[] | null,
788
- modifyIAsset: UtxoOrOutRef | null,
773
+ /**
774
+ * This for either Propose IAsset, Modify IAsset or Add Collateral asset proposals.
775
+ */
776
+ iAssetOref: OutRef | null,
777
+ /**
778
+ * This is when adding new collateral asset. It should be all collateral assets of the given iAsset
779
+ * where it's adding the new collateral asset.
780
+ */
781
+ allCollateralAssetsOrefsOfIAsset: OutRef[] | null,
782
+ collateralAssetOref: OutRef | null,
783
+ stableswapPoolOrCdpCreatorOref: OutRef | null,
789
784
  sysParams: SystemParams,
790
785
  lucid: LucidEvolution,
791
786
  currentSlot: number,
@@ -795,7 +790,10 @@ export async function executeProposal(
795
790
 
796
791
  const ownAddr = await lucid.wallet().address();
797
792
 
798
- const govUtxo = await resolveUtxo(gov, lucid, 'Expected a single gov UTXO');
793
+ const govUtxo = matchSingle(
794
+ await lucid.utxosByOutRef([govOref]),
795
+ (_) => new Error('Expected a single gov UTXO'),
796
+ );
799
797
 
800
798
  const govDatum = parseGovDatumOrThrow(getInlineDatumOrThrow(govUtxo));
801
799
 
@@ -824,27 +822,15 @@ export async function executeProposal(
824
822
  new Error('Expected a single upgrade auth token policy ref Script UTXO'),
825
823
  );
826
824
 
827
- const executeUtxo = await resolveUtxo(
828
- execute,
829
- lucid,
830
- 'Expected a single execute UTXO',
825
+ const executeUtxo = matchSingle(
826
+ await lucid.utxosByOutRef([executeOref]),
827
+ (_) => new Error('Expected a single execute UTXO'),
831
828
  );
832
829
 
833
830
  const executeDatum = parseExecuteDatumOrThrow(
834
831
  getInlineDatumOrThrow(executeUtxo),
835
832
  );
836
833
 
837
- const indyWithdrawalAmt = assetClassValueOf(
838
- executeDatum.treasuryWithdrawal
839
- ? createValueFromWithdrawal(executeDatum.treasuryWithdrawal)
840
- : {},
841
- fromSystemParamsAsset(sysParams.govParams.indyAsset),
842
- );
843
- const newTreasuryWithdrawnIndyAmtCapped = bigintMin(
844
- 4_822_081n * OCD_DECIMAL_UNIT,
845
- govDatum.treasuryIndyWithdrawnAmt + indyWithdrawalAmt,
846
- );
847
-
848
834
  const tx = lucid.newTx();
849
835
 
850
836
  // Handle treasury withdrawal
@@ -852,13 +838,13 @@ export async function executeProposal(
852
838
  O.fromNullable(executeDatum.treasuryWithdrawal),
853
839
  O.match(
854
840
  () => {
855
- if (treasuryWithdrawal) {
841
+ if (treasuryWithdrawalOref) {
856
842
  throw new Error('Cannot provide withdrawal oref when no withdrawal.');
857
843
  }
858
844
  return Promise.resolve();
859
845
  },
860
846
  async (withdrawal) => {
861
- if (!treasuryWithdrawal) {
847
+ if (!treasuryWithdrawalOref) {
862
848
  throw new Error('Have to provide withdrawal oref when withdrawal.');
863
849
  }
864
850
 
@@ -871,10 +857,9 @@ export async function executeProposal(
871
857
  (_) => new Error('Expected a single Treasury Ref Script UTXO'),
872
858
  );
873
859
 
874
- const treasuryWithdrawalUtxo = await resolveUtxo(
875
- treasuryWithdrawal,
876
- lucid,
877
- 'Expected a single withdrawal UTXO',
860
+ const treasuryWithdrawalUtxo = matchSingle(
861
+ await lucid.utxosByOutRef([treasuryWithdrawalOref]),
862
+ (_) => new Error('Expected a single withdrawal UTXO'),
878
863
  );
879
864
 
880
865
  const withdrawalVal = createValueFromWithdrawal(withdrawal);
@@ -889,7 +874,7 @@ export async function executeProposal(
889
874
  network,
890
875
  sysParams.validatorHashes.treasuryHash,
891
876
  sysParams.treasuryParams.treasuryUtxosStakeCredential
892
- ? fromSysParamsScriptCredential(
877
+ ? fromSysParamsCredential(
893
878
  sysParams.treasuryParams.treasuryUtxosStakeCredential,
894
879
  )
895
880
  : undefined,
@@ -909,9 +894,9 @@ export async function executeProposal(
909
894
  {
910
895
  kind: 'inline',
911
896
  value: serialiseWithdrawalOutputDatum([
912
- fromText('IndigoTreasuryWithdrawal'),
897
+ fromHex(fromText('IndigoTreasuryWithdrawal')),
913
898
  {
914
- txHash: { hash: executeUtxo.txHash },
899
+ txHash: fromHex(executeUtxo.txHash),
915
900
  outputIndex: BigInt(executeUtxo.outputIndex),
916
901
  },
917
902
  ]),
@@ -923,7 +908,7 @@ export async function executeProposal(
923
908
  );
924
909
 
925
910
  await match(executeDatum.content)
926
- .with({ ProposeAsset: P.select() }, async (proposeContent) => {
911
+ .with({ ProposeIAsset: P.select() }, async (proposeContent) => {
927
912
  const iassetTokenPolicyRefScriptUtxo = matchSingle(
928
913
  await lucid.utxosByOutRef([
929
914
  fromSystemParamsScriptRef(
@@ -946,11 +931,13 @@ export async function executeProposal(
946
931
  new Error('Expected a single SP auth token policy ref Script UTXO'),
947
932
  );
948
933
 
949
- const cdpRefScriptUtxo = matchSingle(
934
+ const iassetRefScriptUtxo = matchSingle(
950
935
  await lucid.utxosByOutRef([
951
- fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
936
+ fromSystemParamsScriptRef(
937
+ sysParams.scriptReferences.iassetValidatorRef,
938
+ ),
952
939
  ]),
953
- (_) => new Error('Expected a single CDP Ref Script UTXO'),
940
+ (_) => new Error('Expected a single IAsset Ref Script UTXO'),
954
941
  );
955
942
 
956
943
  if (!allIAssetOrefs) {
@@ -958,7 +945,7 @@ export async function executeProposal(
958
945
  }
959
946
 
960
947
  const iassetToReference = await findRelativeIAssetForInsertion(
961
- toText(proposeContent.asset),
948
+ proposeContent.asset,
962
949
  allIAssetOrefs,
963
950
  lucid,
964
951
  );
@@ -982,7 +969,7 @@ export async function executeProposal(
982
969
 
983
970
  tx.readFrom([
984
971
  govRefScriptUtxo,
985
- cdpRefScriptUtxo,
972
+ iassetRefScriptUtxo,
986
973
  iassetTokenPolicyRefScriptUtxo,
987
974
  stabilityPoolTokenPolicyRefScriptUtxo,
988
975
  ])
@@ -995,15 +982,17 @@ export async function executeProposal(
995
982
  kind: 'inline',
996
983
  value: serialiseGovDatum({
997
984
  ...govDatum,
998
- treasuryIndyWithdrawnAmt: newTreasuryWithdrawnIndyAmtCapped,
999
985
  iassetsCount: govDatum.iassetsCount + 1n,
1000
986
  }),
1001
987
  },
1002
988
  govUtxo.assets,
1003
989
  )
1004
990
  .pay.ToContract(
1005
- createScriptAddress(network, sysParams.validatorHashes.cdpHash),
1006
- { kind: 'inline', value: serialiseIAssetDatum(newIAsset) },
991
+ createScriptAddress(network, sysParams.validatorHashes.iassetHash),
992
+ {
993
+ kind: 'inline',
994
+ value: serialiseIAssetDatum({ IAssetContent: newIAsset }),
995
+ },
1007
996
  iassetAuthVal,
1008
997
  )
1009
998
  .pay.ToContract(
@@ -1016,9 +1005,9 @@ export async function executeProposal(
1016
1005
  value: serialiseStabilityPoolDatum(
1017
1006
  {
1018
1007
  StabilityPool: {
1019
- asset: fromHex(proposeContent.asset),
1020
- poolSnapshot: initSpSnapshot,
1021
- epochToScaleToSum: initEpochToScaleToSumMap(),
1008
+ iasset: proposeContent.asset,
1009
+ state: initSpState,
1010
+ assetStates: [],
1022
1011
  },
1023
1012
  },
1024
1013
  true,
@@ -1041,14 +1030,22 @@ export async function executeProposal(
1041
1030
  throw new Error('Expected some referenced iasset.');
1042
1031
  },
1043
1032
  (newRefIAsset) => {
1044
- tx.collectFrom(
1045
- [i.utxo],
1046
- serialiseCdpRedeemer('UpdateOrInsertAsset'),
1047
- ).pay.ToContract(
1033
+ tx.collectFrom([i.utxo], {
1034
+ kind: 'selected',
1035
+ makeRedeemer: (inputIndices) =>
1036
+ serialiseIAssetRedeemer({
1037
+ UpdateOrInsertAsset: {
1038
+ executeInputIdx: inputIndices[0],
1039
+ },
1040
+ }),
1041
+ inputs: [executeUtxo],
1042
+ }).pay.ToContract(
1048
1043
  i.utxo.address,
1049
1044
  {
1050
1045
  kind: 'inline',
1051
- value: serialiseIAssetDatum(newRefIAsset),
1046
+ value: serialiseIAssetDatum({
1047
+ IAssetContent: newRefIAsset,
1048
+ }),
1052
1049
  },
1053
1050
  i.utxo.assets,
1054
1051
  );
@@ -1058,96 +1055,430 @@ export async function executeProposal(
1058
1055
  ),
1059
1056
  );
1060
1057
  })
1061
- .with({ ModifyAsset: P.select() }, async (modifyContent) => {
1062
- const cdpRefScriptUtxo = matchSingle(
1058
+ .with({ ModifyIAsset: P.select() }, async (modifyContent) => {
1059
+ const iassetRefScriptUtxo = matchSingle(
1063
1060
  await lucid.utxosByOutRef([
1064
- fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
1061
+ fromSystemParamsScriptRef(
1062
+ sysParams.scriptReferences.iassetValidatorRef,
1063
+ ),
1065
1064
  ]),
1066
1065
  (_) => new Error('Expected a single CDP Ref Script UTXO'),
1067
1066
  );
1068
1067
 
1069
- if (!modifyIAsset) {
1068
+ if (!iAssetOref) {
1070
1069
  throw new Error('Have to provide iasset oref when modify asset.');
1071
1070
  }
1072
1071
 
1073
- const iassetUtxo = await resolveUtxo(
1074
- modifyIAsset,
1075
- lucid,
1076
- 'Expected a single iasset UTXO',
1072
+ const iassetUtxo = matchSingle(
1073
+ await lucid.utxosByOutRef([iAssetOref]),
1074
+ (_) => new Error('Expected a single iasset UTXO'),
1077
1075
  );
1078
1076
 
1079
1077
  const iassetDatum = parseIAssetDatumOrThrow(
1080
1078
  getInlineDatumOrThrow(iassetUtxo),
1081
1079
  );
1082
1080
 
1083
- tx.readFrom([cdpRefScriptUtxo])
1084
- .collectFrom([iassetUtxo], serialiseCdpRedeemer('UpdateOrInsertAsset'))
1081
+ tx.readFrom([iassetRefScriptUtxo])
1082
+ .collectFrom([iassetUtxo], {
1083
+ kind: 'selected',
1084
+ makeRedeemer: (inputIndices) =>
1085
+ serialiseIAssetRedeemer({
1086
+ UpdateOrInsertAsset: {
1087
+ executeInputIdx: inputIndices[0],
1088
+ },
1089
+ }),
1090
+ inputs: [executeUtxo],
1091
+ })
1085
1092
  .pay.ToContract(
1086
- createScriptAddress(network, sysParams.validatorHashes.cdpHash),
1093
+ createScriptAddress(network, sysParams.validatorHashes.iassetHash),
1087
1094
  {
1088
1095
  kind: 'inline',
1089
1096
  value: serialiseIAssetDatum({
1090
- assetName: iassetDatum.assetName,
1091
- price: modifyContent.newAssetPriceInfo,
1092
- interestOracleNft: modifyContent.newInterestOracleNft,
1093
- redemptionRatio: modifyContent.newRedemptionRatioPercentage,
1094
- maintenanceRatio: modifyContent.newMaintenanceRatioPercentage,
1095
- liquidationRatio: modifyContent.newLiquidationRatioPercentage,
1096
- debtMintingFeePercentage:
1097
- modifyContent.newDebtMintingFeePercentage,
1098
- liquidationProcessingFeePercentage:
1099
- modifyContent.newLiquidationProcessingFeePercentage,
1100
- stabilityPoolWithdrawalFeePercentage:
1101
- modifyContent.newStabilityPoolWithdrawalFeePercentage,
1102
- redemptionReimbursementPercentage:
1103
- modifyContent.newRedemptionReimbursementPercentage,
1104
- redemptionProcessingFeePercentage:
1105
- modifyContent.newRedemptionProcessingFeePercentage,
1106
- interestCollectorPortionPercentage:
1107
- modifyContent.newInterestCollectorPortionPercentage,
1108
- firstIAsset: iassetDatum.firstIAsset,
1109
- nextIAsset: iassetDatum.nextIAsset,
1097
+ IAssetContent: {
1098
+ assetName: iassetDatum.assetName,
1099
+ collateralAssetsCount: iassetDatum.collateralAssetsCount,
1100
+ debtMintingFeeRatio: modifyContent.newDebtMintingFeeRatio,
1101
+ liquidationProcessingFeeRatio:
1102
+ modifyContent.newLiquidationProcessingFeeRatio,
1103
+ stabilityPoolWithdrawalFeeRatio:
1104
+ modifyContent.newStabilityPoolWithdrawalFeeRatio,
1105
+ redemptionReimbursementRatio:
1106
+ modifyContent.newRedemptionReimbursementRatio,
1107
+ redemptionProcessingFeeRatio:
1108
+ modifyContent.newRedemptionProcessingFeeRatio,
1109
+ firstIAsset: iassetDatum.firstIAsset,
1110
+ nextIAsset: iassetDatum.nextIAsset,
1111
+ },
1110
1112
  }),
1111
1113
  },
1112
1114
  iassetUtxo.assets,
1115
+ )
1116
+ .readFrom([govUtxo])
1117
+ .setMinFee(900_000n);
1118
+ })
1119
+ .with({ AddCollateralAsset: P.select() }, async (addCollateralContent) => {
1120
+ const collateralAssetTokenPolicyRefScriptUtxo = matchSingle(
1121
+ await lucid.utxosByOutRef([
1122
+ fromSystemParamsScriptRef(
1123
+ sysParams.scriptReferences.authTokenPolicies
1124
+ .collateralAssetTokenRef,
1125
+ ),
1126
+ ]),
1127
+ (_) =>
1128
+ new Error(
1129
+ 'Expected a single collateral asset auth token policy ref Script UTXO',
1130
+ ),
1131
+ );
1132
+
1133
+ const iassetRefScriptUtxo = matchSingle(
1134
+ await lucid.utxosByOutRef([
1135
+ fromSystemParamsScriptRef(
1136
+ sysParams.scriptReferences.iassetValidatorRef,
1137
+ ),
1138
+ ]),
1139
+ (_) => new Error('Expected a single iasset Ref Script UTXO'),
1140
+ );
1141
+
1142
+ if (!iAssetOref) {
1143
+ throw new Error(
1144
+ 'Have to provide iasset oref when add collateral asset.',
1113
1145
  );
1146
+ }
1114
1147
 
1115
- if (indyWithdrawalAmt > 0) {
1116
- tx.readFrom([govRefScriptUtxo])
1117
- .collectFrom([govUtxo], serialiseGovRedeemer('UpgradeGov'))
1118
- .pay.ToContract(
1119
- govUtxo.address,
1120
- {
1121
- kind: 'inline',
1122
- value: serialiseGovDatum({
1123
- ...govDatum,
1124
- treasuryIndyWithdrawnAmt: newTreasuryWithdrawnIndyAmtCapped,
1125
- }),
1126
- },
1127
- govUtxo.assets,
1128
- );
1129
- } else {
1130
- tx.readFrom([govUtxo]);
1148
+ const iassetUtxo = matchSingle(
1149
+ await lucid.utxosByOutRef([iAssetOref]),
1150
+ (_) => new Error('Expected a single iasset UTXO'),
1151
+ );
1152
+ const iassetDatum = parseIAssetDatumOrThrow(
1153
+ getInlineDatumOrThrow(iassetUtxo),
1154
+ );
1155
+
1156
+ if (!allCollateralAssetsOrefsOfIAsset) {
1157
+ throw new Error(
1158
+ 'Have to provide all collateral asset orefs when propose asset.',
1159
+ );
1131
1160
  }
1161
+
1162
+ const collateralAssetToReference =
1163
+ await findRelativeCollateralAssetForInsertion(
1164
+ addCollateralContent.collateralAsset,
1165
+ allCollateralAssetsOrefsOfIAsset,
1166
+ lucid,
1167
+ );
1168
+
1169
+ const { newCollateralAsset, newReferencedCollateralAsset } =
1170
+ collateralAssetCreationDatumHelper(
1171
+ addCollateralContent,
1172
+ F.pipe(
1173
+ collateralAssetToReference,
1174
+ O.map((i) => i.datum),
1175
+ ),
1176
+ );
1177
+
1178
+ const collateralAssetAuthVal = mkAssetsOf(
1179
+ fromSystemParamsAsset(sysParams.executeParams.collateralAssetToken),
1180
+ 1n,
1181
+ );
1182
+
1183
+ tx.readFrom([
1184
+ iassetRefScriptUtxo,
1185
+ collateralAssetTokenPolicyRefScriptUtxo,
1186
+ ]);
1187
+ tx.readFrom([govUtxo])
1188
+ .mintAssets(collateralAssetAuthVal, Data.void())
1189
+ .collectFrom([iassetUtxo], {
1190
+ kind: 'selected',
1191
+ makeRedeemer: (inputIndices) =>
1192
+ serialiseIAssetRedeemer({
1193
+ UpdateOrInsertAsset: {
1194
+ executeInputIdx: inputIndices[0],
1195
+ },
1196
+ }),
1197
+ inputs: [executeUtxo],
1198
+ })
1199
+ .pay.ToContract(
1200
+ createScriptAddress(network, sysParams.validatorHashes.iassetHash),
1201
+ {
1202
+ kind: 'inline',
1203
+ value: serialiseIAssetDatum({
1204
+ IAssetContent: {
1205
+ ...iassetDatum,
1206
+ collateralAssetsCount: iassetDatum.collateralAssetsCount + 1n,
1207
+ },
1208
+ }),
1209
+ },
1210
+ iassetUtxo.assets,
1211
+ )
1212
+ .pay.ToContract(
1213
+ createScriptAddress(network, sysParams.validatorHashes.iassetHash),
1214
+ {
1215
+ kind: 'inline',
1216
+ value: serialiseIAssetDatum({
1217
+ CollateralAssetContent: newCollateralAsset,
1218
+ }),
1219
+ },
1220
+ collateralAssetAuthVal,
1221
+ );
1222
+
1223
+ F.pipe(
1224
+ collateralAssetToReference,
1225
+ O.match(
1226
+ () => {
1227
+ // no action
1228
+ },
1229
+ (i) => {
1230
+ F.pipe(
1231
+ newReferencedCollateralAsset,
1232
+ O.match(
1233
+ () => {
1234
+ throw new Error('Expected some referenced collateral asset');
1235
+ },
1236
+ (newRefCollateralAsset) => {
1237
+ tx.collectFrom([i.utxo], {
1238
+ kind: 'selected',
1239
+ makeRedeemer: (inputIndices) =>
1240
+ serialiseIAssetRedeemer({
1241
+ UpdateOrInsertAsset: {
1242
+ executeInputIdx: inputIndices[0],
1243
+ },
1244
+ }),
1245
+ inputs: [executeUtxo],
1246
+ }).pay.ToContract(
1247
+ createScriptAddress(
1248
+ network,
1249
+ sysParams.validatorHashes.iassetHash,
1250
+ ),
1251
+ {
1252
+ kind: 'inline',
1253
+ value: serialiseIAssetDatum({
1254
+ CollateralAssetContent: newRefCollateralAsset,
1255
+ }),
1256
+ },
1257
+ i.utxo.assets,
1258
+ );
1259
+ },
1260
+ ),
1261
+ );
1262
+ },
1263
+ ),
1264
+ );
1132
1265
  })
1133
- .with({ TextProposal: P.any }, () => {
1134
- if (indyWithdrawalAmt > 0) {
1135
- tx.readFrom([govRefScriptUtxo])
1136
- .collectFrom([govUtxo], serialiseGovRedeemer('UpgradeGov'))
1266
+ .with(
1267
+ { UpdateCollateralAsset: P.select() },
1268
+ async (updateCollateralContent) => {
1269
+ const iassetRefScriptUtxo = matchSingle(
1270
+ await lucid.utxosByOutRef([
1271
+ fromSystemParamsScriptRef(
1272
+ sysParams.scriptReferences.iassetValidatorRef,
1273
+ ),
1274
+ ]),
1275
+ (_) => new Error('Expected a single iasset Ref Script UTXO'),
1276
+ );
1277
+
1278
+ if (!collateralAssetOref) {
1279
+ throw new Error(
1280
+ 'Have to provide collateral asset oref when add collateral asset.',
1281
+ );
1282
+ }
1283
+
1284
+ const collateralAssetUtxo = matchSingle(
1285
+ await lucid.utxosByOutRef([collateralAssetOref]),
1286
+ (_) => new Error('Expected a single collateral asset UTXO'),
1287
+ );
1288
+ const collateralAssetDatum = parseCollateralAssetDatumOrThrow(
1289
+ getInlineDatumOrThrow(collateralAssetUtxo),
1290
+ );
1291
+
1292
+ if (
1293
+ !isSameAssetClass(
1294
+ collateralAssetDatum.collateralAsset,
1295
+ updateCollateralContent.collateralAsset,
1296
+ ) ||
1297
+ toHex(collateralAssetDatum.iasset) !==
1298
+ toHex(updateCollateralContent.correspondingIAsset)
1299
+ ) {
1300
+ throw new Error('Wrong collateral asset.');
1301
+ }
1302
+
1303
+ tx.readFrom([iassetRefScriptUtxo])
1304
+ .collectFrom([collateralAssetUtxo], {
1305
+ kind: 'selected',
1306
+ makeRedeemer: (inputIndices) =>
1307
+ serialiseIAssetRedeemer({
1308
+ UpdateOrInsertAsset: {
1309
+ executeInputIdx: inputIndices[0],
1310
+ },
1311
+ }),
1312
+ inputs: [executeUtxo],
1313
+ })
1137
1314
  .pay.ToContract(
1138
- govUtxo.address,
1315
+ createScriptAddress(network, sysParams.validatorHashes.iassetHash),
1139
1316
  {
1140
1317
  kind: 'inline',
1141
- value: serialiseGovDatum({
1142
- ...govDatum,
1143
- treasuryIndyWithdrawnAmt: newTreasuryWithdrawnIndyAmtCapped,
1318
+ value: serialiseIAssetDatum({
1319
+ CollateralAssetContent: {
1320
+ ...collateralAssetDatum,
1321
+ priceInfo: updateCollateralContent.newAssetPriceInfo,
1322
+ interestOracleNft:
1323
+ updateCollateralContent.newInterestOracleNft,
1324
+ redemptionRatio: updateCollateralContent.newRedemptionRatio,
1325
+ maintenanceRatio: updateCollateralContent.newMaintenanceRatio,
1326
+ liquidationRatio: updateCollateralContent.newLiquidationRatio,
1327
+ minCollateralAmt: updateCollateralContent.newMinCollateralAmt,
1328
+ },
1144
1329
  }),
1145
1330
  },
1146
- govUtxo.assets,
1147
- );
1148
- } else {
1149
- tx.readFrom([govUtxo]);
1331
+ collateralAssetUtxo.assets,
1332
+ )
1333
+ .readFrom([govUtxo])
1334
+ .setMinFee(900_000n);
1335
+ },
1336
+ )
1337
+ .with({ TextProposal: P.any }, () => {
1338
+ tx.readFrom([govUtxo]);
1339
+ })
1340
+ .with({ ProposeStableswapPool: P.select() }, async (proposeContent) => {
1341
+ const cdpCreatorRefScriptUtxo = matchSingle(
1342
+ await lucid.utxosByOutRef([
1343
+ fromSystemParamsScriptRef(
1344
+ sysParams.scriptReferences.cdpCreatorValidatorRef,
1345
+ ),
1346
+ ]),
1347
+ (_) => new Error('Expected a single CDP Creator Ref Script UTXO'),
1348
+ );
1349
+
1350
+ const cdpTokenPolicyRefScriptUtxo = matchSingle(
1351
+ await lucid.utxosByOutRef([
1352
+ fromSystemParamsScriptRef(
1353
+ sysParams.scriptReferences.authTokenPolicies.cdpAuthTokenRef,
1354
+ ),
1355
+ ]),
1356
+ (_) =>
1357
+ new Error('Expected a single CDP auth token policy ref Script UTXO'),
1358
+ );
1359
+
1360
+ if (!iAssetOref) {
1361
+ throw new Error(
1362
+ 'Have to provide iasset oref when proposing stableswap pool.',
1363
+ );
1364
+ }
1365
+
1366
+ const iassetUtxo = matchSingle(
1367
+ await lucid.utxosByOutRef([iAssetOref]),
1368
+ (_) => new Error('Expected a single iasset UTXO'),
1369
+ );
1370
+
1371
+ const cdpAuthVal = mkAssetsOf(
1372
+ fromSystemParamsAsset(sysParams.executeParams.cdpToken),
1373
+ 1n,
1374
+ );
1375
+
1376
+ if (!stableswapPoolOrCdpCreatorOref) {
1377
+ throw new Error(
1378
+ 'Have to provide cdp creator oref when propose stableswap pool.',
1379
+ );
1150
1380
  }
1381
+
1382
+ const cdpCreatorUtxo = matchSingle(
1383
+ await lucid.utxosByOutRef([stableswapPoolOrCdpCreatorOref]),
1384
+ (_) => new Error('Expected a single iasset UTXO'),
1385
+ );
1386
+
1387
+ tx.readFrom([
1388
+ cdpCreatorRefScriptUtxo,
1389
+ cdpTokenPolicyRefScriptUtxo,
1390
+ govUtxo,
1391
+ iassetUtxo,
1392
+ ])
1393
+ .mintAssets(cdpAuthVal, Data.void())
1394
+ .collectFrom(
1395
+ [cdpCreatorUtxo],
1396
+ serialiseCDPCreatorRedeemer('CreateStableswapPool'),
1397
+ )
1398
+ .pay.ToContract(
1399
+ createScriptAddress(
1400
+ network,
1401
+ sysParams.validatorHashes.cdpCreatorHash,
1402
+ ),
1403
+ { kind: 'inline', value: Data.void() },
1404
+ cdpCreatorUtxo.assets,
1405
+ )
1406
+ .pay.ToContract(
1407
+ createScriptAddress(network, sysParams.validatorHashes.cdpHash),
1408
+ {
1409
+ kind: 'inline',
1410
+ value: serialiseStableswapPoolDatum({
1411
+ iasset: proposeContent.iasset,
1412
+ collateralAsset: proposeContent.collateralAsset,
1413
+ collateralToIassetRatio: proposeContent.collateralToIassetRatio,
1414
+ mintingFeeRatio: proposeContent.mintingFeeRatio,
1415
+ redemptionFeeRatio: proposeContent.redemptionFeeRatio,
1416
+ mintingEnabled: true,
1417
+ redemptionEnabled: true,
1418
+ feeManager: proposeContent.feeManager,
1419
+ minMintOrderAmount: proposeContent.minMintingAmount,
1420
+ minRedemptionOrderAmount: proposeContent.minRedemptionAmount,
1421
+ stableswapValHash: proposeContent.stableswapValHash,
1422
+ }),
1423
+ },
1424
+ cdpAuthVal,
1425
+ );
1426
+ })
1427
+ .with({ ModifyStableswapPool: P.select() }, async (modifyContent) => {
1428
+ const cdpRefScriptUtxo = matchSingle(
1429
+ await lucid.utxosByOutRef([
1430
+ fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
1431
+ ]),
1432
+ (_) => new Error('Expected a single CDP Ref Script UTXO'),
1433
+ );
1434
+
1435
+ if (!stableswapPoolOrCdpCreatorOref) {
1436
+ throw new Error(
1437
+ 'Have to provide stableswap pool oref when propose stableswap pool.',
1438
+ );
1439
+ }
1440
+
1441
+ const stableswapPoolUtxo = matchSingle(
1442
+ await lucid.utxosByOutRef([stableswapPoolOrCdpCreatorOref]),
1443
+ (_) => new Error('Expected a single stableswap pool UTXO'),
1444
+ );
1445
+
1446
+ tx.readFrom([govRefScriptUtxo, cdpRefScriptUtxo, govUtxo])
1447
+ .collectFrom([stableswapPoolUtxo], {
1448
+ kind: 'selected',
1449
+ makeRedeemer: (inputIndices) =>
1450
+ serialiseCdpRedeemer({
1451
+ Stableswap: {
1452
+ forwardingInputIndex: inputIndices[0],
1453
+ },
1454
+ }),
1455
+ inputs: [executeUtxo],
1456
+ })
1457
+ .pay.ToContract(
1458
+ createScriptAddress(network, sysParams.validatorHashes.cdpHash),
1459
+ {
1460
+ kind: 'inline',
1461
+ value: serialiseStableswapPoolDatum({
1462
+ iasset: modifyContent.iasset,
1463
+ collateralAsset: modifyContent.collateralAsset,
1464
+ collateralToIassetRatio: modifyContent.newCollateralToIassetRatio,
1465
+ mintingFeeRatio: modifyContent.newMintingFeeRatio,
1466
+ redemptionFeeRatio: modifyContent.newRedemptionFeeRatio,
1467
+ mintingEnabled: modifyContent.newMintingEnabled,
1468
+ redemptionEnabled: modifyContent.newRedemptionEnabled,
1469
+ feeManager: modifyContent.newFeeManager,
1470
+ minMintOrderAmount: modifyContent.newMinMintingAmount,
1471
+ minRedemptionOrderAmount: modifyContent.newMinRedemptionAmount,
1472
+ stableswapValHash: modifyContent.newStableswapValHash,
1473
+ }),
1474
+ },
1475
+ stableswapPoolUtxo.assets,
1476
+ )
1477
+ // This has to be added as otherwise there is the following error:
1478
+ // TxBuilderError: { Complete: RedeemerBuilder: Coin selection had to be updated
1479
+ // after building redeemers, possibly leading to incorrect indices. Try setting
1480
+ // a minimum fee of 1416197 lovelaces. }
1481
+ .setMinFee(1416197n);
1151
1482
  })
1152
1483
  .with(
1153
1484
  { ModifyProtocolParams: { newParams: P.select() } },
@@ -1161,7 +1492,6 @@ export async function executeProposal(
1161
1492
  value: serialiseGovDatum({
1162
1493
  ...govDatum,
1163
1494
  protocolParams: newProtocolParams,
1164
- treasuryIndyWithdrawnAmt: newTreasuryWithdrawnIndyAmtCapped,
1165
1495
  }),
1166
1496
  },
1167
1497
  govUtxo.assets,
@@ -1169,12 +1499,11 @@ export async function executeProposal(
1169
1499
  },
1170
1500
  )
1171
1501
  .with({ UpgradeProtocol: P.select() }, async (d) => {
1172
- const upgradeDetails = parseUpgradePaths(d.content);
1173
-
1174
1502
  const versionRecordTokenPolicyRefScriptUtxo = matchSingle(
1175
1503
  await lucid.utxosByOutRef([
1176
1504
  fromSystemParamsScriptRef(
1177
- sysParams.scriptReferences.versionRecordTokenPolicyRef,
1505
+ sysParams.scriptReferences.authTokenPolicies
1506
+ .versionRecordTokenPolicyRef,
1178
1507
  ),
1179
1508
  ]),
1180
1509
  (_) =>
@@ -1198,12 +1527,11 @@ export async function executeProposal(
1198
1527
  {
1199
1528
  kind: 'inline',
1200
1529
  value: serialiseVersionRecordDatum({
1201
- upgradeId: upgradeDetails.upgradeId,
1202
- upgradePaths: new Map(
1203
- upgradeDetails.upgradePaths
1204
- .entries()
1205
- .map(([h1, h2]) => [h1, h2.upgradeSymbol]),
1206
- ),
1530
+ upgradeId: d.content.upgradeId,
1531
+ upgradePaths: d.content.upgradePaths.map(([h1, h2]) => [
1532
+ h1,
1533
+ h2.upgradeSymbol,
1534
+ ]),
1207
1535
  }),
1208
1536
  },
1209
1537
  versionRecordNftVal,
@@ -1216,13 +1544,12 @@ export async function executeProposal(
1216
1544
  value: serialiseGovDatum({
1217
1545
  ...govDatum,
1218
1546
  currentVersion: govDatum.currentVersion + 1n,
1219
- treasuryIndyWithdrawnAmt: newTreasuryWithdrawnIndyAmtCapped,
1220
1547
  }),
1221
1548
  },
1222
1549
  govUtxo.assets,
1223
1550
  );
1224
1551
  })
1225
- .exhaustive();
1552
+ .otherwise(() => {});
1226
1553
 
1227
1554
  tx.readFrom([upgradeTokenPolicyRefScriptUtxo, executeRefScriptUtxo])
1228
1555
  .validFrom(Number(currentTime) - ONE_SECOND)
@@ -1232,7 +1559,8 @@ export async function executeProposal(
1232
1559
  mkAssetsOf(fromSystemParamsAsset(sysParams.govParams.upgradeToken), -1n),
1233
1560
  Data.void(),
1234
1561
  )
1235
- .addSigner(ownAddr);
1562
+ .addSigner(ownAddr)
1563
+ .setMinFee(922932n);
1236
1564
 
1237
1565
  return tx;
1238
1566
  }