@indigo-labs/indigo-sdk 0.5.4 → 0.5.6
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.
- package/dist/index.d.mts +11 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +17 -0
- package/dist/index.mjs +15 -0
- package/package.json +11 -1
- package/.claude/settings.local.json +0 -7
- package/.github/workflows/ci.yml +0 -66
- package/.husky/pre-commit +0 -1
- package/.nvmrc +0 -1
- package/.prettierignore +0 -1
- package/.prettierrc +0 -6
- package/eslint.config.mjs +0 -42
- package/scripts/bench.sh +0 -62
- package/scripts/run-repeat.sh +0 -22
- package/src/contracts/cdp/helpers.ts +0 -173
- package/src/contracts/cdp/scripts.ts +0 -70
- package/src/contracts/cdp/transactions.ts +0 -1584
- package/src/contracts/cdp/types-new.ts +0 -277
- package/src/contracts/cdp/types.ts +0 -43
- package/src/contracts/cdp-creator/queries.ts +0 -30
- package/src/contracts/cdp-creator/scripts.ts +0 -45
- package/src/contracts/cdp-creator/types-new.ts +0 -50
- package/src/contracts/cdp-creator/types.ts +0 -24
- package/src/contracts/collector/scripts.ts +0 -32
- package/src/contracts/collector/transactions.ts +0 -50
- package/src/contracts/collector/types-new.ts +0 -17
- package/src/contracts/execute/scripts.ts +0 -57
- package/src/contracts/execute/types-new.ts +0 -44
- package/src/contracts/execute/types.ts +0 -24
- package/src/contracts/gov/helpers.ts +0 -351
- package/src/contracts/gov/scripts.ts +0 -41
- package/src/contracts/gov/transactions.ts +0 -1607
- package/src/contracts/gov/types-new.ts +0 -268
- package/src/contracts/gov/types.ts +0 -22
- package/src/contracts/iasset/helpers.ts +0 -170
- package/src/contracts/iasset/queries.ts +0 -102
- package/src/contracts/iasset/scripts.ts +0 -38
- package/src/contracts/iasset/types.ts +0 -154
- package/src/contracts/initialize/actions.ts +0 -789
- package/src/contracts/initialize/helpers.ts +0 -616
- package/src/contracts/initialize/types.ts +0 -116
- package/src/contracts/interest-collection/helpers.ts +0 -19
- package/src/contracts/interest-collection/queries.ts +0 -66
- package/src/contracts/interest-collection/scripts.ts +0 -44
- package/src/contracts/interest-collection/transactions.ts +0 -433
- package/src/contracts/interest-collection/types-new.ts +0 -50
- package/src/contracts/interest-collection/types.ts +0 -26
- package/src/contracts/interest-oracle/helpers.ts +0 -61
- package/src/contracts/interest-oracle/scripts.ts +0 -18
- package/src/contracts/interest-oracle/transactions.ts +0 -151
- package/src/contracts/interest-oracle/types-new.ts +0 -32
- package/src/contracts/interest-oracle/types.ts +0 -16
- package/src/contracts/one-shot/scripts.ts +0 -12
- package/src/contracts/one-shot/transactions.ts +0 -66
- package/src/contracts/one-shot/types.ts +0 -22
- package/src/contracts/poll/helpers.ts +0 -37
- package/src/contracts/poll/scripts.ts +0 -71
- package/src/contracts/poll/types-poll-manager.ts +0 -20
- package/src/contracts/poll/types-poll-new.ts +0 -196
- package/src/contracts/poll/types-poll-shard.ts +0 -16
- package/src/contracts/price-oracle/helpers.ts +0 -30
- package/src/contracts/price-oracle/scripts.ts +0 -18
- package/src/contracts/price-oracle/transactions.ts +0 -115
- package/src/contracts/price-oracle/types-new.ts +0 -50
- package/src/contracts/price-oracle/types.ts +0 -16
- package/src/contracts/pyth-feed/helpers.ts +0 -82
- package/src/contracts/pyth-feed/scripts.ts +0 -15
- package/src/contracts/pyth-feed/types.ts +0 -181
- package/src/contracts/rob/helpers.ts +0 -666
- package/src/contracts/rob/scripts.ts +0 -35
- package/src/contracts/rob/transactions.ts +0 -423
- package/src/contracts/rob/types-new.ts +0 -128
- package/src/contracts/rob/types.ts +0 -16
- package/src/contracts/rob-leverage/helpers.ts +0 -422
- package/src/contracts/rob-leverage/transactions.ts +0 -368
- package/src/contracts/stability-pool/helpers.ts +0 -798
- package/src/contracts/stability-pool/queries.ts +0 -132
- package/src/contracts/stability-pool/scripts.ts +0 -51
- package/src/contracts/stability-pool/transactions.ts +0 -921
- package/src/contracts/stability-pool/types-new.ts +0 -385
- package/src/contracts/stability-pool/types.ts +0 -25
- package/src/contracts/stableswap/helpers.ts +0 -507
- package/src/contracts/stableswap/scripts.ts +0 -37
- package/src/contracts/stableswap/transactions.ts +0 -475
- package/src/contracts/stableswap/types-new.ts +0 -131
- package/src/contracts/stableswap/types.ts +0 -17
- package/src/contracts/staking/helpers.ts +0 -136
- package/src/contracts/staking/scripts.ts +0 -41
- package/src/contracts/staking/transactions.ts +0 -349
- package/src/contracts/staking/types-new.ts +0 -137
- package/src/contracts/staking/types.ts +0 -17
- package/src/contracts/treasury/helpers.ts +0 -21
- package/src/contracts/treasury/queries.ts +0 -100
- package/src/contracts/treasury/scripts.ts +0 -27
- package/src/contracts/treasury/transactions.ts +0 -334
- package/src/contracts/treasury/types-new.ts +0 -69
- package/src/contracts/treasury/types.ts +0 -14
- package/src/contracts/version-registry/scripts.ts +0 -29
- package/src/contracts/version-registry/types-new.ts +0 -18
- package/src/contracts/version-registry/types.ts +0 -17
- package/src/index.ts +0 -93
- package/src/scripts/always-fail-validator.ts +0 -7
- package/src/scripts/auth-token-policy.ts +0 -24
- package/src/scripts/iasset-policy.ts +0 -23
- package/src/types/evolution-schema-options.ts +0 -16
- package/src/types/generic.ts +0 -52
- package/src/types/multisig.ts +0 -48
- package/src/types/on-chain-decimal.ts +0 -57
- package/src/types/rational.ts +0 -61
- package/src/types/system-params.ts +0 -463
- package/src/utils/array-utils.ts +0 -108
- package/src/utils/bigint-utils.ts +0 -35
- package/src/utils/helper-txs.ts +0 -31
- package/src/utils/indigo-helpers.ts +0 -17
- package/src/utils/lucid-utils.ts +0 -125
- package/src/utils/oracle-helpers.ts +0 -62
- package/src/utils/pyth/decode.ts +0 -223
- package/src/utils/pyth/encode.ts +0 -262
- package/src/utils/pyth/index.ts +0 -14
- package/src/utils/pyth/types.ts +0 -87
- package/src/utils/time-helpers.ts +0 -4
- package/src/utils/utils.ts +0 -25
- package/src/validators/always-succeed-validator.ts +0 -6
- package/src/validators/cdp-creator-validator.ts +0 -7
- package/src/validators/cdp-redeem-validator.ts +0 -7
- package/src/validators/cdp-validator.ts +0 -7
- package/src/validators/collector-validator.ts +0 -7
- package/src/validators/execute-validator.ts +0 -7
- package/src/validators/governance-validator.ts +0 -7
- package/src/validators/iasset-validator.ts +0 -7
- package/src/validators/interest-collection-validator.ts +0 -7
- package/src/validators/interest-oracle-validator.ts +0 -7
- package/src/validators/one-shot-policy.ts +0 -7
- package/src/validators/poll-manager-validator.ts +0 -7
- package/src/validators/poll-shard-validator.ts +0 -7
- package/src/validators/price-oracle-validator.ts +0 -7
- package/src/validators/pyth-feed-validator.ts +0 -7
- package/src/validators/rob-validator.ts +0 -7
- package/src/validators/stability-pool-validator.ts +0 -7
- package/src/validators/stableswap-validator.ts +0 -7
- package/src/validators/staking-validator.ts +0 -7
- package/src/validators/treasury-validator.ts +0 -7
- package/src/validators/version-record-policy.ts +0 -7
- package/src/validators/version-registry-validator.ts +0 -7
- package/tests/always-succeed/script.ts +0 -7
- package/tests/array-utils.test.ts +0 -92
- package/tests/bigint-utils.test.ts +0 -41
- package/tests/cdp/actions.ts +0 -680
- package/tests/cdp/cdp-helpers.ts +0 -55
- package/tests/cdp/cdp-queries.ts +0 -423
- package/tests/cdp/cdp.test.ts +0 -6730
- package/tests/cdp/transactions-mutated.ts +0 -1733
- package/tests/data/system-params.json +0 -1155
- package/tests/datums.test.ts +0 -302
- package/tests/endpoints/initialize.ts +0 -78
- package/tests/endpoints/interest-collector.ts +0 -37
- package/tests/endpoints/treasury.ts +0 -70
- package/tests/gov/actions.ts +0 -381
- package/tests/gov/gov.test.ts +0 -5147
- package/tests/gov/governance-queries.ts +0 -37
- package/tests/hash-checks.test.ts +0 -112
- package/tests/indigo-test-helpers.ts +0 -99
- package/tests/initialize.test.ts +0 -79
- package/tests/interest-calculations.test.ts +0 -120
- package/tests/interest-collection/interest-collection.test.ts +0 -843
- package/tests/interest-collection/transactions-mutated.ts +0 -260
- package/tests/interest-oracle.test.ts +0 -93
- package/tests/mock/assets-mock.ts +0 -243
- package/tests/mock/protocol-params-mock.ts +0 -21
- package/tests/price-oracle/actions.ts +0 -161
- package/tests/price-oracle/price-oracle-queries.ts +0 -12
- package/tests/price-oracle/price-oracle.test.ts +0 -228
- package/tests/price-oracle/transactions-mutated.ts +0 -59
- package/tests/pyth/endpoints.ts +0 -96
- package/tests/pyth/helpers.ts +0 -37
- package/tests/pyth/pyth-encoding.test.ts +0 -376
- package/tests/pyth/pyth-feeds.test.ts +0 -943
- package/tests/pyth/pyth-indigo.test.ts +0 -505
- package/tests/pyth/pyth.test.ts +0 -299
- package/tests/queries/collector-queries.ts +0 -27
- package/tests/queries/execute-queries.ts +0 -47
- package/tests/queries/iasset-queries.ts +0 -149
- package/tests/queries/interest-oracle-queries.ts +0 -12
- package/tests/queries/poll-queries.ts +0 -97
- package/tests/queries/staking-queries.ts +0 -54
- package/tests/rob/actions.ts +0 -56
- package/tests/rob/rob-leverage.test.ts +0 -2555
- package/tests/rob/rob-queries.ts +0 -95
- package/tests/rob/rob.test.ts +0 -3677
- package/tests/rob/transactions-mutated.ts +0 -844
- package/tests/script-size.test.ts +0 -240
- package/tests/setup.ts +0 -135
- package/tests/stability-pool/actions.ts +0 -229
- package/tests/stability-pool.test.ts +0 -6491
- package/tests/stableswap/stableswap-actions.ts +0 -83
- package/tests/stableswap/stableswap-queries.ts +0 -98
- package/tests/stableswap/stableswap.test.ts +0 -4471
- package/tests/stableswap/transactions-mutated.ts +0 -348
- package/tests/staking.test.ts +0 -172
- package/tests/test-helpers.ts +0 -94
- package/tests/treasury/actions.ts +0 -67
- package/tests/treasury/treasury.test.ts +0 -320
- package/tests/utils/asserts.ts +0 -87
- package/tests/utils/benchmark-utils.ts +0 -130
- package/tests/utils/index.ts +0 -168
- package/tsconfig.build.json +0 -4
- package/tsconfig.json +0 -27
- package/vitest.config.ts +0 -11
|
@@ -1,422 +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 { OCD_DECIMAL_UNIT, OnChainDecimal } from '../../types/on-chain-decimal';
|
|
56
|
-
import { bigintMax, bigintMin, fromDecimal } from '../../utils/bigint-utils';
|
|
57
|
-
import { array as A, function as F } from 'fp-ts';
|
|
58
|
-
import { Decimal } from 'decimal.js';
|
|
59
|
-
import {
|
|
60
|
-
calculateSpendAmtWhenRobBuyOrder,
|
|
61
|
-
calculateTotalCollateralForRedemption,
|
|
62
|
-
robCollateralAmtToSpend,
|
|
63
|
-
} from '../rob/helpers';
|
|
64
|
-
import {
|
|
65
|
-
Rational,
|
|
66
|
-
rationalFloor,
|
|
67
|
-
rationalFromInt,
|
|
68
|
-
rationalMul,
|
|
69
|
-
} from '../../types/rational';
|
|
70
|
-
import { AssetClass } from '@3rd-eye-labs/cardano-offchain-common';
|
|
71
|
-
import { RobDatum } from '../rob/types-new';
|
|
72
|
-
import { calculateFeeFromRatio } from '../../utils/indigo-helpers';
|
|
73
|
-
import { iassetValueOfCollateral } from '../cdp/helpers';
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* How many LRP redemptions can we fit into a TX with CDP open.
|
|
77
|
-
*/
|
|
78
|
-
export const MAX_REDEMPTIONS_WITH_CDP_OPEN = 4;
|
|
79
|
-
|
|
80
|
-
type ROBRedemptionDetails = {
|
|
81
|
-
utxo: UTxO;
|
|
82
|
-
redeemedCollateral: bigint;
|
|
83
|
-
/**
|
|
84
|
-
* The amount of iAssets paid to ROB.
|
|
85
|
-
*/
|
|
86
|
-
iassetsPayoutAmt: bigint;
|
|
87
|
-
reimbursementIAssetAmt: bigint;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
type ApproximateLeverageRedemptionsResult = {
|
|
91
|
-
leverage: number;
|
|
92
|
-
collateralRatio: Rational;
|
|
93
|
-
redeemedCollateral: bigint;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* We assume exact precision. However, actual redemptions include rounding and
|
|
98
|
-
* the rounding behaviour changes based on the number of redemptions.
|
|
99
|
-
* This may slightly tweak the numbers and the result can be different.
|
|
100
|
-
*
|
|
101
|
-
* The math is described at the top of this code file.
|
|
102
|
-
*/
|
|
103
|
-
export function approximateLeverageRedemptions(
|
|
104
|
-
baseCollateral: bigint,
|
|
105
|
-
targetLeverage: number,
|
|
106
|
-
redemptionReimbursementRatio: Rational,
|
|
107
|
-
debtMintingFeeRatio: Rational,
|
|
108
|
-
): ApproximateLeverageRedemptionsResult {
|
|
109
|
-
const debtMintingFeeRatioDecimal = Decimal(debtMintingFeeRatio.numerator).div(
|
|
110
|
-
debtMintingFeeRatio.denominator,
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
const redemptionReimbursementRatioDecimal = Decimal(
|
|
114
|
-
redemptionReimbursementRatio.numerator,
|
|
115
|
-
).div(redemptionReimbursementRatio.denominator);
|
|
116
|
-
|
|
117
|
-
const totalFeeRatio = debtMintingFeeRatioDecimal.add(
|
|
118
|
-
redemptionReimbursementRatioDecimal,
|
|
119
|
-
);
|
|
120
|
-
|
|
121
|
-
// b''
|
|
122
|
-
const bExFees = Decimal(baseCollateral)
|
|
123
|
-
.mul(targetLeverage)
|
|
124
|
-
.minus(baseCollateral)
|
|
125
|
-
.floor();
|
|
126
|
-
|
|
127
|
-
// b = b’’ / (1-f_r - f_m)
|
|
128
|
-
const b = bExFees.div(Decimal(1).minus(totalFeeRatio)).floor();
|
|
129
|
-
|
|
130
|
-
// c = (d + b * (1 - f_r - f_m)) / b
|
|
131
|
-
const collateralRatio: Rational = {
|
|
132
|
-
numerator: fromDecimal(Decimal(baseCollateral).add(bExFees)),
|
|
133
|
-
denominator: fromDecimal(b),
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
return {
|
|
137
|
-
leverage: targetLeverage,
|
|
138
|
-
collateralRatio: collateralRatio,
|
|
139
|
-
redeemedCollateral: fromDecimal(bExFees),
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export function summarizeActualLeverageRedemptions(
|
|
144
|
-
lovelacesForRedemptionWithReimbursement: bigint,
|
|
145
|
-
redemptionReimbursementRatio: Rational,
|
|
146
|
-
iassetPrice: Rational,
|
|
147
|
-
// Picking from the beginning until the iasset redemption amount is satisfied.
|
|
148
|
-
redemptionLrps: [UTxO, RobDatum][],
|
|
149
|
-
): {
|
|
150
|
-
redemptions: ROBRedemptionDetails[];
|
|
151
|
-
/**
|
|
152
|
-
* The actual amount received from redemptions (i.e. without the reimbursement fee).
|
|
153
|
-
*/
|
|
154
|
-
totalRedeemedCollateral: bigint;
|
|
155
|
-
/**
|
|
156
|
-
* Total amount of IAssets to cover the reimbursement fee.
|
|
157
|
-
*/
|
|
158
|
-
totalReimbursedIAsset: bigint;
|
|
159
|
-
/**
|
|
160
|
-
* Total amount of IAssets paid to ROBs, including the reimbursement.
|
|
161
|
-
*/
|
|
162
|
-
totalIAssetPayout: bigint;
|
|
163
|
-
} {
|
|
164
|
-
type Accumulator = {
|
|
165
|
-
/// The remaining collateral to spend from ROBs
|
|
166
|
-
remainingCollateralToSpend: bigint;
|
|
167
|
-
redemptions: ROBRedemptionDetails[];
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
const redemptionDetails = F.pipe(
|
|
171
|
-
redemptionLrps,
|
|
172
|
-
A.reduce<[UTxO, RobDatum], Accumulator>(
|
|
173
|
-
{
|
|
174
|
-
remainingCollateralToSpend: lovelacesForRedemptionWithReimbursement,
|
|
175
|
-
redemptions: [],
|
|
176
|
-
},
|
|
177
|
-
(acc, lrp) => {
|
|
178
|
-
if (
|
|
179
|
-
acc.remainingCollateralToSpend <= 0n ||
|
|
180
|
-
iassetValueOfCollateral(
|
|
181
|
-
acc.remainingCollateralToSpend,
|
|
182
|
-
iassetPrice,
|
|
183
|
-
) <= 0n
|
|
184
|
-
) {
|
|
185
|
-
return acc;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const collateralToSpend = robCollateralAmtToSpend(
|
|
189
|
-
lrp[0].assets,
|
|
190
|
-
lrp[1].orderType,
|
|
191
|
-
);
|
|
192
|
-
|
|
193
|
-
if (collateralToSpend === 0n) {
|
|
194
|
-
return acc;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const newRemainingCollateral = bigintMax(
|
|
198
|
-
acc.remainingCollateralToSpend - collateralToSpend,
|
|
199
|
-
0n,
|
|
200
|
-
);
|
|
201
|
-
const collateralToSpendInitial =
|
|
202
|
-
acc.remainingCollateralToSpend - newRemainingCollateral;
|
|
203
|
-
|
|
204
|
-
const finalPayoutIAssets = calculateSpendAmtWhenRobBuyOrder(
|
|
205
|
-
collateralToSpendInitial,
|
|
206
|
-
redemptionReimbursementRatio,
|
|
207
|
-
iassetPrice,
|
|
208
|
-
);
|
|
209
|
-
|
|
210
|
-
const feeIAssetAmt = calculateFeeFromRatio(
|
|
211
|
-
redemptionReimbursementRatio,
|
|
212
|
-
finalPayoutIAssets,
|
|
213
|
-
);
|
|
214
|
-
|
|
215
|
-
// We need to calculate the new number since redemptionIAssets got corrected by rounding.
|
|
216
|
-
const finalCollateralToSpend = rationalFloor(
|
|
217
|
-
rationalMul(
|
|
218
|
-
rationalFromInt(finalPayoutIAssets - feeIAssetAmt),
|
|
219
|
-
iassetPrice,
|
|
220
|
-
),
|
|
221
|
-
);
|
|
222
|
-
|
|
223
|
-
return {
|
|
224
|
-
remainingCollateralToSpend:
|
|
225
|
-
acc.remainingCollateralToSpend - finalCollateralToSpend,
|
|
226
|
-
redemptions: [
|
|
227
|
-
...acc.redemptions,
|
|
228
|
-
{
|
|
229
|
-
utxo: lrp[0],
|
|
230
|
-
iassetsPayoutAmt: finalPayoutIAssets,
|
|
231
|
-
redeemedCollateral: finalCollateralToSpend,
|
|
232
|
-
reimbursementIAssetAmt: feeIAssetAmt,
|
|
233
|
-
},
|
|
234
|
-
],
|
|
235
|
-
};
|
|
236
|
-
},
|
|
237
|
-
),
|
|
238
|
-
);
|
|
239
|
-
|
|
240
|
-
const res = F.pipe(
|
|
241
|
-
redemptionDetails.redemptions,
|
|
242
|
-
A.reduce<
|
|
243
|
-
ROBRedemptionDetails,
|
|
244
|
-
{
|
|
245
|
-
redeemedCollateral: bigint;
|
|
246
|
-
payoutIAssets: bigint;
|
|
247
|
-
reimbursementIAssets: bigint;
|
|
248
|
-
}
|
|
249
|
-
>(
|
|
250
|
-
{
|
|
251
|
-
redeemedCollateral: 0n,
|
|
252
|
-
payoutIAssets: 0n,
|
|
253
|
-
reimbursementIAssets: 0n,
|
|
254
|
-
},
|
|
255
|
-
(acc, details) => {
|
|
256
|
-
return {
|
|
257
|
-
redeemedCollateral:
|
|
258
|
-
acc.redeemedCollateral + details.redeemedCollateral,
|
|
259
|
-
reimbursementIAssets:
|
|
260
|
-
acc.reimbursementIAssets + details.reimbursementIAssetAmt,
|
|
261
|
-
payoutIAssets: acc.payoutIAssets + details.iassetsPayoutAmt,
|
|
262
|
-
};
|
|
263
|
-
},
|
|
264
|
-
),
|
|
265
|
-
);
|
|
266
|
-
|
|
267
|
-
return {
|
|
268
|
-
redemptions: redemptionDetails.redemptions,
|
|
269
|
-
totalRedeemedCollateral: res.redeemedCollateral,
|
|
270
|
-
totalReimbursedIAsset: res.reimbursementIAssets,
|
|
271
|
-
totalIAssetPayout: res.payoutIAssets,
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* The math is described at the top of this code file.
|
|
277
|
-
*/
|
|
278
|
-
export function calculateCollateralRatioFromLeverage(
|
|
279
|
-
iasset: Uint8Array<ArrayBufferLike>,
|
|
280
|
-
collateralAsset: AssetClass,
|
|
281
|
-
leverage: number,
|
|
282
|
-
baseCollateral: bigint,
|
|
283
|
-
iassetPrice: Rational,
|
|
284
|
-
debtMintingFeePercentage: OnChainDecimal,
|
|
285
|
-
redemptionReimbursementPercentage: OnChainDecimal,
|
|
286
|
-
allLrps: [UTxO, RobDatum][],
|
|
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 maxAvailableCollateralForRedemption =
|
|
304
|
-
calculateTotalCollateralForRedemption(
|
|
305
|
-
iasset,
|
|
306
|
-
collateralAsset,
|
|
307
|
-
iassetPrice,
|
|
308
|
-
allLrps,
|
|
309
|
-
MAX_REDEMPTIONS_WITH_CDP_OPEN,
|
|
310
|
-
);
|
|
311
|
-
|
|
312
|
-
if (
|
|
313
|
-
leverage <= 1 ||
|
|
314
|
-
baseCollateral <= 0n ||
|
|
315
|
-
maxAvailableCollateralForRedemption <= 0n
|
|
316
|
-
) {
|
|
317
|
-
return undefined;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
// b''
|
|
321
|
-
const bExFees = Decimal(baseCollateral)
|
|
322
|
-
.mul(leverage)
|
|
323
|
-
.minus(baseCollateral)
|
|
324
|
-
.floor();
|
|
325
|
-
|
|
326
|
-
// b = b’’ / (1-f_r - f_m)
|
|
327
|
-
const b = bExFees.div(Decimal(1).minus(totalFeeRatio)).floor();
|
|
328
|
-
|
|
329
|
-
const cappedB = bigintMin(
|
|
330
|
-
maxAvailableCollateralForRedemption,
|
|
331
|
-
fromDecimal(b),
|
|
332
|
-
);
|
|
333
|
-
|
|
334
|
-
const cappedBExFees = Decimal(cappedB)
|
|
335
|
-
.mul(Decimal(1).minus(totalFeeRatio))
|
|
336
|
-
.floor();
|
|
337
|
-
|
|
338
|
-
// c = (d + b * (1 - f_r - f_m)) / b
|
|
339
|
-
const collateralRatio = Decimal(
|
|
340
|
-
Decimal(baseCollateral).add(cappedBExFees),
|
|
341
|
-
).div(cappedB);
|
|
342
|
-
|
|
343
|
-
return {
|
|
344
|
-
getOnChainInt: fromDecimal(
|
|
345
|
-
collateralRatio.mul(100n * OCD_DECIMAL_UNIT).floor(),
|
|
346
|
-
),
|
|
347
|
-
};
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* The math is described at the top of this code file.
|
|
352
|
-
*/
|
|
353
|
-
export function calculateLeverageFromCollateralRatio(
|
|
354
|
-
iasset: Uint8Array<ArrayBufferLike>,
|
|
355
|
-
collateralAsset: AssetClass,
|
|
356
|
-
collateralRatio: Rational,
|
|
357
|
-
baseCollateral: bigint,
|
|
358
|
-
iassetPrice: Rational,
|
|
359
|
-
debtMintingFeeRatio: Rational,
|
|
360
|
-
redemptionReimbursementRatio: Rational,
|
|
361
|
-
allLrps: [UTxO, RobDatum][],
|
|
362
|
-
): number | undefined {
|
|
363
|
-
const debtMintingFeeRatioDecimal = Decimal(debtMintingFeeRatio.numerator).div(
|
|
364
|
-
debtMintingFeeRatio.denominator,
|
|
365
|
-
);
|
|
366
|
-
const redemptionReimbursementRatioDecimal = Decimal(
|
|
367
|
-
redemptionReimbursementRatio.numerator,
|
|
368
|
-
).div(redemptionReimbursementRatio.denominator);
|
|
369
|
-
|
|
370
|
-
const totalFeeRatio = debtMintingFeeRatioDecimal.add(
|
|
371
|
-
redemptionReimbursementRatioDecimal,
|
|
372
|
-
);
|
|
373
|
-
|
|
374
|
-
const collateralRatioDecimal = Decimal(collateralRatio.numerator).div(
|
|
375
|
-
collateralRatio.denominator,
|
|
376
|
-
);
|
|
377
|
-
|
|
378
|
-
const maxAvailableCollateralForRedemption =
|
|
379
|
-
calculateTotalCollateralForRedemption(
|
|
380
|
-
iasset,
|
|
381
|
-
collateralAsset,
|
|
382
|
-
iassetPrice,
|
|
383
|
-
allLrps,
|
|
384
|
-
MAX_REDEMPTIONS_WITH_CDP_OPEN,
|
|
385
|
-
);
|
|
386
|
-
|
|
387
|
-
if (
|
|
388
|
-
collateralRatioDecimal.toNumber() <= 1 ||
|
|
389
|
-
baseCollateral <= 0n ||
|
|
390
|
-
maxAvailableCollateralForRedemption <= 0n
|
|
391
|
-
) {
|
|
392
|
-
return undefined;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
// The leverage unconstrained by the liquidity in LRP
|
|
396
|
-
const theoreticalMaxLeverage = Decimal(Decimal(1).minus(totalFeeRatio))
|
|
397
|
-
.div(collateralRatioDecimal.minus(1).add(totalFeeRatio))
|
|
398
|
-
.add(1);
|
|
399
|
-
|
|
400
|
-
// b''
|
|
401
|
-
const bExFees = theoreticalMaxLeverage
|
|
402
|
-
.mul(baseCollateral)
|
|
403
|
-
.minus(baseCollateral)
|
|
404
|
-
.floor();
|
|
405
|
-
|
|
406
|
-
// b = b’’ / (1-f_r - f_m)
|
|
407
|
-
const b = bExFees.div(Decimal(1).minus(totalFeeRatio)).floor();
|
|
408
|
-
|
|
409
|
-
const cappedB = bigintMin(
|
|
410
|
-
maxAvailableCollateralForRedemption,
|
|
411
|
-
fromDecimal(b),
|
|
412
|
-
);
|
|
413
|
-
|
|
414
|
-
const cappedBExFees = Decimal(cappedB)
|
|
415
|
-
.mul(Decimal(1).minus(totalFeeRatio))
|
|
416
|
-
.floor();
|
|
417
|
-
|
|
418
|
-
return Decimal(baseCollateral)
|
|
419
|
-
.add(cappedBExFees)
|
|
420
|
-
.div(baseCollateral)
|
|
421
|
-
.toNumber();
|
|
422
|
-
}
|