@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,115 @@
1
+ import { Core as EvoCore } from '@evolution-sdk/evolution';
2
+ import { Data } from '@lucid-evolution/lucid';
3
+ import { OracleAssetNftSchema } from '../price-oracle/types';
4
+ import { AssetClassSchema } from '../../types/generic';
5
+ import { OnChainDecimalSchema } from '../../types/on-chain-decimal';
6
+ import { IAssetPriceInfoSchema } from '../cdp/types';
7
+ import { DEFAULT_SCHEMA_OPTIONS } from '../../types/evolution-schema-options';
8
+
9
+ export const ProtocolParamsSchema = Data.Object({
10
+ proposalDeposit: Data.Integer(),
11
+ votingPeriod: Data.Integer(),
12
+ effectiveDelay: Data.Integer(),
13
+ expirationPeriod: Data.Integer(),
14
+ collateralFeePercentage: OnChainDecimalSchema,
15
+ proposingPeriod: Data.Integer(),
16
+ /// Total numer of shards used for voting.
17
+ totalShards: Data.Integer(),
18
+ /// The minimum number of votes (yes + no votes) for a proposal to be possible to pass.
19
+ minimumQuorum: Data.Integer(),
20
+ /// Maximum amount of lovelaces that can be spent at once from the treasury.
21
+ maxTreasuryLovelaceSpend: Data.Integer(),
22
+ /// Maximum amount of INDY that can be spent at once from the treasury.
23
+ maxTreasuryIndySpend: Data.Integer(),
24
+ });
25
+
26
+ export type ProtocolParams = Data.Static<typeof ProtocolParamsSchema>;
27
+ export const ProtocolParams = ProtocolParamsSchema as unknown as ProtocolParams;
28
+
29
+ const ProposeAssetContentSchema = Data.Object({
30
+ asset: Data.Bytes(),
31
+ priceOracleNft: OracleAssetNftSchema,
32
+ interestOracleNft: AssetClassSchema,
33
+ redemptionRatioPercentage: OnChainDecimalSchema,
34
+ maintenanceRatioPercentage: OnChainDecimalSchema,
35
+ liquidationRatioPercentage: OnChainDecimalSchema,
36
+ debtMintingFeePercentage: OnChainDecimalSchema,
37
+ liquidationProcessingFeePercentage: OnChainDecimalSchema,
38
+ stabilityPoolWithdrawalFeePercentage: OnChainDecimalSchema,
39
+ redemptionReimbursementPercentage: OnChainDecimalSchema,
40
+ redemptionProcessingFeePercentage: OnChainDecimalSchema,
41
+ interestCollectorPortionPercentage: OnChainDecimalSchema,
42
+ });
43
+ export type ProposeAssetContent = Data.Static<typeof ProposeAssetContentSchema>;
44
+
45
+ export const ProposalContentSchema = Data.Enum([
46
+ Data.Object({
47
+ ProposeAsset: ProposeAssetContentSchema,
48
+ }),
49
+ Data.Object({
50
+ ModifyAsset: Data.Object({
51
+ asset: Data.Bytes(),
52
+ newAssetPriceInfo: IAssetPriceInfoSchema,
53
+ newInterestOracleNft: AssetClassSchema,
54
+ newRedemptionRatioPercentage: OnChainDecimalSchema,
55
+ newMaintenanceRatioPercentage: OnChainDecimalSchema,
56
+ newLiquidationRatioPercentage: OnChainDecimalSchema,
57
+ newDebtMintingFeePercentage: OnChainDecimalSchema,
58
+ newLiquidationProcessingFeePercentage: OnChainDecimalSchema,
59
+ newStabilityPoolWithdrawalFeePercentage: OnChainDecimalSchema,
60
+ newRedemptionReimbursementPercentage: OnChainDecimalSchema,
61
+ newRedemptionProcessingFeePercentage: OnChainDecimalSchema,
62
+ newInterestCollectorPortionPercentage: OnChainDecimalSchema,
63
+ }),
64
+ }),
65
+ Data.Object({
66
+ ModifyProtocolParams: Data.Object({
67
+ newParams: ProtocolParamsSchema,
68
+ }),
69
+ }),
70
+ Data.Object({
71
+ UpgradeProtocol: Data.Object({
72
+ content:
73
+ // NOTICE: This is replaced by evolution-sdk encoding of `UpgradePathsSchema` defined in this file.
74
+ Data.Any(),
75
+ }),
76
+ }),
77
+ Data.Object({
78
+ TextProposal: Data.Object({
79
+ bytes: Data.Bytes(),
80
+ }),
81
+ }),
82
+ ]);
83
+ export type ProposalContent = Data.Static<typeof ProposalContentSchema>;
84
+ export const ProposalContent =
85
+ ProposalContentSchema as unknown as ProposalContent;
86
+
87
+ const UpgradePathSchema = EvoCore.TSchema.Struct({
88
+ upgradeSymbol: EvoCore.TSchema.ByteArray,
89
+ });
90
+
91
+ const UpgradePathsSchema = EvoCore.TSchema.Struct({
92
+ upgradeId: EvoCore.TSchema.Integer,
93
+ /// Underlying representation of the following mapping: ValidatorHash -> UpgradePath
94
+ upgradePaths: EvoCore.TSchema.Map(
95
+ EvoCore.TSchema.ByteArray,
96
+ UpgradePathSchema,
97
+ ),
98
+ });
99
+ export type UpgradePaths = typeof UpgradePathsSchema.Type;
100
+
101
+ export function serialiseUpgradePaths(d: UpgradePaths): Data {
102
+ return Data.from(
103
+ EvoCore.Data.withSchema(
104
+ UpgradePathsSchema,
105
+ DEFAULT_SCHEMA_OPTIONS,
106
+ ).toCBORHex(d),
107
+ );
108
+ }
109
+
110
+ export function parseUpgradePaths(d: Data): UpgradePaths {
111
+ return EvoCore.Data.withSchema(
112
+ UpgradePathsSchema,
113
+ DEFAULT_SCHEMA_OPTIONS,
114
+ ).fromCBORHex(Data.to(d));
115
+ }
@@ -0,0 +1,89 @@
1
+ import { Data, Datum, Redeemer } from '@lucid-evolution/lucid';
2
+ import { AddressSchema, AssetClassSchema } from '../../types/generic';
3
+ import { option as O, function as F } from 'fp-ts';
4
+ import { ProposalContentSchema, ProtocolParamsSchema } from './types-new';
5
+
6
+ const GovDatumSchema = Data.Object({
7
+ currentProposal: Data.Integer(),
8
+ protocolParams: ProtocolParamsSchema,
9
+ currentVersion: Data.Integer(),
10
+ iassetsCount: Data.Integer(),
11
+ activeProposals: Data.Integer(),
12
+ treasuryIndyWithdrawnAmt: Data.Integer(),
13
+ });
14
+ export type GovDatum = Data.Static<typeof GovDatumSchema>;
15
+ const GovDatum = GovDatumSchema as unknown as GovDatum;
16
+
17
+ const GovParamsSchema = Data.Object({
18
+ govNFT: AssetClassSchema,
19
+ pollToken: AssetClassSchema,
20
+ upgradeToken: AssetClassSchema,
21
+ indyAsset: AssetClassSchema,
22
+ versionRecordToken: AssetClassSchema,
23
+ pollManagerValHash: Data.Bytes(),
24
+ gBiasTime: Data.Integer(),
25
+ daoIdentityToken: AssetClassSchema,
26
+ iAssetAuthToken: AssetClassSchema,
27
+ });
28
+ export type GovParams = Data.Static<typeof GovParamsSchema>;
29
+ export const GovParams = GovParamsSchema as unknown as GovParams;
30
+
31
+ const ValueWithdrawalItemSchema = Data.Tuple(
32
+ [Data.Bytes(), Data.Bytes(), Data.Integer()],
33
+ { hasConstr: true },
34
+ );
35
+ export type TreasuryWithdrawalItem = Data.Static<
36
+ typeof ValueWithdrawalItemSchema
37
+ >;
38
+
39
+ export const TreasuryWithdrawalSchema = Data.Object({
40
+ destination: AddressSchema,
41
+ value: Data.Array(ValueWithdrawalItemSchema),
42
+ });
43
+ export type TreasuryWithdrawal = Data.Static<typeof TreasuryWithdrawalSchema>;
44
+
45
+ const GovRedeemerSchema = Data.Enum([
46
+ Data.Object({
47
+ CreatePoll: Data.Object({
48
+ currentTime: Data.Integer(),
49
+ proposalOwner: Data.Bytes(),
50
+ content: ProposalContentSchema,
51
+ treasuryWithdrawal: Data.Nullable(TreasuryWithdrawalSchema),
52
+ }),
53
+ }),
54
+ Data.Object({ WitnessEndPoll: Data.Object({ currentTime: Data.Integer() }) }),
55
+ Data.Literal('UpgradeGov'),
56
+ Data.Literal('UpgradeVersion'),
57
+ ]);
58
+
59
+ export type GovRedeemer = Data.Static<typeof GovRedeemerSchema>;
60
+ export const GovRedeemer = GovRedeemerSchema as unknown as GovRedeemer;
61
+
62
+ export function parseGovDatum(datum: Datum): O.Option<GovDatum> {
63
+ try {
64
+ return O.some(Data.from<GovDatum>(datum, GovDatum));
65
+ } catch (_) {
66
+ return O.none;
67
+ }
68
+ }
69
+
70
+ export function parseGovDatumOrThrow(datum: Datum): GovDatum {
71
+ return F.pipe(
72
+ parseGovDatum(datum),
73
+ O.match(() => {
74
+ throw new Error('Expected a Gov datum.');
75
+ }, F.identity),
76
+ );
77
+ }
78
+
79
+ export function serialiseGovDatum(d: GovDatum): Datum {
80
+ return Data.to<GovDatum>(d, GovDatum);
81
+ }
82
+
83
+ export function serialiseGovRedeemer(redeemer: GovRedeemer): Redeemer {
84
+ return Data.to<GovRedeemer>(redeemer, GovRedeemer);
85
+ }
86
+
87
+ export function castGovParams(params: GovParams): Data {
88
+ return Data.castTo(params, GovParams);
89
+ }
@@ -1,8 +1,14 @@
1
- import { InterestOracleDatum } from '../types/indigo/interest-oracle';
2
- import { oneYear } from '../helpers/time-helpers';
1
+ import { InterestOracleDatum } from './types';
2
+ import { ONE_YEAR } from '../../utils/time-helpers';
3
+ import {
4
+ OCD_DECIMAL_UNIT,
5
+ ocdAdd,
6
+ ocdCeil,
7
+ ocdMul,
8
+ OnChainDecimal,
9
+ } from '../../types/on-chain-decimal';
3
10
 
4
11
  const unitaryInterestPrecision = 1_000_000_000_000_000_000n;
5
- const decimalUnit = 1_000_000n;
6
12
 
7
13
  export function calculateUnitaryInterest(
8
14
  timePeriod: bigint,
@@ -10,8 +16,8 @@ export function calculateUnitaryInterest(
10
16
  ): bigint {
11
17
  return (
12
18
  (timePeriod * interestRate * unitaryInterestPrecision) /
13
- oneYear /
14
- decimalUnit
19
+ ONE_YEAR /
20
+ OCD_DECIMAL_UNIT
15
21
  );
16
22
  }
17
23
 
@@ -41,8 +47,8 @@ export function calculateAccruedInterest(
41
47
  ((now - interestOracleDatum.lastUpdated) *
42
48
  interestOracleDatum.interestRate.getOnChainInt *
43
49
  mintedAmount) /
44
- oneYear /
45
- decimalUnit;
50
+ ONE_YEAR /
51
+ OCD_DECIMAL_UNIT;
46
52
 
47
53
  return interestFromPreviousRates + lastRateInterest;
48
54
  } else {
@@ -50,8 +56,30 @@ export function calculateAccruedInterest(
50
56
  ((now - interestLastSettled) *
51
57
  interestOracleDatum.interestRate.getOnChainInt *
52
58
  mintedAmount) /
53
- oneYear /
54
- decimalUnit
59
+ ONE_YEAR /
60
+ OCD_DECIMAL_UNIT
55
61
  );
56
62
  }
57
63
  }
64
+
65
+ /**
66
+ * Calculate the amount of interest needed to achieve 100% collateral ratio.
67
+ */
68
+ export function computeInterestLovelacesFor100PercentCR(
69
+ collateral: bigint,
70
+ mintedAmt: bigint,
71
+ price: OnChainDecimal,
72
+ ): bigint {
73
+ const amt = ocdCeil(
74
+ ocdAdd(
75
+ { getOnChainInt: collateral * OCD_DECIMAL_UNIT },
76
+ ocdMul({ getOnChainInt: -mintedAmt * OCD_DECIMAL_UNIT }, price),
77
+ ),
78
+ );
79
+
80
+ if (amt <= 0) {
81
+ return 0n;
82
+ } else {
83
+ return amt;
84
+ }
85
+ }
@@ -0,0 +1,18 @@
1
+ import { applyParamsToScript } from '@lucid-evolution/lucid';
2
+ import { SpendingValidator } from '@lucid-evolution/lucid';
3
+ import {
4
+ castInterestOracleParams,
5
+ InterestOracleParams,
6
+ } from '../interest-oracle/types';
7
+ import { _interestOracleValidator } from '../../validators/interest-oracle-validator';
8
+
9
+ export function mkInterestOracleValidator(
10
+ params: InterestOracleParams,
11
+ ): SpendingValidator {
12
+ return {
13
+ type: 'PlutusV2',
14
+ script: applyParamsToScript(_interestOracleValidator.cborHex, [
15
+ castInterestOracleParams(params),
16
+ ]),
17
+ };
18
+ }
@@ -0,0 +1,149 @@
1
+ import {
2
+ fromText,
3
+ LucidEvolution,
4
+ OutRef,
5
+ toUnit,
6
+ TxBuilder,
7
+ UTxO,
8
+ validatorToAddress,
9
+ } from '@lucid-evolution/lucid';
10
+ import { AssetClass } from '../../types/generic';
11
+ import {
12
+ InterestOracleParams,
13
+ parseInterestOracleDatum,
14
+ serialiseFeedInterestOracleRedeemer,
15
+ serialiseInterestOracleDatum,
16
+ } from './types';
17
+ import { oneShotMintTx } from '../one-shot/transactions';
18
+ import { mkInterestOracleValidator } from './scripts';
19
+ import { findInterestOracle } from '../../../tests/queries/interest-oracle-queries';
20
+ import { ONE_SECOND } from '../../utils/time-helpers';
21
+ import { calculateUnitaryInterestSinceOracleLastUpdated } from '../interest-oracle/helpers';
22
+ import { getInlineDatumOrThrow } from '../../utils/lucid-utils';
23
+
24
+ export async function startInterestOracle(
25
+ initialUnitaryInterest: bigint,
26
+ initialInterestRate: bigint,
27
+ initialLastInterestUpdate: bigint,
28
+ oracleParams: InterestOracleParams,
29
+ lucid: LucidEvolution,
30
+ interestTokenName?: string,
31
+ withScriptRef: boolean = false,
32
+ refOutRef?: OutRef,
33
+ ): Promise<[TxBuilder, AssetClass]> {
34
+ const network = lucid.config().network!;
35
+
36
+ const tokenName = interestTokenName ?? 'INTEREST_ORACLE';
37
+ if (!refOutRef) {
38
+ refOutRef = (await lucid.wallet().getUtxos())[0];
39
+ }
40
+
41
+ const [tx, policyId] = await oneShotMintTx(lucid, {
42
+ referenceOutRef: {
43
+ txHash: refOutRef.txHash,
44
+ outputIdx: BigInt(refOutRef.outputIndex),
45
+ },
46
+ mintAmounts: [
47
+ {
48
+ tokenName: fromText(tokenName),
49
+ amount: 1n,
50
+ },
51
+ ],
52
+ });
53
+
54
+ const validator = mkInterestOracleValidator(oracleParams);
55
+
56
+ tx.pay.ToContract(
57
+ validatorToAddress(network, validator),
58
+ {
59
+ kind: 'inline',
60
+ value: serialiseInterestOracleDatum({
61
+ unitaryInterest: initialUnitaryInterest,
62
+ interestRate: {
63
+ getOnChainInt: initialInterestRate,
64
+ },
65
+ lastUpdated: initialLastInterestUpdate,
66
+ }),
67
+ },
68
+ {
69
+ lovelace: 2_500_000n,
70
+ [toUnit(policyId, fromText(tokenName))]: 1n,
71
+ },
72
+ );
73
+
74
+ if (withScriptRef) {
75
+ tx.pay.ToAddressWithData(
76
+ validatorToAddress(network, validator),
77
+ undefined,
78
+ undefined,
79
+ validator,
80
+ );
81
+ }
82
+
83
+ return [
84
+ tx,
85
+ {
86
+ currencySymbol: policyId,
87
+ tokenName: fromText(tokenName),
88
+ },
89
+ ];
90
+ }
91
+
92
+ export async function feedInterestOracle(
93
+ params: InterestOracleParams,
94
+ newInterestRate: bigint,
95
+ lucid: LucidEvolution,
96
+ assetClass?: AssetClass,
97
+ utxo?: UTxO,
98
+ scriptRef?: UTxO,
99
+ ): Promise<TxBuilder> {
100
+ if (!assetClass && !utxo)
101
+ throw new Error('Either interest oracle nft or utxo must be provided');
102
+ if (assetClass && !utxo) {
103
+ utxo = await findInterestOracle(lucid, assetClass);
104
+ }
105
+
106
+ const now = BigInt(Date.now());
107
+ const tx = lucid.newTx();
108
+ const datum = parseInterestOracleDatum(getInlineDatumOrThrow(utxo!));
109
+
110
+ if (scriptRef) {
111
+ tx.readFrom([scriptRef]);
112
+ } else {
113
+ tx.attach.Script(mkInterestOracleValidator(params));
114
+ }
115
+
116
+ tx.collectFrom(
117
+ [utxo!],
118
+ serialiseFeedInterestOracleRedeemer({
119
+ newInterestRate: {
120
+ getOnChainInt: newInterestRate,
121
+ },
122
+ currentTime: now,
123
+ }),
124
+ );
125
+
126
+ tx.pay.ToContract(
127
+ utxo!.address,
128
+ {
129
+ kind: 'inline',
130
+ value: serialiseInterestOracleDatum({
131
+ unitaryInterest:
132
+ datum.unitaryInterest +
133
+ calculateUnitaryInterestSinceOracleLastUpdated(now, datum),
134
+ interestRate: {
135
+ getOnChainInt: newInterestRate,
136
+ },
137
+ lastUpdated: now,
138
+ }),
139
+ },
140
+ utxo!.assets,
141
+ );
142
+
143
+ tx.validFrom(Number(now) - ONE_SECOND);
144
+ tx.validTo(Number(now + params.biasTime) - ONE_SECOND);
145
+
146
+ tx.addSignerKey(params.owner);
147
+
148
+ return tx;
149
+ }
@@ -1,5 +1,5 @@
1
1
  import { Data, Datum } from '@lucid-evolution/lucid';
2
- import { OnChainDecimalSchema } from '../on-chain-decimal';
2
+ import { OnChainDecimalSchema } from '../../types/on-chain-decimal';
3
3
 
4
4
  export const InterestOracleDatumSchema = Data.Object({
5
5
  unitaryInterest: Data.Integer(),
@@ -0,0 +1,27 @@
1
+ import { applyParamsToScript, SpendingValidator } from '@lucid-evolution/lucid';
2
+ import { castLrpParams, LRPParams } from './types';
3
+ import { fromSystemParamsAsset, LrpParamsSP } from '../../types/system-params';
4
+ import { _lrpValidator } from '../../validators/lrp-validator';
5
+
6
+ export const mkLrpValidator = (params: LRPParams): SpendingValidator => {
7
+ return {
8
+ type: 'PlutusV2',
9
+ script: applyParamsToScript(_lrpValidator.cborHex, [castLrpParams(params)]),
10
+ };
11
+ };
12
+
13
+ export const mkLrpValidatorFromSP = (
14
+ params: LrpParamsSP,
15
+ ): SpendingValidator => {
16
+ return {
17
+ type: 'PlutusV2',
18
+ script: applyParamsToScript(_lrpValidator.cborHex, [
19
+ castLrpParams({
20
+ versionRecordToken: fromSystemParamsAsset(params.versionRecordToken),
21
+ iassetNft: fromSystemParamsAsset(params.iassetNft),
22
+ minRedemptionLovelacesAmt: BigInt(params.minRedemptionLovelacesAmt),
23
+ iassetPolicyId: params.iassetPolicyId.unCurrencySymbol,
24
+ }),
25
+ ]),
26
+ };
27
+ };
@@ -14,7 +14,7 @@ import {
14
14
  addrDetails,
15
15
  createScriptAddress,
16
16
  getInlineDatumOrThrow,
17
- } from '../helpers/lucid-utils';
17
+ } from '../../utils/lucid-utils';
18
18
  import { match, P } from 'ts-pattern';
19
19
  import { unzip, zip } from 'fp-ts/lib/Array';
20
20
  import { reduceWithIndex } from 'fp-ts/lib/Array';
@@ -24,18 +24,18 @@ import {
24
24
  parseLrpDatum,
25
25
  serialiseLrpDatum,
26
26
  serialiseLrpRedeemer,
27
- } from '../types/indigo/lrp';
28
- import { parsePriceOracleDatum } from '../types/indigo/price-oracle';
29
- import { ocdMul, OnChainDecimal } from '../types/on-chain-decimal';
30
- import { parseIAssetDatum } from '../types/indigo/cdp';
27
+ } from './types';
28
+ import { parsePriceOracleDatum } from '../price-oracle/types';
29
+ import { ocdMul, OnChainDecimal } from '../../types/on-chain-decimal';
30
+ import { parseIAssetDatumOrThrow } from '../cdp/types';
31
31
  import {
32
32
  assetClassValueOf,
33
33
  mkAssetsOf,
34
34
  mkLovelacesOf,
35
- } from '../helpers/value-helpers';
36
- import { calculateFeeFromPercentage } from '../helpers/indigo-helpers';
37
- import { matchSingle } from '../helpers/helpers';
38
- import { AssetClass } from '../types/generic';
35
+ } from '../../utils/value-helpers';
36
+ import { calculateFeeFromPercentage } from '../../utils/indigo-helpers';
37
+ import { matchSingle } from '../../utils/utils';
38
+ import { AssetClass } from '../../types/generic';
39
39
 
40
40
  const MIN_UTXO_COLLATERAL_AMT = 2_000_000n;
41
41
 
@@ -116,7 +116,9 @@ export async function redeemLrp(
116
116
  (_) => new Error('Expected a single IAsset UTXO'),
117
117
  );
118
118
 
119
- const iassetDatum = parseIAssetDatum(getInlineDatumOrThrow(iassetUtxo));
119
+ const iassetDatum = parseIAssetDatumOrThrow(
120
+ getInlineDatumOrThrow(iassetUtxo),
121
+ );
120
122
 
121
123
  const [lrpsToRedeemOutRefs, lrpRedemptionIAssetAmt] =
122
124
  unzip(redemptionLrpsData);
@@ -192,7 +194,7 @@ export async function redeemLrp(
192
194
  },
193
195
  addAssets(
194
196
  lrpUtxo.assets,
195
- mkLovelacesOf(-(lovelacesForRedemption - reimburstmentLovelaces)),
197
+ mkLovelacesOf(-lovelacesForRedemption + reimburstmentLovelaces),
196
198
  mkAssetsOf(
197
199
  {
198
200
  currencySymbol: lrpParams.iassetPolicyId,
@@ -236,8 +238,6 @@ export async function adjustLrp(
236
238
  lrpRefScriptOutRef: OutRef,
237
239
  lrpParams: LRPParams,
238
240
  ): Promise<TxBuilder> {
239
- const ownAddr = await lucid.wallet().address();
240
-
241
241
  const lrpScriptRefUtxo = matchSingle(
242
242
  await lucid.utxosByOutRef([lrpRefScriptOutRef]),
243
243
  (_) => new Error('Expected a single LRP Ref Script UTXO'),
@@ -292,7 +292,7 @@ export async function adjustLrp(
292
292
  mkLovelacesOf(lovelacesAdjustAmt),
293
293
  ),
294
294
  )
295
- .addSigner(ownAddr);
295
+ .addSignerKey(lrpDatum.owner);
296
296
  }
297
297
 
298
298
  /**
@@ -1,6 +1,6 @@
1
1
  import { Data, Datum, Redeemer } from '@lucid-evolution/lucid';
2
- import { AssetClassSchema, OutputReferenceSchema } from '../generic';
3
- import { OnChainDecimalSchema } from '../on-chain-decimal';
2
+ import { AssetClassSchema, OutputReferenceSchema } from '../../types/generic';
3
+ import { OnChainDecimalSchema } from '../../types/on-chain-decimal';
4
4
 
5
5
  export const LRPParamsSchema = Data.Object({
6
6
  versionRecordToken: AssetClassSchema,
@@ -3,7 +3,7 @@ import {
3
3
  MintingPolicy,
4
4
  ScriptType,
5
5
  } from '@lucid-evolution/lucid';
6
- import { castOneShotParams, OneShotParams } from '../types/one-shot';
6
+ import { castOneShotParams, OneShotParams } from './types';
7
7
 
8
8
  /**
9
9
  Generated from this code in Aiken v1.1.13
@@ -9,9 +9,9 @@ import {
9
9
  toUnit,
10
10
  TxBuilder,
11
11
  } from '@lucid-evolution/lucid';
12
- import { OneShotParams } from '../types/one-shot';
13
- import { matchSingle } from '../helpers/helpers';
14
- import { mkOneShotPolicy } from '../scripts/one-shot-policy';
12
+ import { OneShotParams } from './types';
13
+ import { matchSingle } from '../../utils/utils';
14
+ import { mkOneShotPolicy } from './scripts';
15
15
  import { reduce } from 'fp-ts/lib/Array';
16
16
 
17
17
  export async function oneShotMintTx(
@@ -0,0 +1,55 @@
1
+ import {
2
+ calculateTotalVestedRewards,
3
+ calculateTotalVestedTeam,
4
+ } from '../vesting/helpers';
5
+ import { PollStatus } from './types-poll';
6
+ import { ocdDiv, OnChainDecimal } from '../../types/on-chain-decimal';
7
+
8
+ function q(
9
+ initialIndyDist: bigint,
10
+ pollStatus: PollStatus,
11
+ currentTime: bigint,
12
+ treasuryIndyWithdrawnAmt: bigint,
13
+ ): OnChainDecimal {
14
+ if (pollStatus.yesVotes + pollStatus.noVotes === 0n)
15
+ return { getOnChainInt: 0n };
16
+ else {
17
+ const e =
18
+ treasuryIndyWithdrawnAmt +
19
+ calculateTotalVestedRewards(currentTime) +
20
+ calculateTotalVestedTeam(currentTime) +
21
+ initialIndyDist;
22
+
23
+ const q =
24
+ ocdDiv(
25
+ { getOnChainInt: pollStatus.yesVotes },
26
+ { getOnChainInt: BigInt(Math.floor(Math.sqrt(Number(e)))) },
27
+ ).getOnChainInt -
28
+ ocdDiv(
29
+ { getOnChainInt: pollStatus.noVotes },
30
+ {
31
+ getOnChainInt: BigInt(
32
+ Math.floor(
33
+ Math.sqrt(Number(pollStatus.yesVotes + pollStatus.noVotes)),
34
+ ),
35
+ ),
36
+ },
37
+ ).getOnChainInt;
38
+
39
+ return { getOnChainInt: BigInt(q) };
40
+ }
41
+ }
42
+
43
+ export function pollPassQuorum(
44
+ initialIndyDist: bigint,
45
+ pollStatus: PollStatus,
46
+ currentTime: bigint,
47
+ minQuorum: bigint,
48
+ treasuryIndyWithdrawnAmt: bigint,
49
+ ): boolean {
50
+ return (
51
+ pollStatus.yesVotes + pollStatus.noVotes >= minQuorum &&
52
+ q(initialIndyDist, pollStatus, currentTime, treasuryIndyWithdrawnAmt)
53
+ .getOnChainInt > 50_000n
54
+ );
55
+ }