@indigo-labs/indigo-sdk 0.1.28 → 0.2.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 (135) hide show
  1. package/.github/workflows/ci.yml +8 -3
  2. package/dist/index.d.mts +1300 -671
  3. package/dist/index.d.ts +1300 -671
  4. package/dist/index.js +4663 -2220
  5. package/dist/index.mjs +4594 -2190
  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} +14 -14
  29. package/src/{types/indigo/lrp.ts → contracts/lrp/types.ts} +2 -2
  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 +676 -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 +39 -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 +191 -65
  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 -752
  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 -690
  117. package/src/contracts/staking.ts +0 -348
  118. package/src/contracts/treasury.ts +0 -112
  119. package/src/helpers/asset-helpers.ts +0 -62
  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
@@ -0,0 +1,72 @@
1
+ import { applyParamsToScript, SpendingValidator } from '@lucid-evolution/lucid';
2
+ import {
3
+ castPollManagerParams,
4
+ PollManagerParams,
5
+ } from '../poll/types-poll-manager';
6
+ import {
7
+ PollManagerParamsSP,
8
+ PollShardParamsSP,
9
+ fromSystemParamsAsset,
10
+ } from '../../types/system-params';
11
+ import { castPollShardParams, PollShardParams } from './types-poll-shard';
12
+ import { _pollManagerValidator } from '../../validators/poll-manager-validator';
13
+ import { _pollShardValidator } from '../../validators/poll-shard-validator';
14
+
15
+ export const mkPollManagerValidator = (
16
+ params: PollManagerParams,
17
+ ): SpendingValidator => {
18
+ return {
19
+ type: 'PlutusV2',
20
+ script: applyParamsToScript(_pollManagerValidator.cborHex, [
21
+ castPollManagerParams(params),
22
+ ]),
23
+ };
24
+ };
25
+
26
+ export const mkPollManagerValidatorFromSP = (
27
+ params: PollManagerParamsSP,
28
+ ): SpendingValidator => {
29
+ return {
30
+ type: 'PlutusV2',
31
+ script: applyParamsToScript(_pollManagerValidator.cborHex, [
32
+ castPollManagerParams({
33
+ govNFT: fromSystemParamsAsset(params.govNFT),
34
+ pollToken: fromSystemParamsAsset(params.pollToken),
35
+ upgradeToken: fromSystemParamsAsset(params.upgradeToken),
36
+ indyAsset: fromSystemParamsAsset(params.indyAsset),
37
+ govExecuteValHash: params.govExecuteValHash,
38
+ pBiasTime: BigInt(params.pBiasTime),
39
+ shardValHash: params.shardsValHash,
40
+ treasuryValHash: params.treasuryValHash,
41
+ initialIndyDistribution: BigInt(params.initialIndyDistribution),
42
+ }),
43
+ ]),
44
+ };
45
+ };
46
+
47
+ export const mkPollShardValidator = (
48
+ params: PollShardParams,
49
+ ): SpendingValidator => {
50
+ return {
51
+ type: 'PlutusV2',
52
+ script: applyParamsToScript(_pollShardValidator.cborHex, [
53
+ castPollShardParams(params),
54
+ ]),
55
+ };
56
+ };
57
+
58
+ export const mkPollShardValidatorFromSP = (
59
+ params: PollShardParamsSP,
60
+ ): SpendingValidator => {
61
+ return {
62
+ type: 'PlutusV2',
63
+ script: applyParamsToScript(_pollShardValidator.cborHex, [
64
+ castPollShardParams({
65
+ pollToken: fromSystemParamsAsset(params.pollToken),
66
+ stakingToken: fromSystemParamsAsset(params.stakingToken),
67
+ indyAsset: fromSystemParamsAsset(params.indyAsset),
68
+ stakingValHash: params.stakingValHash,
69
+ }),
70
+ ]),
71
+ };
72
+ };
@@ -0,0 +1,38 @@
1
+ import { Data, Redeemer } from '@lucid-evolution/lucid';
2
+ import { AssetClassSchema } from '../../types/generic';
3
+
4
+ const PollManagerParamsSchema = Data.Object({
5
+ govNFT: AssetClassSchema,
6
+ pollToken: AssetClassSchema,
7
+ upgradeToken: AssetClassSchema,
8
+ indyAsset: AssetClassSchema,
9
+ govExecuteValHash: Data.Bytes(),
10
+ pBiasTime: Data.Integer(),
11
+ shardValHash: Data.Bytes(),
12
+ treasuryValHash: Data.Bytes(),
13
+ initialIndyDistribution: Data.Integer(),
14
+ });
15
+ export type PollManagerParams = Data.Static<typeof PollManagerParamsSchema>;
16
+ export const PollManagerParams =
17
+ PollManagerParamsSchema as unknown as PollManagerParams;
18
+
19
+ const PollManagerRedeemerSchema = Data.Enum([
20
+ Data.Object({
21
+ EndPoll: Data.Object({ currentTime: Data.Integer() }),
22
+ }),
23
+ Data.Object({ CreateShards: Data.Object({ currentTime: Data.Integer() }) }),
24
+ Data.Object({
25
+ MergeShardsManager: Data.Object({ currentTime: Data.Integer() }),
26
+ }),
27
+ ]);
28
+ export type PollManagerRedeemer = Data.Static<typeof PollManagerRedeemerSchema>;
29
+ export const PollManagerRedeemer =
30
+ PollManagerRedeemerSchema as unknown as PollManagerRedeemer;
31
+
32
+ export function serialisePollManagerRedeemer(r: PollManagerRedeemer): Redeemer {
33
+ return Data.to<PollManagerRedeemer>(r, PollManagerRedeemer);
34
+ }
35
+
36
+ export function castPollManagerParams(params: PollManagerParams): Data {
37
+ return Data.castTo(params, PollManagerParams);
38
+ }
@@ -0,0 +1,38 @@
1
+ import { Data, Redeemer } from '@lucid-evolution/lucid';
2
+ import { AssetClassSchema, OutputReferenceSchema } from '../../types/generic';
3
+
4
+ const PollShardParamsSchema = Data.Object({
5
+ pollToken: AssetClassSchema,
6
+ stakingToken: AssetClassSchema,
7
+ indyAsset: AssetClassSchema,
8
+ stakingValHash: Data.Bytes(),
9
+ });
10
+ export type PollShardParams = Data.Static<typeof PollShardParamsSchema>;
11
+ export const PollShardParams =
12
+ PollShardParamsSchema as unknown as PollShardParams;
13
+
14
+ const VoteOptionSchema = Data.Enum([Data.Literal('Yes'), Data.Literal('No')]);
15
+ export type VoteOption = Data.Static<typeof VoteOptionSchema>;
16
+
17
+ const PollShardRedeemerSchema = Data.Enum([
18
+ Data.Object({ Vote: Data.Object({ content: VoteOptionSchema }) }),
19
+ Data.Object({
20
+ MergeShards: Data.Object({
21
+ currentTime: Data.Integer(),
22
+ pollManagerRef: OutputReferenceSchema,
23
+ }),
24
+ }),
25
+ ]);
26
+ export type PollShardRedeemer = Data.Static<typeof PollShardRedeemerSchema>;
27
+ export const PollShardRedeemer =
28
+ PollShardRedeemerSchema as unknown as PollShardRedeemer;
29
+
30
+ export function serialisePollShardRedeemer(
31
+ redeemer: PollShardRedeemer,
32
+ ): Redeemer {
33
+ return Data.to<PollShardRedeemer>(redeemer, PollShardRedeemer);
34
+ }
35
+
36
+ export function castPollShardParams(params: PollShardParams): Data {
37
+ return Data.castTo(params, PollShardParams);
38
+ }
@@ -0,0 +1,88 @@
1
+ import { Data, Datum } from '@lucid-evolution/lucid';
2
+ import { TreasuryWithdrawalSchema } from '../gov/types';
3
+ import { AddressSchema } from '../../types/generic';
4
+ import { option as O, function as F } from 'fp-ts';
5
+ import { match, P } from 'ts-pattern';
6
+ import { ProposalContentSchema } from '../gov/types-new';
7
+
8
+ const PollStatusSchema = Data.Object({
9
+ yesVotes: Data.Integer(),
10
+ noVotes: Data.Integer(),
11
+ });
12
+ export type PollStatus = Data.Static<typeof PollStatusSchema>;
13
+
14
+ const PollManagerContentSchema = Data.Object({
15
+ pollId: Data.Integer(),
16
+ pollOwner: Data.Bytes(),
17
+ content: ProposalContentSchema,
18
+ treasuryWithdrawal: Data.Nullable(TreasuryWithdrawalSchema),
19
+ status: PollStatusSchema,
20
+ votingEndTime: Data.Integer(),
21
+ createdShardsCount: Data.Integer(),
22
+ talliedShardsCount: Data.Integer(),
23
+ totalShardsCount: Data.Integer(),
24
+ proposingEndTime: Data.Integer(),
25
+ expirationTime: Data.Integer(),
26
+ protocolVersion: Data.Integer(),
27
+ minimumQuorum: Data.Integer(),
28
+ });
29
+ export type PollManagerContent = Data.Static<typeof PollManagerContentSchema>;
30
+
31
+ const PollShardContentSchema = Data.Object({
32
+ pollId: Data.Integer(),
33
+ status: PollStatusSchema,
34
+ votingEndTime: Data.Integer(),
35
+ managerAddress: AddressSchema,
36
+ });
37
+ export type PollShardContent = Data.Static<typeof PollShardContentSchema>;
38
+
39
+ const PollDatumSchema = Data.Enum([
40
+ Data.Object({
41
+ PollManager: Data.Object({ content: PollManagerContentSchema }),
42
+ }),
43
+ Data.Object({ PollShard: Data.Object({ content: PollShardContentSchema }) }),
44
+ ]);
45
+ export type PollDatum = Data.Static<typeof PollDatumSchema>;
46
+ export const PollDatum = PollDatumSchema as unknown as PollDatum;
47
+
48
+ export function parsePollManager(datum: Datum): O.Option<PollManagerContent> {
49
+ try {
50
+ return match(Data.from<PollDatum>(datum, PollDatum))
51
+ .with({ PollManager: P.select() }, (res) => O.some(res.content))
52
+ .otherwise(() => O.none);
53
+ } catch (_) {
54
+ return O.none;
55
+ }
56
+ }
57
+
58
+ export function parsePollManagerOrThrow(datum: Datum): PollManagerContent {
59
+ return F.pipe(
60
+ parsePollManager(datum),
61
+ O.match(() => {
62
+ throw new Error('Expected a Poll manager datum.');
63
+ }, F.identity),
64
+ );
65
+ }
66
+
67
+ export function parsePollShard(datum: Datum): O.Option<PollShardContent> {
68
+ try {
69
+ return match(Data.from<PollDatum>(datum, PollDatum))
70
+ .with({ PollShard: P.select() }, (res) => O.some(res.content))
71
+ .otherwise(() => O.none);
72
+ } catch (_) {
73
+ return O.none;
74
+ }
75
+ }
76
+
77
+ export function parsePollShardOrThrow(datum: Datum): PollShardContent {
78
+ return F.pipe(
79
+ parsePollShard(datum),
80
+ O.match(() => {
81
+ throw new Error('Expected a Poll shard datum.');
82
+ }, F.identity),
83
+ );
84
+ }
85
+
86
+ export function serialisePollDatum(datum: PollDatum): Datum {
87
+ return Data.to<PollDatum>(datum, PollDatum);
88
+ }
@@ -3,10 +3,7 @@ import {
3
3
  ScriptType,
4
4
  SpendingValidator,
5
5
  } from '@lucid-evolution/lucid';
6
- import {
7
- castPriceOracleParams,
8
- PriceOracleParams,
9
- } from '../types/indigo/price-oracle';
6
+ import { castPriceOracleParams, PriceOracleParams } from './types';
10
7
 
11
8
  const priceOracleValidatorData = {
12
9
  type: 'PlutusV2' as ScriptType,
@@ -0,0 +1,112 @@
1
+ import {
2
+ addAssets,
3
+ fromText,
4
+ LucidEvolution,
5
+ OutRef,
6
+ slotToUnixTime,
7
+ TxBuilder,
8
+ validatorToAddress,
9
+ } from '@lucid-evolution/lucid';
10
+ import { AssetClass } from '../../types/generic';
11
+ import {
12
+ OracleAssetNft,
13
+ PriceOracleDatum,
14
+ PriceOracleParams,
15
+ serialisePriceOracleDatum,
16
+ serialisePriceOracleRedeemer,
17
+ } from './types';
18
+ import { mkPriceOracleValidator } from './scripts';
19
+ import { oneShotMintTx } from '../one-shot/transactions';
20
+ import { mkAssetsOf, mkLovelacesOf } from '../../utils/value-helpers';
21
+ import { OnChainDecimal } from '../../types/on-chain-decimal';
22
+ import { matchSingle } from '../../utils/utils';
23
+ import { ONE_SECOND } from '../../utils/time-helpers';
24
+
25
+ export async function startPriceOracleTx(
26
+ lucid: LucidEvolution,
27
+ assetName: string,
28
+ startPrice: OnChainDecimal,
29
+ oracleParams: PriceOracleParams,
30
+ now: number = Date.now(),
31
+ refOutRef?: OutRef,
32
+ ): Promise<[TxBuilder, OracleAssetNft]> {
33
+ if (!refOutRef) {
34
+ refOutRef = (await lucid.wallet().getUtxos())[0];
35
+ }
36
+
37
+ const [tx, oracleNftPolicyId] = await oneShotMintTx(lucid, {
38
+ referenceOutRef: {
39
+ txHash: refOutRef.txHash,
40
+ outputIdx: BigInt(refOutRef.outputIndex),
41
+ },
42
+ mintAmounts: [
43
+ {
44
+ tokenName: fromText(assetName),
45
+ amount: 1n,
46
+ },
47
+ ],
48
+ });
49
+
50
+ const priceOracleNft: AssetClass = {
51
+ currencySymbol: oracleNftPolicyId,
52
+ tokenName: fromText(assetName),
53
+ };
54
+
55
+ const oracleValidator = mkPriceOracleValidator(oracleParams);
56
+
57
+ const oracleDatum: PriceOracleDatum = {
58
+ price: startPrice,
59
+ expiration: BigInt(now) + oracleParams.expiration,
60
+ };
61
+
62
+ tx.pay.ToContract(
63
+ validatorToAddress(lucid.config().network!, oracleValidator),
64
+ { kind: 'inline', value: serialisePriceOracleDatum(oracleDatum) },
65
+ addAssets(mkLovelacesOf(5_000_000n), mkAssetsOf(priceOracleNft, 1n)),
66
+ );
67
+
68
+ return [tx, { oracleNft: priceOracleNft }];
69
+ }
70
+
71
+ export async function feedPriceOracleTx(
72
+ lucid: LucidEvolution,
73
+ oracleOref: OutRef,
74
+ newPrice: OnChainDecimal,
75
+ oracleParams: PriceOracleParams,
76
+ currentSlot: number,
77
+ ): Promise<TxBuilder> {
78
+ const network = lucid.config().network!;
79
+ const currentTime = BigInt(slotToUnixTime(network, currentSlot));
80
+
81
+ const priceOracleUtxo = matchSingle(
82
+ await lucid.utxosByOutRef([oracleOref]),
83
+ (_) => new Error('Expected a single price oracle UTXO'),
84
+ );
85
+
86
+ const oracleValidator = mkPriceOracleValidator(oracleParams);
87
+
88
+ return lucid
89
+ .newTx()
90
+ .validFrom(Number(currentTime - oracleParams.biasTime) + ONE_SECOND)
91
+ .validTo(Number(currentTime + oracleParams.biasTime) - ONE_SECOND)
92
+ .attach.SpendingValidator(oracleValidator)
93
+ .collectFrom(
94
+ [priceOracleUtxo],
95
+ serialisePriceOracleRedeemer({
96
+ currentTime: currentTime,
97
+ newPrice: newPrice,
98
+ }),
99
+ )
100
+ .pay.ToContract(
101
+ priceOracleUtxo.address,
102
+ {
103
+ kind: 'inline',
104
+ value: serialisePriceOracleDatum({
105
+ price: newPrice,
106
+ expiration: currentTime + oracleParams.expiration,
107
+ }),
108
+ },
109
+ priceOracleUtxo.assets,
110
+ )
111
+ .addSignerKey(oracleParams.owner);
112
+ }
@@ -1,9 +1,9 @@
1
- import { Data, Datum } from '@lucid-evolution/lucid';
2
- import { AssetClassSchema } from '../generic';
3
- import { OnChainDecimalSchema } from '../on-chain-decimal';
1
+ import { Data, Datum, Redeemer } from '@lucid-evolution/lucid';
2
+ import { AssetClassSchema } from '../../types/generic';
3
+ import { OnChainDecimalSchema } from '../../types/on-chain-decimal';
4
4
 
5
5
  export const OracleAssetNftSchema = Data.Object({
6
- oracleNft: Data.Object({ asset: AssetClassSchema }),
6
+ oracleNft: AssetClassSchema,
7
7
  });
8
8
  export type OracleAssetNft = Data.Static<typeof OracleAssetNftSchema>;
9
9
 
@@ -25,6 +25,18 @@ export const PriceOracleDatumSchema = Data.Object({
25
25
  export type PriceOracleDatum = Data.Static<typeof PriceOracleDatumSchema>;
26
26
  const PriceOracleDatum = PriceOracleDatumSchema as unknown as PriceOracleDatum;
27
27
 
28
+ const PriceOracleRedeemerSchema = Data.Object({
29
+ currentTime: Data.Integer(),
30
+ newPrice: OnChainDecimalSchema,
31
+ });
32
+ export type PriceOracleRedeemer = Data.Static<typeof PriceOracleRedeemerSchema>;
33
+ const PriceOracleRedeemer =
34
+ PriceOracleRedeemerSchema as unknown as PriceOracleRedeemer;
35
+
36
+ export function serialisePriceOracleRedeemer(r: PriceOracleRedeemer): Redeemer {
37
+ return Data.to<PriceOracleRedeemer>(r, PriceOracleRedeemer);
38
+ }
39
+
28
40
  export function parsePriceOracleDatum(datum: Datum): PriceOracleDatum {
29
41
  return Data.from<PriceOracleDatum>(datum, PriceOracleDatum);
30
42
  }
@@ -1,4 +1,5 @@
1
1
  import { UTxO } from '@lucid-evolution/lucid';
2
+ import { getInlineDatumOrThrow } from '../../utils/lucid-utils';
2
3
  import {
3
4
  EpochToScaleToSum,
4
5
  fromSPInteger,
@@ -10,10 +11,23 @@ import {
10
11
  SPInteger,
11
12
  spMul,
12
13
  spSub,
14
+ StabilityPoolContent,
13
15
  StabilityPoolSnapshot,
14
- } from '../types/indigo/stability-pool';
16
+ } from './types-new';
17
+ import { match } from 'ts-pattern';
15
18
 
16
- const newScaleMultiplier = 1000000000n;
19
+ const newScaleMultiplier = 1_000_000_000n;
20
+
21
+ export const initSpSnapshot: StabilityPoolSnapshot = {
22
+ productVal: { value: 1_000_000_000_000_000_000n },
23
+ depositVal: { value: 0n },
24
+ sumVal: { value: 0n },
25
+ epoch: 0n,
26
+ scale: 0n,
27
+ };
28
+
29
+ export const initEpochToScaleToSumMap = (): EpochToScaleToSum =>
30
+ new Map([[{ epoch: 0n, scale: 0n }, { value: 0n }]]);
17
31
 
18
32
  export function getSumFromEpochToScaleToSum(
19
33
  e2s2s: EpochToScaleToSum,
@@ -28,6 +42,9 @@ export function getSumFromEpochToScaleToSum(
28
42
  return undefined;
29
43
  }
30
44
 
45
+ /**
46
+ * It's necessary to use this to update map entries because typescript can't compare keys as objects.
47
+ */
31
48
  export function setSumInEpochToScaleToSum(
32
49
  e2s2s: EpochToScaleToSum,
33
50
  epoch: bigint,
@@ -42,7 +59,6 @@ export function setSumInEpochToScaleToSum(
42
59
  }
43
60
 
44
61
  map.set({ epoch, scale }, sum);
45
-
46
62
  return map;
47
63
  }
48
64
 
@@ -104,11 +120,11 @@ function findEpochToScaleToSum(
104
120
  let ess1: EpochToScaleToSum;
105
121
  try {
106
122
  ess1 = parseSnapshotEpochToScaleToSumDatum(
107
- snapshotEpochToScaleToSumTokenRef1.datum,
123
+ getInlineDatumOrThrow(snapshotEpochToScaleToSumTokenRef1),
108
124
  ).snapshot;
109
125
  } catch (_) {
110
126
  ess1 = parseStabilityPoolDatum(
111
- snapshotEpochToScaleToSumTokenRef1.datum,
127
+ getInlineDatumOrThrow(snapshotEpochToScaleToSumTokenRef1),
112
128
  ).epochToScaleToSum;
113
129
  }
114
130
 
@@ -119,7 +135,7 @@ function findEpochToScaleToSum(
119
135
 
120
136
  if (snapshotEpochToScaleToSumTokenRef2) {
121
137
  const ess2 = parseSnapshotEpochToScaleToSumDatum(
122
- snapshotEpochToScaleToSumTokenRef2.datum,
138
+ getInlineDatumOrThrow(snapshotEpochToScaleToSumTokenRef2),
123
139
  );
124
140
  const ess2Ref: SnapshotESSSearchResult = {
125
141
  utxo: snapshotEpochToScaleToSumTokenRef2,
@@ -205,3 +221,91 @@ export function updatePoolSnapshotWithdrawalFee(
205
221
 
206
222
  return [newPoolDepositVal, newPoolProduct];
207
223
  }
224
+
225
+ export function liquidationHelper(
226
+ spContent: StabilityPoolContent,
227
+ iassetBurnAmt: bigint,
228
+ /**
229
+ * The collateral absorbed
230
+ */
231
+ reward: bigint,
232
+ ): { newSpContent: StabilityPoolContent } {
233
+ const lossPerUnitStaked = spDiv(
234
+ mkSPInteger(iassetBurnAmt),
235
+ spContent.poolSnapshot.depositVal,
236
+ );
237
+ const productFactor = spSub(mkSPInteger(1n), lossPerUnitStaked);
238
+
239
+ const isScaleIncrease =
240
+ spMul(spContent.poolSnapshot.productVal, productFactor).value <
241
+ newScaleMultiplier;
242
+
243
+ const newSumSnapshot = spAdd(
244
+ spContent.poolSnapshot.sumVal,
245
+ spDiv(
246
+ spMul(mkSPInteger(reward), spContent.poolSnapshot.productVal),
247
+ spContent.poolSnapshot.depositVal,
248
+ ),
249
+ );
250
+ const newProductSnapshot = spMul(
251
+ {
252
+ value:
253
+ spContent.poolSnapshot.productVal.value *
254
+ (isScaleIncrease ? newScaleMultiplier : 1n),
255
+ },
256
+ productFactor,
257
+ );
258
+
259
+ const isEpochIncrease = newProductSnapshot.value <= 0n;
260
+
261
+ const newSnapshot: StabilityPoolSnapshot = isEpochIncrease
262
+ ? { ...initSpSnapshot, epoch: spContent.poolSnapshot.epoch + 1n }
263
+ : {
264
+ productVal: newProductSnapshot,
265
+ depositVal: spSub(
266
+ spContent.poolSnapshot.depositVal,
267
+ mkSPInteger(iassetBurnAmt),
268
+ ),
269
+ sumVal: newSumSnapshot,
270
+ epoch: spContent.poolSnapshot.epoch,
271
+ scale: spContent.poolSnapshot.scale + (isScaleIncrease ? 1n : 0n),
272
+ };
273
+
274
+ const newMap = setSumInEpochToScaleToSum(
275
+ spContent.epochToScaleToSum,
276
+ spContent.poolSnapshot.epoch,
277
+ spContent.poolSnapshot.scale,
278
+ newSumSnapshot,
279
+ );
280
+
281
+ const newEpochToScaleToSum = match(true)
282
+ .when(
283
+ () => isEpochIncrease,
284
+ () =>
285
+ setSumInEpochToScaleToSum(
286
+ newMap,
287
+ spContent.poolSnapshot.epoch + 1n,
288
+ spContent.poolSnapshot.scale,
289
+ { value: 0n },
290
+ ),
291
+ )
292
+ .when(
293
+ () => isScaleIncrease,
294
+ () =>
295
+ setSumInEpochToScaleToSum(
296
+ newMap,
297
+ spContent.poolSnapshot.epoch,
298
+ spContent.poolSnapshot.scale + 1n,
299
+ newSumSnapshot,
300
+ ),
301
+ )
302
+ .otherwise(() => newMap);
303
+
304
+ return {
305
+ newSpContent: {
306
+ asset: spContent.asset,
307
+ epochToScaleToSum: newEpochToScaleToSum,
308
+ poolSnapshot: newSnapshot,
309
+ },
310
+ };
311
+ }
@@ -0,0 +1,46 @@
1
+ import { applyParamsToScript, SpendingValidator } from '@lucid-evolution/lucid';
2
+ import { castStabilityPoolParams, StabilityPoolParams } from './types';
3
+ import {
4
+ fromSystemParamsAsset,
5
+ StabilityPoolParamsSP,
6
+ } from '../../types/system-params';
7
+ import { _stabilityPoolValidator } from '../../validators/stability-pool-validator';
8
+
9
+ export const mkStabilityPoolValidator = (
10
+ params: StabilityPoolParams,
11
+ ): SpendingValidator => {
12
+ return {
13
+ type: 'PlutusV2',
14
+ script: applyParamsToScript(_stabilityPoolValidator.cborHex, [
15
+ castStabilityPoolParams(params),
16
+ ]),
17
+ };
18
+ };
19
+
20
+ export const mkStabilityPoolValidatorFromSP = (
21
+ params: StabilityPoolParamsSP,
22
+ ): SpendingValidator => {
23
+ return {
24
+ type: 'PlutusV2',
25
+ script: applyParamsToScript(_stabilityPoolValidator.cborHex, [
26
+ castStabilityPoolParams({
27
+ assetSymbol: params.assetSymbol.unCurrencySymbol,
28
+ stabilityPoolToken: fromSystemParamsAsset(params.stabilityPoolToken),
29
+ snapshotEpochToScaleToSumToken: fromSystemParamsAsset(
30
+ params.snapshotEpochToScaleToSumToken,
31
+ ),
32
+ accountToken: fromSystemParamsAsset(params.accountToken),
33
+ cdpToken: fromSystemParamsAsset(params.cdpToken),
34
+ iAssetAuthToken: fromSystemParamsAsset(params.iAssetAuthToken),
35
+ versionRecordToken: fromSystemParamsAsset(params.versionRecordToken),
36
+ collectorValHash: params.collectorValHash,
37
+ govNFT: fromSystemParamsAsset(params.govNFT),
38
+ accountCreateFeeLovelaces: BigInt(params.accountCreateFeeLovelaces),
39
+ accountAdjustmentFeeLovelaces: BigInt(
40
+ params.accountAdjustmentFeeLovelaces,
41
+ ),
42
+ requestCollateralLovelaces: BigInt(params.requestCollateralLovelaces),
43
+ }),
44
+ ]),
45
+ };
46
+ };