@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,10 +1,130 @@
1
- import { Credential, fromText, OutRef, toText } from '@lucid-evolution/lucid';
2
- import { AssetClass, CurrencySymbol, TokenName } from './generic';
1
+ import { AssetClass } from '@3rd-eye-labs/cardano-offchain-common';
2
+ import {
3
+ Credential,
4
+ fromHex,
5
+ fromText,
6
+ OutRef,
7
+ toHex,
8
+ toText,
9
+ } from '@lucid-evolution/lucid';
10
+ import { AssetClassD, StakeCredential } from './generic';
11
+ import {
12
+ DerivedPythPrice,
13
+ PythFeedParams,
14
+ toDataDerivedPythPrice,
15
+ } from '../contracts/pyth-feed/types';
16
+ import { match } from 'ts-pattern';
17
+ import { P } from 'ts-pattern';
18
+
19
+ export interface CurrencySymbolSP {
20
+ unCurrencySymbol: string;
21
+ }
22
+
23
+ export interface TokenNameSP {
24
+ unTokenName: string;
25
+ }
3
26
 
4
27
  /**
5
28
  * AssetClassSP used in System Params
6
29
  */
7
- export type AssetClassSP = [CurrencySymbol, TokenName];
30
+ export type AssetClassSP = [CurrencySymbolSP, TokenNameSP];
31
+
32
+ export type PythPriceConfigurationSP = {
33
+ priceFeedId: number;
34
+ };
35
+
36
+ export type DerivedPythPriceSP =
37
+ | {
38
+ tag: 'value';
39
+ val: PythPriceConfigurationSP;
40
+ }
41
+ | {
42
+ tag: 'inverse';
43
+ val: DerivedPythPriceSP;
44
+ }
45
+ | {
46
+ tag: 'divide';
47
+ val: [DerivedPythPriceSP, DerivedPythPriceSP];
48
+ };
49
+
50
+ export type PythFeedParamsSP = {
51
+ config: DerivedPythPriceSP;
52
+ pythStatePolicyId: CurrencySymbolSP;
53
+ };
54
+
55
+ export function fromSysParamsDerivedPythPrice(
56
+ cfg: DerivedPythPriceSP,
57
+ ): DerivedPythPrice {
58
+ return match(cfg)
59
+ .returnType<DerivedPythPrice>()
60
+ .with({ tag: 'value', val: P.select() }, (val) => {
61
+ return {
62
+ Value: {
63
+ configuration: {
64
+ priceFeedId: BigInt(val.priceFeedId),
65
+ },
66
+ },
67
+ };
68
+ })
69
+ .with({ tag: 'inverse', val: P.select() }, (val) => {
70
+ return {
71
+ Inverse: {
72
+ value: toDataDerivedPythPrice(fromSysParamsDerivedPythPrice(val)),
73
+ },
74
+ };
75
+ })
76
+ .with({ tag: 'divide', val: P.select() }, (val) => {
77
+ return {
78
+ Divide: {
79
+ x: toDataDerivedPythPrice(fromSysParamsDerivedPythPrice(val[0])),
80
+ y: toDataDerivedPythPrice(fromSysParamsDerivedPythPrice(val[1])),
81
+ },
82
+ };
83
+ })
84
+ .exhaustive();
85
+ }
86
+
87
+ export function fromSysParamsPythFeedParams(
88
+ params: PythFeedParamsSP,
89
+ ): PythFeedParams {
90
+ return {
91
+ config: fromSysParamsDerivedPythPrice(params.config),
92
+ pythStatePolicyId: fromHex(params.pythStatePolicyId.unCurrencySymbol),
93
+ };
94
+ }
95
+
96
+ export type PythFeedConfig = {
97
+ params: PythFeedParamsSP;
98
+ pythFeedValHash: string;
99
+ pythFeedValScriptRef: ScriptReference;
100
+ };
101
+
102
+ export type PythConfig = {
103
+ pythStateAssetClass: AssetClassSP;
104
+ /**
105
+ * Key is in the format: "iAsset.ascii/Collateral.currency.hex.Collateral.tokenName.hex".
106
+ * Use the `getPythFeedConfig` helper to querying.
107
+ */
108
+ pythFeeds: {
109
+ [key: string]: PythFeedConfig;
110
+ };
111
+ };
112
+
113
+ export function getPythFeedConfig(
114
+ cfg: PythConfig,
115
+ iasset: Uint8Array<ArrayBufferLike>,
116
+ collateralAsset: AssetClass,
117
+ ): PythFeedConfig {
118
+ const res =
119
+ cfg.pythFeeds[
120
+ `${toText(toHex(iasset))}/${toHex(collateralAsset.currencySymbol)}.${toHex(collateralAsset.tokenName)}`
121
+ ];
122
+
123
+ if (res == null)
124
+ throw new Error('No Pyth config known for such assets combination');
125
+
126
+ return res;
127
+ }
8
128
 
9
129
  export interface SystemParams {
10
130
  versionRecordParams: VersionRecordParams;
@@ -13,31 +133,38 @@ export interface SystemParams {
13
133
  startTime: StartTime;
14
134
  stakingParams: StakingParamsSP;
15
135
  stabilityPoolParams: StabilityPoolParamsSP;
136
+ stableswapParams: StableswapParamsSP;
16
137
  scriptReferences: ScriptReferences;
17
138
  pollShardParams: PollShardParamsSP;
18
139
  pollManagerParams: PollManagerParamsSP;
19
- lrpParams: LrpParamsSP;
140
+ robParams: RobParamsSP;
141
+ interestCollectionParams: InterestCollectionParamsSP;
20
142
  indyToken: AssetClassSP;
21
143
  govParams: GovParamsSP;
22
144
  executeParams: ExecuteParamsSP;
23
- distributionParams: DistributionParams;
24
145
  collectorParams: CollectorParamsSP;
25
146
  cdpParams: CdpParamsSP;
26
147
  cdpCreatorParams: CDPCreatorParamsSP;
148
+ cdpRedeemParams: CdpRedeemParamsSP;
149
+ iassetParams: IAssetParamsSP;
150
+ pythConfig: PythConfig;
27
151
  }
28
152
  export type ValidatorHashes = {
29
153
  versionRegistryHash: string;
30
154
  treasuryHash: string;
155
+ stableswapHash: string;
31
156
  stakingHash: string;
32
157
  stabilityPoolHash: string;
33
158
  pollShardHash: string;
34
159
  pollManagerHash: string;
35
- lrpHash: string;
160
+ robHash: string;
161
+ interestCollectionHash: string;
36
162
  govHash: string;
37
163
  executeHash: string;
38
164
  collectorHash: string;
39
165
  cdpHash: string;
40
166
  cdpCreatorHash: string;
167
+ iassetHash: string;
41
168
  };
42
169
  export interface AddressCredential {
43
170
  tag: string;
@@ -60,7 +187,7 @@ export interface VersionRecordParams {
60
187
  export interface TreasuryParamsSP {
61
188
  upgradeToken: AssetClassSP;
62
189
  versionRecordToken: AssetClassSP;
63
- treasuryUtxosStakeCredential?: ScriptCredential;
190
+ treasuryUtxosStakeCredential: ScriptCredential | undefined;
64
191
  }
65
192
  export interface StartTime {
66
193
  slot: number;
@@ -78,32 +205,43 @@ export interface StabilityPoolParamsSP {
78
205
  versionRecordToken: AssetClassSP;
79
206
  stabilityPoolToken: AssetClassSP;
80
207
  snapshotEpochToScaleToSumToken: AssetClassSP;
81
- requestCollateralLovelaces: number;
82
208
  iAssetAuthToken: AssetClassSP;
83
- govNFT: AssetClassSP;
84
- collectorValHash: string;
209
+ iassetValHash: string;
85
210
  cdpToken: AssetClassSP;
86
- assetSymbol: CurrencySymbol;
211
+ assetSymbol: CurrencySymbolSP;
87
212
  accountToken: AssetClassSP;
88
213
  accountCreateFeeLovelaces: number;
89
- accountAdjustmentFeeLovelaces: number;
214
+ accountProcessingCooldownMs: number;
215
+ accountProcessingBiasMs: number;
216
+ stakeCredential: ScriptCredential | undefined;
217
+ }
218
+
219
+ export interface StableswapParamsSP {
220
+ iassetSymbol: CurrencySymbolSP;
221
+ cdpToken: AssetClassSP;
222
+ versionRecordToken: AssetClassSP;
223
+ cdpValHash: string;
224
+ treasuryValHash: string;
90
225
  }
91
226
 
92
227
  export interface ScriptReferences {
93
228
  versionRegistryValidatorRef: ScriptReference;
94
- versionRecordTokenPolicyRef: ScriptReference;
95
229
  treasuryValidatorRef: ScriptReference;
96
230
  stakingValidatorRef: ScriptReference;
97
231
  stabilityPoolValidatorRef: ScriptReference;
232
+ stableswapValidatorRef: ScriptReference;
98
233
  pollShardValidatorRef: ScriptReference;
99
234
  pollManagerValidatorRef: ScriptReference;
100
- lrpValidatorRef: ScriptReference;
235
+ robValidatorRef: ScriptReference;
236
+ interestCollectionValidatorRef: ScriptReference;
101
237
  iAssetTokenPolicyRef: ScriptReference;
102
238
  governanceValidatorRef: ScriptReference;
103
239
  executeValidatorRef: ScriptReference;
104
240
  collectorValidatorRef: ScriptReference;
105
241
  cdpValidatorRef: ScriptReference;
106
242
  cdpCreatorValidatorRef: ScriptReference;
243
+ cdpRedeemValidatorRef: ScriptReference;
244
+ iassetValidatorRef: ScriptReference;
107
245
  authTokenPolicies: AuthTokenPolicies;
108
246
  }
109
247
  export interface Output {
@@ -125,7 +263,9 @@ export interface AddressCredentialOrDatum {
125
263
  contents: string;
126
264
  }
127
265
  export interface Amount {
128
- getValue?: ((CurrencySymbol | (number[] | null)[] | null)[] | null)[] | null;
266
+ getValue?:
267
+ | ((CurrencySymbolSP | (number[] | null)[] | null)[] | null)[]
268
+ | null;
129
269
  }
130
270
  export interface AddressSP {
131
271
  addressStakingCredential?: null;
@@ -150,15 +290,29 @@ export interface AuthTokenPolicies {
150
290
  pollManagerTokenRef: ScriptReference;
151
291
  iAssetAuthTokenRef: ScriptReference;
152
292
  iAssetTokenRef: ScriptReference;
293
+ collateralAssetTokenRef: ScriptReference;
153
294
  cdpAuthTokenRef: ScriptReference;
154
295
  accountTokenRef: ScriptReference;
296
+ versionRecordTokenPolicyRef: ScriptReference;
155
297
  }
156
298
 
157
- export interface LrpParamsSP {
299
+ export interface RobParamsSP {
158
300
  versionRecordToken: AssetClassSP;
159
- iassetNft: AssetClassSP;
160
- iassetPolicyId: CurrencySymbol;
161
- minRedemptionLovelacesAmt: bigint;
301
+ iassetAuthToken: AssetClassSP;
302
+ collateralAssetAuthToken: AssetClassSP;
303
+ iassetValHash: string;
304
+ iassetPolicyId: CurrencySymbolSP;
305
+ }
306
+
307
+ export interface InterestCollectionParamsSP {
308
+ versionRecordNft: AssetClassSP;
309
+ multisigUtxoNft: AssetClassSP;
310
+ cdpAuthTk: AssetClassSP;
311
+ collateralAssetAuthTk: AssetClassSP;
312
+ govAuthTk: AssetClassSP;
313
+ cdpAssetSymbol: CurrencySymbolSP;
314
+ cdpBiasTime: bigint;
315
+ interestSettlementCooldown: bigint;
162
316
  }
163
317
 
164
318
  export interface PollShardParamsSP {
@@ -174,7 +328,6 @@ export interface PollManagerParamsSP {
174
328
  shardsValHash: string;
175
329
  pollToken: AssetClassSP;
176
330
  pBiasTime: bigint;
177
- initialIndyDistribution: bigint;
178
331
  indyAsset: AssetClassSP;
179
332
  govNFT: AssetClassSP;
180
333
  govExecuteValHash: string;
@@ -185,71 +338,97 @@ export interface GovParamsSP {
185
338
  upgradeToken: AssetClassSP;
186
339
  pollToken: AssetClassSP;
187
340
  pollManagerValHash: string;
341
+ iassetValHash: string;
188
342
  indyAsset: AssetClassSP;
189
343
  iAssetAuthToken: AssetClassSP;
190
344
  govNFT: AssetClassSP;
191
345
  gBiasTime: bigint;
192
346
  daoIdentityToken: AssetClassSP;
347
+ iassetSymbol: CurrencySymbolSP;
193
348
  }
194
349
 
195
350
  export interface ExecuteParamsSP {
196
351
  govNFT: AssetClassSP;
197
352
  upgradeToken: AssetClassSP;
198
353
  iAssetToken: AssetClassSP;
354
+ collateralAssetToken: AssetClassSP;
199
355
  stabilityPoolToken: AssetClassSP;
356
+ cdpCreatorToken: AssetClassSP;
357
+ cdpToken: AssetClassSP;
200
358
  versionRecordToken: AssetClassSP;
359
+ iassetValHash: string;
201
360
  cdpValHash: string;
202
361
  sPoolValHash: string;
203
362
  versionRegistryValHash: string;
204
363
  treasuryValHash: string;
205
- indyAsset: AssetClassSP;
206
- }
207
- export interface DistributionParams {
208
- treasuryIndyAmount: number;
209
- totalINDYSupply: number;
210
- initialIndyDistribution: number;
211
364
  }
212
365
  export interface CollectorParamsSP {
213
366
  versionRecordToken: AssetClassSP;
214
367
  stakingToken: AssetClassSP;
215
368
  stakingManagerNFT: AssetClassSP;
216
369
  }
370
+ export interface CdpRedeemParamsSP {
371
+ treasuryValHash: string;
372
+ iassetValHash: string;
373
+ partialRedemptionExtraFeeLovelace: number;
374
+ iAssetAuthToken: AssetClassSP;
375
+ collateralAssetAuthToken: AssetClassSP;
376
+ govNFT: AssetClassSP;
377
+ interestCollectorValHash: string;
378
+ cdpAuthToken: AssetClassSP;
379
+ cdpAssetSymbol: CurrencySymbolSP;
380
+ biasTime: bigint;
381
+ }
217
382
  export interface CdpParamsSP {
218
- versionRecordToken: AssetClassSP;
219
383
  upgradeToken: AssetClassSP;
384
+ versionRecordToken: AssetClassSP;
220
385
  treasuryValHash: string;
221
386
  stabilityPoolAuthToken: AssetClassSP;
222
387
  spValHash: string;
223
- partialRedemptionExtraFeeLovelace: number;
224
- minCollateralInLovelace: number;
388
+ iassetValHash: string;
225
389
  iAssetAuthToken: AssetClassSP;
226
- govNFT: AssetClassSP;
227
- collectorValHash: string;
390
+ collateralAssetAuthToken: AssetClassSP;
391
+ interestCollectorValHash: string;
392
+ cdpRedeemValHash: string;
228
393
  cdpAuthToken: AssetClassSP;
229
- cdpAssetSymbol: CurrencySymbol;
230
- biasTime: number;
394
+ cdpAssetSymbol: CurrencySymbolSP;
395
+ biasTime: bigint;
396
+ }
397
+
398
+ export interface IAssetParamsSP {
399
+ upgradeToken: AssetClassSP;
400
+ versionRecordToken: AssetClassSP;
231
401
  }
232
402
 
233
403
  export interface CDPCreatorParamsSP {
234
404
  versionRecordToken: AssetClassSP;
235
- minCollateralInLovelace: number;
405
+ upgradeToken: AssetClassSP;
236
406
  iAssetAuthTk: AssetClassSP;
237
- collectorValHash: string;
407
+ collateralAssetAuthTk: AssetClassSP;
408
+ treasuryValHash: string;
238
409
  cdpScriptHash: string;
410
+ iassetValHash: string;
239
411
  cdpCreatorNft: AssetClassSP;
240
412
  cdpAuthTk: AssetClassSP;
241
- cdpAssetCs: CurrencySymbol;
413
+ cdpAssetCs: CurrencySymbolSP;
242
414
  biasTime: bigint;
243
415
  }
244
416
 
245
417
  export function toSystemParamsAsset(asset: AssetClass): AssetClassSP {
246
418
  return [
247
- { unCurrencySymbol: asset.currencySymbol },
248
- { unTokenName: toText(asset.tokenName) },
419
+ { unCurrencySymbol: toHex(asset.currencySymbol) },
420
+ { unTokenName: toText(toHex(asset.tokenName)) },
249
421
  ];
250
422
  }
251
423
 
252
424
  export function fromSystemParamsAsset(asset: AssetClassSP): AssetClass {
425
+ return {
426
+ currencySymbol: fromHex(asset[0].unCurrencySymbol),
427
+ tokenName: fromHex(fromText(asset[1].unTokenName)),
428
+ };
429
+ }
430
+
431
+ export function fromSystemParamsAssetLucid(asset: AssetClassSP): AssetClassD {
253
432
  return {
254
433
  currencySymbol: asset[0].unCurrencySymbol,
255
434
  tokenName: fromText(asset[1].unTokenName),
@@ -260,8 +439,25 @@ export function fromSystemParamsScriptRef(ref: ScriptReference): OutRef {
260
439
  return { outputIndex: ref.input.index, txHash: ref.input.transactionId };
261
440
  }
262
441
 
263
- export function fromSysParamsScriptCredential(
264
- cred: ScriptCredential,
265
- ): Credential {
442
+ export function fromSysParamsCredential(cred: ScriptCredential): Credential {
443
+ if (cred.contents.tag === 'PublicKeyCredential') {
444
+ return { type: 'Key', hash: cred.contents.contents };
445
+ }
266
446
  return { type: 'Script', hash: cred.contents.contents };
267
447
  }
448
+
449
+ export function fromSysParamsStakeCredential(
450
+ cred: ScriptCredential,
451
+ ): StakeCredential {
452
+ return {
453
+ Inline: [
454
+ cred.contents.tag === 'PublicKeyCredential'
455
+ ? {
456
+ PublicKeyCredential: [cred.contents.contents],
457
+ }
458
+ : {
459
+ ScriptCredential: [cred.contents.contents],
460
+ },
461
+ ],
462
+ };
463
+ }
@@ -1,4 +1,11 @@
1
- import { array as A, ord as Ord } from 'fp-ts';
1
+ import {
2
+ array as A,
3
+ ord as Ord,
4
+ readonlyArray as RA,
5
+ option as O,
6
+ function as F,
7
+ } from 'fp-ts';
8
+ import { Predicate } from 'fp-ts/lib/Predicate';
2
9
 
3
10
  export function shuffle<T>(arr: T[]): T[] {
4
11
  const source = [...arr];
@@ -37,3 +44,65 @@ export function insertSorted<T>(arr: T[], item: T, ord: Ord.Ord<T>): T[] {
37
44
 
38
45
  return [...arr, item];
39
46
  }
47
+
48
+ /**
49
+ * Replace when existent, otherwise add at the end.
50
+ */
51
+ export const repsertReadonlyArr =
52
+ <K, V>(predicate: Predicate<K>, val: V, mkKey: () => K) =>
53
+ (arr: ReadonlyArray<readonly [K, V]>): ReadonlyArray<readonly [K, V]> => {
54
+ const idx = F.pipe(
55
+ arr,
56
+ RA.findIndex(([key, _]) => predicate(key)),
57
+ );
58
+
59
+ const res = F.pipe(
60
+ idx,
61
+ O.chain((i) =>
62
+ F.pipe(
63
+ arr,
64
+ RA.modifyAt(i, ([key, _]) => [key, val]),
65
+ ),
66
+ ),
67
+ );
68
+
69
+ return F.pipe(
70
+ res,
71
+ O.getOrElse<ReadonlyArray<readonly [K, V]>>(() => [
72
+ ...arr,
73
+ [mkKey(), val],
74
+ ]),
75
+ );
76
+ };
77
+
78
+ export const repsertWithReadonlyArr =
79
+ <K, V>(
80
+ predicate: Predicate<K>,
81
+ replaceVal: (oldVal: V) => V,
82
+ newVal: () => V,
83
+ mkKey: () => K,
84
+ ) =>
85
+ (arr: ReadonlyArray<readonly [K, V]>): ReadonlyArray<readonly [K, V]> => {
86
+ const idx = F.pipe(
87
+ arr,
88
+ RA.findIndex(([key, _]) => predicate(key)),
89
+ );
90
+
91
+ const res = F.pipe(
92
+ idx,
93
+ O.chain((i) =>
94
+ F.pipe(
95
+ arr,
96
+ RA.modifyAt(i, ([key, oldVal]) => [key, replaceVal(oldVal)]),
97
+ ),
98
+ ),
99
+ );
100
+
101
+ return F.pipe(
102
+ res,
103
+ O.getOrElse<ReadonlyArray<readonly [K, V]>>(() => [
104
+ ...arr,
105
+ [mkKey(), newVal()],
106
+ ]),
107
+ );
108
+ };
@@ -1,6 +1,14 @@
1
1
  import Decimal from 'decimal.js';
2
2
  import { array as A, ord as Ord } from 'fp-ts';
3
3
 
4
+ /**
5
+ * Plutus division behaves differently when working with negative numbers vs positive ones.
6
+ */
7
+ export function divideOnChainCompatible(a: bigint, b: bigint): bigint {
8
+ const res = a / b;
9
+ return res + (res < 0n ? -1n : 0n);
10
+ }
11
+
4
12
  export function bigintMax(a: bigint, b: bigint): bigint {
5
13
  return a > b ? a : b;
6
14
  }
@@ -17,6 +25,10 @@ export function fromDecimal(val: Decimal): bigint {
17
25
  return BigInt(val.toString());
18
26
  }
19
27
 
28
+ export function zeroNegatives(a: bigint): bigint {
29
+ return bigintMax(0n, a);
30
+ }
31
+
20
32
  export const BigIntOrd: Ord.Ord<bigint> = {
21
33
  equals: (x, y) => x === y,
22
34
  compare: (first, second) => (first < second ? -1 : first > second ? 1 : 0),
@@ -1,19 +1,17 @@
1
1
  import {
2
- OCD_DECIMAL_UNIT,
3
- ocdDiv,
4
- ocdMul,
5
- OnChainDecimal,
6
- } from '../types/on-chain-decimal';
2
+ Rational,
3
+ rationalFloor,
4
+ rationalFromInt,
5
+ rationalMul,
6
+ } from '../types/rational';
7
7
 
8
- export function calculateFeeFromPercentage(
9
- feePercentage: OnChainDecimal,
8
+ export function calculateFeeFromRatio(
9
+ feeRatio: Rational,
10
10
  amount: bigint,
11
11
  ): bigint {
12
12
  if (amount < 0) {
13
13
  return 0n;
14
14
  }
15
15
 
16
- return ocdDiv(ocdMul({ getOnChainInt: amount }, feePercentage), {
17
- getOnChainInt: 100n * OCD_DECIMAL_UNIT,
18
- }).getOnChainInt;
16
+ return rationalFloor(rationalMul(rationalFromInt(amount), feeRatio));
19
17
  }
@@ -2,53 +2,26 @@ import {
2
2
  addAssets,
3
3
  Address,
4
4
  Assets,
5
+ calculateMinLovelaceFromUTxO,
5
6
  Credential,
6
7
  credentialToAddress,
8
+ Data,
7
9
  Datum,
8
10
  LucidEvolution,
9
11
  Network,
12
+ OutputDatum,
10
13
  OutRef,
11
14
  paymentCredentialOf,
15
+ ProtocolParameters,
16
+ Script,
12
17
  ScriptHash,
13
18
  scriptHashToCredential,
14
- SLOT_CONFIG_NETWORK,
15
19
  stakeCredentialOf,
20
+ TxBuilder,
16
21
  UTxO,
17
22
  } from '@lucid-evolution/lucid';
18
23
  import { ScriptReference } from '../types/system-params';
19
- import { matchSingle } from './utils';
20
-
21
- /**
22
- * Union type accepting either a full UTxO or just an OutRef.
23
- * When a full UTxO is provided, no network fetch is needed.
24
- * When only an OutRef is provided, the UTxO will be fetched via utxosByOutRef.
25
- */
26
- export type UtxoOrOutRef = UTxO | OutRef;
27
-
28
- /**
29
- * Resolves a UTxOOrOutRef to a full UTxO.
30
- * If the input is already a UTxO (has 'address' property), returns it directly.
31
- * If the input is an OutRef, fetches the UTxO from the network.
32
- *
33
- * @param input - Either a full UTxO or an OutRef
34
- * @param lucid - The LucidEvolution instance for network queries
35
- * @param errorMsg - Custom error message if the UTxO cannot be found
36
- * @returns The resolved UTxO
37
- */
38
- export async function resolveUtxo(
39
- input: UtxoOrOutRef,
40
- lucid: LucidEvolution,
41
- errorMsg: string = 'Expected a single UTXO',
42
- ): Promise<UTxO> {
43
- // UTxO has 'address' property, OutRef only has 'txHash' and 'outputIndex'
44
- if ('address' in input) {
45
- return input;
46
- }
47
- return matchSingle(
48
- await lucid.utxosByOutRef([input]),
49
- (_) => new Error(errorMsg),
50
- );
51
- }
24
+ import { match, P } from 'ts-pattern';
52
25
 
53
26
  /**
54
27
  * Returns the inline datum.
@@ -73,12 +46,14 @@ export async function addrDetails(
73
46
  lucid: LucidEvolution,
74
47
  ): Promise<[Credential, Credential | undefined]> {
75
48
  const addr = await lucid.wallet().address();
49
+
76
50
  let stakeCredential = undefined;
77
51
  try {
78
52
  stakeCredential = stakeCredentialOf(addr);
79
53
  } catch (_) {
80
54
  // No stake credential
81
55
  }
56
+
82
57
  return [paymentCredentialOf(addr), stakeCredential];
83
58
  }
84
59
 
@@ -105,14 +80,46 @@ export async function scriptRef(
105
80
  return utxos[0];
106
81
  }
107
82
 
83
+ /**
84
+ * Complete, sign with wallet, submit a tx and wait for confirmation.
85
+ */
86
+ export async function submitTx(
87
+ lucid: LucidEvolution,
88
+ tx: TxBuilder,
89
+ ): Promise<void> {
90
+ const txHash = await tx
91
+ .complete()
92
+ .then((t) => t.sign.withWallet().complete())
93
+ .then((t) => t.submit());
94
+ await lucid.awaitTx(txHash);
95
+ }
96
+
108
97
  export function balance(utxos: UTxO[]): Assets {
109
98
  return utxos.reduce((acc, utxo) => addAssets(acc, utxo.assets), {});
110
99
  }
111
100
 
112
- export function updateSlotConfigNetwork(
113
- slotConfigNetwork: typeof SLOT_CONFIG_NETWORK,
114
- ) {
115
- for (const [key, value] of Object.entries(slotConfigNetwork)) {
116
- SLOT_CONFIG_NETWORK[key as keyof typeof SLOT_CONFIG_NETWORK] = value;
117
- }
101
+ /**
102
+ * Estimate the min lovelace for a UTXO so it can be created.
103
+ */
104
+ export function estimateUtxoMinLovelace(
105
+ protocolParameters: ProtocolParameters,
106
+ destinationAddr: string,
107
+ assets: Assets,
108
+ destinationDatum: OutputDatum | undefined = undefined,
109
+ scriptRef: Script | undefined = undefined,
110
+ ): bigint {
111
+ return calculateMinLovelaceFromUTxO(protocolParameters.coinsPerUtxoByte, {
112
+ address: destinationAddr,
113
+ datumHash: match(destinationDatum)
114
+ .with({ kind: 'hash', value: P.select() }, (dHash) => dHash)
115
+ .otherwise(() => null),
116
+ datum: match(destinationDatum)
117
+ .with({ kind: 'inline', value: P.select() }, (data) => Data.to(data))
118
+ .otherwise(() => null),
119
+ assets: assets,
120
+ // Use dummy tx hash and out idx
121
+ txHash: '0000000000000000000000000000000000000000000000000000000000000000',
122
+ outputIndex: 0,
123
+ scriptRef: scriptRef ?? null,
124
+ } satisfies UTxO);
118
125
  }