@indigo-labs/indigo-sdk 0.3.10 → 0.3.12

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.
@@ -6,6 +6,9 @@ import {
6
6
  UTxO,
7
7
  toText,
8
8
  fromText,
9
+ Assets,
10
+ addAssets,
11
+ fromHex,
9
12
  } from '@lucid-evolution/lucid';
10
13
  import {
11
14
  addrDetails,
@@ -16,6 +19,7 @@ import {
16
19
  fromSystemParamsAsset,
17
20
  liquidateCdp,
18
21
  mintCdp,
22
+ mkTreasuryAddr,
19
23
  openCdp,
20
24
  redeemCdp,
21
25
  SystemParams,
@@ -36,13 +40,16 @@ import {
36
40
  runAndAwaitTxBuilder,
37
41
  } from '../test-helpers';
38
42
  import { AssetInfo } from '../endpoints/initialize';
39
- import { expect } from 'vitest';
43
+ import { assert, expect } from 'vitest';
40
44
  import { feedPriceOracleTx } from '../../src/contracts/price-oracle/transactions';
41
45
  import {
46
+ adaAssetClass,
42
47
  AssetClass,
43
48
  assetClassToUnit,
49
+ assetClassValueOf,
44
50
  getInlineDatumOrThrow,
45
51
  matchSingle,
52
+ mkAssetsOf,
46
53
  } from '@3rd-eye-labs/cardano-offchain-common';
47
54
  import { parseCdpDatumOrThrow } from '../../src/contracts/cdp/types-new';
48
55
  import { match, P } from 'ts-pattern';
@@ -50,6 +57,9 @@ import { findPriceOracle } from '../price-oracle/price-oracle-queries';
50
57
  import { findCollateralAsset } from '../queries/iasset-queries';
51
58
  import { runFeedPriceToOracle } from '../price-oracle/actions';
52
59
  import { rationalFromInt, rationalMul } from '../../src/types/rational';
60
+ import { array as A } from 'fp-ts';
61
+ import { sendValueTo } from '../utils';
62
+ import { findRandomTreasuryUtxoWithAsset } from '../queries/treasury-queries';
53
63
 
54
64
  // Selects users wallet and opens a CDP with the given initial collateral and mint amount
55
65
  export async function runOpenCdp(
@@ -60,6 +70,7 @@ export async function runOpenCdp(
60
70
  initialCollateral: bigint,
61
71
  initialMint: bigint,
62
72
  pythMessage?: string,
73
+ directTreasuryPayment: boolean = false,
63
74
  ): Promise<TxBuilder> {
64
75
  const orefs = await findAllNecessaryOrefs(
65
76
  context.lucid,
@@ -90,7 +101,7 @@ export async function runOpenCdp(
90
101
  orefs.collateralAsset.utxo,
91
102
  priceOracleUtxo,
92
103
  orefs.interestOracleUtxo,
93
- orefs.treasuryUtxo,
104
+ directTreasuryPayment ? undefined : orefs.treasuryUtxo,
94
105
  context.lucid,
95
106
  context.emulator.slot,
96
107
  pythMessage,
@@ -196,6 +207,7 @@ export async function runMintCdp(
196
207
  collateralAsset: AssetClass,
197
208
  amount: bigint = 100_000n,
198
209
  pythMessage?: string,
210
+ directTreasuryPayment: boolean = false,
199
211
  ): Promise<TxBuilder> {
200
212
  const [pkh, skh] = await addrDetails(context.lucid);
201
213
 
@@ -233,7 +245,7 @@ export async function runMintCdp(
233
245
  orefs.collateralAsset.utxo,
234
246
  priceOracleUtxo,
235
247
  orefs.interestOracleUtxo,
236
- orefs.treasuryUtxo,
248
+ directTreasuryPayment ? undefined : orefs.treasuryUtxo,
237
249
  orefs.interestCollectorUtxo,
238
250
  sysParams,
239
251
  context.lucid,
@@ -320,6 +332,7 @@ export async function runRedeemCdp(
320
332
  collateralAsset: AssetClass,
321
333
  pkh: string,
322
334
  skh: Credential | undefined,
335
+ directTreasuryPayment: boolean = false,
323
336
  ): Promise<TxBuilder> {
324
337
  const cdp = await findCdp(
325
338
  context.lucid,
@@ -350,7 +363,7 @@ export async function runRedeemCdp(
350
363
  priceOracleUtxo,
351
364
  orefs.interestOracleUtxo,
352
365
  orefs.interestCollectorUtxo,
353
- orefs.treasuryUtxo,
366
+ directTreasuryPayment ? undefined : orefs.treasuryUtxo,
354
367
  orefs.govUtxo,
355
368
  sysParams,
356
369
  context.lucid,
@@ -513,6 +526,8 @@ export async function runLiquidateCdp(
513
526
  context: LucidContext,
514
527
  sysParams: SystemParams,
515
528
  frozenCdpUtxo: UTxO,
529
+ treasuryUtxo?: UTxO,
530
+ directTreasuryPayment: boolean = false,
516
531
  ): Promise<TxBuilder> {
517
532
  const cdpDatum = parseCdpDatumOrThrow(getInlineDatumOrThrow(frozenCdpUtxo));
518
533
  const orefs = await findAllNecessaryOrefs(
@@ -525,7 +540,11 @@ export async function runLiquidateCdp(
525
540
  frozenCdpUtxo,
526
541
  orefs.stabilityPoolUtxo,
527
542
  orefs.interestCollectorUtxo,
528
- orefs.treasuryUtxo,
543
+ directTreasuryPayment
544
+ ? undefined
545
+ : treasuryUtxo
546
+ ? treasuryUtxo
547
+ : orefs.treasuryUtxo,
529
548
  sysParams,
530
549
  context.lucid,
531
550
  );
@@ -544,6 +563,7 @@ export async function executeLiquidation(
544
563
  liquidateWrapper: (liquidateTx: TxBuilder) => Promise<void> = async (tx) => {
545
564
  await runAndAwaitTxBuilder(context.lucid, tx);
546
565
  },
566
+ directTreasuryPayment: boolean = false,
547
567
  ) {
548
568
  const price = await findPrice(
549
569
  context.lucid,
@@ -566,6 +586,46 @@ export async function executeLiquidation(
566
586
  ),
567
587
  );
568
588
 
589
+ const user4Value = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
590
+ addAssets(acc, utxo.assets),
591
+ )(await context.lucid.utxosAt(context.users['user4'].address));
592
+
593
+ const iassetAc: AssetClass = {
594
+ currencySymbol: fromHex(
595
+ sysParams.cdpParams.cdpAssetSymbol.unCurrencySymbol,
596
+ ),
597
+ tokenName: fromHex(fromText(assetInfo.iassetTokenNameAscii)),
598
+ };
599
+
600
+ // Send iAssets to treasury so liquidation is performed from a clean address.
601
+ await sendValueTo(
602
+ mkTreasuryAddr(context.lucid, sysParams),
603
+ mkAssetsOf(iassetAc, assetClassValueOf(user4Value, iassetAc)),
604
+ context.lucid,
605
+ );
606
+
607
+ const user4collateralAssetAmount = assetClassValueOf(
608
+ user4Value,
609
+ collateralAsset,
610
+ );
611
+
612
+ // Send collateral asset to treasury so liquidation is performed from a clean address.
613
+ if (collateralAsset !== adaAssetClass && user4collateralAssetAmount > 0n) {
614
+ await sendValueTo(
615
+ mkTreasuryAddr(context.lucid, sysParams),
616
+ mkAssetsOf(collateralAsset, user4collateralAssetAmount),
617
+ context.lucid,
618
+ );
619
+ }
620
+
621
+ const user4ValueBeforeLiquidation = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
622
+ addAssets(acc, utxo.assets),
623
+ )(await context.lucid.utxosAt(context.users['user4'].address));
624
+
625
+ assert(Object.keys(user4ValueBeforeLiquidation).length === 1);
626
+
627
+ context.emulator.awaitSlot(1000);
628
+
569
629
  await runFeedPriceToOracle(
570
630
  context,
571
631
  sysParams,
@@ -596,8 +656,29 @@ export async function executeLiquidation(
596
656
  (_) => new Error('Expected only single frozen CDP'),
597
657
  );
598
658
 
659
+ let treasuryUtxo = undefined;
660
+
661
+ if (!directTreasuryPayment) {
662
+ treasuryUtxo = await findRandomTreasuryUtxoWithAsset(
663
+ context.lucid,
664
+ sysParams,
665
+ collateralAsset,
666
+ );
667
+
668
+ // This treasury UTxO should contain ADA and collateral asset.
669
+ if (collateralAsset !== adaAssetClass) {
670
+ assert(Object.keys(treasuryUtxo.assets).length === 2);
671
+ }
672
+ }
673
+
599
674
  await liquidateWrapper(
600
- await runLiquidateCdp(context, sysParams, frozenCdp.utxo),
675
+ await runLiquidateCdp(
676
+ context,
677
+ sysParams,
678
+ frozenCdp.utxo,
679
+ treasuryUtxo,
680
+ directTreasuryPayment,
681
+ ),
601
682
  );
602
683
 
603
684
  await runFeedPriceToOracle(