@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,424 +0,0 @@
1
- /**
2
- * The following is the math related to the leverage calculations.
3
- *
4
- * Leverage is the multiplier you apply to the base deposit and you get the amount of final collateral
5
- * the CDP should have. Additionally, the minted amount is used to pay for fees. The leverage a user picks, is
6
- * already taking into account the fees, i.e. the fees are paid from the borrowed assets.
7
- *
8
- * There's a direct relationship between collateral ratio and leverage multiplier. Each leverage multiplier
9
- * results in a single collateral ratio and vice versa. Maximum potential leverage is the leverage that
10
- * results in collateral ratio being the maintenance collateral ratio of the corresponding iAsset.
11
- *
12
- * `d` = base deposit
13
- * `b` = total borrowed value (including the fees)
14
- * `L` = leverage
15
- * `f_m` = debt minting fee
16
- * `f_r` = reimbursement fee
17
- * `c` = collateral ratio
18
- *
19
- * The following is a detailed derivation of the math:
20
- *
21
- * 1. Since the redemption fee is proportional to the borrowed amount,
22
- * we can express the ADA we get from the order book as `b'=b*(1-f_r)`,
23
- * since some of the borrowed amount goes back to the order book.
24
- *
25
- * 2. Since all the minted iAsset are used to get borrowed ADA,
26
- * the value of the minted asset will be `b`.
27
- *
28
- * 3. The minting fee is a percentage of the value of the minted iAsset.
29
- * Therefore the available ADA to add as collateral is `b''=b' - b*f_m = b*(1 - f_r - f_m)`.
30
- *
31
- * 4. The collateral ratio can now be expressed as `c = (d + b * (1 - f_r - f_m)) / b`.
32
- *
33
- * 5. Working out the expression, we can express `b` in terms of everything else: `b = d / (c - 1 + f_r + f_m)`.
34
- *
35
- * 6. The minted amount will be `b / asset_price`.
36
- *
37
- * 7. Collateral amount of the CDP is `d + b * (1 - f_r - f_m)`
38
- *
39
- * 8. Leverage calculation: `L = (d + b * (1 - f_r - f_m)) / d`.
40
- *
41
- * Plugging in the `b` formula we get: `L = (d + (d / (c - 1 + f_r + f_m)) * (1 - f_r - f_m)) / d`.
42
- *
43
- * Simplified, yields the following:
44
- * `L = 1 + ((1 - f_r - f_m) / (c - 1 + f_r + f_m))`
45
- *
46
- * 9. `b'' = b * (1 - f_r - f_m)`
47
- * Solved for `b` yields the following:
48
- * `b = b'' / (1 - f_r - f_m)`
49
- *
50
- * 10. Having leverage and base deposit, we can find `b''`:
51
- * `b’’ = d(L - 1)`
52
- */
53
-
54
- import { UTxO } from '@lucid-evolution/lucid';
55
- import {
56
- OCD_DECIMAL_UNIT,
57
- ocdMul,
58
- OnChainDecimal,
59
- } from '../../types/on-chain-decimal';
60
- import { calculateFeeFromPercentage } from '../../utils/indigo-helpers';
61
- import { bigintMax, bigintMin, fromDecimal } from '../../utils/bigint-utils';
62
- import { array as A, function as F } from 'fp-ts';
63
- import { Decimal } from 'decimal.js';
64
- import { LrpParamsSP } from '../../types/system-params';
65
- import {
66
- calculateTotalAdaForRedemption,
67
- lrpRedeemableLovelacesInclReimb,
68
- } from '../lrp/helpers';
69
- import { LRPDatum } from '../lrp/types';
70
-
71
- /**
72
- * How many LRP redemptions can we fit into a TX with CDP open.
73
- */
74
- export const MAX_REDEMPTIONS_WITH_CDP_OPEN = 4;
75
-
76
- type LRPRedemptionDetails = {
77
- utxo: UTxO;
78
- /**
79
- * This is including the reimbursement fee.
80
- **/
81
- redemptionLovelacesAmtInclReimbursement: bigint;
82
- iassetsForRedemptionAmt: bigint;
83
- reimbursementLovelacesAmt: bigint;
84
- };
85
-
86
- type ApproximateLeverageRedemptionsResult = {
87
- leverage: number;
88
- collateralRatio: OnChainDecimal;
89
- lovelacesForRedemptionWithReimbursement: bigint;
90
- };
91
- /**
92
- * We assume exact precision. However, actual redemptions include rounding and
93
- * the rounding behaviour changes based on the number of redemptions.
94
- * This may slightly tweak the numbers and the result can be different.
95
- *
96
- * The math is described at the top of this code file.
97
- */
98
- export function approximateLeverageRedemptions(
99
- baseCollateral: bigint,
100
- targetLeverage: number,
101
- redemptionReimbursementPercentage: OnChainDecimal,
102
- debtMintingFeePercentage: OnChainDecimal,
103
- ): ApproximateLeverageRedemptionsResult {
104
- const debtMintingFeeRatioDecimal = Decimal(
105
- debtMintingFeePercentage.getOnChainInt,
106
- )
107
- .div(OCD_DECIMAL_UNIT)
108
- .div(100);
109
- const redemptionReimbursementRatioDecimal = Decimal(
110
- redemptionReimbursementPercentage.getOnChainInt,
111
- )
112
- .div(OCD_DECIMAL_UNIT)
113
- .div(100);
114
-
115
- const totalFeeRatio = debtMintingFeeRatioDecimal.add(
116
- redemptionReimbursementRatioDecimal,
117
- );
118
-
119
- // b''
120
- const bExFees = Decimal(baseCollateral)
121
- .mul(targetLeverage)
122
- .minus(baseCollateral)
123
- .floor();
124
-
125
- // b = b’’ / (1-f_r - f_m)
126
- const b = bExFees.div(Decimal(1).minus(totalFeeRatio)).floor();
127
-
128
- // c = (d + b * (1 - f_r - f_m)) / b
129
- const collateralRatio = {
130
- getOnChainInt: fromDecimal(
131
- Decimal(Decimal(baseCollateral).add(bExFees))
132
- .div(b)
133
- .mul(100n * OCD_DECIMAL_UNIT)
134
- .floor(),
135
- ),
136
- };
137
-
138
- return {
139
- leverage: targetLeverage,
140
- collateralRatio: collateralRatio,
141
- lovelacesForRedemptionWithReimbursement: fromDecimal(b),
142
- };
143
- }
144
-
145
- export function summarizeActualLeverageRedemptions(
146
- lovelacesForRedemptionWithReimbursement: bigint,
147
- redemptionReimbursementPercentage: OnChainDecimal,
148
- iassetPrice: OnChainDecimal,
149
- lrpParams: LrpParamsSP,
150
- // Picking from the beginning until the iasset redemption amount is satisfied.
151
- redemptionLrps: [UTxO, LRPDatum][],
152
- ): {
153
- redemptions: LRPRedemptionDetails[];
154
- /**
155
- * The actual amount received from redemptions (i.e. without the reimbursement fee).
156
- */
157
- totalRedeemedLovelaces: bigint;
158
- /**
159
- * Total lovelaces amt that has been reimbursted
160
- */
161
- totalReimbursementLovelaces: bigint;
162
- totalRedemptionIAssets: bigint;
163
- } {
164
- const priceDecimal = Decimal(iassetPrice.getOnChainInt).div(OCD_DECIMAL_UNIT);
165
-
166
- type Accumulator = {
167
- /// This is including the redemption reimbursement
168
- remainingRedemptionLovelacesInclReim: bigint;
169
- redemptions: LRPRedemptionDetails[];
170
- };
171
-
172
- const redemptionDetails = F.pipe(
173
- redemptionLrps,
174
- A.reduce<[UTxO, LRPDatum], Accumulator>(
175
- {
176
- remainingRedemptionLovelacesInclReim:
177
- lovelacesForRedemptionWithReimbursement,
178
- redemptions: [],
179
- },
180
- (acc, lrp) => {
181
- if (
182
- acc.remainingRedemptionLovelacesInclReim <
183
- lrpParams.minRedemptionLovelacesAmt
184
- ) {
185
- return acc;
186
- }
187
-
188
- const lovelacesToSpend = lrpRedeemableLovelacesInclReimb(
189
- lrp,
190
- lrpParams,
191
- );
192
-
193
- if (lovelacesToSpend === 0n) {
194
- return acc;
195
- }
196
-
197
- const newRemainingLovelaces = bigintMax(
198
- acc.remainingRedemptionLovelacesInclReim - lovelacesToSpend,
199
- 0n,
200
- );
201
- const redemptionLovelacesInitial =
202
- acc.remainingRedemptionLovelacesInclReim - newRemainingLovelaces;
203
-
204
- const finalRedemptionIAssets = fromDecimal(
205
- Decimal(redemptionLovelacesInitial).div(priceDecimal).floor(),
206
- );
207
- // We need to calculate the new number since redemptionIAssets got corrected by rounding.
208
- const finalRedemptionLovelaces = ocdMul(
209
- {
210
- getOnChainInt: finalRedemptionIAssets,
211
- },
212
- iassetPrice,
213
- ).getOnChainInt;
214
-
215
- const reimbursementLovelaces = calculateFeeFromPercentage(
216
- redemptionReimbursementPercentage,
217
- finalRedemptionLovelaces,
218
- );
219
-
220
- return {
221
- remainingRedemptionLovelacesInclReim:
222
- acc.remainingRedemptionLovelacesInclReim - finalRedemptionLovelaces,
223
- redemptions: [
224
- ...acc.redemptions,
225
- {
226
- utxo: lrp[0],
227
- iassetsForRedemptionAmt: finalRedemptionIAssets,
228
- redemptionLovelacesAmtInclReimbursement: finalRedemptionLovelaces,
229
- reimbursementLovelacesAmt: reimbursementLovelaces,
230
- },
231
- ],
232
- };
233
- },
234
- ),
235
- );
236
-
237
- const res = F.pipe(
238
- redemptionDetails.redemptions,
239
- A.reduce<
240
- LRPRedemptionDetails,
241
- {
242
- redeemedLovelaces: bigint;
243
- redemptionIAssets: bigint;
244
- reimbursementLovelaces: bigint;
245
- }
246
- >(
247
- {
248
- redeemedLovelaces: 0n,
249
- redemptionIAssets: 0n,
250
- reimbursementLovelaces: 0n,
251
- },
252
- (acc, details) => {
253
- return {
254
- redeemedLovelaces:
255
- acc.redeemedLovelaces +
256
- details.redemptionLovelacesAmtInclReimbursement -
257
- details.reimbursementLovelacesAmt,
258
- reimbursementLovelaces:
259
- acc.reimbursementLovelaces + details.reimbursementLovelacesAmt,
260
- redemptionIAssets:
261
- acc.redemptionIAssets + details.iassetsForRedemptionAmt,
262
- };
263
- },
264
- ),
265
- );
266
-
267
- return {
268
- redemptions: redemptionDetails.redemptions,
269
- totalRedeemedLovelaces: res.redeemedLovelaces,
270
- totalReimbursementLovelaces: res.reimbursementLovelaces,
271
- totalRedemptionIAssets: res.redemptionIAssets,
272
- };
273
- }
274
-
275
- /**
276
- * The math is described at the top of this code file.
277
- */
278
- export function calculateCollateralRatioFromLeverage(
279
- iasset: string,
280
- leverage: number,
281
- baseCollateral: bigint,
282
- iassetPrice: OnChainDecimal,
283
- debtMintingFeePercentage: OnChainDecimal,
284
- redemptionReimbursementPercentage: OnChainDecimal,
285
- lrpParams: LrpParamsSP,
286
- allLrps: [UTxO, LRPDatum][],
287
- ): OnChainDecimal | undefined {
288
- const debtMintingFeeRatioDecimal = Decimal(
289
- debtMintingFeePercentage.getOnChainInt,
290
- )
291
- .div(OCD_DECIMAL_UNIT)
292
- .div(100);
293
- const redemptionReimbursementRatioDecimal = Decimal(
294
- redemptionReimbursementPercentage.getOnChainInt,
295
- )
296
- .div(OCD_DECIMAL_UNIT)
297
- .div(100);
298
-
299
- const totalFeeRatio = debtMintingFeeRatioDecimal.add(
300
- redemptionReimbursementRatioDecimal,
301
- );
302
-
303
- const maxAvailableAdaForRedemptionInclReimb = calculateTotalAdaForRedemption(
304
- iasset,
305
- iassetPrice,
306
- lrpParams,
307
- allLrps,
308
- MAX_REDEMPTIONS_WITH_CDP_OPEN,
309
- );
310
-
311
- if (
312
- leverage <= 1 ||
313
- baseCollateral <= 0n ||
314
- maxAvailableAdaForRedemptionInclReimb <= 0n
315
- ) {
316
- return undefined;
317
- }
318
-
319
- // b''
320
- const bExFees = Decimal(baseCollateral)
321
- .mul(leverage)
322
- .minus(baseCollateral)
323
- .floor();
324
-
325
- // b = b’’ / (1-f_r - f_m)
326
- const b = bExFees.div(Decimal(1).minus(totalFeeRatio)).floor();
327
-
328
- const cappedB = bigintMin(
329
- maxAvailableAdaForRedemptionInclReimb,
330
- fromDecimal(b),
331
- );
332
-
333
- const cappedBExFees = Decimal(cappedB)
334
- .mul(Decimal(1).minus(totalFeeRatio))
335
- .floor();
336
-
337
- // c = (d + b * (1 - f_r - f_m)) / b
338
- const collateralRatio = Decimal(
339
- Decimal(baseCollateral).add(cappedBExFees),
340
- ).div(cappedB);
341
-
342
- return {
343
- getOnChainInt: fromDecimal(
344
- collateralRatio.mul(100n * OCD_DECIMAL_UNIT).floor(),
345
- ),
346
- };
347
- }
348
-
349
- /**
350
- * The math is described at the top of this code file.
351
- */
352
- export function calculateLeverageFromCollateralRatio(
353
- iasset: string,
354
- collateralRatioPercentage: OnChainDecimal,
355
- baseCollateral: bigint,
356
- iassetPrice: OnChainDecimal,
357
- debtMintingFeePercentage: OnChainDecimal,
358
- redemptionReimbursementPercentage: OnChainDecimal,
359
- lrpParams: LrpParamsSP,
360
- allLrps: [UTxO, LRPDatum][],
361
- ): number | undefined {
362
- const debtMintingFeeRatioDecimal = Decimal(
363
- debtMintingFeePercentage.getOnChainInt,
364
- )
365
- .div(OCD_DECIMAL_UNIT)
366
- .div(100);
367
- const redemptionReimbursementRatioDecimal = Decimal(
368
- redemptionReimbursementPercentage.getOnChainInt,
369
- )
370
- .div(OCD_DECIMAL_UNIT)
371
- .div(100);
372
-
373
- const totalFeeRatio = debtMintingFeeRatioDecimal.add(
374
- redemptionReimbursementRatioDecimal,
375
- );
376
-
377
- const collateralRatio = Decimal(collateralRatioPercentage.getOnChainInt)
378
- .div(OCD_DECIMAL_UNIT)
379
- .div(100);
380
-
381
- const maxAvailableAdaForRedemptionInclReimb = calculateTotalAdaForRedemption(
382
- iasset,
383
- iassetPrice,
384
- lrpParams,
385
- allLrps,
386
- MAX_REDEMPTIONS_WITH_CDP_OPEN,
387
- );
388
-
389
- if (
390
- collateralRatio.toNumber() <= 1 ||
391
- baseCollateral <= 0n ||
392
- maxAvailableAdaForRedemptionInclReimb <= 0n
393
- ) {
394
- return undefined;
395
- }
396
-
397
- // The leverage unconstrained by the liquidity in LRP
398
- const theoreticalMaxLeverage = Decimal(Decimal(1).minus(totalFeeRatio))
399
- .div(collateralRatio.minus(1).add(totalFeeRatio))
400
- .add(1);
401
-
402
- // b''
403
- const bExFees = theoreticalMaxLeverage
404
- .mul(baseCollateral)
405
- .minus(baseCollateral)
406
- .floor();
407
-
408
- // b = b’’ / (1-f_r - f_m)
409
- const b = bExFees.div(Decimal(1).minus(totalFeeRatio)).floor();
410
-
411
- const cappedB = bigintMin(
412
- maxAvailableAdaForRedemptionInclReimb,
413
- fromDecimal(b),
414
- );
415
-
416
- const cappedBExFees = Decimal(cappedB)
417
- .mul(Decimal(1).minus(totalFeeRatio))
418
- .floor();
419
-
420
- return Decimal(baseCollateral)
421
- .add(cappedBExFees)
422
- .div(baseCollateral)
423
- .toNumber();
424
- }
@@ -1,294 +0,0 @@
1
- import { addAssets, TxBuilder, UTxO } from '@lucid-evolution/lucid';
2
- import {
3
- LRPDatum,
4
- parseLrpDatumOrThrow,
5
- serialiseLrpDatum,
6
- serialiseLrpRedeemer,
7
- } from './types';
8
- import { ocdMul, OnChainDecimal } from '../../types/on-chain-decimal';
9
- import {
10
- lovelacesAmt,
11
- mkAssetsOf,
12
- mkLovelacesOf,
13
- } from '../../utils/value-helpers';
14
- import { calculateFeeFromPercentage } from '../../utils/indigo-helpers';
15
- import { bigintMax, BigIntOrd, sum } from '../../utils/bigint-utils';
16
- import { array as A, function as F, ord as Ord, option as O } from 'fp-ts';
17
- import { insertSorted, shuffle } from '../../utils/array-utils';
18
- import { LrpParamsSP, SystemParams } from '../../types/system-params';
19
- import { match, P } from 'ts-pattern';
20
- import { getInlineDatumOrThrow } from '../../utils/lucid-utils';
21
-
22
- export const MIN_LRP_COLLATERAL_AMT = 2_000_000n;
23
-
24
- /**
25
- * Calculate the actually redeemable lovelaces taking into account:
26
- * - LRP datum
27
- * - UTXO's value
28
- * - min redemption
29
- *
30
- * This helps to handle incorrectly initialised LRPs, too.
31
- */
32
- export function lrpRedeemableLovelacesInclReimb(
33
- lrp: [UTxO, LRPDatum],
34
- lrpParams: LrpParamsSP,
35
- ): bigint {
36
- const datum = lrp[1];
37
- const utxo = lrp[0];
38
-
39
- let res = 0n;
40
- // When incorrectly initialised
41
- if (datum.lovelacesToSpend > lovelacesAmt(utxo.assets)) {
42
- res = bigintMax(lovelacesAmt(utxo.assets) - MIN_LRP_COLLATERAL_AMT, 0n);
43
- } else {
44
- res = datum.lovelacesToSpend;
45
- }
46
-
47
- if (res < lrpParams.minRedemptionLovelacesAmt) {
48
- return 0n;
49
- }
50
-
51
- return res;
52
- }
53
-
54
- export function buildRedemptionsTx(
55
- /** The tuple represents the LRP UTXO and the amount of iAssets to redeem against it. */
56
- redemptions: [UTxO, bigint][],
57
- price: OnChainDecimal,
58
- redemptionReimbursementPercentage: OnChainDecimal,
59
- sysParams: SystemParams,
60
- tx: TxBuilder,
61
- /**
62
- * The number of Tx outputs before these.
63
- */
64
- txOutputsBeforeCount: bigint,
65
- ): TxBuilder {
66
- const [[mainLrpUtxo, _], __] = match(redemptions)
67
- .with(
68
- [P._, ...P.array()],
69
- ([[firstLrp, _], ...rest]): [[UTxO, bigint], [UTxO, bigint][]] => [
70
- [firstLrp, _],
71
- rest,
72
- ],
73
- )
74
- .otherwise(() => {
75
- throw new Error('Expects at least 1 UTXO to redeem.');
76
- });
77
-
78
- const mainLrpDatum = parseLrpDatumOrThrow(getInlineDatumOrThrow(mainLrpUtxo));
79
-
80
- return F.pipe(
81
- redemptions,
82
- A.reduceWithIndex<[UTxO, bigint], TxBuilder>(
83
- tx,
84
- (idx, acc, [lrpUtxo, redeemIAssetAmt]) => {
85
- const lovelacesForRedemption = ocdMul(
86
- {
87
- getOnChainInt: redeemIAssetAmt,
88
- },
89
- price,
90
- ).getOnChainInt;
91
- const reimburstmentLovelaces = calculateFeeFromPercentage(
92
- redemptionReimbursementPercentage,
93
- lovelacesForRedemption,
94
- );
95
-
96
- const lrpRawInlineDatum = getInlineDatumOrThrow(lrpUtxo);
97
- const lrpDatum = parseLrpDatumOrThrow(lrpRawInlineDatum);
98
-
99
- const resultVal = addAssets(
100
- lrpUtxo.assets,
101
- mkLovelacesOf(-lovelacesForRedemption + reimburstmentLovelaces),
102
- mkAssetsOf(
103
- {
104
- currencySymbol:
105
- sysParams.lrpParams.iassetPolicyId.unCurrencySymbol,
106
- tokenName: mainLrpDatum.iasset,
107
- },
108
- redeemIAssetAmt,
109
- ),
110
- );
111
-
112
- if (lovelacesAmt(resultVal) < MIN_LRP_COLLATERAL_AMT) {
113
- throw new Error('LRP was incorrectly initialised.');
114
- }
115
-
116
- return acc
117
- .collectFrom(
118
- [lrpUtxo],
119
- serialiseLrpRedeemer(
120
- idx === 0
121
- ? { Redeem: { continuingOutputIdx: txOutputsBeforeCount + 0n } }
122
- : {
123
- RedeemAuxiliary: {
124
- continuingOutputIdx: txOutputsBeforeCount + BigInt(idx),
125
- mainRedeemOutRef: {
126
- txHash: { hash: mainLrpUtxo.txHash },
127
- outputIndex: BigInt(mainLrpUtxo.outputIndex),
128
- },
129
- asset: mainLrpDatum.iasset,
130
- assetPrice: price,
131
- redemptionReimbursementPercentage:
132
- redemptionReimbursementPercentage,
133
- },
134
- },
135
- ),
136
- )
137
- .pay.ToContract(
138
- lrpUtxo.address,
139
- {
140
- kind: 'inline',
141
- value: serialiseLrpDatum(
142
- {
143
- ...lrpDatum,
144
- lovelacesToSpend:
145
- lrpDatum.lovelacesToSpend - lovelacesForRedemption,
146
- },
147
- { _tag: 'adaptiveReplace', spentLrpDatum: lrpRawInlineDatum },
148
- ),
149
- },
150
- resultVal,
151
- );
152
- },
153
- ),
154
- );
155
- }
156
-
157
- /**
158
- * Given all available LRP UTXOs, calculate total available ADA that can be redeemed (including reimbursement fee).
159
- * Taking into account ncorrectly initialised LRPs (without base collateral).
160
- */
161
- export function calculateTotalAdaForRedemption(
162
- iasset: string,
163
- iassetPrice: OnChainDecimal,
164
- lrpParams: LrpParamsSP,
165
- allLrps: [UTxO, LRPDatum][],
166
- /**
167
- * How many LRPs can be redeemed in a single Tx.
168
- */
169
- maxLrpsInTx: number,
170
- ): bigint {
171
- return F.pipe(
172
- allLrps,
173
- A.filterMap(([utxo, datum]) => {
174
- if (
175
- datum.iasset !== iasset ||
176
- datum.maxPrice.getOnChainInt < iassetPrice.getOnChainInt
177
- ) {
178
- return O.none;
179
- }
180
-
181
- const lovelacesToSpend = lrpRedeemableLovelacesInclReimb(
182
- [utxo, datum],
183
- lrpParams,
184
- );
185
-
186
- if (lovelacesToSpend === 0n) {
187
- return O.none;
188
- }
189
-
190
- // Subtract the reimbursement fee here on each iteration to simulate real redemptions.
191
- return O.some(lovelacesToSpend);
192
- }),
193
- // From largest to smallest
194
- A.sort(Ord.reverse(BigIntOrd)),
195
- // We can fit only this number of redemptions with CDP open into a single Tx.
196
- A.takeLeft(maxLrpsInTx),
197
- sum,
198
- );
199
- }
200
-
201
- export function randomLrpsSubsetSatisfyingTargetLovelaces(
202
- iasset: string,
203
- // Including the reimbursement percentage
204
- targetLovelacesToSpend: bigint,
205
- iassetPrice: OnChainDecimal,
206
- allLrps: [UTxO, LRPDatum][],
207
- lrpParams: LrpParamsSP,
208
- /**
209
- * How many LRPs can be redeemed in a single Tx.
210
- */
211
- maxLrpsInTx: number,
212
- randomiseFn: (arr: [UTxO, LRPDatum][]) => [UTxO, LRPDatum][] = shuffle,
213
- ): [UTxO, LRPDatum][] {
214
- if (targetLovelacesToSpend < lrpParams.minRedemptionLovelacesAmt) {
215
- throw new Error("Can't redeem less than the minimum.");
216
- }
217
-
218
- const shuffled = randomiseFn(
219
- F.pipe(
220
- allLrps,
221
- A.filter(
222
- ([_, datum]) =>
223
- datum.iasset === iasset &&
224
- datum.maxPrice.getOnChainInt >= iassetPrice.getOnChainInt,
225
- ),
226
- ),
227
- );
228
-
229
- // Sorted from highest to lowest by lovelaces to spend
230
- let result: [UTxO, LRPDatum][] = [];
231
- let runningSum = 0n;
232
-
233
- for (let i = 0; i < shuffled.length; i++) {
234
- const element = shuffled[i];
235
-
236
- const lovelacesToSpend = lrpRedeemableLovelacesInclReimb(
237
- element,
238
- lrpParams,
239
- );
240
-
241
- // Do not add LRPs with smaller lovelacesToSpend than the minRedemption
242
- // to the random subset.
243
- if (lovelacesToSpend < lrpParams.minRedemptionLovelacesAmt) {
244
- continue;
245
- }
246
-
247
- // When we can't add a new redemption because otherwise the min redemption
248
- // wouldn't be satisfied.
249
- // Try to replace the smallest collected with a following larger one when available.
250
- if (
251
- result.length > 0 &&
252
- targetLovelacesToSpend - runningSum < lrpParams.minRedemptionLovelacesAmt
253
- ) {
254
- const last = result[result.length - 1];
255
-
256
- // Pop the smallest collected when the current is larger.
257
- if (lrpRedeemableLovelacesInclReimb(last, lrpParams) < lovelacesToSpend) {
258
- const popped = result.pop()!;
259
- runningSum -= lrpRedeemableLovelacesInclReimb(popped, lrpParams);
260
- } else {
261
- continue;
262
- }
263
- }
264
-
265
- result = insertSorted(
266
- result,
267
- element,
268
- Ord.contramap<bigint, [UTxO, LRPDatum]>(
269
- ([_, dat]) => dat.lovelacesToSpend,
270
- // From highest to lowest
271
- )(Ord.reverse(BigIntOrd)),
272
- );
273
- runningSum += lovelacesToSpend;
274
-
275
- // When more items than max allowed, pop the one with smallest value
276
- if (result.length > maxLrpsInTx) {
277
- const popped = result.pop()!;
278
- runningSum -= lrpRedeemableLovelacesInclReimb(popped, lrpParams);
279
- }
280
-
281
- if (runningSum >= targetLovelacesToSpend) {
282
- return result;
283
- }
284
- }
285
-
286
- if (
287
- targetLovelacesToSpend - runningSum >=
288
- lrpParams.minRedemptionLovelacesAmt
289
- ) {
290
- throw new Error("Couldn't achieve target lovelaces");
291
- }
292
-
293
- return result;
294
- }