@indigo-labs/indigo-sdk 0.2.42 → 0.3.1

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 (202) hide show
  1. package/.github/workflows/ci.yml +4 -2
  2. package/README.md +88 -15
  3. package/dist/index.d.mts +3012 -2194
  4. package/dist/index.d.ts +3012 -2194
  5. package/dist/index.js +9849 -6198
  6. package/dist/index.mjs +8733 -4933
  7. package/package.json +14 -3
  8. package/src/contracts/cdp/helpers.ts +68 -72
  9. package/src/contracts/cdp/scripts.ts +50 -13
  10. package/src/contracts/cdp/transactions.ts +841 -546
  11. package/src/contracts/cdp/types-new.ts +256 -0
  12. package/src/contracts/cdp/types.ts +26 -144
  13. package/src/contracts/cdp-creator/scripts.ts +15 -9
  14. package/src/contracts/cdp-creator/types-new.ts +50 -0
  15. package/src/contracts/cdp-creator/types.ts +5 -31
  16. package/src/contracts/collector/scripts.ts +1 -1
  17. package/src/contracts/collector/transactions.ts +23 -13
  18. package/src/contracts/collector/types-new.ts +17 -0
  19. package/src/contracts/execute/scripts.ts +19 -10
  20. package/src/contracts/execute/types-new.ts +44 -0
  21. package/src/contracts/execute/types.ts +5 -38
  22. package/src/contracts/gov/helpers.ts +187 -51
  23. package/src/contracts/gov/scripts.ts +17 -10
  24. package/src/contracts/gov/transactions.ts +599 -271
  25. package/src/contracts/gov/types-new.ts +253 -100
  26. package/src/contracts/gov/types.ts +4 -71
  27. package/src/contracts/iasset/helpers.ts +172 -0
  28. package/src/contracts/iasset/scripts.ts +38 -0
  29. package/src/contracts/iasset/types.ts +154 -0
  30. package/src/contracts/initialize/actions.ts +768 -0
  31. package/src/contracts/initialize/helpers.ts +611 -36
  32. package/src/contracts/initialize/types.ts +102 -28
  33. package/src/contracts/interest-collection/helpers.ts +19 -0
  34. package/src/contracts/interest-collection/scripts.ts +44 -0
  35. package/src/contracts/interest-collection/transactions.ts +436 -0
  36. package/src/contracts/interest-collection/types-new.ts +50 -0
  37. package/src/contracts/interest-collection/types.ts +26 -0
  38. package/src/contracts/interest-oracle/helpers.ts +2 -30
  39. package/src/contracts/interest-oracle/scripts.ts +1 -1
  40. package/src/contracts/interest-oracle/transactions.ts +21 -16
  41. package/src/contracts/interest-oracle/types-new.ts +32 -0
  42. package/src/contracts/interest-oracle/types.ts +1 -40
  43. package/src/contracts/one-shot/transactions.ts +1 -2
  44. package/src/contracts/poll/helpers.ts +5 -23
  45. package/src/contracts/poll/scripts.ts +12 -13
  46. package/src/contracts/poll/types-poll-manager.ts +1 -19
  47. package/src/contracts/poll/types-poll-new.ts +170 -0
  48. package/src/contracts/poll/types-poll-shard.ts +2 -24
  49. package/src/contracts/price-oracle/helpers.ts +1 -4
  50. package/src/contracts/price-oracle/scripts.ts +3 -8
  51. package/src/contracts/price-oracle/transactions.ts +32 -25
  52. package/src/contracts/price-oracle/types-new.ts +50 -0
  53. package/src/contracts/price-oracle/types.ts +2 -36
  54. package/src/contracts/pyth-feed/helpers.ts +58 -0
  55. package/src/contracts/pyth-feed/scripts.ts +15 -0
  56. package/src/contracts/pyth-feed/types.ts +181 -0
  57. package/src/contracts/rob/helpers.ts +405 -0
  58. package/src/contracts/rob/scripts.ts +35 -0
  59. package/src/contracts/rob/transactions.ts +410 -0
  60. package/src/contracts/rob/types-new.ts +128 -0
  61. package/src/contracts/rob/types.ts +16 -0
  62. package/src/contracts/rob-leverage/helpers.ts +424 -0
  63. package/src/contracts/{leverage → rob-leverage}/transactions.ts +68 -48
  64. package/src/contracts/stability-pool/helpers.ts +714 -230
  65. package/src/contracts/stability-pool/scripts.ts +20 -15
  66. package/src/contracts/stability-pool/transactions.ts +628 -496
  67. package/src/contracts/stability-pool/types-new.ts +247 -100
  68. package/src/contracts/stability-pool/types.ts +5 -22
  69. package/src/contracts/stableswap/helpers.ts +22 -0
  70. package/src/contracts/stableswap/scripts.ts +37 -0
  71. package/src/contracts/stableswap/transactions.ts +647 -0
  72. package/src/contracts/stableswap/types-new.ts +131 -0
  73. package/src/contracts/stableswap/types.ts +17 -0
  74. package/src/contracts/staking/helpers.ts +49 -34
  75. package/src/contracts/staking/scripts.ts +1 -1
  76. package/src/contracts/staking/transactions.ts +85 -130
  77. package/src/contracts/staking/types-new.ts +60 -28
  78. package/src/contracts/staking/types.ts +1 -28
  79. package/src/contracts/treasury/helpers.ts +21 -0
  80. package/src/contracts/treasury/scripts.ts +16 -26
  81. package/src/contracts/treasury/transactions.ts +256 -27
  82. package/src/contracts/treasury/types-new.ts +69 -0
  83. package/src/contracts/treasury/types.ts +2 -43
  84. package/src/contracts/version-registry/scripts.ts +2 -2
  85. package/src/contracts/version-registry/types-new.ts +6 -7
  86. package/src/index.ts +37 -20
  87. package/src/scripts/auth-token-policy.ts +3 -2
  88. package/src/scripts/iasset-policy.ts +3 -2
  89. package/src/types/evolution-schema-options.ts +3 -3
  90. package/src/types/generic.ts +17 -89
  91. package/src/types/multisig.ts +48 -0
  92. package/src/types/on-chain-decimal.ts +14 -7
  93. package/src/types/rational.ts +61 -0
  94. package/src/types/system-params.ts +237 -41
  95. package/src/utils/array-utils.ts +70 -1
  96. package/src/utils/bigint-utils.ts +12 -0
  97. package/src/utils/indigo-helpers.ts +8 -10
  98. package/src/utils/lucid-utils.ts +47 -40
  99. package/src/utils/oracle-helpers.ts +62 -0
  100. package/src/utils/pyth/decode.ts +223 -0
  101. package/src/utils/pyth/encode.ts +262 -0
  102. package/src/utils/pyth/index.ts +14 -0
  103. package/src/utils/pyth/types.ts +87 -0
  104. package/src/validators/always-succeed-validator.ts +6 -0
  105. package/src/validators/cdp-creator-validator.ts +2 -2
  106. package/src/validators/cdp-redeem-validator.ts +7 -0
  107. package/src/validators/cdp-validator.ts +2 -2
  108. package/src/validators/collector-validator.ts +2 -2
  109. package/src/validators/execute-validator.ts +2 -2
  110. package/src/validators/governance-validator.ts +2 -2
  111. package/src/validators/iasset-validator.ts +7 -0
  112. package/src/validators/interest-collection-validator.ts +7 -0
  113. package/src/validators/interest-oracle-validator.ts +2 -2
  114. package/src/validators/poll-manager-validator.ts +2 -2
  115. package/src/validators/poll-shard-validator.ts +2 -2
  116. package/src/validators/price-oracle-validator.ts +7 -0
  117. package/src/validators/pyth-feed-validator.ts +7 -0
  118. package/src/validators/rob-validator.ts +7 -0
  119. package/src/validators/stability-pool-validator.ts +2 -2
  120. package/src/validators/stableswap-validator.ts +7 -0
  121. package/src/validators/staking-validator.ts +2 -2
  122. package/src/validators/treasury-validator.ts +2 -2
  123. package/src/validators/version-record-policy.ts +2 -2
  124. package/src/validators/version-registry-validator.ts +2 -2
  125. package/tests/always-succeed/script.ts +7 -0
  126. package/tests/bigint-utils.test.ts +41 -0
  127. package/tests/cdp/actions.ts +610 -0
  128. package/tests/cdp/cdp-helpers.ts +55 -0
  129. package/tests/cdp/cdp-queries.ts +440 -0
  130. package/tests/cdp/cdp.test.ts +6087 -0
  131. package/tests/cdp/transactions-mutated.ts +1729 -0
  132. package/tests/data/system-params.json +177 -34
  133. package/tests/datums.test.ts +209 -210
  134. package/tests/endpoints/initialize.ts +68 -0
  135. package/tests/endpoints/interest-collector.ts +37 -0
  136. package/tests/endpoints/treasury.ts +70 -0
  137. package/tests/gov/actions.ts +406 -0
  138. package/tests/gov/gov.test.ts +4450 -0
  139. package/tests/{queries → gov}/governance-queries.ts +6 -3
  140. package/tests/hash-checks.test.ts +38 -11
  141. package/tests/indigo-test-helpers.ts +100 -0
  142. package/tests/initialize.test.ts +61 -9
  143. package/tests/interest-collection/interest-collection.test.ts +892 -0
  144. package/tests/interest-collection/interest-collector-queries.ts +49 -0
  145. package/tests/interest-collection/transactions-mutated.ts +260 -0
  146. package/tests/interest-oracle.test.ts +43 -35
  147. package/tests/mock/assets-mock.ts +234 -23
  148. package/tests/mock/protocol-params-mock.ts +21 -0
  149. package/tests/price-oracle/actions.ts +163 -0
  150. package/tests/price-oracle/price-oracle-queries.ts +12 -0
  151. package/tests/price-oracle/price-oracle.test.ts +240 -0
  152. package/tests/price-oracle/transactions-mutated.ts +62 -0
  153. package/tests/pyth/endpoints.ts +96 -0
  154. package/tests/pyth/helpers.ts +37 -0
  155. package/tests/pyth/pyth-encoding.test.ts +376 -0
  156. package/tests/pyth/pyth-indigo.test.ts +509 -0
  157. package/tests/pyth/pyth.test.ts +300 -0
  158. package/tests/queries/execute-queries.ts +6 -5
  159. package/tests/queries/iasset-queries.ts +175 -5
  160. package/tests/queries/interest-oracle-queries.ts +4 -2
  161. package/tests/queries/poll-queries.ts +8 -9
  162. package/tests/queries/stability-pool-queries.ts +95 -48
  163. package/tests/queries/staking-queries.ts +4 -2
  164. package/tests/queries/treasury-queries.ts +80 -5
  165. package/tests/rob/actions.ts +58 -0
  166. package/tests/{lrp-leverage.test.ts → rob/rob-leverage.test.ts} +393 -296
  167. package/tests/rob/rob-queries.ts +95 -0
  168. package/tests/rob/rob.test.ts +3762 -0
  169. package/tests/rob/transactions-mutated.ts +853 -0
  170. package/tests/script-size.test.ts +240 -0
  171. package/tests/setup.ts +135 -0
  172. package/tests/stability-pool/actions.ts +220 -0
  173. package/tests/stability-pool.test.ts +6121 -667
  174. package/tests/stableswap/stableswap-actions.ts +84 -0
  175. package/tests/stableswap/stableswap-queries.ts +89 -0
  176. package/tests/stableswap/stableswap.test.ts +3891 -0
  177. package/tests/stableswap/transactions-mutated.ts +348 -0
  178. package/tests/staking.test.ts +82 -99
  179. package/tests/test-helpers.ts +58 -11
  180. package/tests/treasury.test.ts +242 -0
  181. package/tests/utils/asserts.ts +74 -0
  182. package/tests/utils/benchmark-utils.ts +81 -0
  183. package/tests/utils/index.ts +122 -4
  184. package/tsconfig.json +9 -1
  185. package/vitest.config.ts +3 -1
  186. package/src/contracts/collector/types.ts +0 -16
  187. package/src/contracts/initialize/transactions.ts +0 -891
  188. package/src/contracts/leverage/helpers.ts +0 -424
  189. package/src/contracts/lrp/helpers.ts +0 -294
  190. package/src/contracts/lrp/scripts.ts +0 -27
  191. package/src/contracts/lrp/transactions.ts +0 -250
  192. package/src/contracts/lrp/types.ts +0 -131
  193. package/src/contracts/poll/types-poll.ts +0 -88
  194. package/src/contracts/vesting/helpers.ts +0 -218
  195. package/src/utils/value-helpers.ts +0 -37
  196. package/src/validators/lrp-validator.ts +0 -7
  197. package/tests/cdp.test.ts +0 -1528
  198. package/tests/gov.test.ts +0 -2011
  199. package/tests/lrp.test.ts +0 -673
  200. package/tests/queries/cdp-queries.ts +0 -220
  201. package/tests/queries/lrp-queries.ts +0 -76
  202. package/tests/queries/price-oracle-queries.ts +0 -10
@@ -1,45 +1,620 @@
1
- import { LucidEvolution, TxBuilder } from '@lucid-evolution/lucid';
2
-
3
- /** Token names used during protocol initialization. */
4
- export const INDY_TOKEN_NAME = 'INDY';
5
- export const DAO_TOKEN_NAME = 'DAO';
6
- export const GOV_NFT_TOKEN_NAME = 'GOV_NFT';
7
- export const POLL_MANAGER_TOKEN_NAME = 'POLL_MANAGER';
8
- export const UPGRADE_TOKEN_NAME = 'UPGRADE';
9
- export const IASSET_TOKEN_NAME = 'IASSET';
10
- export const STABILITY_POOL_TOKEN_NAME = 'STABILITY_POOL';
11
- export const VERSION_RECORD_TOKEN_NAME = 'VERSION_RECORD';
12
- export const CDP_CREATOR_TOKEN_NAME = 'CDP_CREATOR';
13
- export const CDP_TOKEN_NAME = 'CDP';
14
- export const STAKING_MANAGER_TOKEN_NAME = 'STAKING_MANAGER';
15
- export const STAKING_TOKEN_NAME = 'STAKING_POSITION';
16
- export const SNAPSHOT_EPOCH_TO_SCALE_TO_SUM_TOKEN_NAME =
17
- 'SNAPSHOT_EPOCH_TO_SCALE_TO_SUM';
18
- export const ACCOUNT_TOKEN_NAME = 'SP_ACCOUNT';
19
-
20
- export const TOTAL_INDY_SUPPLY = 35000000000000n;
21
- export const TREASURY_INDY_AMOUNT = 100_000n;
22
- export const NUM_CDP_CREATORS = 2n;
23
- export const NUM_COLLECTORS = 2n;
24
-
25
- /**
26
- * Script hash of a validator that always fails; used to create script reference UTxOs.
27
- */
28
- export const ALWAYS_FAIL_VALIDATOR_HASH =
1
+ import {
2
+ addAssets,
3
+ credentialToAddress,
4
+ Data,
5
+ fromHex,
6
+ fromText,
7
+ LucidEvolution,
8
+ mintingPolicyToId,
9
+ PolicyId,
10
+ SpendingValidator,
11
+ validatorToScriptHash,
12
+ } from '@lucid-evolution/lucid';
13
+ import {
14
+ AssetClass,
15
+ assetClassToUnit,
16
+ assetClassValueOf,
17
+ lovelacesAmt,
18
+ mkAssetsOf,
19
+ mkLovelacesOf,
20
+ } from '@3rd-eye-labs/cardano-offchain-common';
21
+ import {
22
+ addrDetails,
23
+ createScriptAddress,
24
+ submitTx,
25
+ } from '../../utils/lucid-utils';
26
+ import { runOneShotMintTx } from '../one-shot/transactions';
27
+ import {
28
+ fromSysParamsCredential,
29
+ getPythFeedConfig,
30
+ Input,
31
+ } from '../../types/system-params';
32
+ import type {
33
+ AssetInfo,
34
+ CollateralAssetInfo,
35
+ InitialAssetParam,
36
+ InitialCollateralAssetParam,
37
+ InitializeOptions,
38
+ } from './types';
39
+ import type {
40
+ CdpParamsSP,
41
+ CollectorParamsSP,
42
+ PythConfig,
43
+ } from '../../types/system-params';
44
+ import type {
45
+ CDPCreatorParamsSP,
46
+ TreasuryParamsSP,
47
+ } from '../../types/system-params';
48
+ import type {
49
+ IAssetParamsSP,
50
+ StabilityPoolParamsSP,
51
+ StakingParamsSP,
52
+ } from '../../types/system-params';
53
+ import type { GovParamsSP } from '../../types/system-params';
54
+ import type { InterestCollectionDatum } from '../interest-collection/types-new';
55
+ import type { PriceOracleParams } from '../price-oracle/types';
56
+ import type { InterestOracleParams } from '../interest-oracle/types';
57
+ import { mkAuthTokenPolicy } from '../../scripts/auth-token-policy';
58
+ import { mkIAssetValidatorFromSP } from '../iasset/scripts';
59
+ import { mkCollectorValidatorFromSP } from '../collector/scripts';
60
+ import { mkCDPCreatorValidatorFromSP } from '../cdp-creator/scripts';
61
+ import { mkTreasuryValidatorFromSP } from '../treasury/scripts';
62
+ import { mkStakingValidatorFromSP } from '../staking/scripts';
63
+ import { mkGovValidatorFromSP } from '../gov/scripts';
64
+ import { mkStabilityPoolValidatorFromSP } from '../stability-pool/scripts';
65
+ import { serialiseInterestCollectionDatum } from '../interest-collection/types-new';
66
+ import { startPriceOracleTx } from '../price-oracle/transactions';
67
+ import { startInterestOracle } from '../interest-oracle/transactions';
68
+ import { serialiseStakingDatum } from '../staking/types-new';
69
+ import { initSpState } from '../stability-pool/helpers';
70
+ import {
71
+ serialiseStabilityPoolDatum,
72
+ StabilityPoolContent,
73
+ } from '../stability-pool/types-new';
74
+ import {
75
+ CollateralAssetContent,
76
+ IAssetContent,
77
+ IAssetPriceInfo,
78
+ serialiseIAssetDatum,
79
+ } from '../iasset/types';
80
+ import { GovDatum, ProtocolParams, serialiseGovDatum } from '../gov/types-new';
81
+ import { match } from 'ts-pattern';
82
+ import { runAndAwaitTxBuilder } from '../../../tests/test-helpers';
83
+ import {
84
+ serialiseStableswapPoolDatum,
85
+ StableswapPoolContent,
86
+ } from '../cdp/types-new';
87
+ import { mkCdpValidatorFromSP } from '../cdp/scripts';
88
+
89
+ const alwaysFailValidatorHash =
29
90
  'ea84d625650d066e1645e3e81d9c70a73f9ed837bd96dc49850ae744';
30
91
 
31
- /**
32
- * Complete, sign, submit a TxBuilder and wait for confirmation.
33
- * Used by initialize transactions so the contract does not depend on test helpers.
34
- */
35
- export async function submitAndAwaitTx(
92
+ /** Default init options used by tests; users must pass explicit options to init(). */
93
+ export const DEFAULT_INIT_OPTIONS: InitializeOptions = {
94
+ numCdpCreators: 2n,
95
+ numCollectors: 2n,
96
+ numInterestCollectors: 2n,
97
+ numTreasuryUtxos: 5n,
98
+ treasuryIndyAmount: 1_000_000n,
99
+ totalIndySupply: 35000000000000n,
100
+
101
+ accountCreateFeeLovelaces: 5_000_000n,
102
+ accountProcessingCooldownMs: 300_000n,
103
+ accountProcessingBiasTime: 120_000n,
104
+
105
+ interestSettlementCooldown: 432_000_000n,
106
+
107
+ partialRedemptionExtraFeeLovelace: 10_000_000,
108
+ biasTime: 1200_000n,
109
+ gBiasTime: 120_000n,
110
+
111
+ interestCollectorUtxoLovelaces: 5_000_000n,
112
+ };
113
+
114
+ export const INIT_TOKEN_NAMES = {
115
+ indy: 'INDY',
116
+ dao: 'DAO',
117
+ multisigUtxo: 'INTEREST_COLLECTION_ADMIN',
118
+ govNft: 'GOV_NFT',
119
+ pollManager: 'POLL_MANAGER',
120
+ upgrade: 'UPGRADE',
121
+ iasset: 'IASSET',
122
+ collateralAsset: 'COLLATERAL_ASSET',
123
+ stabilityPool: 'STABILITY_POOL',
124
+ versionRecord: 'VERSION_RECORD',
125
+ cdpCreator: 'CDP_CREATOR',
126
+ cdp: 'CDP',
127
+ stakingManager: 'STAKING_MANAGER',
128
+ staking: 'STAKING_POSITION',
129
+ snapshotEpochToScaleToSum: 'SNAPSHOT_EPOCH_TO_SCALE_TO_SUM',
130
+ account: 'SP_ACCOUNT',
131
+ } as const;
132
+
133
+ export async function mintOneTimeToken(
134
+ lucid: LucidEvolution,
135
+ tokenName: string,
136
+ amount: bigint,
137
+ ): Promise<PolicyId> {
138
+ const utxos = await lucid.wallet().getUtxos();
139
+ return await runOneShotMintTx(lucid, {
140
+ referenceOutRef: {
141
+ txHash: utxos[0].txHash,
142
+ outputIdx: BigInt(utxos[0].outputIndex),
143
+ },
144
+ mintAmounts: [{ tokenName: tokenName, amount: amount }],
145
+ });
146
+ }
147
+
148
+ /** Mint a one-shot token and return its AssetClass. */
149
+ export async function mintOneTimeAsset(
36
150
  lucid: LucidEvolution,
37
- tx: TxBuilder,
38
- ): Promise<string> {
151
+ tokenName: string,
152
+ amount: bigint,
153
+ ): Promise<AssetClass> {
154
+ const policyId = await mintOneTimeToken(lucid, fromText(tokenName), amount);
155
+ return {
156
+ currencySymbol: fromHex(policyId),
157
+ tokenName: fromHex(fromText(tokenName)),
158
+ };
159
+ }
160
+
161
+ /** Build an auth-token AssetClass derived from a parent token. */
162
+ export function deriveAuthToken(
163
+ parent: AssetClass,
164
+ tokenName: string,
165
+ ): AssetClass {
166
+ const policy = mkAuthTokenPolicy(parent, fromText(tokenName));
167
+ return {
168
+ currencySymbol: fromHex(mintingPolicyToId(policy)),
169
+ tokenName: fromHex(fromText(tokenName)),
170
+ };
171
+ }
172
+
173
+ export async function initScriptRef(
174
+ lucid: LucidEvolution,
175
+ validator: SpendingValidator,
176
+ ): Promise<Input> {
177
+ const tx = lucid.newTx().pay.ToContract(
178
+ credentialToAddress(lucid.config().network!, {
179
+ hash: alwaysFailValidatorHash,
180
+ type: 'Script',
181
+ }),
182
+ undefined,
183
+ undefined,
184
+ validator,
185
+ );
39
186
  const txHash = await tx
40
187
  .complete()
41
188
  .then((t) => t.sign.withWallet().complete())
42
189
  .then((t) => t.submit());
43
190
  await lucid.awaitTx(txHash);
44
- return txHash;
191
+ return { transactionId: txHash, index: 0 };
192
+ }
193
+
194
+ export async function initCollector(
195
+ lucid: LucidEvolution,
196
+ collectorParams: CollectorParamsSP,
197
+ numCollectors: bigint,
198
+ ): Promise<void> {
199
+ const tx = lucid.newTx();
200
+
201
+ for (let i = 0; i < Number(numCollectors); i++) {
202
+ tx.pay.ToContract(
203
+ createScriptAddress(
204
+ lucid.config().network!,
205
+ validatorToScriptHash(mkCollectorValidatorFromSP(collectorParams)),
206
+ ),
207
+ {
208
+ kind: 'inline',
209
+ value: Data.void(),
210
+ },
211
+ );
212
+ }
213
+
214
+ await submitTx(lucid, tx);
215
+ }
216
+
217
+ export async function initInterestCollector(
218
+ lucid: LucidEvolution,
219
+ interestCollectionValHash: string,
220
+ multisigUtxoNft: AssetClass,
221
+ numInterestCollectors: bigint,
222
+ interestCollectorUtxoLovelaces: bigint,
223
+ ): Promise<void> {
224
+ const [pkh, _] = await addrDetails(lucid);
225
+ const interestCollectionAdminDatum: InterestCollectionDatum = {
226
+ admin_permissions: {
227
+ Signature: {
228
+ keyHash: fromHex(pkh.hash),
229
+ },
230
+ },
231
+ };
232
+
233
+ await submitTx(
234
+ lucid,
235
+ lucid.newTx().pay.ToContract(
236
+ createScriptAddress(lucid.config().network!, interestCollectionValHash),
237
+ {
238
+ kind: 'inline',
239
+ value: serialiseInterestCollectionDatum(interestCollectionAdminDatum),
240
+ },
241
+ mkAssetsOf(multisigUtxoNft, 1n),
242
+ ),
243
+ );
244
+
245
+ const interestCollectionUtxoDeploymentTx = lucid.newTx();
246
+
247
+ for (let i = 0; i < Number(numInterestCollectors); i++) {
248
+ interestCollectionUtxoDeploymentTx.pay.ToContract(
249
+ createScriptAddress(lucid.config().network!, interestCollectionValHash),
250
+ undefined,
251
+ mkLovelacesOf(interestCollectorUtxoLovelaces),
252
+ );
253
+ }
254
+
255
+ await submitTx(lucid, interestCollectionUtxoDeploymentTx);
256
+ }
257
+
258
+ export async function initCDPCreator(
259
+ lucid: LucidEvolution,
260
+ cdpCreatorParams: CDPCreatorParamsSP,
261
+ numCdpCreators: bigint,
262
+ ): Promise<void> {
263
+ const tx = lucid.newTx();
264
+
265
+ for (let i = 0; i < Number(numCdpCreators); i++) {
266
+ tx.pay.ToContract(
267
+ credentialToAddress(lucid.config().network!, {
268
+ hash: validatorToScriptHash(
269
+ mkCDPCreatorValidatorFromSP(cdpCreatorParams),
270
+ ),
271
+ type: 'Script',
272
+ }),
273
+ { kind: 'inline', value: Data.void() },
274
+ {
275
+ [cdpCreatorParams.cdpCreatorNft[0].unCurrencySymbol +
276
+ fromText(cdpCreatorParams.cdpCreatorNft[1].unTokenName)]: 1n,
277
+ },
278
+ );
279
+ }
280
+
281
+ await submitTx(lucid, tx);
282
+ }
283
+
284
+ export async function initTreasury(
285
+ lucid: LucidEvolution,
286
+ treasuryParams: TreasuryParamsSP,
287
+ daoAsset: AssetClass,
288
+ indyAsset: AssetClass,
289
+ treasuryIndyAmount: bigint,
290
+ numTreasuryCollectors: bigint,
291
+ ): Promise<void> {
292
+ const treasuryAddr = createScriptAddress(
293
+ lucid.config().network!,
294
+ validatorToScriptHash(mkTreasuryValidatorFromSP(treasuryParams)),
295
+ treasuryParams.treasuryUtxosStakeCredential != null
296
+ ? fromSysParamsCredential(treasuryParams.treasuryUtxosStakeCredential)
297
+ : undefined,
298
+ );
299
+ const tx = lucid
300
+ .newTx()
301
+ .pay.ToContract(
302
+ treasuryAddr,
303
+ { kind: 'inline', value: Data.void() },
304
+ addAssets(
305
+ mkAssetsOf(daoAsset, 1n),
306
+ mkAssetsOf(indyAsset, treasuryIndyAmount),
307
+ ),
308
+ );
309
+
310
+ for (let i = 0; i < Number(numTreasuryCollectors); i++) {
311
+ tx.pay.ToContract(treasuryAddr, {
312
+ kind: 'inline',
313
+ value: Data.void(),
314
+ });
315
+ }
316
+
317
+ await submitTx(lucid, tx);
318
+ }
319
+
320
+ export async function initStakingManager(
321
+ lucid: LucidEvolution,
322
+ stakingParams: StakingParamsSP,
323
+ ): Promise<void> {
324
+ const tx = lucid.newTx().pay.ToContract(
325
+ createScriptAddress(
326
+ lucid.config().network!,
327
+ validatorToScriptHash(mkStakingValidatorFromSP(stakingParams)),
328
+ ),
329
+ {
330
+ kind: 'inline',
331
+ value: serialiseStakingDatum({
332
+ totalStake: 0n,
333
+ managerSnapshot: { snapshotAda: 0n },
334
+ }),
335
+ },
336
+ {
337
+ [stakingParams.stakingManagerNFT[0].unCurrencySymbol +
338
+ fromText(stakingParams.stakingManagerNFT[1].unTokenName)]: 1n,
339
+ },
340
+ );
341
+ await submitTx(lucid, tx);
342
+ }
343
+
344
+ export async function handleOracleForCollateralAsset(
345
+ lucid: LucidEvolution,
346
+ iasset: InitialAssetParam,
347
+ collateralAsset: InitialCollateralAssetParam,
348
+ pythConfig: PythConfig,
349
+ currentSlot: number,
350
+ ): Promise<{ info: IAssetPriceInfo; params: PriceOracleParams | undefined }> {
351
+ return match(collateralAsset.priceOracle)
352
+ .returnType<
353
+ Promise<{ info: IAssetPriceInfo; params: PriceOracleParams | undefined }>
354
+ >()
355
+ .with({ tag: '_indigo_oracle_nft' }, async (val) => {
356
+ const [pkh, _] = await addrDetails(lucid);
357
+
358
+ const priceOracleParams: PriceOracleParams = {
359
+ owner: pkh.hash,
360
+ biasTime: val.params.biasTime,
361
+ expirationPeriod: val.params.expirationPeriod,
362
+ };
363
+
364
+ const [priceOracleStartTx, priceOracleNft] = await startPriceOracleTx(
365
+ lucid,
366
+ val.tokenName,
367
+ val.startPrice,
368
+ priceOracleParams,
369
+ currentSlot,
370
+ );
371
+ await runAndAwaitTxBuilder(lucid, priceOracleStartTx);
372
+
373
+ return { info: { OracleNft: priceOracleNft }, params: priceOracleParams };
374
+ })
375
+ .with({ tag: '_pyth_oracle_nft' }, () => {
376
+ return Promise.resolve({
377
+ info: {
378
+ DeferredValidation: {
379
+ feedValHash: fromHex(
380
+ getPythFeedConfig(
381
+ pythConfig,
382
+ fromHex(fromText(iasset.name)),
383
+ collateralAsset.collateralAsset,
384
+ ).pythFeedValHash,
385
+ ),
386
+ },
387
+ } satisfies IAssetPriceInfo,
388
+ params: undefined,
389
+ });
390
+ })
391
+ .exhaustive();
392
+ }
393
+
394
+ export async function initializeAsset(
395
+ lucid: LucidEvolution,
396
+ iassetParams: IAssetParamsSP,
397
+ iassetToken: AssetClass,
398
+ collateralAssetAuthToken: AssetClass,
399
+ cdpAuthToken: AssetClass,
400
+ cdpParams: CdpParamsSP,
401
+ stableswapValidatorHash: string,
402
+ stabilityPoolParams: StabilityPoolParamsSP,
403
+ stabilityPoolToken: AssetClass,
404
+ asset: InitialAssetParam,
405
+ pythConfig: PythConfig,
406
+ currentSlot: number,
407
+ ): Promise<AssetInfo> {
408
+ const iassetName = fromHex(fromText(asset.name));
409
+
410
+ const collateralAssetInfos: CollateralAssetInfo[] = [];
411
+
412
+ for (const collateralAsset of asset.collateralAssets) {
413
+ const [pkh, _] = await addrDetails(lucid);
414
+
415
+ const interestOracleParams: InterestOracleParams = {
416
+ owner: pkh.hash,
417
+ biasTime: collateralAsset.interestOracle.params.biasTime,
418
+ };
419
+ const [startInterestOracleTx, interestOracleNft] =
420
+ await startInterestOracle(
421
+ 0n,
422
+ collateralAsset.interestOracle.initialInterestRate,
423
+ 0n,
424
+ interestOracleParams,
425
+ lucid,
426
+ collateralAsset.interestOracle.tokenName,
427
+ );
428
+ await submitTx(lucid, startInterestOracleTx);
429
+ const priceInfo = await handleOracleForCollateralAsset(
430
+ lucid,
431
+ asset,
432
+ collateralAsset,
433
+ pythConfig,
434
+ currentSlot,
435
+ );
436
+
437
+ const collateralAssetContent: CollateralAssetContent = {
438
+ iasset: iassetName,
439
+ collateralAsset: collateralAsset.collateralAsset,
440
+ extraDecimals: collateralAsset.extraDecimals,
441
+ priceInfo: priceInfo.info,
442
+ interestOracleNft: interestOracleNft,
443
+ redemptionRatio: collateralAsset.redemptionRatio,
444
+ maintenanceRatio: collateralAsset.maintenanceRatio,
445
+ liquidationRatio: collateralAsset.liquidationRatio,
446
+ minCollateralAmt: collateralAsset.minCollateralAmt,
447
+ firstCollateralAsset: collateralAsset.firstCollateralAsset,
448
+ nextCollateralAsset:
449
+ collateralAsset.nextCollateralAsset != null
450
+ ? collateralAsset.nextCollateralAsset
451
+ : null,
452
+ };
453
+
454
+ const collateralAssetTx = lucid.newTx().pay.ToContract(
455
+ createScriptAddress(
456
+ lucid.config().network!,
457
+ validatorToScriptHash(mkIAssetValidatorFromSP(iassetParams)),
458
+ ),
459
+ {
460
+ kind: 'inline',
461
+ value: serialiseIAssetDatum({
462
+ CollateralAssetContent: collateralAssetContent,
463
+ }),
464
+ },
465
+ mkAssetsOf(collateralAssetAuthToken, 1n),
466
+ );
467
+ await submitTx(lucid, collateralAssetTx);
468
+
469
+ collateralAssetInfos.push({
470
+ collateralAsset: collateralAsset.collateralAsset,
471
+ interestOracleNft: interestOracleNft,
472
+ interestOracleParams: interestOracleParams,
473
+ oracleParams: priceInfo.params,
474
+ });
475
+ }
476
+
477
+ for (const stableswapPool of asset.stablepools) {
478
+ const stableswapPoolDatum: StableswapPoolContent = {
479
+ collateralAsset: stableswapPool.collateralAsset,
480
+ collateralToIassetRatio: stableswapPool.collateralToIassetRatio,
481
+ mintingFeeRatio: stableswapPool.mintingFeeRatio,
482
+ redemptionFeeRatio: stableswapPool.redemptionFeeRatio,
483
+ mintingEnabled: stableswapPool.mintingEnabled,
484
+ redemptionEnabled: stableswapPool.redemptionEnabled,
485
+ feeManager: stableswapPool.feeManager
486
+ ? fromHex(stableswapPool.feeManager)
487
+ : null,
488
+ minMintOrderAmount: stableswapPool.minMintOrderAmount,
489
+ minRedemptionOrderAmount: stableswapPool.minRedemptionOrderAmount,
490
+ iasset: iassetName,
491
+ stableswapValHash: fromHex(stableswapValidatorHash),
492
+ };
493
+
494
+ await submitTx(
495
+ lucid,
496
+ lucid.newTx().pay.ToContract(
497
+ createScriptAddress(
498
+ lucid.config().network!,
499
+ validatorToScriptHash(mkCdpValidatorFromSP(cdpParams)),
500
+ ),
501
+ {
502
+ kind: 'inline',
503
+ value: serialiseStableswapPoolDatum(stableswapPoolDatum),
504
+ },
505
+ mkAssetsOf(cdpAuthToken, 1n),
506
+ ),
507
+ );
508
+ }
509
+
510
+ const iassetDatum: IAssetContent = {
511
+ assetName: iassetName,
512
+ collateralAssetsCount: BigInt(asset.collateralAssets.length),
513
+ debtMintingFeeRatio: asset.debtMintingFeeRatio,
514
+ liquidationProcessingFeeRatio: asset.liquidationProcessingFeeRatio,
515
+ stabilityPoolWithdrawalFeeRatio: asset.stabilityPoolWithdrawalFeeRatio,
516
+ redemptionReimbursementRatio: asset.redemptionReimbursementRatio,
517
+ redemptionProcessingFeeRatio: asset.redemptionProcessingFeeRatio,
518
+ firstIAsset: asset.firstAsset,
519
+ nextIAsset: asset.nextAsset ? fromHex(fromText(asset.nextAsset)) : null,
520
+ };
521
+
522
+ const assetTx = lucid.newTx().pay.ToContract(
523
+ createScriptAddress(
524
+ lucid.config().network!,
525
+ validatorToScriptHash(mkIAssetValidatorFromSP(iassetParams)),
526
+ ),
527
+ {
528
+ kind: 'inline',
529
+ value: serialiseIAssetDatum({ IAssetContent: iassetDatum }),
530
+ },
531
+ mkAssetsOf(iassetToken, 1n),
532
+ );
533
+ await submitTx(lucid, assetTx);
534
+
535
+ const stabilityPoolDatum: StabilityPoolContent = {
536
+ iasset: fromHex(fromText(asset.name)),
537
+ state: initSpState,
538
+ assetStates: [],
539
+ };
540
+
541
+ const spTx = lucid.newTx().pay.ToContract(
542
+ credentialToAddress(lucid.config().network!, {
543
+ hash: validatorToScriptHash(
544
+ mkStabilityPoolValidatorFromSP(stabilityPoolParams),
545
+ ),
546
+ type: 'Script',
547
+ }),
548
+ {
549
+ kind: 'inline',
550
+ value: serialiseStabilityPoolDatum({ StabilityPool: stabilityPoolDatum }),
551
+ },
552
+ mkAssetsOf(stabilityPoolToken, 1n),
553
+ );
554
+ await submitTx(lucid, spTx);
555
+
556
+ return {
557
+ iassetTokenNameAscii: asset.name,
558
+ collateralAssets: collateralAssetInfos,
559
+ };
560
+ }
561
+
562
+ export async function initGovernance(
563
+ lucid: LucidEvolution,
564
+ governanceParams: GovParamsSP,
565
+ govToken: AssetClass,
566
+ initialAssets: InitialAssetParam[],
567
+ protocolParams: ProtocolParams,
568
+ ): Promise<void> {
569
+ const datum: GovDatum = {
570
+ currentProposal: 0n,
571
+ currentVersion: 0n,
572
+ protocolParams: protocolParams,
573
+ activeProposals: 0n,
574
+ iassetsCount: BigInt(initialAssets.length),
575
+ };
576
+ const tx = lucid.newTx().pay.ToContract(
577
+ credentialToAddress(lucid.config().network!, {
578
+ hash: validatorToScriptHash(mkGovValidatorFromSP(governanceParams)),
579
+ type: 'Script',
580
+ }),
581
+ { kind: 'inline', value: serialiseGovDatum(datum) },
582
+ mkAssetsOf(govToken, 1n),
583
+ );
584
+
585
+ await submitTx(lucid, tx);
586
+ }
587
+
588
+ export async function mintAuthTokenDirect(
589
+ lucid: LucidEvolution,
590
+ asset: AssetClass,
591
+ tokenName: string,
592
+ amount: bigint,
593
+ ): Promise<void> {
594
+ const script = mkAuthTokenPolicy(asset, fromText(tokenName));
595
+ const policyId = mintingPolicyToId(script);
596
+ const address = await lucid.wallet().address();
597
+
598
+ // Only select utxos that contain exactly 1 asset of the given asset class and sort by lovelaces amount in descending order
599
+ const utxos = (await lucid.utxosAtWithUnit(address, assetClassToUnit(asset)))
600
+ .filter((utxo) => assetClassValueOf(utxo.assets, asset) === 1n)
601
+ .sort((a, b) => Number(lovelacesAmt(b.assets) - lovelacesAmt(a.assets)));
602
+ if (utxos.length === 0) {
603
+ throw new Error('No utxos found');
604
+ }
605
+
606
+ const utxo = utxos[0];
607
+
608
+ const tx = lucid
609
+ .newTx()
610
+ .attach.MintingPolicy(script)
611
+ .collectFrom([utxo])
612
+ .mintAssets(
613
+ {
614
+ [policyId + fromText(tokenName)]: amount,
615
+ },
616
+ Data.void(),
617
+ );
618
+
619
+ await submitTx(lucid, tx);
45
620
  }