@indigo-labs/indigo-sdk 0.2.2 → 0.2.4

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.
@@ -0,0 +1,274 @@
1
+ import {
2
+ LucidEvolution,
3
+ TxBuilder,
4
+ OutRef,
5
+ UTxO,
6
+ addAssets,
7
+ slotToUnixTime,
8
+ Data,
9
+ } from '@lucid-evolution/lucid';
10
+ import {
11
+ addrDetails,
12
+ createScriptAddress,
13
+ getInlineDatumOrThrow,
14
+ } from '../../utils/lucid-utils';
15
+ import { parsePriceOracleDatum } from '../price-oracle/types';
16
+ import { ocdMul } from '../../types/on-chain-decimal';
17
+ import { parseIAssetDatumOrThrow, serialiseCdpDatum } from '../cdp/types';
18
+ import { mkAssetsOf, mkLovelacesOf } from '../../utils/value-helpers';
19
+ import { calculateFeeFromPercentage } from '../../utils/indigo-helpers';
20
+ import { matchSingle } from '../../utils/utils';
21
+ import {
22
+ fromSystemParamsAsset,
23
+ fromSystemParamsScriptRef,
24
+ SystemParams,
25
+ } from '../../types/system-params';
26
+ import { oracleExpirationAwareValidity } from '../price-oracle/helpers';
27
+ import { parseInterestOracleDatum } from '../interest-oracle/types';
28
+ import { serialiseCDPCreatorRedeemer } from '../cdp-creator/types';
29
+ import { collectorFeeTx } from '../collector/transactions';
30
+ import { calculateUnitaryInterestSinceOracleLastUpdated } from '../interest-oracle/helpers';
31
+ import {
32
+ approximateLeverageRedemptions,
33
+ summarizeActualLeverageRedemptions,
34
+ calculateLeverageFromCollateralRatio,
35
+ MAX_REDEMPTIONS_WITH_CDP_OPEN,
36
+ } from './helpers';
37
+ import { LRPDatum } from '../lrp/types';
38
+ import {
39
+ buildRedemptionsTx,
40
+ randomLrpsSubsetSatisfyingTargetLovelaces,
41
+ } from '../lrp/helpers';
42
+
43
+ export async function leverageCdpWithLrp(
44
+ leverage: number,
45
+ baseCollateral: bigint,
46
+ priceOracleOutRef: OutRef,
47
+ iassetOutRef: OutRef,
48
+ cdpCreatorOref: OutRef,
49
+ interestOracleOref: OutRef,
50
+ collectorOref: OutRef,
51
+ sysParams: SystemParams,
52
+ lucid: LucidEvolution,
53
+ allLrps: [UTxO, LRPDatum][],
54
+ currentSlot: number,
55
+ ): Promise<TxBuilder> {
56
+ const network = lucid.config().network!;
57
+ const currentTime = BigInt(slotToUnixTime(network, currentSlot));
58
+
59
+ const [pkh, skh] = await addrDetails(lucid);
60
+
61
+ const lrpScriptRefUtxo = matchSingle(
62
+ await lucid.utxosByOutRef([
63
+ fromSystemParamsScriptRef(sysParams.scriptReferences.lrpValidatorRef),
64
+ ]),
65
+ (_) => new Error('Expected a single LRP Ref Script UTXO'),
66
+ );
67
+
68
+ const cdpCreatorRefScriptUtxo = matchSingle(
69
+ await lucid.utxosByOutRef([
70
+ fromSystemParamsScriptRef(
71
+ sysParams.scriptReferences.cdpCreatorValidatorRef,
72
+ ),
73
+ ]),
74
+ (_) => new Error('Expected a single cdp creator Ref Script UTXO'),
75
+ );
76
+ const cdpAuthTokenPolicyRefScriptUtxo = matchSingle(
77
+ await lucid.utxosByOutRef([
78
+ fromSystemParamsScriptRef(
79
+ sysParams.scriptReferences.authTokenPolicies.cdpAuthTokenRef,
80
+ ),
81
+ ]),
82
+ (_) => new Error('Expected a single cdp auth token policy Ref Script UTXO'),
83
+ );
84
+ const iAssetTokenPolicyRefScriptUtxo = matchSingle(
85
+ await lucid.utxosByOutRef([
86
+ fromSystemParamsScriptRef(
87
+ sysParams.scriptReferences.iAssetTokenPolicyRef,
88
+ ),
89
+ ]),
90
+ (_) => new Error('Expected a single iasset token policy Ref Script UTXO'),
91
+ );
92
+
93
+ const cdpCreatorUtxo = matchSingle(
94
+ await lucid.utxosByOutRef([cdpCreatorOref]),
95
+ (_) => new Error('Expected a single CDP creator UTXO'),
96
+ );
97
+
98
+ const interestOracleUtxo = matchSingle(
99
+ await lucid.utxosByOutRef([interestOracleOref]),
100
+ (_) => new Error('Expected a single interest oracle UTXO'),
101
+ );
102
+ const interestOracleDatum = parseInterestOracleDatum(
103
+ getInlineDatumOrThrow(interestOracleUtxo),
104
+ );
105
+
106
+ const priceOracleUtxo = matchSingle(
107
+ await lucid.utxosByOutRef([priceOracleOutRef]),
108
+ (_) => new Error('Expected a single price oracle UTXO'),
109
+ );
110
+ const priceOracleDatum = parsePriceOracleDatum(
111
+ getInlineDatumOrThrow(priceOracleUtxo),
112
+ );
113
+
114
+ const iassetUtxo = matchSingle(
115
+ await lucid.utxosByOutRef([iassetOutRef]),
116
+ (_) => new Error('Expected a single IAsset UTXO'),
117
+ );
118
+ const iassetDatum = parseIAssetDatumOrThrow(
119
+ getInlineDatumOrThrow(iassetUtxo),
120
+ );
121
+
122
+ const maxLeverage = calculateLeverageFromCollateralRatio(
123
+ iassetDatum.assetName,
124
+ iassetDatum.maintenanceRatio,
125
+ baseCollateral,
126
+ priceOracleDatum.price,
127
+ iassetDatum.debtMintingFeePercentage,
128
+ iassetDatum.redemptionReimbursementPercentage,
129
+ sysParams.lrpParams,
130
+ allLrps,
131
+ );
132
+
133
+ if (!maxLeverage) {
134
+ throw new Error("Can't calculate max leverage with those parameters.");
135
+ }
136
+
137
+ const leverageSummary = approximateLeverageRedemptions(
138
+ baseCollateral,
139
+ leverage,
140
+ iassetDatum.redemptionReimbursementPercentage,
141
+ iassetDatum.debtMintingFeePercentage,
142
+ );
143
+
144
+ if (maxLeverage < leverageSummary.leverage) {
145
+ throw new Error("Can't use more leverage than max.");
146
+ }
147
+
148
+ if (
149
+ leverageSummary.collateralRatio.getOnChainInt <
150
+ iassetDatum.maintenanceRatio.getOnChainInt
151
+ ) {
152
+ throw new Error(
153
+ "Can't have collateral ratio smaller than maintenance ratio",
154
+ );
155
+ }
156
+
157
+ const redemptionDetails = summarizeActualLeverageRedemptions(
158
+ leverageSummary.lovelacesForRedemptionWithReimbursement,
159
+ iassetDatum.redemptionReimbursementPercentage,
160
+ priceOracleDatum.price,
161
+ sysParams.lrpParams,
162
+ randomLrpsSubsetSatisfyingTargetLovelaces(
163
+ iassetDatum.assetName,
164
+ leverageSummary.lovelacesForRedemptionWithReimbursement,
165
+ priceOracleDatum.price,
166
+ allLrps,
167
+ sysParams.lrpParams,
168
+ MAX_REDEMPTIONS_WITH_CDP_OPEN,
169
+ ),
170
+ );
171
+
172
+ const mintedAmt = redemptionDetails.totalRedemptionIAssets;
173
+
174
+ const debtMintingFee = calculateFeeFromPercentage(
175
+ iassetDatum.debtMintingFeePercentage,
176
+ ocdMul({ getOnChainInt: mintedAmt }, priceOracleDatum.price).getOnChainInt,
177
+ );
178
+
179
+ const collateralAmt =
180
+ redemptionDetails.totalRedeemedLovelaces + baseCollateral - debtMintingFee;
181
+
182
+ const cdpNftVal = mkAssetsOf(
183
+ fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken),
184
+ 1n,
185
+ );
186
+
187
+ const iassetTokensVal = mkAssetsOf(
188
+ {
189
+ currencySymbol: sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
190
+ tokenName: iassetDatum.assetName,
191
+ },
192
+ mintedAmt,
193
+ );
194
+
195
+ const txValidity = oracleExpirationAwareValidity(
196
+ currentSlot,
197
+ Number(sysParams.cdpCreatorParams.biasTime),
198
+ Number(priceOracleDatum.expiration),
199
+ network,
200
+ );
201
+
202
+ const tx = lucid
203
+ .newTx()
204
+ .validFrom(txValidity.validFrom)
205
+ .validTo(txValidity.validTo)
206
+ // Ref inputs
207
+ .readFrom([priceOracleUtxo, interestOracleUtxo, iassetUtxo])
208
+ // Ref scripts
209
+ .readFrom([
210
+ cdpCreatorRefScriptUtxo,
211
+ cdpAuthTokenPolicyRefScriptUtxo,
212
+ iAssetTokenPolicyRefScriptUtxo,
213
+ lrpScriptRefUtxo,
214
+ ])
215
+ .mintAssets(cdpNftVal, Data.void())
216
+ .mintAssets(iassetTokensVal, Data.void())
217
+ .collectFrom(
218
+ [cdpCreatorUtxo],
219
+ serialiseCDPCreatorRedeemer({
220
+ CreateCDP: {
221
+ cdpOwner: pkh.hash,
222
+ minted: mintedAmt,
223
+ collateral: collateralAmt,
224
+ currentTime: currentTime,
225
+ },
226
+ }),
227
+ )
228
+ .pay.ToContract(
229
+ createScriptAddress(network, sysParams.validatorHashes.cdpHash, skh),
230
+ {
231
+ kind: 'inline',
232
+ value: serialiseCdpDatum({
233
+ cdpOwner: pkh.hash,
234
+ iasset: iassetDatum.assetName,
235
+ mintedAmt: mintedAmt,
236
+ cdpFees: {
237
+ ActiveCDPInterestTracking: {
238
+ lastSettled: currentTime,
239
+ unitaryInterestSnapshot:
240
+ calculateUnitaryInterestSinceOracleLastUpdated(
241
+ currentTime,
242
+ interestOracleDatum,
243
+ ) + interestOracleDatum.unitaryInterest,
244
+ },
245
+ },
246
+ }),
247
+ },
248
+ addAssets(cdpNftVal, mkLovelacesOf(collateralAmt)),
249
+ )
250
+ .pay.ToContract(
251
+ cdpCreatorUtxo.address,
252
+ { kind: 'inline', value: Data.void() },
253
+ cdpCreatorUtxo.assets,
254
+ )
255
+ .addSignerKey(pkh.hash);
256
+
257
+ buildRedemptionsTx(
258
+ redemptionDetails.redemptions.map((r) => [
259
+ r.utxo,
260
+ r.iassetsForRedemptionAmt,
261
+ ]),
262
+ priceOracleDatum.price,
263
+ iassetDatum.redemptionReimbursementPercentage,
264
+ sysParams,
265
+ tx,
266
+ 2n,
267
+ );
268
+
269
+ if (debtMintingFee > 0) {
270
+ await collectorFeeTx(debtMintingFee, lucid, sysParams, tx, collectorOref);
271
+ }
272
+
273
+ return tx;
274
+ }
@@ -0,0 +1,290 @@
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 lrpDatum = parseLrpDatumOrThrow(getInlineDatumOrThrow(lrpUtxo));
97
+
98
+ const resultVal = addAssets(
99
+ lrpUtxo.assets,
100
+ mkLovelacesOf(-lovelacesForRedemption + reimburstmentLovelaces),
101
+ mkAssetsOf(
102
+ {
103
+ currencySymbol:
104
+ sysParams.lrpParams.iassetPolicyId.unCurrencySymbol,
105
+ tokenName: mainLrpDatum.iasset,
106
+ },
107
+ redeemIAssetAmt,
108
+ ),
109
+ );
110
+
111
+ if (lovelacesAmt(resultVal) < MIN_LRP_COLLATERAL_AMT) {
112
+ throw new Error('LRP was incorrectly initialised.');
113
+ }
114
+
115
+ return acc
116
+ .collectFrom(
117
+ [lrpUtxo],
118
+ serialiseLrpRedeemer(
119
+ idx === 0
120
+ ? { Redeem: { continuingOutputIdx: txOutputsBeforeCount + 0n } }
121
+ : {
122
+ RedeemAuxiliary: {
123
+ continuingOutputIdx: txOutputsBeforeCount + BigInt(idx),
124
+ mainRedeemOutRef: {
125
+ txHash: { hash: mainLrpUtxo.txHash },
126
+ outputIndex: BigInt(mainLrpUtxo.outputIndex),
127
+ },
128
+ asset: mainLrpDatum.iasset,
129
+ assetPrice: price,
130
+ redemptionReimbursementPercentage:
131
+ redemptionReimbursementPercentage,
132
+ },
133
+ },
134
+ ),
135
+ )
136
+ .pay.ToContract(
137
+ lrpUtxo.address,
138
+ {
139
+ kind: 'inline',
140
+ value: serialiseLrpDatum({
141
+ ...lrpDatum,
142
+ lovelacesToSpend:
143
+ lrpDatum.lovelacesToSpend - lovelacesForRedemption,
144
+ }),
145
+ },
146
+ resultVal,
147
+ );
148
+ },
149
+ ),
150
+ );
151
+ }
152
+
153
+ /**
154
+ * Given all available LRP UTXOs, calculate total available ADA that can be redeemed (including reimbursement fee).
155
+ * Taking into account ncorrectly initialised LRPs (without base collateral).
156
+ */
157
+ export function calculateTotalAdaForRedemption(
158
+ iasset: string,
159
+ iassetPrice: OnChainDecimal,
160
+ lrpParams: LrpParamsSP,
161
+ allLrps: [UTxO, LRPDatum][],
162
+ /**
163
+ * How many LRPs can be redeemed in a single Tx.
164
+ */
165
+ maxLrpsInTx: number,
166
+ ): bigint {
167
+ return F.pipe(
168
+ allLrps,
169
+ A.filterMap(([utxo, datum]) => {
170
+ if (
171
+ datum.iasset !== iasset ||
172
+ datum.maxPrice.getOnChainInt < iassetPrice.getOnChainInt
173
+ ) {
174
+ return O.none;
175
+ }
176
+
177
+ const lovelacesToSpend = lrpRedeemableLovelacesInclReimb(
178
+ [utxo, datum],
179
+ lrpParams,
180
+ );
181
+
182
+ if (lovelacesToSpend === 0n) {
183
+ return O.none;
184
+ }
185
+
186
+ // Subtract the reimbursement fee here on each iteration to simulate real redemptions.
187
+ return O.some(lovelacesToSpend);
188
+ }),
189
+ // From largest to smallest
190
+ A.sort(Ord.reverse(BigIntOrd)),
191
+ // We can fit only this number of redemptions with CDP open into a single Tx.
192
+ A.takeLeft(maxLrpsInTx),
193
+ sum,
194
+ );
195
+ }
196
+
197
+ export function randomLrpsSubsetSatisfyingTargetLovelaces(
198
+ iasset: string,
199
+ // Including the reimbursement percentage
200
+ targetLovelacesToSpend: bigint,
201
+ iassetPrice: OnChainDecimal,
202
+ allLrps: [UTxO, LRPDatum][],
203
+ lrpParams: LrpParamsSP,
204
+ /**
205
+ * How many LRPs can be redeemed in a single Tx.
206
+ */
207
+ maxLrpsInTx: number,
208
+ randomiseFn: (arr: [UTxO, LRPDatum][]) => [UTxO, LRPDatum][] = shuffle,
209
+ ): [UTxO, LRPDatum][] {
210
+ if (targetLovelacesToSpend < lrpParams.minRedemptionLovelacesAmt) {
211
+ throw new Error("Can't redeem less than the minimum.");
212
+ }
213
+
214
+ const shuffled = randomiseFn(
215
+ F.pipe(
216
+ allLrps,
217
+ A.filter(
218
+ ([_, datum]) =>
219
+ datum.iasset === iasset &&
220
+ datum.maxPrice.getOnChainInt >= iassetPrice.getOnChainInt,
221
+ ),
222
+ ),
223
+ );
224
+
225
+ // Sorted from highest to lowest by lovelaces to spend
226
+ let result: [UTxO, LRPDatum][] = [];
227
+ let runningSum = 0n;
228
+
229
+ for (let i = 0; i < shuffled.length; i++) {
230
+ const element = shuffled[i];
231
+
232
+ const lovelacesToSpend = lrpRedeemableLovelacesInclReimb(
233
+ element,
234
+ lrpParams,
235
+ );
236
+
237
+ // Do not add LRPs with smaller lovelacesToSpend than the minRedemption
238
+ // to the random subset.
239
+ if (lovelacesToSpend < lrpParams.minRedemptionLovelacesAmt) {
240
+ continue;
241
+ }
242
+
243
+ // When we can't add a new redemption because otherwise the min redemption
244
+ // wouldn't be satisfied.
245
+ // Try to replace the smallest collected with a following larger one when available.
246
+ if (
247
+ result.length > 0 &&
248
+ targetLovelacesToSpend - runningSum < lrpParams.minRedemptionLovelacesAmt
249
+ ) {
250
+ const last = result[result.length - 1];
251
+
252
+ // Pop the smallest collected when the current is larger.
253
+ if (lrpRedeemableLovelacesInclReimb(last, lrpParams) < lovelacesToSpend) {
254
+ const popped = result.pop()!;
255
+ runningSum -= lrpRedeemableLovelacesInclReimb(popped, lrpParams);
256
+ } else {
257
+ continue;
258
+ }
259
+ }
260
+
261
+ result = insertSorted(
262
+ result,
263
+ element,
264
+ Ord.contramap<bigint, [UTxO, LRPDatum]>(
265
+ ([_, dat]) => dat.lovelacesToSpend,
266
+ // From highest to lowest
267
+ )(Ord.reverse(BigIntOrd)),
268
+ );
269
+ runningSum += lovelacesToSpend;
270
+
271
+ // When more items than max allowed, pop the one with smallest value
272
+ if (result.length > maxLrpsInTx) {
273
+ const popped = result.pop()!;
274
+ runningSum -= lrpRedeemableLovelacesInclReimb(popped, lrpParams);
275
+ }
276
+
277
+ if (runningSum >= targetLovelacesToSpend) {
278
+ return result;
279
+ }
280
+ }
281
+
282
+ if (
283
+ targetLovelacesToSpend - runningSum >=
284
+ lrpParams.minRedemptionLovelacesAmt
285
+ ) {
286
+ throw new Error("Couldn't achieve target lovelaces");
287
+ }
288
+
289
+ return result;
290
+ }