@indigo-labs/indigo-sdk 0.1.19 → 0.1.21

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 (135) hide show
  1. package/.github/workflows/ci.yml +8 -3
  2. package/dist/index.d.mts +1298 -677
  3. package/dist/index.d.ts +1298 -677
  4. package/dist/index.js +4650 -2217
  5. package/dist/index.mjs +4592 -2192
  6. package/eslint.config.mjs +7 -1
  7. package/package.json +9 -4
  8. package/src/contracts/cdp/helpers.ts +167 -0
  9. package/src/contracts/cdp/scripts.ts +33 -0
  10. package/src/contracts/cdp/transactions.ts +1310 -0
  11. package/src/contracts/cdp/types.ts +161 -0
  12. package/src/contracts/cdp-creator/scripts.ts +39 -0
  13. package/src/{types/indigo/cdp-creator.ts → contracts/cdp-creator/types.ts} +6 -4
  14. package/src/contracts/collector/scripts.ts +32 -0
  15. package/src/contracts/collector/transactions.ts +44 -0
  16. package/src/contracts/execute/scripts.ts +48 -0
  17. package/src/contracts/execute/types.ts +57 -0
  18. package/src/contracts/gov/helpers.ts +157 -0
  19. package/src/contracts/gov/scripts.ts +34 -0
  20. package/src/contracts/gov/transactions.ts +1224 -0
  21. package/src/contracts/gov/types-new.ts +115 -0
  22. package/src/contracts/gov/types.ts +89 -0
  23. package/src/{helpers/interest-oracle.ts → contracts/interest-oracle/helpers.ts} +37 -9
  24. package/src/contracts/interest-oracle/scripts.ts +18 -0
  25. package/src/contracts/interest-oracle/transactions.ts +149 -0
  26. package/src/{types/indigo/interest-oracle.ts → contracts/interest-oracle/types.ts} +1 -1
  27. package/src/contracts/lrp/scripts.ts +27 -0
  28. package/src/contracts/{lrp.ts → lrp/transactions.ts} +23 -23
  29. package/src/{types/indigo/lrp.ts → contracts/lrp/types.ts} +3 -3
  30. package/src/{scripts/one-shot-policy.ts → contracts/one-shot/scripts.ts} +1 -1
  31. package/src/contracts/{one-shot.ts → one-shot/transactions.ts} +3 -3
  32. package/src/contracts/poll/helpers.ts +55 -0
  33. package/src/contracts/poll/scripts.ts +72 -0
  34. package/src/contracts/poll/types-poll-manager.ts +38 -0
  35. package/src/contracts/poll/types-poll-shard.ts +38 -0
  36. package/src/contracts/poll/types-poll.ts +88 -0
  37. package/src/{scripts/price-oracle-validator.ts → contracts/price-oracle/scripts.ts} +1 -4
  38. package/src/contracts/price-oracle/transactions.ts +112 -0
  39. package/src/{types/indigo/price-oracle.ts → contracts/price-oracle/types.ts} +16 -4
  40. package/src/{helpers/stability-pool-helpers.ts → contracts/stability-pool/helpers.ts} +110 -6
  41. package/src/contracts/stability-pool/scripts.ts +46 -0
  42. package/src/contracts/stability-pool/transactions.ts +660 -0
  43. package/src/contracts/stability-pool/types-new.ts +208 -0
  44. package/src/contracts/stability-pool/types.ts +42 -0
  45. package/src/contracts/staking/helpers.ts +116 -0
  46. package/src/contracts/staking/scripts.ts +41 -0
  47. package/src/contracts/staking/transactions.ts +268 -0
  48. package/src/contracts/staking/types-new.ts +81 -0
  49. package/src/contracts/staking/types.ts +41 -0
  50. package/src/contracts/treasury/scripts.ts +37 -0
  51. package/src/contracts/treasury/transactions.ts +44 -0
  52. package/src/contracts/treasury/types.ts +55 -0
  53. package/src/contracts/version-registry/scripts.ts +29 -0
  54. package/src/contracts/version-registry/types-new.ts +19 -0
  55. package/src/{types/indigo/version-record.ts → contracts/version-registry/types.ts} +1 -1
  56. package/src/contracts/vesting/helpers.ts +267 -0
  57. package/src/index.ts +38 -33
  58. package/src/types/evolution-schema-options.ts +16 -0
  59. package/src/types/generic.ts +78 -60
  60. package/src/types/on-chain-decimal.ts +22 -0
  61. package/src/types/system-params.ts +22 -11
  62. package/src/utils/bigint-utils.ts +7 -0
  63. package/src/{helpers → utils}/helper-txs.ts +1 -0
  64. package/src/utils/time-helpers.ts +4 -0
  65. package/src/{helpers/helpers.ts → utils/utils.ts} +0 -10
  66. package/src/{helpers → utils}/value-helpers.ts +10 -0
  67. package/src/{scripts → validators}/cdp-creator-validator.ts +4 -50
  68. package/src/{scripts → validators}/cdp-validator.ts +3 -5
  69. package/src/{scripts → validators}/collector-validator.ts +2 -3
  70. package/src/{scripts/version-registry.ts → validators/execute-validator.ts} +3 -11
  71. package/src/{scripts/gov-validator.ts → validators/governance-validator.ts} +3 -40
  72. package/src/{scripts → validators}/interest-oracle-validator.ts +4 -20
  73. package/src/validators/lrp-validator.ts +7 -0
  74. package/src/validators/poll-manager-validator.ts +7 -0
  75. package/src/{scripts → validators}/poll-shard-validator.ts +3 -43
  76. package/src/{scripts → validators}/stability-pool-validator.ts +4 -57
  77. package/src/{scripts → validators}/staking-validator.ts +2 -3
  78. package/src/{scripts → validators}/treasury-validator.ts +2 -3
  79. package/src/{scripts → validators}/version-record-policy.ts +4 -23
  80. package/src/validators/version-registry-validator.ts +7 -0
  81. package/tests/cdp.test.ts +1565 -0
  82. package/tests/datums.test.ts +125 -108
  83. package/tests/endpoints/initialize.ts +240 -338
  84. package/tests/gov.test.ts +1874 -0
  85. package/tests/hash-checks.test.ts +26 -21
  86. package/tests/indigo-test-helpers.ts +1 -55
  87. package/tests/initialize.test.ts +10 -5
  88. package/tests/interest-calculations.test.ts +18 -18
  89. package/tests/interest-oracle.test.ts +20 -18
  90. package/tests/lrp.test.ts +180 -78
  91. package/tests/mock/assets-mock.ts +59 -0
  92. package/tests/queries/cdp-queries.ts +144 -0
  93. package/tests/queries/collector-queries.ts +26 -0
  94. package/tests/queries/execute-queries.ts +46 -0
  95. package/tests/queries/governance-queries.ts +19 -16
  96. package/tests/queries/iasset-queries.ts +46 -23
  97. package/tests/queries/interest-oracle-queries.ts +3 -6
  98. package/tests/queries/lrp-queries.ts +2 -2
  99. package/tests/queries/poll-queries.ts +97 -0
  100. package/tests/queries/price-oracle-queries.ts +5 -22
  101. package/tests/queries/stability-pool-queries.ts +10 -8
  102. package/tests/queries/staking-queries.ts +28 -19
  103. package/tests/queries/treasury-queries.ts +19 -0
  104. package/tests/stability-pool.test.ts +186 -71
  105. package/tests/staking.test.ts +30 -23
  106. package/tests/test-helpers.ts +11 -2
  107. package/tests/utils/asserts.ts +13 -0
  108. package/tests/utils/index.ts +50 -0
  109. package/tsconfig.json +3 -1
  110. package/vitest.config.ts +1 -1
  111. package/.github/workflows/test.yml +0 -44
  112. package/src/contracts/cdp.ts +0 -748
  113. package/src/contracts/collector.ts +0 -98
  114. package/src/contracts/gov.ts +0 -1
  115. package/src/contracts/interest-oracle.ts +0 -149
  116. package/src/contracts/stability-pool.ts +0 -692
  117. package/src/contracts/staking.ts +0 -348
  118. package/src/contracts/treasury.ts +0 -112
  119. package/src/helpers/asset-helpers.ts +0 -57
  120. package/src/helpers/staking-helpers.ts +0 -94
  121. package/src/helpers/time-helpers.ts +0 -4
  122. package/src/scripts/execute-validator.ts +0 -52
  123. package/src/scripts/lrp-validator.ts +0 -40
  124. package/src/scripts/poll-manager-validator.ts +0 -52
  125. package/src/types/indigo/cdp.ts +0 -88
  126. package/src/types/indigo/execute.ts +0 -21
  127. package/src/types/indigo/gov.ts +0 -51
  128. package/src/types/indigo/poll-manager.ts +0 -21
  129. package/src/types/indigo/poll-shard.ts +0 -16
  130. package/src/types/indigo/stability-pool.ts +0 -233
  131. package/src/types/indigo/staking.ts +0 -99
  132. /package/src/{types/one-shot.ts → contracts/one-shot/types.ts} +0 -0
  133. /package/src/{helpers/price-oracle-helpers.ts → contracts/price-oracle/helpers.ts} +0 -0
  134. /package/src/{helpers → utils}/indigo-helpers.ts +0 -0
  135. /package/src/{helpers → utils}/lucid-utils.ts +0 -0
package/tests/lrp.test.ts CHANGED
@@ -11,23 +11,23 @@ import {
11
11
  UTxO,
12
12
  validatorToScriptHash,
13
13
  } from '@lucid-evolution/lucid';
14
- import { LRPParams, parseLrpDatum } from '../src/types/indigo/lrp';
15
- import { mkLrpValidator } from '../src/scripts/lrp-validator';
16
- import { runCreateScriptRefTx } from '../src/helpers/helper-txs';
17
- import { runOneShotMintTx } from '../src/contracts/one-shot';
14
+ import { LRPParams, parseLrpDatum } from '../src/contracts/lrp/types';
15
+ import { mkLrpValidator } from '../src/contracts/lrp/scripts';
16
+ import { runCreateScriptRefTx } from '../src/utils/helper-txs';
17
+ import { runOneShotMintTx } from '../src/contracts/one-shot/transactions';
18
18
  import {
19
19
  adjustLrp,
20
20
  cancelLrp,
21
21
  claimLrp,
22
22
  openLrp,
23
23
  redeemLrp,
24
- } from '../src/contracts/lrp';
24
+ } from '../src/contracts/lrp/transactions';
25
25
  import { findLrp } from './queries/lrp-queries';
26
- import { addrDetails, getInlineDatumOrThrow } from '../src/helpers/lucid-utils';
27
- import { runAndAwaitTx } from './test-helpers';
28
- import { matchSingle } from '../src/helpers/helpers';
29
- import { runCreateIAsset, runStartPriceOracle } from './indigo-test-helpers';
30
- import { mkPriceOracleValidator } from '../src/scripts/price-oracle-validator';
26
+ import { addrDetails, getInlineDatumOrThrow } from '../src/utils/lucid-utils';
27
+ import { runAndAwaitTx, runAndAwaitTxBuilder } from './test-helpers';
28
+ import { matchSingle } from '../src/utils/utils';
29
+ import { runCreateIAsset } from './indigo-test-helpers';
30
+ import { mkPriceOracleValidator } from '../src/contracts/price-oracle/scripts';
31
31
  import { AssetClass, OracleAssetNft, PriceOracleParams } from '../src';
32
32
  import { alwaysFailValidator } from '../src/scripts/always-fail-validator';
33
33
  import {
@@ -37,8 +37,9 @@ import {
37
37
  } from '../src/types/on-chain-decimal';
38
38
  import { findPriceOracle } from './queries/price-oracle-queries';
39
39
  import { findIAsset } from './queries/iasset-queries';
40
- import { assetClassValueOf, lovelacesAmt } from '../src/helpers/value-helpers';
40
+ import { assetClassValueOf, lovelacesAmt } from '../src/utils/value-helpers';
41
41
  import { strictEqual } from 'assert';
42
+ import { startPriceOracleTx } from '../src/contracts/price-oracle/transactions';
42
43
 
43
44
  type LRPTestContext = {
44
45
  iassetAc: AssetClass;
@@ -77,19 +78,19 @@ async function initTest(
77
78
  };
78
79
  const oracleValidator = mkPriceOracleValidator(priceOracleParams);
79
80
  const oracleValidatorHash = validatorToScriptHash(oracleValidator);
80
- const oracleNft = await runStartPriceOracle(
81
+ const [tx, oracleNft] = await startPriceOracleTx(
81
82
  lucid,
82
- oracleValidatorHash,
83
- priceOracleParams,
84
- network,
85
- fromText('ORACLE_IBTC'),
83
+ 'ORACLE_IBTC',
86
84
  iassetPrice,
85
+ priceOracleParams,
87
86
  );
88
87
 
88
+ await runAndAwaitTxBuilder(lucid, tx);
89
+
89
90
  const iassetValHash = validatorToScriptHash(alwaysFailValidator);
90
91
  const iassetNft = await runCreateIAsset(lucid, network, iassetValHash, {
91
92
  assetName: iassetTokenName,
92
- price: { Oracle: oracleNft },
93
+ price: { Oracle: { content: oracleNft } },
93
94
  interestOracleNft: { currencySymbol: '', tokenName: '' },
94
95
  redemptionRatio: OCD_ONE,
95
96
  maintenanceRatio: OCD_ONE,
@@ -199,7 +200,7 @@ describe('LRP', () => {
199
200
  lovelacesAmt(adjustedUtxo1.assets) >=
200
201
  parseLrpDatum(getInlineDatumOrThrow(adjustedUtxo1)).lovelacesToSpend,
201
202
  'Lovelaces to spend has to be smaller than actual lovelaces in UTXO',
202
- );
203
+ ).toBeTruthy();
203
204
 
204
205
  await runAndAwaitTx(
205
206
  lucid,
@@ -234,7 +235,7 @@ describe('LRP', () => {
234
235
  lovelacesAmt(adjustedUtxo2.assets) >=
235
236
  parseLrpDatum(getInlineDatumOrThrow(adjustedUtxo2)).lovelacesToSpend,
236
237
  'Lovelaces to spend has to be smaller than actual lovelaces in UTXO',
237
- );
238
+ ).toBeTruthy();
238
239
  });
239
240
 
240
241
  it('claim', async () => {
@@ -323,18 +324,15 @@ describe('LRP', () => {
323
324
  redeemLrp(
324
325
  [[lrpUtxo, redemptionIAssetAmt]],
325
326
  lrpRefScriptOutRef,
326
- await findPriceOracle(
327
- lucid,
328
- network,
329
- testCtx.oracleValHash,
330
- testCtx.oracleNft,
331
- ),
332
- await findIAsset(
333
- lucid,
334
- testCtx.iassetValHash,
335
- testCtx.iassetNft,
336
- toText(iassetTokenName),
337
- ),
327
+ await findPriceOracle(lucid, testCtx.oracleNft),
328
+ (
329
+ await findIAsset(
330
+ lucid,
331
+ testCtx.iassetValHash,
332
+ testCtx.iassetNft,
333
+ toText(iassetTokenName),
334
+ )
335
+ ).utxo,
338
336
  lucid,
339
337
  lrpParams,
340
338
  network,
@@ -449,18 +447,15 @@ describe('LRP', () => {
449
447
  redeemLrp(
450
448
  [[lrpUtxo, redemptionIAssetAmt]],
451
449
  lrpRefScriptOutRef,
452
- await findPriceOracle(
453
- lucid,
454
- network,
455
- testCtx.oracleValHash,
456
- testCtx.oracleNft,
457
- ),
458
- await findIAsset(
459
- lucid,
460
- testCtx.iassetValHash,
461
- testCtx.iassetNft,
462
- toText(iassetTokenName),
463
- ),
450
+ await findPriceOracle(lucid, testCtx.oracleNft),
451
+ (
452
+ await findIAsset(
453
+ lucid,
454
+ testCtx.iassetValHash,
455
+ testCtx.iassetNft,
456
+ toText(iassetTokenName),
457
+ )
458
+ ).utxo,
464
459
  lucid,
465
460
  lrpParams,
466
461
  network,
@@ -495,7 +490,7 @@ describe('LRP', () => {
495
490
  );
496
491
  });
497
492
 
498
- it('redeem, redeem again and cancel', async () => {
493
+ it('single redemption and cancel', async () => {
499
494
  const network: Network = 'Custom';
500
495
  const account1 = generateEmulatorAccount({
501
496
  lovelace: 80_000_000_000n, // 80,000 ADA
@@ -581,18 +576,130 @@ describe('LRP', () => {
581
576
  redeemLrp(
582
577
  [[lrpUtxo, redemptionIAssetAmt]],
583
578
  lrpRefScriptOutRef,
584
- await findPriceOracle(
579
+ await findPriceOracle(lucid, testCtx.oracleNft),
580
+ (
581
+ await findIAsset(
582
+ lucid,
583
+ testCtx.iassetValHash,
584
+ testCtx.iassetNft,
585
+ toText(iassetTokenName),
586
+ )
587
+ ).utxo,
588
+ lucid,
589
+ lrpParams,
590
+ network,
591
+ ),
592
+ );
593
+
594
+ const redeemedLrp = await findSingleOwnLrp();
595
+
596
+ strictEqual(
597
+ assetClassValueOf(redeemedLrp.assets, redemptionAsset),
598
+ redemptionIAssetAmt,
599
+ 'LRP has wrong number of iassets after redemption',
600
+ );
601
+
602
+ await runAndAwaitTx(
603
+ lucid,
604
+ cancelLrp(redeemedLrp, lrpRefScriptOutRef, lucid),
605
+ );
606
+ });
607
+
608
+ it('redeem, redeem again and cancel', async () => {
609
+ const network: Network = 'Custom';
610
+ const account1 = generateEmulatorAccount({
611
+ lovelace: 80_000_000_000n, // 80,000 ADA
612
+ });
613
+
614
+ const emulator = new Emulator([account1]);
615
+ const lucid = await Lucid(emulator, network);
616
+
617
+ lucid.selectWallet.fromSeed(account1.seedPhrase);
618
+
619
+ const iassetTokenName = fromText('iBTC');
620
+ const testCtx = await initTest(
621
+ lucid,
622
+ network,
623
+ iassetTokenName,
624
+ 10_000_000n,
625
+ OCD_ONE,
626
+ );
627
+
628
+ const [ownPkh, _] = await addrDetails(lucid);
629
+
630
+ const lrpParams: LRPParams = {
631
+ versionRecordToken: {
632
+ currencySymbol: fromText('smth'),
633
+ tokenName: fromText('version_record'),
634
+ },
635
+ iassetNft: testCtx.iassetNft,
636
+ iassetPolicyId: testCtx.iassetAc.currencySymbol,
637
+ minRedemptionLovelacesAmt: 1_000_000n,
638
+ };
639
+
640
+ const lrpValidator = mkLrpValidator(lrpParams);
641
+ const lrpValidatorHash = validatorToScriptHash(lrpValidator);
642
+ const lrpRefScriptOutRef = await runCreateScriptRefTx(
643
+ lucid,
644
+ lrpValidator,
645
+ network,
646
+ );
647
+
648
+ const findSingleOwnLrp = async (): Promise<UTxO> => {
649
+ return matchSingle(
650
+ await findLrp(
585
651
  lucid,
586
652
  network,
587
- testCtx.oracleValHash,
588
- testCtx.oracleNft,
589
- ),
590
- await findIAsset(
591
- lucid,
592
- testCtx.iassetValHash,
593
- testCtx.iassetNft,
594
- toText(iassetTokenName),
653
+ lrpValidatorHash,
654
+ ownPkh.hash,
655
+ iassetTokenName,
595
656
  ),
657
+ (res) =>
658
+ new Error('Expected a single LRP UTXO.: ' + JSON.stringify(res)),
659
+ );
660
+ };
661
+
662
+ await runAndAwaitTx(
663
+ lucid,
664
+ openLrp(
665
+ iassetTokenName,
666
+ 20_000_000n,
667
+ { getOnChainInt: 1_000_000n },
668
+ lucid,
669
+ lrpValidatorHash,
670
+ network,
671
+ ),
672
+ );
673
+
674
+ const lrpUtxo = await findSingleOwnLrp();
675
+
676
+ const redemptionAsset: AssetClass = {
677
+ currencySymbol: testCtx.iassetAc.currencySymbol,
678
+ tokenName: iassetTokenName,
679
+ };
680
+
681
+ strictEqual(
682
+ assetClassValueOf(lrpUtxo.assets, redemptionAsset),
683
+ 0n,
684
+ 'LRP should have no iassets before redemption',
685
+ );
686
+
687
+ const redemptionIAssetAmt = 5_000_000n;
688
+
689
+ await runAndAwaitTx(
690
+ lucid,
691
+ redeemLrp(
692
+ [[lrpUtxo, redemptionIAssetAmt]],
693
+ lrpRefScriptOutRef,
694
+ await findPriceOracle(lucid, testCtx.oracleNft),
695
+ (
696
+ await findIAsset(
697
+ lucid,
698
+ testCtx.iassetValHash,
699
+ testCtx.iassetNft,
700
+ toText(iassetTokenName),
701
+ )
702
+ ).utxo,
596
703
  lucid,
597
704
  lrpParams,
598
705
  network,
@@ -606,29 +713,27 @@ describe('LRP', () => {
606
713
  redeemLrp(
607
714
  [[redeemedLrp, redemptionIAssetAmt]],
608
715
  lrpRefScriptOutRef,
609
- await findPriceOracle(
610
- lucid,
611
- network,
612
- testCtx.oracleValHash,
613
- testCtx.oracleNft,
614
- ),
615
- await findIAsset(
616
- lucid,
617
- testCtx.iassetValHash,
618
- testCtx.iassetNft,
619
- toText(iassetTokenName),
620
- ),
716
+ await findPriceOracle(lucid, testCtx.oracleNft),
717
+ (
718
+ await findIAsset(
719
+ lucid,
720
+ testCtx.iassetValHash,
721
+ testCtx.iassetNft,
722
+ toText(iassetTokenName),
723
+ )
724
+ ).utxo,
621
725
  lucid,
622
726
  lrpParams,
623
727
  network,
624
728
  ),
625
729
  );
626
-
730
+
627
731
  strictEqual(
628
732
  assetClassValueOf(redeemedLrp.assets, redemptionAsset),
629
733
  redemptionIAssetAmt,
630
734
  'LRP has wrong number of iassets after redemption',
631
735
  );
736
+
632
737
  const closableLrp = await findSingleOwnLrp();
633
738
 
634
739
  await runAndAwaitTx(
@@ -736,18 +841,15 @@ describe('LRP', () => {
736
841
  [lrpUtxo2, 4_000_000n],
737
842
  ],
738
843
  lrpRefScriptOutRef,
739
- await findPriceOracle(
740
- lucid,
741
- network,
742
- testCtx.oracleValHash,
743
- testCtx.oracleNft,
744
- ),
745
- await findIAsset(
746
- lucid,
747
- testCtx.iassetValHash,
748
- testCtx.iassetNft,
749
- toText(iassetTokenName),
750
- ),
844
+ await findPriceOracle(lucid, testCtx.oracleNft),
845
+ (
846
+ await findIAsset(
847
+ lucid,
848
+ testCtx.iassetValHash,
849
+ testCtx.iassetNft,
850
+ toText(iassetTokenName),
851
+ )
852
+ ).utxo,
751
853
  lucid,
752
854
  lrpParams,
753
855
  network,
@@ -0,0 +1,59 @@
1
+ export type InitialAsset = {
2
+ name: string;
3
+ priceOracle: {
4
+ tokenName: string;
5
+ startPrice: bigint;
6
+ params: {
7
+ biasTime: bigint;
8
+ expirationTime: bigint;
9
+ };
10
+ };
11
+ initerestOracle: {
12
+ tokenName: string;
13
+ initialInterestRate: bigint;
14
+ params: {
15
+ biasTime: bigint;
16
+ };
17
+ };
18
+ redemptionRatioPercentage: bigint;
19
+ maintenanceRatioPercentage: bigint;
20
+ liquidationRatioPercentage: bigint;
21
+ debtMintingFeePercentage: bigint;
22
+ liquidationProcessingFeePercentage: bigint;
23
+ stabilityPoolWithdrawalFeePercentage: bigint;
24
+ redemptionReimbursementPercentage: bigint;
25
+ redemptionProcessingFeePercentage: bigint;
26
+ interestCollectorPortionPercentage: bigint;
27
+ firstAsset: boolean;
28
+ nextAsset?: string;
29
+ };
30
+
31
+ export const iusdInitialAssetCfg: InitialAsset = {
32
+ name: 'iUSD',
33
+ priceOracle: {
34
+ tokenName: 'iUSD_ORACLE',
35
+ startPrice: 1_000_000n,
36
+ params: {
37
+ biasTime: 120_000n,
38
+ expirationTime: 1_800_000n,
39
+ },
40
+ },
41
+ initerestOracle: {
42
+ tokenName: 'iUSD_ORACLE',
43
+ initialInterestRate: 1_000_000n,
44
+ params: {
45
+ biasTime: 120_000n,
46
+ },
47
+ },
48
+ redemptionRatioPercentage: 200_000_000n,
49
+ maintenanceRatioPercentage: 150_000_000n,
50
+ liquidationRatioPercentage: 120_000_000n,
51
+ debtMintingFeePercentage: 500_000n,
52
+ liquidationProcessingFeePercentage: 2_000_000n,
53
+ stabilityPoolWithdrawalFeePercentage: 500_000n,
54
+ redemptionReimbursementPercentage: 1_000_000n,
55
+ redemptionProcessingFeePercentage: 1_000_000n,
56
+ interestCollectorPortionPercentage: 40_000_000n,
57
+ firstAsset: true,
58
+ nextAsset: undefined,
59
+ };
@@ -0,0 +1,144 @@
1
+ import {
2
+ Credential,
3
+ fromText,
4
+ LucidEvolution,
5
+ ScriptHash,
6
+ UTxO,
7
+ } from '@lucid-evolution/lucid';
8
+ import {
9
+ AssetClass,
10
+ CDPContent,
11
+ createScriptAddress,
12
+ fromSystemParamsAsset,
13
+ getRandomElement,
14
+ matchSingle,
15
+ parseCdpDatum,
16
+ SystemParams,
17
+ } from '../../src';
18
+ import { assetClassToUnit } from '../../src/utils/value-helpers';
19
+ import { option as O, array as A, function as F } from 'fp-ts';
20
+
21
+ export async function findAllActiveCdps(
22
+ lucid: LucidEvolution,
23
+ sysParams: SystemParams,
24
+ assetAscii: string,
25
+ stakeCred?: Credential,
26
+ ): Promise<{ utxo: UTxO; datum: CDPContent }[]> {
27
+ const cdpUtxos = await lucid.utxosAtWithUnit(
28
+ createScriptAddress(
29
+ lucid.config().network!,
30
+ sysParams.validatorHashes.cdpHash,
31
+ stakeCred,
32
+ ),
33
+ assetClassToUnit(fromSystemParamsAsset(sysParams.cdpParams.cdpAuthToken)),
34
+ );
35
+
36
+ return F.pipe(
37
+ cdpUtxos.map((utxo) =>
38
+ F.pipe(
39
+ O.fromNullable(utxo.datum),
40
+ O.flatMap(parseCdpDatum),
41
+ O.flatMap((datum) => {
42
+ if (datum.iasset === fromText(assetAscii) && datum.cdpOwner) {
43
+ return O.some({ utxo, datum: datum });
44
+ } else {
45
+ return O.none;
46
+ }
47
+ }),
48
+ ),
49
+ ),
50
+ A.compact,
51
+ );
52
+ }
53
+
54
+ export async function findCdp(
55
+ lucid: LucidEvolution,
56
+ cdpScriptHash: ScriptHash,
57
+ cdpNft: AssetClass,
58
+ ownerPkh: string,
59
+ stakeCred?: Credential,
60
+ ): Promise<{ utxo: UTxO; datum: CDPContent }> {
61
+ const cdpUtxos = await lucid.utxosAtWithUnit(
62
+ createScriptAddress(lucid.config().network!, cdpScriptHash, stakeCred),
63
+ assetClassToUnit(cdpNft),
64
+ );
65
+
66
+ return matchSingle(
67
+ F.pipe(
68
+ cdpUtxos.map((utxo) =>
69
+ F.pipe(
70
+ O.fromNullable(utxo.datum),
71
+ O.flatMap(parseCdpDatum),
72
+ O.flatMap((datum) => {
73
+ if (datum.cdpOwner === ownerPkh) {
74
+ return O.some({ utxo, datum: datum });
75
+ } else {
76
+ return O.none;
77
+ }
78
+ }),
79
+ ),
80
+ ),
81
+ A.compact,
82
+ ),
83
+ (res) => new Error('Expected a single CDP UTXO.: ' + JSON.stringify(res)),
84
+ );
85
+ }
86
+
87
+ export async function findFrozenCDPs(
88
+ lucid: LucidEvolution,
89
+ cdpScriptHash: ScriptHash,
90
+ cdpNft: AssetClass,
91
+ assetAscii: string,
92
+ ): Promise<{ utxo: UTxO; datum: CDPContent }[]> {
93
+ const cdpUtxos = await lucid.utxosAtWithUnit(
94
+ createScriptAddress(lucid.config().network!, cdpScriptHash),
95
+ assetClassToUnit(cdpNft),
96
+ );
97
+
98
+ return F.pipe(
99
+ cdpUtxos.map((utxo) =>
100
+ F.pipe(
101
+ O.fromNullable(utxo.datum),
102
+ O.flatMap(parseCdpDatum),
103
+ O.flatMap((datum) => {
104
+ if (datum.cdpOwner == null && datum.iasset === fromText(assetAscii)) {
105
+ return O.some({ utxo, datum: datum });
106
+ } else {
107
+ return O.none;
108
+ }
109
+ }),
110
+ ),
111
+ ),
112
+ A.compact,
113
+ );
114
+ }
115
+
116
+ export async function findAllCdpCreators(
117
+ lucid: LucidEvolution,
118
+ cdpCreatorScriptHash: string,
119
+ cdpCreatorNft: AssetClass,
120
+ ): Promise<UTxO[]> {
121
+ return lucid.utxosAtWithUnit(
122
+ createScriptAddress(lucid.config().network!, cdpCreatorScriptHash),
123
+ assetClassToUnit(cdpCreatorNft),
124
+ );
125
+ }
126
+
127
+ export async function findRandomCdpCreator(
128
+ lucid: LucidEvolution,
129
+ cdpCreatorScriptHash: string,
130
+ cdpCreatorNft: AssetClass,
131
+ ): Promise<UTxO> {
132
+ const cdpCreatorUtxos = await findAllCdpCreators(
133
+ lucid,
134
+ cdpCreatorScriptHash,
135
+ cdpCreatorNft,
136
+ );
137
+
138
+ return F.pipe(
139
+ O.fromNullable(getRandomElement(cdpCreatorUtxos)),
140
+ O.match(() => {
141
+ throw new Error('Expected some cdp creator UTXOs.');
142
+ }, F.identity),
143
+ );
144
+ }
@@ -0,0 +1,26 @@
1
+ import { LucidEvolution, UTxO } from '@lucid-evolution/lucid';
2
+ import { createScriptAddress, getRandomElement } from '../../src';
3
+ import { option as O, function as F } from 'fp-ts';
4
+
5
+ export async function findAllCollectors(
6
+ lucid: LucidEvolution,
7
+ collectorScriptHash: string,
8
+ ): Promise<UTxO[]> {
9
+ return lucid.utxosAt(
10
+ createScriptAddress(lucid.config().network!, collectorScriptHash),
11
+ );
12
+ }
13
+
14
+ export async function findRandomCollector(
15
+ lucid: LucidEvolution,
16
+ collectorScriptHash: string,
17
+ ): Promise<UTxO> {
18
+ const allCollectors = await findAllCollectors(lucid, collectorScriptHash);
19
+
20
+ return F.pipe(
21
+ O.fromNullable(getRandomElement(allCollectors)),
22
+ O.match(() => {
23
+ throw new Error('Expected some poll shard UTXOs.');
24
+ }, F.identity),
25
+ );
26
+ }
@@ -0,0 +1,46 @@
1
+ import { LucidEvolution, ScriptHash, UTxO } from '@lucid-evolution/lucid';
2
+ import {
3
+ AssetClass,
4
+ createScriptAddress,
5
+ ExecuteDatum,
6
+ matchSingle,
7
+ parseExecuteDatum,
8
+ } from '../../src';
9
+ import { assetClassToUnit } from '../../src/utils/value-helpers';
10
+ import { option as O, array as A, function as F } from 'fp-ts';
11
+
12
+ export async function findExecute(
13
+ lucid: LucidEvolution,
14
+ executeHash: ScriptHash,
15
+ executeNft: AssetClass,
16
+ upgradeId: bigint,
17
+ ): Promise<{
18
+ utxo: UTxO;
19
+ datum: ExecuteDatum;
20
+ }> {
21
+ const executeUtxos = await lucid.utxosAtWithUnit(
22
+ createScriptAddress(lucid.config().network!, executeHash),
23
+ assetClassToUnit(executeNft),
24
+ );
25
+
26
+ return matchSingle(
27
+ F.pipe(
28
+ executeUtxos.map((utxo) =>
29
+ F.pipe(
30
+ O.fromNullable(utxo.datum),
31
+ O.flatMap(parseExecuteDatum),
32
+ O.flatMap((datum) => {
33
+ if (datum.id === upgradeId) {
34
+ return O.some({ utxo, datum: datum });
35
+ } else {
36
+ return O.none;
37
+ }
38
+ }),
39
+ ),
40
+ ),
41
+ A.compact,
42
+ ),
43
+ (res) =>
44
+ new Error('Expected a single Execute UTXO.: ' + JSON.stringify(res)),
45
+ );
46
+ }
@@ -1,31 +1,34 @@
1
1
  import { LucidEvolution, ScriptHash, UTxO } from '@lucid-evolution/lucid';
2
- import { createScriptAddress } from '../../src/helpers/lucid-utils';
2
+ import { createScriptAddress } from '../../src/utils/lucid-utils';
3
3
  import { AssetClass } from '../../src/types/generic';
4
- import { assetClassToUnit } from '../../src/helpers/value-helpers';
5
- import { matchSingle, parseGovDatum } from '../../src';
4
+ import { assetClassToUnit } from '../../src/utils/value-helpers';
5
+ import { GovDatum, matchSingle, parseGovDatum } from '../../src';
6
+ import { option as O, array as A, function as F } from 'fp-ts';
6
7
 
7
8
  export async function findGov(
8
9
  lucid: LucidEvolution,
9
10
  govScriptHash: ScriptHash,
10
11
  govNft: AssetClass,
11
- ): Promise<UTxO> {
12
+ ): Promise<{
13
+ utxo: UTxO;
14
+ datum: GovDatum;
15
+ }> {
12
16
  const govUtxos = await lucid.utxosAtWithUnit(
13
- createScriptAddress(lucid.config().network, govScriptHash),
17
+ createScriptAddress(lucid.config().network!, govScriptHash),
14
18
  assetClassToUnit(govNft),
15
19
  );
16
20
 
17
21
  return matchSingle(
18
- govUtxos.filter((utxo) => {
19
- if (utxo.datum != null) {
20
- try {
21
- parseGovDatum(utxo.datum);
22
- return true; // TODO: implement Gov Datum
23
- } catch (_) {
24
- // when incompatible datum
25
- return false;
26
- }
27
- }
28
- }),
22
+ F.pipe(
23
+ govUtxos.map((utxo) =>
24
+ F.pipe(
25
+ O.fromNullable(utxo.datum),
26
+ O.flatMap(parseGovDatum),
27
+ O.map((datum) => ({ utxo, datum: datum })),
28
+ ),
29
+ ),
30
+ A.compact,
31
+ ),
29
32
  (res) => new Error('Expected a single Gov UTXO.: ' + JSON.stringify(res)),
30
33
  );
31
34
  }