@indigo-labs/indigo-sdk 0.5.4 → 0.5.6

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 (208) hide show
  1. package/dist/index.d.mts +11 -1
  2. package/dist/index.d.ts +11 -1
  3. package/dist/index.js +17 -0
  4. package/dist/index.mjs +15 -0
  5. package/package.json +11 -1
  6. package/.claude/settings.local.json +0 -7
  7. package/.github/workflows/ci.yml +0 -66
  8. package/.husky/pre-commit +0 -1
  9. package/.nvmrc +0 -1
  10. package/.prettierignore +0 -1
  11. package/.prettierrc +0 -6
  12. package/eslint.config.mjs +0 -42
  13. package/scripts/bench.sh +0 -62
  14. package/scripts/run-repeat.sh +0 -22
  15. package/src/contracts/cdp/helpers.ts +0 -173
  16. package/src/contracts/cdp/scripts.ts +0 -70
  17. package/src/contracts/cdp/transactions.ts +0 -1584
  18. package/src/contracts/cdp/types-new.ts +0 -277
  19. package/src/contracts/cdp/types.ts +0 -43
  20. package/src/contracts/cdp-creator/queries.ts +0 -30
  21. package/src/contracts/cdp-creator/scripts.ts +0 -45
  22. package/src/contracts/cdp-creator/types-new.ts +0 -50
  23. package/src/contracts/cdp-creator/types.ts +0 -24
  24. package/src/contracts/collector/scripts.ts +0 -32
  25. package/src/contracts/collector/transactions.ts +0 -50
  26. package/src/contracts/collector/types-new.ts +0 -17
  27. package/src/contracts/execute/scripts.ts +0 -57
  28. package/src/contracts/execute/types-new.ts +0 -44
  29. package/src/contracts/execute/types.ts +0 -24
  30. package/src/contracts/gov/helpers.ts +0 -351
  31. package/src/contracts/gov/scripts.ts +0 -41
  32. package/src/contracts/gov/transactions.ts +0 -1607
  33. package/src/contracts/gov/types-new.ts +0 -268
  34. package/src/contracts/gov/types.ts +0 -22
  35. package/src/contracts/iasset/helpers.ts +0 -170
  36. package/src/contracts/iasset/queries.ts +0 -102
  37. package/src/contracts/iasset/scripts.ts +0 -38
  38. package/src/contracts/iasset/types.ts +0 -154
  39. package/src/contracts/initialize/actions.ts +0 -789
  40. package/src/contracts/initialize/helpers.ts +0 -616
  41. package/src/contracts/initialize/types.ts +0 -116
  42. package/src/contracts/interest-collection/helpers.ts +0 -19
  43. package/src/contracts/interest-collection/queries.ts +0 -66
  44. package/src/contracts/interest-collection/scripts.ts +0 -44
  45. package/src/contracts/interest-collection/transactions.ts +0 -433
  46. package/src/contracts/interest-collection/types-new.ts +0 -50
  47. package/src/contracts/interest-collection/types.ts +0 -26
  48. package/src/contracts/interest-oracle/helpers.ts +0 -61
  49. package/src/contracts/interest-oracle/scripts.ts +0 -18
  50. package/src/contracts/interest-oracle/transactions.ts +0 -151
  51. package/src/contracts/interest-oracle/types-new.ts +0 -32
  52. package/src/contracts/interest-oracle/types.ts +0 -16
  53. package/src/contracts/one-shot/scripts.ts +0 -12
  54. package/src/contracts/one-shot/transactions.ts +0 -66
  55. package/src/contracts/one-shot/types.ts +0 -22
  56. package/src/contracts/poll/helpers.ts +0 -37
  57. package/src/contracts/poll/scripts.ts +0 -71
  58. package/src/contracts/poll/types-poll-manager.ts +0 -20
  59. package/src/contracts/poll/types-poll-new.ts +0 -196
  60. package/src/contracts/poll/types-poll-shard.ts +0 -16
  61. package/src/contracts/price-oracle/helpers.ts +0 -30
  62. package/src/contracts/price-oracle/scripts.ts +0 -18
  63. package/src/contracts/price-oracle/transactions.ts +0 -115
  64. package/src/contracts/price-oracle/types-new.ts +0 -50
  65. package/src/contracts/price-oracle/types.ts +0 -16
  66. package/src/contracts/pyth-feed/helpers.ts +0 -82
  67. package/src/contracts/pyth-feed/scripts.ts +0 -15
  68. package/src/contracts/pyth-feed/types.ts +0 -181
  69. package/src/contracts/rob/helpers.ts +0 -666
  70. package/src/contracts/rob/scripts.ts +0 -35
  71. package/src/contracts/rob/transactions.ts +0 -423
  72. package/src/contracts/rob/types-new.ts +0 -128
  73. package/src/contracts/rob/types.ts +0 -16
  74. package/src/contracts/rob-leverage/helpers.ts +0 -422
  75. package/src/contracts/rob-leverage/transactions.ts +0 -368
  76. package/src/contracts/stability-pool/helpers.ts +0 -798
  77. package/src/contracts/stability-pool/queries.ts +0 -132
  78. package/src/contracts/stability-pool/scripts.ts +0 -51
  79. package/src/contracts/stability-pool/transactions.ts +0 -921
  80. package/src/contracts/stability-pool/types-new.ts +0 -385
  81. package/src/contracts/stability-pool/types.ts +0 -25
  82. package/src/contracts/stableswap/helpers.ts +0 -507
  83. package/src/contracts/stableswap/scripts.ts +0 -37
  84. package/src/contracts/stableswap/transactions.ts +0 -475
  85. package/src/contracts/stableswap/types-new.ts +0 -131
  86. package/src/contracts/stableswap/types.ts +0 -17
  87. package/src/contracts/staking/helpers.ts +0 -136
  88. package/src/contracts/staking/scripts.ts +0 -41
  89. package/src/contracts/staking/transactions.ts +0 -349
  90. package/src/contracts/staking/types-new.ts +0 -137
  91. package/src/contracts/staking/types.ts +0 -17
  92. package/src/contracts/treasury/helpers.ts +0 -21
  93. package/src/contracts/treasury/queries.ts +0 -100
  94. package/src/contracts/treasury/scripts.ts +0 -27
  95. package/src/contracts/treasury/transactions.ts +0 -334
  96. package/src/contracts/treasury/types-new.ts +0 -69
  97. package/src/contracts/treasury/types.ts +0 -14
  98. package/src/contracts/version-registry/scripts.ts +0 -29
  99. package/src/contracts/version-registry/types-new.ts +0 -18
  100. package/src/contracts/version-registry/types.ts +0 -17
  101. package/src/index.ts +0 -93
  102. package/src/scripts/always-fail-validator.ts +0 -7
  103. package/src/scripts/auth-token-policy.ts +0 -24
  104. package/src/scripts/iasset-policy.ts +0 -23
  105. package/src/types/evolution-schema-options.ts +0 -16
  106. package/src/types/generic.ts +0 -52
  107. package/src/types/multisig.ts +0 -48
  108. package/src/types/on-chain-decimal.ts +0 -57
  109. package/src/types/rational.ts +0 -61
  110. package/src/types/system-params.ts +0 -463
  111. package/src/utils/array-utils.ts +0 -108
  112. package/src/utils/bigint-utils.ts +0 -35
  113. package/src/utils/helper-txs.ts +0 -31
  114. package/src/utils/indigo-helpers.ts +0 -17
  115. package/src/utils/lucid-utils.ts +0 -125
  116. package/src/utils/oracle-helpers.ts +0 -62
  117. package/src/utils/pyth/decode.ts +0 -223
  118. package/src/utils/pyth/encode.ts +0 -262
  119. package/src/utils/pyth/index.ts +0 -14
  120. package/src/utils/pyth/types.ts +0 -87
  121. package/src/utils/time-helpers.ts +0 -4
  122. package/src/utils/utils.ts +0 -25
  123. package/src/validators/always-succeed-validator.ts +0 -6
  124. package/src/validators/cdp-creator-validator.ts +0 -7
  125. package/src/validators/cdp-redeem-validator.ts +0 -7
  126. package/src/validators/cdp-validator.ts +0 -7
  127. package/src/validators/collector-validator.ts +0 -7
  128. package/src/validators/execute-validator.ts +0 -7
  129. package/src/validators/governance-validator.ts +0 -7
  130. package/src/validators/iasset-validator.ts +0 -7
  131. package/src/validators/interest-collection-validator.ts +0 -7
  132. package/src/validators/interest-oracle-validator.ts +0 -7
  133. package/src/validators/one-shot-policy.ts +0 -7
  134. package/src/validators/poll-manager-validator.ts +0 -7
  135. package/src/validators/poll-shard-validator.ts +0 -7
  136. package/src/validators/price-oracle-validator.ts +0 -7
  137. package/src/validators/pyth-feed-validator.ts +0 -7
  138. package/src/validators/rob-validator.ts +0 -7
  139. package/src/validators/stability-pool-validator.ts +0 -7
  140. package/src/validators/stableswap-validator.ts +0 -7
  141. package/src/validators/staking-validator.ts +0 -7
  142. package/src/validators/treasury-validator.ts +0 -7
  143. package/src/validators/version-record-policy.ts +0 -7
  144. package/src/validators/version-registry-validator.ts +0 -7
  145. package/tests/always-succeed/script.ts +0 -7
  146. package/tests/array-utils.test.ts +0 -92
  147. package/tests/bigint-utils.test.ts +0 -41
  148. package/tests/cdp/actions.ts +0 -680
  149. package/tests/cdp/cdp-helpers.ts +0 -55
  150. package/tests/cdp/cdp-queries.ts +0 -423
  151. package/tests/cdp/cdp.test.ts +0 -6730
  152. package/tests/cdp/transactions-mutated.ts +0 -1733
  153. package/tests/data/system-params.json +0 -1155
  154. package/tests/datums.test.ts +0 -302
  155. package/tests/endpoints/initialize.ts +0 -78
  156. package/tests/endpoints/interest-collector.ts +0 -37
  157. package/tests/endpoints/treasury.ts +0 -70
  158. package/tests/gov/actions.ts +0 -381
  159. package/tests/gov/gov.test.ts +0 -5147
  160. package/tests/gov/governance-queries.ts +0 -37
  161. package/tests/hash-checks.test.ts +0 -112
  162. package/tests/indigo-test-helpers.ts +0 -99
  163. package/tests/initialize.test.ts +0 -79
  164. package/tests/interest-calculations.test.ts +0 -120
  165. package/tests/interest-collection/interest-collection.test.ts +0 -843
  166. package/tests/interest-collection/transactions-mutated.ts +0 -260
  167. package/tests/interest-oracle.test.ts +0 -93
  168. package/tests/mock/assets-mock.ts +0 -243
  169. package/tests/mock/protocol-params-mock.ts +0 -21
  170. package/tests/price-oracle/actions.ts +0 -161
  171. package/tests/price-oracle/price-oracle-queries.ts +0 -12
  172. package/tests/price-oracle/price-oracle.test.ts +0 -228
  173. package/tests/price-oracle/transactions-mutated.ts +0 -59
  174. package/tests/pyth/endpoints.ts +0 -96
  175. package/tests/pyth/helpers.ts +0 -37
  176. package/tests/pyth/pyth-encoding.test.ts +0 -376
  177. package/tests/pyth/pyth-feeds.test.ts +0 -943
  178. package/tests/pyth/pyth-indigo.test.ts +0 -505
  179. package/tests/pyth/pyth.test.ts +0 -299
  180. package/tests/queries/collector-queries.ts +0 -27
  181. package/tests/queries/execute-queries.ts +0 -47
  182. package/tests/queries/iasset-queries.ts +0 -149
  183. package/tests/queries/interest-oracle-queries.ts +0 -12
  184. package/tests/queries/poll-queries.ts +0 -97
  185. package/tests/queries/staking-queries.ts +0 -54
  186. package/tests/rob/actions.ts +0 -56
  187. package/tests/rob/rob-leverage.test.ts +0 -2555
  188. package/tests/rob/rob-queries.ts +0 -95
  189. package/tests/rob/rob.test.ts +0 -3677
  190. package/tests/rob/transactions-mutated.ts +0 -844
  191. package/tests/script-size.test.ts +0 -240
  192. package/tests/setup.ts +0 -135
  193. package/tests/stability-pool/actions.ts +0 -229
  194. package/tests/stability-pool.test.ts +0 -6491
  195. package/tests/stableswap/stableswap-actions.ts +0 -83
  196. package/tests/stableswap/stableswap-queries.ts +0 -98
  197. package/tests/stableswap/stableswap.test.ts +0 -4471
  198. package/tests/stableswap/transactions-mutated.ts +0 -348
  199. package/tests/staking.test.ts +0 -172
  200. package/tests/test-helpers.ts +0 -94
  201. package/tests/treasury/actions.ts +0 -67
  202. package/tests/treasury/treasury.test.ts +0 -320
  203. package/tests/utils/asserts.ts +0 -87
  204. package/tests/utils/benchmark-utils.ts +0 -130
  205. package/tests/utils/index.ts +0 -168
  206. package/tsconfig.build.json +0 -4
  207. package/tsconfig.json +0 -27
  208. package/vitest.config.ts +0 -11
@@ -1,116 +0,0 @@
1
- import { AssetClass } from '@3rd-eye-labs/cardano-offchain-common';
2
- import { InterestOracleParams } from '../interest-oracle/types';
3
- import { PriceOracleParams } from '../price-oracle/types';
4
- import { Rational } from '../../types/rational';
5
- import { PythFeedParamsSP } from '../../types/system-params';
6
-
7
- export type CollateralAssetInfo = {
8
- collateralAsset: AssetClass;
9
- oracleParams: PriceOracleParams | undefined;
10
- interestOracleParams: InterestOracleParams;
11
- interestOracleNft: AssetClass;
12
- };
13
-
14
- export type AssetInfo = {
15
- iassetTokenNameAscii: string;
16
- collateralAssets: CollateralAssetInfo[];
17
- };
18
-
19
- /** Price oracle config for indigo oracle (used during initialize) */
20
- export type IndigoOracleNftParam = {
21
- tag: '_indigo_oracle_nft';
22
- tokenName: string;
23
- startPrice: Rational;
24
- params: {
25
- biasTime: bigint;
26
- expirationPeriod: bigint;
27
- };
28
- };
29
-
30
- /** Price oracle config for Pyth (used during initialize) */
31
- export type PythOracleParam = {
32
- tag: '_pyth_oracle_nft';
33
- pythFeedParams: PythFeedParamsSP;
34
- };
35
-
36
- export type PriceOracleParam = IndigoOracleNftParam | PythOracleParam;
37
-
38
- export type InitialStablepoolParam = {
39
- collateralAsset: AssetClass;
40
- collateralToIassetRatio: Rational;
41
- mintingFeeRatio: Rational;
42
- redemptionFeeRatio: Rational;
43
- mintingEnabled: boolean;
44
- redemptionEnabled: boolean;
45
- feeManager: string | undefined;
46
- minMintOrderAmount: bigint;
47
- minRedemptionOrderAmount: bigint;
48
- };
49
-
50
- /** Collateral asset input for initialize */
51
- export type InitialCollateralAssetParam = {
52
- collateralAsset: AssetClass;
53
- extraDecimals: bigint;
54
- priceOracle: PriceOracleParam;
55
- interestOracle: {
56
- tokenName: string;
57
- initialInterestRate: bigint;
58
- params: {
59
- biasTime: bigint;
60
- };
61
- };
62
- redemptionRatio: Rational;
63
- maintenanceRatio: Rational;
64
- liquidationRatio: Rational;
65
- minCollateralAmt: bigint;
66
- firstCollateralAsset: boolean;
67
- nextCollateralAsset: AssetClass | undefined;
68
- };
69
-
70
- /** IAsset input for initialize */
71
- export type InitialAssetParam = {
72
- name: string;
73
- collateralAssets: InitialCollateralAssetParam[];
74
- stablepools: InitialStablepoolParam[];
75
- debtMintingFeeRatio: Rational;
76
- liquidationProcessingFeeRatio: Rational;
77
- stabilityPoolWithdrawalFeeRatio: Rational;
78
- redemptionReimbursementRatio: Rational;
79
- redemptionProcessingFeeRatio: Rational;
80
- firstAsset: boolean;
81
- nextAsset?: string | undefined;
82
- };
83
-
84
- /** Configurable options for protocol initialization */
85
- export type InitializeOptions = {
86
- /** Number of CDP creator UTxOs to create */
87
- numCdpCreators: bigint;
88
- /** Number of collector UTxOs to create */
89
- numCollectors: bigint;
90
- /** Number of interest collector UTxOs to create */
91
- numInterestCollectors: bigint;
92
- /** Number of treasury UTxo deployments */
93
- numTreasuryUtxos: bigint;
94
- /** INDY amount to send to the treasury */
95
- treasuryIndyAmount: bigint;
96
- /** Total INDY token supply to mint */
97
- totalIndySupply: bigint;
98
-
99
- /** Stability pool: fee in lovelace to create an SP account */
100
- accountCreateFeeLovelaces: bigint;
101
- /** Stability pool: cooldown between account processing (ms) */
102
- accountProcessingCooldownMs: bigint;
103
-
104
- /** Interest collection: cooldown between interest settlement (slot) */
105
- interestSettlementCooldown: bigint;
106
-
107
- /** CDP redeem: extra fee in lovelace for partial redemption */
108
- partialRedemptionExtraFeeLovelace: number;
109
- /** Default Protocol bias time (ms). Used for CDP, CDP Redeem, Stability Pool, Interest Collection, Interest Oracle, Price Oracle, and Stableswap. */
110
- biasTime: bigint;
111
- gBiasTime?: bigint;
112
- accountProcessingBiasTime?: bigint;
113
-
114
- /** Lovelace per interest collector deployment UTxO */
115
- interestCollectorUtxoLovelaces: bigint;
116
- };
@@ -1,19 +0,0 @@
1
- import { toHex } from '@lucid-evolution/lucid';
2
- import { fromDataMultisig, Multisig } from '../../types/multisig';
3
- import { Data } from '@evolution-sdk/evolution';
4
-
5
- export function signersAllOf(multisig: Multisig): string[] {
6
- if ('Signature' in multisig) {
7
- return [toHex(multisig.Signature.keyHash)];
8
- } else {
9
- return multisig.AtLeast.authSignatories
10
- .map((signatory: Data.Data) => {
11
- try {
12
- return signersAllOf(fromDataMultisig(signatory));
13
- } catch {
14
- return [];
15
- }
16
- })
17
- .flat();
18
- }
19
- }
@@ -1,66 +0,0 @@
1
- import { LucidEvolution, UTxO } from '@lucid-evolution/lucid';
2
- import { option as O, function as F } from 'fp-ts';
3
- import {
4
- assetClassToUnit,
5
- assetClassValueOf,
6
- getRandomElement,
7
- matchSingle,
8
- } from '@3rd-eye-labs/cardano-offchain-common';
9
- import { createScriptAddress } from '../../utils/lucid-utils';
10
- import { fromSystemParamsAsset, SystemParams } from '../../types/system-params';
11
-
12
- export async function findAllInterestCollectors(
13
- lucid: LucidEvolution,
14
- sysParams: SystemParams,
15
- ): Promise<UTxO[]> {
16
- return lucid.utxosAt(
17
- createScriptAddress(
18
- lucid.config().network!,
19
- sysParams.validatorHashes.interestCollectionHash,
20
- ),
21
- );
22
- }
23
-
24
- export async function findAdminInterestCollectors(
25
- lucid: LucidEvolution,
26
- sysParams: SystemParams,
27
- ): Promise<UTxO> {
28
- return matchSingle(
29
- await lucid.utxosAtWithUnit(
30
- createScriptAddress(
31
- lucid.config().network!,
32
- sysParams.validatorHashes.interestCollectionHash,
33
- ),
34
- assetClassToUnit(
35
- fromSystemParamsAsset(
36
- sysParams.interestCollectionParams.multisigUtxoNft,
37
- ),
38
- ),
39
- ),
40
- (_) => new Error('Expected a single admin interest collector UTXO'),
41
- );
42
- }
43
-
44
- export async function findRandomNonAdminInterestCollector(
45
- lucid: LucidEvolution,
46
- sysParams: SystemParams,
47
- ): Promise<UTxO> {
48
- const allCollectors = (
49
- await findAllInterestCollectors(lucid, sysParams)
50
- ).filter(
51
- (utxo) =>
52
- !assetClassValueOf(
53
- utxo.assets,
54
- fromSystemParamsAsset(
55
- sysParams.interestCollectionParams.multisigUtxoNft,
56
- ),
57
- ),
58
- );
59
-
60
- return F.pipe(
61
- O.fromNullable(getRandomElement(allCollectors)),
62
- O.match(() => {
63
- throw new Error('Expected some non-admin interest collector UTXOs.');
64
- }, F.identity),
65
- );
66
- }
@@ -1,44 +0,0 @@
1
- import { applyParamsToScript } from '@lucid-evolution/lucid';
2
- import { SpendingValidator } from '@lucid-evolution/lucid';
3
- import { _interestCollectionValidator } from '../../validators/interest-collection-validator';
4
- import {
5
- castInterestCollectionParams,
6
- InterestCollectionParams,
7
- } from './types';
8
- import {
9
- fromSystemParamsAssetLucid,
10
- InterestCollectionParamsSP,
11
- } from '../../types/system-params';
12
-
13
- export function mkInterestCollectionValidator(
14
- params: InterestCollectionParams,
15
- ): SpendingValidator {
16
- return {
17
- type: 'PlutusV3',
18
- script: applyParamsToScript(_interestCollectionValidator.cborHex, [
19
- castInterestCollectionParams(params),
20
- ]),
21
- };
22
- }
23
-
24
- export const mkInterestCollectionValidatorFromSP = (
25
- params: InterestCollectionParamsSP,
26
- ): SpendingValidator => {
27
- return {
28
- type: 'PlutusV3',
29
- script: applyParamsToScript(_interestCollectionValidator.cborHex, [
30
- castInterestCollectionParams({
31
- versionRecordNft: fromSystemParamsAssetLucid(params.versionRecordNft),
32
- multisigUtxoNft: fromSystemParamsAssetLucid(params.multisigUtxoNft),
33
- cdpAuthTk: fromSystemParamsAssetLucid(params.cdpAuthTk),
34
- collateralAssetAuthTk: fromSystemParamsAssetLucid(
35
- params.collateralAssetAuthTk,
36
- ),
37
- govAuthTk: fromSystemParamsAssetLucid(params.govAuthTk),
38
- cdpAssetSymbol: params.cdpAssetSymbol.unCurrencySymbol,
39
- cdpBiasTime: BigInt(params.cdpBiasTime),
40
- interestSettlementCooldown: BigInt(params.interestSettlementCooldown),
41
- }),
42
- ]),
43
- };
44
- };
@@ -1,433 +0,0 @@
1
- import {
2
- Data,
3
- LucidEvolution,
4
- OutRef,
5
- toHex,
6
- TxBuilder,
7
- UTxO,
8
- addAssets,
9
- Assets,
10
- fromHex,
11
- sortUTxOs,
12
- getInputIndices,
13
- } from '@lucid-evolution/lucid';
14
- import {
15
- fromSystemParamsAsset,
16
- fromSystemParamsScriptRef,
17
- SystemParams,
18
- } from '../../types/system-params';
19
- import { matchSingle } from '../../utils/utils';
20
- import {
21
- parseInterestCollectionDatum,
22
- serialiseInterestCollectionDatum,
23
- serialiseInterestCollectionRedeemer,
24
- } from './types-new';
25
- import {
26
- assetClassValueOf,
27
- getInlineDatumOrThrow,
28
- mkAssetsOf,
29
- } from '@3rd-eye-labs/cardano-offchain-common';
30
- import { createScriptAddress } from '../../utils/lucid-utils';
31
- import {
32
- CDPContent,
33
- parseCdpDatumOrThrow,
34
- serialiseCdpDatum,
35
- serialiseCdpRedeemer,
36
- } from '../cdp/types-new';
37
- import { parseInterestOracleDatum } from '../interest-oracle/types-new';
38
- import {
39
- calculateAccruedInterest,
40
- calculateUnitaryInterestSinceOracleLastUpdated,
41
- } from '../interest-oracle/helpers';
42
- import { match, P } from 'ts-pattern';
43
- import { array as A, function as F } from 'fp-ts';
44
- import { parseCollateralAssetDatumOrThrow } from '../iasset/types';
45
- import { Multisig } from '../../types/multisig';
46
-
47
- type CDPInfo = {
48
- utxo: UTxO;
49
- datum: CDPContent;
50
- accruedInterest: bigint;
51
- };
52
-
53
- export async function batchCollectInterest(
54
- collateralAssetOref: OutRef,
55
- interestCollectorOutRef: OutRef,
56
- interestOracleOutRef: OutRef,
57
- cdpOutRefs: OutRef[],
58
- params: SystemParams,
59
- lucid: LucidEvolution,
60
- ): Promise<TxBuilder> {
61
- const currentTime = BigInt(lucid.slotToUnixTime(lucid.currentSlot()));
62
-
63
- const interestCollectionRefScriptUtxo = matchSingle(
64
- await lucid.utxosByOutRef([
65
- fromSystemParamsScriptRef(
66
- params.scriptReferences.interestCollectionValidatorRef,
67
- ),
68
- ]),
69
- (_) => new Error('Expected a single interest collection Ref Script UTXO'),
70
- );
71
-
72
- const cdpRefScriptUtxo = matchSingle(
73
- await lucid.utxosByOutRef([
74
- fromSystemParamsScriptRef(params.scriptReferences.cdpValidatorRef),
75
- ]),
76
- (_) => new Error('Expected a single cdp Ref Script UTXO'),
77
- );
78
-
79
- const iAssetTokenPolicyRefScriptUtxo = matchSingle(
80
- await lucid.utxosByOutRef([
81
- fromSystemParamsScriptRef(params.scriptReferences.iAssetTokenPolicyRef),
82
- ]),
83
- (_) => new Error('Expected a single iasset token policy Ref Script UTXO'),
84
- );
85
-
86
- const collateralAssetUtxo = matchSingle(
87
- await lucid.utxosByOutRef([collateralAssetOref]),
88
- (_) => new Error('Expected a single iasset UTXO'),
89
- );
90
-
91
- const collateralAssetDatum = parseCollateralAssetDatumOrThrow(
92
- getInlineDatumOrThrow(collateralAssetUtxo),
93
- );
94
-
95
- const interestCollectorUtxo: UTxO = matchSingle(
96
- await lucid.utxosByOutRef([interestCollectorOutRef]),
97
- (_) => new Error('Expected a single interest collector UTXO'),
98
- );
99
-
100
- // Confirm that the interest collector UTXO is NOT of InterestCollectorDatum type
101
- if (
102
- assetClassValueOf(
103
- interestCollectorUtxo.assets,
104
- fromSystemParamsAsset(params.interestCollectionParams.multisigUtxoNft),
105
- )
106
- ) {
107
- throw new Error(
108
- 'Interest collector UTXO is of InterestCollectorDatum type',
109
- );
110
- }
111
-
112
- const interestOracleUtxo: UTxO = matchSingle(
113
- await lucid.utxosByOutRef([interestOracleOutRef]),
114
- (_) => new Error('Expected a single interest oracle UTXO'),
115
- );
116
-
117
- const interestOracleDatum = parseInterestOracleDatum(
118
- getInlineDatumOrThrow(interestOracleUtxo),
119
- );
120
-
121
- const cdpUtxos = await lucid.utxosByOutRef(cdpOutRefs);
122
-
123
- const sortedCdpUtxos = sortUTxOs(cdpUtxos, 'Canonical');
124
-
125
- if (sortedCdpUtxos.length !== cdpOutRefs.length) {
126
- throw new Error('Expected certain number of CDPs');
127
- }
128
-
129
- function getAccruedInterest(cdpDatum: CDPContent): bigint {
130
- return match(cdpDatum.cdpFees)
131
- .with({ FrozenCDPAccumulatedFees: P.any }, () => {
132
- throw new Error('CDP fees wrong');
133
- })
134
- .with({ ActiveCDPInterestTracking: P.select() }, (interest) => {
135
- return calculateAccruedInterest(
136
- currentTime,
137
- interest.unitaryInterestSnapshot,
138
- cdpDatum.mintedAmt,
139
- interest.lastSettled,
140
- interestOracleDatum,
141
- );
142
- })
143
- .exhaustive();
144
- }
145
-
146
- const cdpsInfo: CDPInfo[] = sortedCdpUtxos.map((cdpUtxo) => {
147
- const cdpDatum = parseCdpDatumOrThrow(getInlineDatumOrThrow(cdpUtxo));
148
- return {
149
- utxo: cdpUtxo,
150
- datum: cdpDatum,
151
- accruedInterest: getAccruedInterest(cdpDatum),
152
- };
153
- });
154
-
155
- const accumulatedInterest = F.pipe(
156
- cdpsInfo,
157
- A.reduce<CDPInfo, bigint>(
158
- 0n,
159
- (acc, cdpInfo) => acc + cdpInfo.accruedInterest,
160
- ),
161
- );
162
-
163
- const updatedUnitarySnapshot =
164
- calculateUnitaryInterestSinceOracleLastUpdated(
165
- currentTime,
166
- interestOracleDatum,
167
- ) + interestOracleDatum.unitaryInterest;
168
-
169
- const referenceInputs = [
170
- collateralAssetUtxo,
171
- interestOracleUtxo,
172
- interestCollectionRefScriptUtxo,
173
- cdpRefScriptUtxo,
174
- iAssetTokenPolicyRefScriptUtxo,
175
- ];
176
-
177
- const referenceInputIndices = getInputIndices(
178
- [collateralAssetUtxo, interestOracleUtxo],
179
- referenceInputs,
180
- false,
181
- );
182
-
183
- const validateFrom = lucid.slotToUnixTime(lucid.currentSlot() - 1);
184
- const validateTo = validateFrom + Number(params.cdpParams.biasTime);
185
-
186
- const tx = lucid
187
- .newTx()
188
- .validFrom(validateFrom)
189
- .validTo(validateTo)
190
- .readFrom(referenceInputs)
191
- .collectFrom([interestCollectorUtxo], {
192
- kind: 'selected',
193
- makeRedeemer: (inputIndices: bigint[]) => {
194
- return serialiseInterestCollectionRedeemer({
195
- BatchCollectInterest: {
196
- currentTime: currentTime,
197
- ownInputIndex: inputIndices[0],
198
- collateralAssetRefInputIndex: referenceInputIndices[0],
199
- interestOracleRefInputIndex: referenceInputIndices[1],
200
- },
201
- });
202
- },
203
- inputs: [interestCollectorUtxo],
204
- })
205
- .mintAssets(
206
- mkAssetsOf(
207
- {
208
- currencySymbol: fromHex(
209
- params.cdpParams.cdpAssetSymbol.unCurrencySymbol,
210
- ),
211
- tokenName: collateralAssetDatum.iasset,
212
- },
213
- accumulatedInterest,
214
- ),
215
- Data.void(),
216
- )
217
- .pay.ToContract(
218
- createScriptAddress(
219
- lucid.config().network!,
220
- params.validatorHashes.interestCollectionHash,
221
- ),
222
- { kind: 'inline', value: Data.void() },
223
- addAssets(
224
- interestCollectorUtxo.assets,
225
- mkAssetsOf(
226
- {
227
- currencySymbol: fromHex(
228
- params.cdpParams.cdpAssetSymbol.unCurrencySymbol,
229
- ),
230
- tokenName: collateralAssetDatum.iasset,
231
- },
232
- accumulatedInterest,
233
- ),
234
- ),
235
- );
236
-
237
- F.pipe(
238
- cdpsInfo,
239
- A.reduce<CDPInfo, TxBuilder>(tx, (acc, cdpInfo) =>
240
- acc
241
- .collectFrom([cdpInfo.utxo], {
242
- kind: 'selected',
243
- makeRedeemer: (inputIndices: bigint[]) => {
244
- return serialiseCdpRedeemer({
245
- SettleInterest: { interestCollectorInputIndex: inputIndices[0] },
246
- });
247
- },
248
- inputs: [interestCollectorUtxo],
249
- })
250
- .pay.ToContract(
251
- cdpInfo.utxo.address,
252
- {
253
- kind: 'inline',
254
- value: serialiseCdpDatum({
255
- ...cdpInfo.datum,
256
- mintedAmt: cdpInfo.datum.mintedAmt + cdpInfo.accruedInterest,
257
- cdpFees: {
258
- ActiveCDPInterestTracking: {
259
- lastSettled: currentTime,
260
- unitaryInterestSnapshot: updatedUnitarySnapshot,
261
- },
262
- },
263
- }),
264
- },
265
- cdpInfo.utxo.assets,
266
- ),
267
- ),
268
- );
269
-
270
- return tx;
271
- }
272
-
273
- export async function collectInterestTx(
274
- value: Assets,
275
- lucid: LucidEvolution,
276
- sysParams: SystemParams,
277
- tx: TxBuilder,
278
- interestCollectorOref: OutRef,
279
- ): Promise<UTxO> {
280
- const interestCollectorUtxo = matchSingle(
281
- await lucid.utxosByOutRef([interestCollectorOref]),
282
- (_) => new Error('Expected a single interest collector UTXO'),
283
- );
284
- const interestCollectorRefScriptUtxo = matchSingle(
285
- await lucid.utxosByOutRef([
286
- fromSystemParamsScriptRef(
287
- sysParams.scriptReferences.interestCollectionValidatorRef,
288
- ),
289
- ]),
290
- (_) => new Error('Expected a single interest collector Ref Script UTXO'),
291
- );
292
-
293
- tx.readFrom([interestCollectorRefScriptUtxo])
294
- .collectFrom(
295
- [interestCollectorUtxo],
296
- serialiseInterestCollectionRedeemer('CollectInterest'),
297
- )
298
- .pay.ToContract(
299
- interestCollectorUtxo.address,
300
- { kind: 'inline', value: Data.void() },
301
- addAssets(interestCollectorUtxo.assets, value),
302
- );
303
-
304
- return interestCollectorRefScriptUtxo;
305
- }
306
-
307
- export async function distributeInterest(
308
- interestCollectorOutRefs: OutRef[],
309
- interestAdminOutRef: OutRef,
310
- params: SystemParams,
311
- lucid: LucidEvolution,
312
- ): Promise<TxBuilder> {
313
- const interestCollectorUtxos = [];
314
-
315
- for (const outRef of interestCollectorOutRefs) {
316
- interestCollectorUtxos.push(
317
- matchSingle(
318
- await lucid.utxosByOutRef([outRef]),
319
- (_) => new Error('Expected a single UTXO with that reference'),
320
- ),
321
- );
322
- }
323
-
324
- const interestAdminUtxo: UTxO = matchSingle(
325
- (await lucid.utxosByOutRef([interestAdminOutRef])).filter((utxo) =>
326
- assetClassValueOf(
327
- utxo.assets,
328
- fromSystemParamsAsset(params.interestCollectionParams.multisigUtxoNft),
329
- ),
330
- ),
331
- (_) => new Error('Expected a single interest admin UTXO'),
332
- );
333
-
334
- const interestAdminDatum = parseInterestCollectionDatum(
335
- getInlineDatumOrThrow(interestAdminUtxo),
336
- );
337
-
338
- // Find the script reference UTXO for the interest collection validator
339
- const interestCollectionRefScriptUtxo = matchSingle(
340
- await lucid.utxosByOutRef([
341
- fromSystemParamsScriptRef(
342
- params.scriptReferences.interestCollectionValidatorRef,
343
- ),
344
- ]),
345
- (_) => new Error('Expected a single interest collection Ref Script UTXO'),
346
- );
347
-
348
- const tx = lucid
349
- .newTx()
350
- .readFrom([interestCollectionRefScriptUtxo, interestAdminUtxo])
351
- .collectFrom(
352
- interestCollectorUtxos,
353
- serialiseInterestCollectionRedeemer('Distribute'),
354
- );
355
-
356
- // TODO: Handle tx contruction for Multisig
357
- if ('Signature' in interestAdminDatum.admin_permissions) {
358
- tx.addSignerKey(
359
- toHex(interestAdminDatum.admin_permissions.Signature.keyHash),
360
- );
361
- } else {
362
- // TODO: Handle other admin permissions types
363
- throw new Error('Unsupported admin permissions type');
364
- }
365
-
366
- return tx;
367
- }
368
-
369
- export async function updatePermissions(
370
- interestAdminOutRef: OutRef,
371
- govOref: OutRef,
372
- newAdminPermissions: Multisig,
373
- expectedSigners: string[],
374
- params: SystemParams,
375
- lucid: LucidEvolution,
376
- ): Promise<TxBuilder> {
377
- const interestAdminUtxo: UTxO = matchSingle(
378
- (await lucid.utxosByOutRef([interestAdminOutRef])).filter((utxo) =>
379
- assetClassValueOf(
380
- utxo.assets,
381
- fromSystemParamsAsset(params.interestCollectionParams.multisigUtxoNft),
382
- ),
383
- ),
384
- (_) => new Error('Expected a single interest admin UTXO'),
385
- );
386
-
387
- const govUtxo: UTxO = matchSingle(
388
- (await lucid.utxosByOutRef([govOref])).filter((utxo) =>
389
- assetClassValueOf(
390
- utxo.assets,
391
- fromSystemParamsAsset(params.interestCollectionParams.govAuthTk),
392
- ),
393
- ),
394
- (_) => new Error('Expected a single interest admin UTXO'),
395
- );
396
-
397
- // Find the script reference UTXO for the interest collection validator
398
- const interestCollectionRefScriptUtxo = matchSingle(
399
- await lucid.utxosByOutRef([
400
- fromSystemParamsScriptRef(
401
- params.scriptReferences.interestCollectionValidatorRef,
402
- ),
403
- ]),
404
- (_) => new Error('Expected a single interest collection Ref Script UTXO'),
405
- );
406
-
407
- const tx = lucid
408
- .newTx()
409
- .readFrom([interestCollectionRefScriptUtxo, govUtxo])
410
- .collectFrom(
411
- [interestAdminUtxo],
412
- serialiseInterestCollectionRedeemer('UpdatePermissions'),
413
- )
414
- .pay.ToContract(
415
- createScriptAddress(
416
- lucid.config().network!,
417
- params.validatorHashes.interestCollectionHash,
418
- ),
419
- {
420
- kind: 'inline',
421
- value: serialiseInterestCollectionDatum({
422
- admin_permissions: newAdminPermissions,
423
- }),
424
- },
425
- interestAdminUtxo.assets,
426
- );
427
-
428
- for (const signer of expectedSigners) {
429
- tx.addSignerKey(signer);
430
- }
431
-
432
- return tx;
433
- }