@indigo-labs/indigo-sdk 0.2.42 → 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 -493
  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,27 +0,0 @@
1
- import { applyParamsToScript, SpendingValidator } from '@lucid-evolution/lucid';
2
- import { castLrpParams, LRPParams } from './types';
3
- import { fromSystemParamsAsset, LrpParamsSP } from '../../types/system-params';
4
- import { _lrpValidator } from '../../validators/lrp-validator';
5
-
6
- export const mkLrpValidator = (params: LRPParams): SpendingValidator => {
7
- return {
8
- type: 'PlutusV2',
9
- script: applyParamsToScript(_lrpValidator.cborHex, [castLrpParams(params)]),
10
- };
11
- };
12
-
13
- export const mkLrpValidatorFromSP = (
14
- params: LrpParamsSP,
15
- ): SpendingValidator => {
16
- return {
17
- type: 'PlutusV2',
18
- script: applyParamsToScript(_lrpValidator.cborHex, [
19
- castLrpParams({
20
- versionRecordToken: fromSystemParamsAsset(params.versionRecordToken),
21
- iassetNft: fromSystemParamsAsset(params.iassetNft),
22
- minRedemptionLovelacesAmt: BigInt(params.minRedemptionLovelacesAmt),
23
- iassetPolicyId: params.iassetPolicyId.unCurrencySymbol,
24
- }),
25
- ]),
26
- };
27
- };
@@ -1,250 +0,0 @@
1
- import {
2
- LucidEvolution,
3
- TxBuilder,
4
- Credential,
5
- OutRef,
6
- addAssets,
7
- unixTimeToSlot,
8
- slotToUnixTime,
9
- } from '@lucid-evolution/lucid';
10
- import {
11
- addrDetails,
12
- createScriptAddress,
13
- getInlineDatumOrThrow,
14
- resolveUtxo,
15
- UtxoOrOutRef,
16
- } from '../../utils/lucid-utils';
17
- import { unzip, zip } from 'fp-ts/lib/Array';
18
- import {
19
- LRPDatum,
20
- parseLrpDatumOrThrow,
21
- serialiseLrpDatum,
22
- serialiseLrpRedeemer,
23
- } from './types';
24
- import { parsePriceOracleDatum } from '../price-oracle/types';
25
- import { OnChainDecimal } from '../../types/on-chain-decimal';
26
- import { parseIAssetDatumOrThrow } from '../cdp/types';
27
- import {
28
- assetClassValueOf,
29
- mkAssetsOf,
30
- mkLovelacesOf,
31
- } from '../../utils/value-helpers';
32
- import { matchSingle } from '../../utils/utils';
33
- import { AssetClass } from '../../types/generic';
34
- import {
35
- fromSystemParamsScriptRef,
36
- SystemParams,
37
- } from '../../types/system-params';
38
- import { buildRedemptionsTx, MIN_LRP_COLLATERAL_AMT } from './helpers';
39
-
40
- export async function openLrp(
41
- assetTokenName: string,
42
- lovelacesAmt: bigint,
43
- maxPrice: OnChainDecimal,
44
- lucid: LucidEvolution,
45
- sysParams: SystemParams,
46
- lrpStakeCredential?: Credential,
47
- ): Promise<TxBuilder> {
48
- const network = lucid.config().network!;
49
-
50
- const [ownPkh, _] = await addrDetails(lucid);
51
-
52
- const newDatum: LRPDatum = {
53
- owner: ownPkh.hash,
54
- iasset: assetTokenName,
55
- maxPrice: maxPrice,
56
- lovelacesToSpend: lovelacesAmt,
57
- };
58
-
59
- return lucid.newTx().pay.ToContract(
60
- createScriptAddress(
61
- network,
62
- sysParams.validatorHashes.lrpHash,
63
- lrpStakeCredential,
64
- ),
65
- {
66
- kind: 'inline',
67
- value: serialiseLrpDatum(newDatum),
68
- },
69
- { lovelace: lovelacesAmt + MIN_LRP_COLLATERAL_AMT },
70
- );
71
- }
72
-
73
- export async function cancelLrp(
74
- lrp: UtxoOrOutRef,
75
- sysParams: SystemParams,
76
- lucid: LucidEvolution,
77
- ): Promise<TxBuilder> {
78
- const lrpScriptRefUtxo = matchSingle(
79
- await lucid.utxosByOutRef([
80
- fromSystemParamsScriptRef(sysParams.scriptReferences.lrpValidatorRef),
81
- ]),
82
- (_) => new Error('Expected a single LRP Ref Script UTXO'),
83
- );
84
-
85
- const lrpUtxo = await resolveUtxo(lrp, lucid, 'Expected a single LRP UTXO.');
86
-
87
- const lrpDatum = parseLrpDatumOrThrow(getInlineDatumOrThrow(lrpUtxo));
88
-
89
- return lucid
90
- .newTx()
91
- .readFrom([lrpScriptRefUtxo])
92
- .collectFrom([lrpUtxo], serialiseLrpRedeemer('Cancel'))
93
- .addSignerKey(lrpDatum.owner);
94
- }
95
-
96
- export async function redeemLrp(
97
- /** The tuple represents the LRP outref and the amount of iAssets to redeem against it. */
98
- redemptionLrpsData: [OutRef, bigint][],
99
- priceOracle: UtxoOrOutRef,
100
- iasset: UtxoOrOutRef,
101
- lucid: LucidEvolution,
102
- sysParams: SystemParams,
103
- ): Promise<TxBuilder> {
104
- const network = lucid.config().network!;
105
-
106
- const lrpScriptRefUtxo = matchSingle(
107
- await lucid.utxosByOutRef([
108
- fromSystemParamsScriptRef(sysParams.scriptReferences.lrpValidatorRef),
109
- ]),
110
- (_) => new Error('Expected a single LRP Ref Script UTXO'),
111
- );
112
-
113
- const priceOracleUtxo = await resolveUtxo(
114
- priceOracle,
115
- lucid,
116
- 'Expected a single price oracle UTXO',
117
- );
118
-
119
- const iassetUtxo = await resolveUtxo(
120
- iasset,
121
- lucid,
122
- 'Expected a single IAsset UTXO',
123
- );
124
-
125
- const iassetDatum = parseIAssetDatumOrThrow(
126
- getInlineDatumOrThrow(iassetUtxo),
127
- );
128
-
129
- const [lrpsToRedeemOutRefs, lrpRedemptionIAssetAmt] =
130
- unzip(redemptionLrpsData);
131
-
132
- const priceOracleDatum = parsePriceOracleDatum(
133
- getInlineDatumOrThrow(priceOracleUtxo),
134
- );
135
-
136
- const redemptionLrps = await lucid
137
- .utxosByOutRef(lrpsToRedeemOutRefs)
138
- .then((val) => zip(val, lrpRedemptionIAssetAmt));
139
-
140
- const tx = buildRedemptionsTx(
141
- redemptionLrps,
142
- priceOracleDatum.price,
143
- iassetDatum.redemptionReimbursementPercentage,
144
- sysParams,
145
- lucid.newTx(),
146
- 0n,
147
- );
148
-
149
- return (
150
- lucid
151
- .newTx()
152
- .validTo(
153
- slotToUnixTime(
154
- network,
155
- unixTimeToSlot(network, Number(priceOracleDatum.expiration)) - 1,
156
- ),
157
- )
158
- // Ref script
159
- .readFrom([lrpScriptRefUtxo])
160
- // Ref inputs
161
- .readFrom([iassetUtxo, priceOracleUtxo])
162
- .compose(tx)
163
- );
164
- }
165
-
166
- /**
167
- * Create Tx adjusting the LRP and claiming the received iAssets
168
- */
169
- export async function adjustLrp(
170
- lucid: LucidEvolution,
171
- lrp: UtxoOrOutRef,
172
- /**
173
- * A positive amount increases the lovelaces in the LRP,
174
- * and a negative amount takes lovelaces from the LRP.
175
- */
176
- lovelacesAdjustAmt: bigint,
177
- newMaxPrice: OnChainDecimal | undefined,
178
- sysParams: SystemParams,
179
- ): Promise<TxBuilder> {
180
- const lrpScriptRefUtxo = matchSingle(
181
- await lucid.utxosByOutRef([
182
- fromSystemParamsScriptRef(sysParams.scriptReferences.lrpValidatorRef),
183
- ]),
184
- (_) => new Error('Expected a single LRP Ref Script UTXO'),
185
- );
186
-
187
- const lrpUtxo = await resolveUtxo(lrp, lucid, 'Expected a single LRP UTXO.');
188
-
189
- const lrpDatum = parseLrpDatumOrThrow(getInlineDatumOrThrow(lrpUtxo));
190
-
191
- const rewardAssetClass: AssetClass = {
192
- currencySymbol: sysParams.lrpParams.iassetPolicyId.unCurrencySymbol,
193
- tokenName: lrpDatum.iasset,
194
- };
195
- const rewardAssetsAmt = assetClassValueOf(lrpUtxo.assets, rewardAssetClass);
196
-
197
- // The claim case
198
- if (lovelacesAdjustAmt === 0n && lrpDatum.lovelacesToSpend === 0n) {
199
- throw new Error(
200
- "When there's no more lovelaces to spend, use close instead of claim.",
201
- );
202
- }
203
-
204
- // Negative adjust case
205
- if (
206
- lovelacesAdjustAmt < 0 &&
207
- lrpDatum.lovelacesToSpend <= lovelacesAdjustAmt
208
- ) {
209
- throw new Error(
210
- "Can't adjust negatively by more than available. Also, for adjusting by exactly the amount deposited, a close action should be used instead.",
211
- );
212
- }
213
-
214
- if (newMaxPrice && newMaxPrice.getOnChainInt < 0n) {
215
- throw new Error('Max price cannot be negative');
216
- }
217
-
218
- return lucid
219
- .newTx()
220
- .readFrom([lrpScriptRefUtxo])
221
- .collectFrom([lrpUtxo], serialiseLrpRedeemer('Cancel'))
222
- .pay.ToContract(
223
- lrpUtxo.address,
224
- {
225
- kind: 'inline',
226
- value: serialiseLrpDatum({
227
- ...lrpDatum,
228
- lovelacesToSpend: lrpDatum.lovelacesToSpend + lovelacesAdjustAmt,
229
- maxPrice: newMaxPrice ? newMaxPrice : lrpDatum.maxPrice,
230
- }),
231
- },
232
- addAssets(
233
- lrpUtxo.assets,
234
- mkAssetsOf(rewardAssetClass, -rewardAssetsAmt),
235
- mkLovelacesOf(lovelacesAdjustAmt),
236
- ),
237
- )
238
- .addSignerKey(lrpDatum.owner);
239
- }
240
-
241
- /**
242
- * Create Tx claiming the received iAssets.
243
- */
244
- export async function claimLrp(
245
- lucid: LucidEvolution,
246
- lrp: UtxoOrOutRef,
247
- sysParams: SystemParams,
248
- ): Promise<TxBuilder> {
249
- return adjustLrp(lucid, lrp, 0n, undefined, sysParams);
250
- }
@@ -1,131 +0,0 @@
1
- import { Data, Datum, Redeemer, UTxO } from '@lucid-evolution/lucid';
2
- import { AssetClassSchema, OutputReferenceSchema } from '../../types/generic';
3
- import {
4
- OnChainDecimal,
5
- OnChainDecimalSchema,
6
- } from '../../types/on-chain-decimal';
7
- import { option as O, function as F } from 'fp-ts';
8
- import { match, P } from 'ts-pattern';
9
-
10
- export const LRPParamsSchema = Data.Object({
11
- versionRecordToken: AssetClassSchema,
12
- iassetNft: AssetClassSchema,
13
- iassetPolicyId: Data.Bytes(),
14
- minRedemptionLovelacesAmt: Data.Integer(),
15
- });
16
- export type LRPParams = Data.Static<typeof LRPParamsSchema>;
17
- const LRPParams = LRPParamsSchema as unknown as LRPParams;
18
-
19
- export const LRPDatumSchema = Data.Object({
20
- owner: Data.Bytes(),
21
- iasset: Data.Bytes(),
22
- maxPrice: OnChainDecimalSchema,
23
- /**
24
- * The amount of lovelaces that is available to be spent.
25
- * This doesn't correspond to the lovelaces in UTXO's value,
26
- * since that can contain fees, too.
27
- */
28
- lovelacesToSpend: Data.Integer(),
29
- });
30
- export type LRPDatum = Data.Static<typeof LRPDatumSchema>;
31
- const LRPDatum = LRPDatumSchema as unknown as LRPDatum;
32
-
33
- export const LRPRedeemerSchema = Data.Enum([
34
- Data.Object({ Redeem: Data.Object({ continuingOutputIdx: Data.Integer() }) }),
35
- Data.Object({
36
- RedeemAuxiliary: Data.Object({
37
- continuingOutputIdx: Data.Integer(),
38
- mainRedeemOutRef: OutputReferenceSchema,
39
- asset: Data.Bytes(),
40
- assetPrice: OnChainDecimalSchema,
41
- redemptionReimbursementPercentage: OnChainDecimalSchema,
42
- }),
43
- }),
44
- Data.Literal('Cancel'),
45
- Data.Literal('UpgradeVersion'),
46
- ]);
47
- export type LRPRedeemer = Data.Static<typeof LRPRedeemerSchema>;
48
- const LRPRedeemer = LRPRedeemerSchema as unknown as LRPRedeemer;
49
-
50
- export function parseLrpDatum(datum: Datum): O.Option<LRPDatum> {
51
- try {
52
- return O.some(Data.from<LRPDatum>(datum, LRPDatum));
53
- } catch (_) {
54
- return O.none;
55
- }
56
- }
57
-
58
- export function parseLrpDatumOrThrow(datum: Datum): LRPDatum {
59
- return F.pipe(
60
- parseLrpDatum(datum),
61
- O.match(() => {
62
- throw new Error('Expected an LRP datum.');
63
- }, F.identity),
64
- );
65
- }
66
-
67
- type LRPSerialisationOptions =
68
- // No replacing, just use non-canonical format.
69
- | { _tag: 'noReplace' }
70
- // Adaptive replace, when the spentDatum is canonical, do the replace,
71
- // otherwise use the non canonical.
72
- | { _tag: 'adaptiveReplace'; spentLrpDatum: string };
73
-
74
- export function serialiseLrpDatum(
75
- datum: LRPDatum,
76
- // Overall, we don't want to do the replacing. We just use non-canonical format.
77
- serialisationOptions: LRPSerialisationOptions = { _tag: 'noReplace' },
78
- ): Datum {
79
- const d = Data.to<LRPDatum>(datum, LRPDatum);
80
-
81
- return match(serialisationOptions)
82
- .returnType<Datum>()
83
- .with({ _tag: 'noReplace' }, () => d)
84
- .with(
85
- { _tag: 'adaptiveReplace', spentLrpDatum: P.select() },
86
- (spentLrpDatum) => {
87
- const isSpentDatumCanonical = spentLrpDatum.includes(
88
- Data.to<OnChainDecimal>(
89
- parseLrpDatumOrThrow(spentLrpDatum).maxPrice,
90
- OnChainDecimal,
91
- {
92
- canonical: true,
93
- },
94
- ),
95
- );
96
-
97
- // When spent datum was canonical, replace.
98
- if (isSpentDatumCanonical) {
99
- // If the lrp was created using a canonical on-chain decimal, we need to serialise it canonically.
100
- // This is due to some issue related to how Aiken compares objects.
101
- // See "Wrong continuing output" trace, specifically the spread of the previous datum ie. expecting the serialisation to be the same as it was created with
102
- // We however do not want to do this for any lrps that are being build canonical.
103
- const ocdSerialisedCanonical = Data.to<OnChainDecimal>(
104
- datum.maxPrice,
105
- OnChainDecimal,
106
- { canonical: true },
107
- );
108
- const ocdSerialisedNonCanonical = Data.to<OnChainDecimal>(
109
- datum.maxPrice,
110
- OnChainDecimal,
111
- { canonical: false },
112
- );
113
-
114
- return d.replace(ocdSerialisedNonCanonical, ocdSerialisedCanonical);
115
- }
116
-
117
- return d;
118
- },
119
- )
120
- .exhaustive();
121
- }
122
-
123
- export function serialiseLrpRedeemer(redeemer: LRPRedeemer): Redeemer {
124
- return Data.to<LRPRedeemer>(redeemer, LRPRedeemer);
125
- }
126
-
127
- export function castLrpParams(params: LRPParams): Data {
128
- return Data.castTo(params, LRPParams);
129
- }
130
-
131
- export type LrpOutput = { datum: LRPDatum; utxo: UTxO };
@@ -1,88 +0,0 @@
1
- import { Data, Datum } from '@lucid-evolution/lucid';
2
- import { TreasuryWithdrawalSchema } from '../gov/types';
3
- import { AddressSchema } from '../../types/generic';
4
- import { option as O, function as F } from 'fp-ts';
5
- import { match, P } from 'ts-pattern';
6
- import { ProposalContentSchema } from '../gov/types-new';
7
-
8
- const PollStatusSchema = Data.Object({
9
- yesVotes: Data.Integer(),
10
- noVotes: Data.Integer(),
11
- });
12
- export type PollStatus = Data.Static<typeof PollStatusSchema>;
13
-
14
- const PollManagerContentSchema = Data.Object({
15
- pollId: Data.Integer(),
16
- pollOwner: Data.Bytes(),
17
- content: ProposalContentSchema,
18
- treasuryWithdrawal: Data.Nullable(TreasuryWithdrawalSchema),
19
- status: PollStatusSchema,
20
- votingEndTime: Data.Integer(),
21
- createdShardsCount: Data.Integer(),
22
- talliedShardsCount: Data.Integer(),
23
- totalShardsCount: Data.Integer(),
24
- proposingEndTime: Data.Integer(),
25
- expirationTime: Data.Integer(),
26
- protocolVersion: Data.Integer(),
27
- minimumQuorum: Data.Integer(),
28
- });
29
- export type PollManagerContent = Data.Static<typeof PollManagerContentSchema>;
30
-
31
- const PollShardContentSchema = Data.Object({
32
- pollId: Data.Integer(),
33
- status: PollStatusSchema,
34
- votingEndTime: Data.Integer(),
35
- managerAddress: AddressSchema,
36
- });
37
- export type PollShardContent = Data.Static<typeof PollShardContentSchema>;
38
-
39
- const PollDatumSchema = Data.Enum([
40
- Data.Object({
41
- PollManager: Data.Object({ content: PollManagerContentSchema }),
42
- }),
43
- Data.Object({ PollShard: Data.Object({ content: PollShardContentSchema }) }),
44
- ]);
45
- export type PollDatum = Data.Static<typeof PollDatumSchema>;
46
- export const PollDatum = PollDatumSchema as unknown as PollDatum;
47
-
48
- export function parsePollManager(datum: Datum): O.Option<PollManagerContent> {
49
- try {
50
- return match(Data.from<PollDatum>(datum, PollDatum))
51
- .with({ PollManager: P.select() }, (res) => O.some(res.content))
52
- .otherwise(() => O.none);
53
- } catch (_) {
54
- return O.none;
55
- }
56
- }
57
-
58
- export function parsePollManagerOrThrow(datum: Datum): PollManagerContent {
59
- return F.pipe(
60
- parsePollManager(datum),
61
- O.match(() => {
62
- throw new Error('Expected a Poll manager datum.');
63
- }, F.identity),
64
- );
65
- }
66
-
67
- export function parsePollShard(datum: Datum): O.Option<PollShardContent> {
68
- try {
69
- return match(Data.from<PollDatum>(datum, PollDatum))
70
- .with({ PollShard: P.select() }, (res) => O.some(res.content))
71
- .otherwise(() => O.none);
72
- } catch (_) {
73
- return O.none;
74
- }
75
- }
76
-
77
- export function parsePollShardOrThrow(datum: Datum): PollShardContent {
78
- return F.pipe(
79
- parsePollShard(datum),
80
- O.match(() => {
81
- throw new Error('Expected a Poll shard datum.');
82
- }, F.identity),
83
- );
84
- }
85
-
86
- export function serialisePollDatum(datum: PollDatum): Datum {
87
- return Data.to<PollDatum>(datum, PollDatum);
88
- }
@@ -1,218 +0,0 @@
1
- import { match, P } from 'ts-pattern';
2
- import { ONE_DAY } from '../../utils/time-helpers';
3
- import { OCD_DECIMAL_UNIT } from '../../types/on-chain-decimal';
4
- import { array as A } from 'fp-ts';
5
- import { bigintMax, bigintMin } from '../../utils/bigint-utils';
6
-
7
- type Schedule = {
8
- vestedAtTime: bigint;
9
- unlockAmt: bigint;
10
- };
11
-
12
- type VestingSchedule = {
13
- maxUnlockable: bigint;
14
- schedule: Schedule[];
15
- };
16
-
17
- export const teamVestingSchedule: Schedule[] = [
18
- {
19
- vestedAtTime: 1669067100000n,
20
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
21
- },
22
- {
23
- vestedAtTime: 1672523100000n,
24
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
25
- },
26
- {
27
- vestedAtTime: 1675201500000n,
28
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
29
- },
30
- {
31
- vestedAtTime: 1677620700000n,
32
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
33
- },
34
- {
35
- vestedAtTime: 1680299100000n,
36
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
37
- },
38
- {
39
- vestedAtTime: 1682891100000n,
40
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
41
- },
42
- {
43
- vestedAtTime: 1685569500000n,
44
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
45
- },
46
- {
47
- vestedAtTime: 1688161500000n,
48
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
49
- },
50
- {
51
- vestedAtTime: 1690839900000n,
52
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
53
- },
54
- {
55
- vestedAtTime: 1693518300000n,
56
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
57
- },
58
- {
59
- vestedAtTime: 1696110300000n,
60
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
61
- },
62
- {
63
- vestedAtTime: 1698788700000n,
64
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
65
- },
66
- {
67
- vestedAtTime: 1701380700000n,
68
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
69
- },
70
- {
71
- vestedAtTime: 1704059100000n,
72
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
73
- },
74
- {
75
- vestedAtTime: 1706737500000n,
76
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
77
- },
78
- {
79
- vestedAtTime: 1709243100000n,
80
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
81
- },
82
- {
83
- vestedAtTime: 1711921500000n,
84
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
85
- },
86
- {
87
- vestedAtTime: 1714513500000n,
88
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
89
- },
90
- {
91
- vestedAtTime: 1717191900000n,
92
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
93
- },
94
- {
95
- vestedAtTime: 1719783900000n,
96
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
97
- },
98
- {
99
- vestedAtTime: 1722462300000n,
100
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
101
- },
102
- {
103
- vestedAtTime: 1725140700000n,
104
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
105
- },
106
- {
107
- vestedAtTime: 1727732700000n,
108
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
109
- },
110
- {
111
- vestedAtTime: 1730411100000n,
112
- unlockAmt: 328_125n * OCD_DECIMAL_UNIT,
113
- },
114
- ];
115
-
116
- export const spDistributionSchedule: VestingSchedule = {
117
- maxUnlockable: 2_013_760n * OCD_DECIMAL_UNIT,
118
- schedule: [
119
- {
120
- vestedAtTime: 1669931100000n,
121
- unlockAmt: 28_768n * OCD_DECIMAL_UNIT,
122
- },
123
- ],
124
- };
125
-
126
- export const liqDistributionSchedule: VestingSchedule = {
127
- maxUnlockable: 316_470n * OCD_DECIMAL_UNIT,
128
- schedule: [
129
- {
130
- vestedAtTime: 1671659100000n,
131
- unlockAmt: 4_795n * OCD_DECIMAL_UNIT,
132
- },
133
- ],
134
- };
135
-
136
- export const spLpDistributionSchedule: VestingSchedule = {
137
- maxUnlockable: 17_016_441n * OCD_DECIMAL_UNIT,
138
- schedule: [
139
- {
140
- vestedAtTime: 1700171100000n,
141
- unlockAmt: 33_563n * OCD_DECIMAL_UNIT,
142
- },
143
- ],
144
- };
145
-
146
- export const govDistributionSchedule: VestingSchedule = {
147
- maxUnlockable: 1_381_248n * OCD_DECIMAL_UNIT,
148
- schedule: [
149
- {
150
- vestedAtTime: 1670363100000n,
151
- unlockAmt: 2_398n * OCD_DECIMAL_UNIT,
152
- },
153
- ],
154
- };
155
-
156
- export function calculateTotalVestedTeam(currentTime: bigint): bigint {
157
- return A.reduce<Schedule, bigint>(0n, (acc, schedule) => {
158
- if (currentTime >= schedule.vestedAtTime) {
159
- return acc + schedule.unlockAmt;
160
- } else {
161
- return acc;
162
- }
163
- })(teamVestingSchedule);
164
- }
165
-
166
- export function calculateVestedPerEpoch(
167
- schedule: VestingSchedule,
168
- currentTime: bigint,
169
- ): bigint {
170
- const vestingFreq = 5n * ONE_DAY;
171
-
172
- function go(sched: Schedule[]): bigint {
173
- return (
174
- match(sched)
175
- .returnType<bigint>()
176
- .with([], () => 0n)
177
- // If the current time is earlier than the first vesting period,
178
- // the vested INDY is zero.
179
- // Otherwise, the vested INDY increases by the given amount every epoch
180
- // after the given date.
181
- // The + 1 indicates that the first vest happens from the given date
182
- // (as opposed to having to wait for an epoch to complete)
183
- .with([P.select()], (sched) =>
184
- bigintMax(
185
- 0n,
186
- ((currentTime - sched.vestedAtTime) / vestingFreq + 1n) *
187
- sched.unlockAmt,
188
- ),
189
- )
190
- // If the current time is past the first period in the schedule,
191
- // at least all INDY from the first period is vested, and can proceed
192
- // to compute the vested amount for remaining periods.
193
- .with([P._, P._, ...P.array()], ([sched1, sched2, ...rest]) =>
194
- currentTime >= sched2.vestedAtTime
195
- ? ((sched2.vestedAtTime - sched1.vestedAtTime) / vestingFreq) *
196
- sched1.unlockAmt +
197
- go([sched2, ...rest])
198
- : bigintMax(
199
- 0n,
200
- ((currentTime - sched1.vestedAtTime) / vestingFreq + 1n) *
201
- sched1.unlockAmt,
202
- ),
203
- )
204
- .run()
205
- );
206
- }
207
-
208
- return bigintMin(schedule.maxUnlockable, go(schedule.schedule));
209
- }
210
-
211
- export function calculateTotalVestedRewards(currentTime: bigint): bigint {
212
- return (
213
- calculateVestedPerEpoch(spDistributionSchedule, currentTime) +
214
- calculateVestedPerEpoch(govDistributionSchedule, currentTime) +
215
- calculateVestedPerEpoch(liqDistributionSchedule, currentTime) +
216
- calculateVestedPerEpoch(spLpDistributionSchedule, currentTime)
217
- );
218
- }