@indigo-labs/indigo-sdk 0.2.42 → 0.3.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 (201) hide show
  1. package/.github/workflows/ci.yml +4 -2
  2. package/dist/index.d.mts +3008 -2194
  3. package/dist/index.d.ts +3008 -2194
  4. package/dist/index.js +9827 -6194
  5. package/dist/index.mjs +8591 -4809
  6. package/package.json +14 -3
  7. package/src/contracts/cdp/helpers.ts +68 -72
  8. package/src/contracts/cdp/scripts.ts +50 -13
  9. package/src/contracts/cdp/transactions.ts +831 -545
  10. package/src/contracts/cdp/types-new.ts +256 -0
  11. package/src/contracts/cdp/types.ts +26 -144
  12. package/src/contracts/cdp-creator/scripts.ts +15 -9
  13. package/src/contracts/cdp-creator/types-new.ts +50 -0
  14. package/src/contracts/cdp-creator/types.ts +5 -31
  15. package/src/contracts/collector/scripts.ts +1 -1
  16. package/src/contracts/collector/transactions.ts +23 -13
  17. package/src/contracts/collector/types-new.ts +17 -0
  18. package/src/contracts/execute/scripts.ts +19 -10
  19. package/src/contracts/execute/types-new.ts +44 -0
  20. package/src/contracts/execute/types.ts +5 -38
  21. package/src/contracts/gov/helpers.ts +187 -51
  22. package/src/contracts/gov/scripts.ts +17 -10
  23. package/src/contracts/gov/transactions.ts +599 -271
  24. package/src/contracts/gov/types-new.ts +253 -100
  25. package/src/contracts/gov/types.ts +4 -71
  26. package/src/contracts/iasset/helpers.ts +172 -0
  27. package/src/contracts/iasset/scripts.ts +38 -0
  28. package/src/contracts/iasset/types.ts +154 -0
  29. package/src/contracts/initialize/actions.ts +768 -0
  30. package/src/contracts/initialize/helpers.ts +611 -36
  31. package/src/contracts/initialize/types.ts +102 -28
  32. package/src/contracts/interest-collection/helpers.ts +19 -0
  33. package/src/contracts/interest-collection/scripts.ts +44 -0
  34. package/src/contracts/interest-collection/transactions.ts +436 -0
  35. package/src/contracts/interest-collection/types-new.ts +50 -0
  36. package/src/contracts/interest-collection/types.ts +26 -0
  37. package/src/contracts/interest-oracle/helpers.ts +2 -30
  38. package/src/contracts/interest-oracle/scripts.ts +1 -1
  39. package/src/contracts/interest-oracle/transactions.ts +21 -16
  40. package/src/contracts/interest-oracle/types-new.ts +32 -0
  41. package/src/contracts/interest-oracle/types.ts +1 -40
  42. package/src/contracts/one-shot/transactions.ts +1 -2
  43. package/src/contracts/poll/helpers.ts +5 -23
  44. package/src/contracts/poll/scripts.ts +12 -13
  45. package/src/contracts/poll/types-poll-manager.ts +1 -19
  46. package/src/contracts/poll/types-poll-new.ts +170 -0
  47. package/src/contracts/poll/types-poll-shard.ts +2 -24
  48. package/src/contracts/price-oracle/helpers.ts +1 -4
  49. package/src/contracts/price-oracle/scripts.ts +3 -8
  50. package/src/contracts/price-oracle/transactions.ts +32 -25
  51. package/src/contracts/price-oracle/types-new.ts +50 -0
  52. package/src/contracts/price-oracle/types.ts +2 -36
  53. package/src/contracts/pyth-feed/helpers.ts +58 -0
  54. package/src/contracts/pyth-feed/scripts.ts +15 -0
  55. package/src/contracts/pyth-feed/types.ts +181 -0
  56. package/src/contracts/rob/helpers.ts +405 -0
  57. package/src/contracts/rob/scripts.ts +35 -0
  58. package/src/contracts/rob/transactions.ts +410 -0
  59. package/src/contracts/rob/types-new.ts +128 -0
  60. package/src/contracts/rob/types.ts +16 -0
  61. package/src/contracts/rob-leverage/helpers.ts +424 -0
  62. package/src/contracts/{leverage → rob-leverage}/transactions.ts +68 -48
  63. package/src/contracts/stability-pool/helpers.ts +714 -230
  64. package/src/contracts/stability-pool/scripts.ts +20 -15
  65. package/src/contracts/stability-pool/transactions.ts +625 -493
  66. package/src/contracts/stability-pool/types-new.ts +237 -100
  67. package/src/contracts/stability-pool/types.ts +5 -22
  68. package/src/contracts/stableswap/helpers.ts +22 -0
  69. package/src/contracts/stableswap/scripts.ts +37 -0
  70. package/src/contracts/stableswap/transactions.ts +647 -0
  71. package/src/contracts/stableswap/types-new.ts +131 -0
  72. package/src/contracts/stableswap/types.ts +17 -0
  73. package/src/contracts/staking/helpers.ts +49 -34
  74. package/src/contracts/staking/scripts.ts +1 -1
  75. package/src/contracts/staking/transactions.ts +85 -130
  76. package/src/contracts/staking/types-new.ts +60 -28
  77. package/src/contracts/staking/types.ts +1 -28
  78. package/src/contracts/treasury/helpers.ts +21 -0
  79. package/src/contracts/treasury/scripts.ts +16 -26
  80. package/src/contracts/treasury/transactions.ts +256 -27
  81. package/src/contracts/treasury/types-new.ts +69 -0
  82. package/src/contracts/treasury/types.ts +2 -43
  83. package/src/contracts/version-registry/scripts.ts +2 -2
  84. package/src/contracts/version-registry/types-new.ts +6 -7
  85. package/src/index.ts +37 -20
  86. package/src/scripts/auth-token-policy.ts +3 -2
  87. package/src/scripts/iasset-policy.ts +3 -2
  88. package/src/types/evolution-schema-options.ts +3 -3
  89. package/src/types/generic.ts +17 -89
  90. package/src/types/multisig.ts +48 -0
  91. package/src/types/on-chain-decimal.ts +14 -7
  92. package/src/types/rational.ts +61 -0
  93. package/src/types/system-params.ts +237 -41
  94. package/src/utils/array-utils.ts +70 -1
  95. package/src/utils/bigint-utils.ts +12 -0
  96. package/src/utils/indigo-helpers.ts +8 -10
  97. package/src/utils/lucid-utils.ts +47 -40
  98. package/src/utils/oracle-helpers.ts +62 -0
  99. package/src/utils/pyth/decode.ts +223 -0
  100. package/src/utils/pyth/encode.ts +262 -0
  101. package/src/utils/pyth/index.ts +14 -0
  102. package/src/utils/pyth/types.ts +87 -0
  103. package/src/validators/always-succeed-validator.ts +6 -0
  104. package/src/validators/cdp-creator-validator.ts +2 -2
  105. package/src/validators/cdp-redeem-validator.ts +7 -0
  106. package/src/validators/cdp-validator.ts +2 -2
  107. package/src/validators/collector-validator.ts +2 -2
  108. package/src/validators/execute-validator.ts +2 -2
  109. package/src/validators/governance-validator.ts +2 -2
  110. package/src/validators/iasset-validator.ts +7 -0
  111. package/src/validators/interest-collection-validator.ts +7 -0
  112. package/src/validators/interest-oracle-validator.ts +2 -2
  113. package/src/validators/poll-manager-validator.ts +2 -2
  114. package/src/validators/poll-shard-validator.ts +2 -2
  115. package/src/validators/price-oracle-validator.ts +7 -0
  116. package/src/validators/pyth-feed-validator.ts +7 -0
  117. package/src/validators/rob-validator.ts +7 -0
  118. package/src/validators/stability-pool-validator.ts +2 -2
  119. package/src/validators/stableswap-validator.ts +7 -0
  120. package/src/validators/staking-validator.ts +2 -2
  121. package/src/validators/treasury-validator.ts +2 -2
  122. package/src/validators/version-record-policy.ts +2 -2
  123. package/src/validators/version-registry-validator.ts +2 -2
  124. package/tests/always-succeed/script.ts +7 -0
  125. package/tests/bigint-utils.test.ts +41 -0
  126. package/tests/cdp/actions.ts +611 -0
  127. package/tests/cdp/cdp-helpers.ts +55 -0
  128. package/tests/cdp/cdp-queries.ts +440 -0
  129. package/tests/cdp/cdp.test.ts +6087 -0
  130. package/tests/cdp/transactions-mutated.ts +1729 -0
  131. package/tests/data/system-params.json +177 -34
  132. package/tests/datums.test.ts +209 -210
  133. package/tests/endpoints/initialize.ts +68 -0
  134. package/tests/endpoints/interest-collector.ts +37 -0
  135. package/tests/endpoints/treasury.ts +70 -0
  136. package/tests/gov/actions.ts +406 -0
  137. package/tests/gov/gov.test.ts +4450 -0
  138. package/tests/{queries → gov}/governance-queries.ts +6 -3
  139. package/tests/hash-checks.test.ts +38 -11
  140. package/tests/indigo-test-helpers.ts +100 -0
  141. package/tests/initialize.test.ts +61 -9
  142. package/tests/interest-collection/interest-collection.test.ts +892 -0
  143. package/tests/interest-collection/interest-collector-queries.ts +49 -0
  144. package/tests/interest-collection/transactions-mutated.ts +260 -0
  145. package/tests/interest-oracle.test.ts +43 -35
  146. package/tests/mock/assets-mock.ts +234 -23
  147. package/tests/mock/protocol-params-mock.ts +21 -0
  148. package/tests/price-oracle/actions.ts +163 -0
  149. package/tests/price-oracle/price-oracle-queries.ts +12 -0
  150. package/tests/price-oracle/price-oracle.test.ts +240 -0
  151. package/tests/price-oracle/transactions-mutated.ts +62 -0
  152. package/tests/pyth/endpoints.ts +96 -0
  153. package/tests/pyth/helpers.ts +37 -0
  154. package/tests/pyth/pyth-encoding.test.ts +376 -0
  155. package/tests/pyth/pyth-indigo.test.ts +509 -0
  156. package/tests/pyth/pyth.test.ts +300 -0
  157. package/tests/queries/execute-queries.ts +6 -5
  158. package/tests/queries/iasset-queries.ts +175 -5
  159. package/tests/queries/interest-oracle-queries.ts +4 -2
  160. package/tests/queries/poll-queries.ts +8 -9
  161. package/tests/queries/stability-pool-queries.ts +95 -48
  162. package/tests/queries/staking-queries.ts +4 -2
  163. package/tests/queries/treasury-queries.ts +80 -5
  164. package/tests/rob/actions.ts +58 -0
  165. package/tests/{lrp-leverage.test.ts → rob/rob-leverage.test.ts} +393 -296
  166. package/tests/rob/rob-queries.ts +95 -0
  167. package/tests/rob/rob.test.ts +3762 -0
  168. package/tests/rob/transactions-mutated.ts +853 -0
  169. package/tests/script-size.test.ts +240 -0
  170. package/tests/setup.ts +135 -0
  171. package/tests/stability-pool/actions.ts +210 -0
  172. package/tests/stability-pool.test.ts +5469 -666
  173. package/tests/stableswap/stableswap-actions.ts +84 -0
  174. package/tests/stableswap/stableswap-queries.ts +89 -0
  175. package/tests/stableswap/stableswap.test.ts +3891 -0
  176. package/tests/stableswap/transactions-mutated.ts +348 -0
  177. package/tests/staking.test.ts +82 -99
  178. package/tests/test-helpers.ts +58 -11
  179. package/tests/treasury.test.ts +242 -0
  180. package/tests/utils/asserts.ts +74 -0
  181. package/tests/utils/benchmark-utils.ts +81 -0
  182. package/tests/utils/index.ts +122 -4
  183. package/tsconfig.json +9 -1
  184. package/vitest.config.ts +3 -1
  185. package/src/contracts/collector/types.ts +0 -16
  186. package/src/contracts/initialize/transactions.ts +0 -891
  187. package/src/contracts/leverage/helpers.ts +0 -424
  188. package/src/contracts/lrp/helpers.ts +0 -294
  189. package/src/contracts/lrp/scripts.ts +0 -27
  190. package/src/contracts/lrp/transactions.ts +0 -250
  191. package/src/contracts/lrp/types.ts +0 -131
  192. package/src/contracts/poll/types-poll.ts +0 -88
  193. package/src/contracts/vesting/helpers.ts +0 -218
  194. package/src/utils/value-helpers.ts +0 -37
  195. package/src/validators/lrp-validator.ts +0 -7
  196. package/tests/cdp.test.ts +0 -1528
  197. package/tests/gov.test.ts +0 -2011
  198. package/tests/lrp.test.ts +0 -673
  199. package/tests/queries/cdp-queries.ts +0 -220
  200. package/tests/queries/lrp-queries.ts +0 -76
  201. package/tests/queries/price-oracle-queries.ts +0 -10
@@ -1,115 +1,268 @@
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';
1
+ import { TSchema, Data } from '@evolution-sdk/evolution';
7
2
  import { DEFAULT_SCHEMA_OPTIONS } from '../../types/evolution-schema-options';
3
+ import {
4
+ AddressSchema,
5
+ AssetClassSchema,
6
+ } from '@3rd-eye-labs/cardano-offchain-common';
7
+ import { option as O, function as F } from 'fp-ts';
8
+ import { IAssetPriceInfoSchema } from '../iasset/types';
9
+ import { MultisigSchema } from '../../types/multisig';
10
+ import { RationalSchema } from '../../types/rational';
8
11
 
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(),
12
+ export const ProtocolParamsSchema = TSchema.Struct({
13
+ proposalDeposit: TSchema.Integer,
14
+ votingPeriod: TSchema.Integer,
15
+ effectiveDelay: TSchema.Integer,
16
+ expirationPeriod: TSchema.Integer,
17
+ proposingPeriod: TSchema.Integer,
18
+ totalShards: TSchema.Integer,
19
+ minimumQuorum: TSchema.Integer,
20
+ maxTreasuryLovelaceSpend: TSchema.Integer,
21
+ maxTreasuryIndySpend: TSchema.Integer,
22
+ cdpRedemptionRequiredSignature: TSchema.NullOr(TSchema.ByteArray),
23
+ electorate: TSchema.Integer,
24
+ foundationMultisig: MultisigSchema,
24
25
  });
25
26
 
26
- export type ProtocolParams = Data.Static<typeof ProtocolParamsSchema>;
27
- export const ProtocolParams = ProtocolParamsSchema as unknown as ProtocolParams;
27
+ export type ProtocolParams = typeof ProtocolParamsSchema.Type;
28
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,
29
+ const UpgradePathSchema = TSchema.Struct({
30
+ upgradeSymbol: TSchema.ByteArray,
89
31
  });
90
32
 
91
- const UpgradePathsSchema = EvoCore.TSchema.Struct({
92
- upgradeId: EvoCore.TSchema.Integer,
33
+ const UpgradePathsSchema = TSchema.Struct({
34
+ upgradeId: TSchema.Integer,
93
35
  /// Underlying representation of the following mapping: ValidatorHash -> UpgradePath
94
- upgradePaths: EvoCore.TSchema.Map(
95
- EvoCore.TSchema.ByteArray,
96
- UpgradePathSchema,
36
+ upgradePaths: TSchema.Array(
37
+ TSchema.Tuple([TSchema.ByteArray, UpgradePathSchema]),
97
38
  ),
98
39
  });
99
40
  export type UpgradePaths = typeof UpgradePathsSchema.Type;
100
41
 
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
- );
42
+ const ProposeIAssetContentSchema = TSchema.Struct({
43
+ asset: TSchema.ByteArray,
44
+ debtMintingFeeRatio: RationalSchema,
45
+ liquidationProcessingFeeRatio: RationalSchema,
46
+ stabilityPoolWithdrawalFeeRatio: RationalSchema,
47
+ redemptionReimbursementRatio: RationalSchema,
48
+ redemptionProcessingFeeRatio: RationalSchema,
49
+ });
50
+ export type ProposeAssetContent = typeof ProposeIAssetContentSchema.Type;
51
+
52
+ const ModifyIAssetContentSchema = TSchema.Struct({
53
+ asset: TSchema.ByteArray,
54
+ newDebtMintingFeeRatio: RationalSchema,
55
+ newLiquidationProcessingFeeRatio: RationalSchema,
56
+ newStabilityPoolWithdrawalFeeRatio: RationalSchema,
57
+ newRedemptionReimbursementRatio: RationalSchema,
58
+ newRedemptionProcessingFeeRatio: RationalSchema,
59
+ });
60
+
61
+ const AddCollateralAssetContentSchema = TSchema.Struct({
62
+ correspondingIAsset: TSchema.ByteArray,
63
+ collateralAsset: AssetClassSchema,
64
+ assetExtraDecimals: TSchema.Integer,
65
+ assetPriceInfo: IAssetPriceInfoSchema,
66
+ interestOracleNft: AssetClassSchema,
67
+ redemptionRatio: RationalSchema,
68
+ maintenanceRatio: RationalSchema,
69
+ liquidationRatio: RationalSchema,
70
+ minCollateralAmt: TSchema.Integer,
71
+ });
72
+
73
+ export type AddCollateralAsssetContent =
74
+ typeof AddCollateralAssetContentSchema.Type;
75
+
76
+ const UpdateCollateralAssetContentSchema = TSchema.Struct({
77
+ correspondingIAsset: TSchema.ByteArray,
78
+ collateralAsset: AssetClassSchema,
79
+ newAssetExtraDecimals: TSchema.Integer,
80
+ newAssetPriceInfo: IAssetPriceInfoSchema,
81
+ newInterestOracleNft: AssetClassSchema,
82
+ newRedemptionRatio: RationalSchema,
83
+ newMaintenanceRatio: RationalSchema,
84
+ newLiquidationRatio: RationalSchema,
85
+ newMinCollateralAmt: TSchema.Integer,
86
+ });
87
+
88
+ const ProposeStableswapPoolContentSchema = TSchema.Struct({
89
+ iasset: TSchema.ByteArray,
90
+ collateralAsset: AssetClassSchema,
91
+ collateralToIassetRatio: RationalSchema,
92
+ mintingFeeRatio: RationalSchema,
93
+ redemptionFeeRatio: RationalSchema,
94
+ feeManager: TSchema.NullOr(TSchema.ByteArray),
95
+ minMintingAmount: TSchema.Integer,
96
+ minRedemptionAmount: TSchema.Integer,
97
+ stableswapValHash: TSchema.ByteArray,
98
+ });
99
+
100
+ export type ProposeStableswapPoolContent =
101
+ typeof ProposeStableswapPoolContentSchema.Type;
102
+
103
+ const ModifyStableswapPoolContentSchema = TSchema.Struct({
104
+ iasset: TSchema.ByteArray,
105
+ collateralAsset: AssetClassSchema,
106
+ newCollateralToIassetRatio: RationalSchema,
107
+ newMintingFeeRatio: RationalSchema,
108
+ newRedemptionFeeRatio: RationalSchema,
109
+ newMintingEnabled: TSchema.Boolean,
110
+ newRedemptionEnabled: TSchema.Boolean,
111
+ newFeeManager: TSchema.NullOr(TSchema.ByteArray),
112
+ newMinMintingAmount: TSchema.Integer,
113
+ newMinRedemptionAmount: TSchema.Integer,
114
+ newStableswapValHash: TSchema.ByteArray,
115
+ });
116
+
117
+ export type ModifyStableswapPoolContent =
118
+ typeof ModifyStableswapPoolContentSchema.Type;
119
+
120
+ export const ProposalContentSchema = TSchema.Union(
121
+ TSchema.Struct(
122
+ {
123
+ ProposeIAsset: ProposeIAssetContentSchema,
124
+ },
125
+ { flatInUnion: true },
126
+ ),
127
+ TSchema.Struct(
128
+ {
129
+ ModifyIAsset: ModifyIAssetContentSchema,
130
+ },
131
+ { flatInUnion: true },
132
+ ),
133
+ TSchema.Struct(
134
+ {
135
+ AddCollateralAsset: AddCollateralAssetContentSchema,
136
+ },
137
+ { flatInUnion: true },
138
+ ),
139
+ TSchema.Struct(
140
+ {
141
+ UpdateCollateralAsset: UpdateCollateralAssetContentSchema,
142
+ },
143
+ { flatInUnion: true },
144
+ ),
145
+ TSchema.Struct(
146
+ {
147
+ ProposeStableswapPool: ProposeStableswapPoolContentSchema,
148
+ },
149
+ { flatInUnion: true },
150
+ ),
151
+ TSchema.Struct(
152
+ {
153
+ ModifyStableswapPool: ModifyStableswapPoolContentSchema,
154
+ },
155
+ { flatInUnion: true },
156
+ ),
157
+ TSchema.Struct(
158
+ {
159
+ ModifyProtocolParams: TSchema.Struct(
160
+ {
161
+ newParams: ProtocolParamsSchema,
162
+ },
163
+ { flatFields: true },
164
+ ),
165
+ },
166
+ { flatInUnion: true },
167
+ ),
168
+ TSchema.Struct(
169
+ {
170
+ UpgradeProtocol: TSchema.Struct(
171
+ {
172
+ content: UpgradePathsSchema,
173
+ },
174
+ { flatFields: true },
175
+ ),
176
+ },
177
+ { flatInUnion: true },
178
+ ),
179
+ TSchema.Struct(
180
+ {
181
+ TextProposal: TSchema.ByteArray,
182
+ },
183
+ { flatInUnion: true },
184
+ ),
185
+ );
186
+ export type ProposalContent = typeof ProposalContentSchema.Type;
187
+
188
+ const TreasuryWithdrawalItemSchema = TSchema.Struct({
189
+ currencySymbol: TSchema.ByteArray,
190
+ tokenName: TSchema.ByteArray,
191
+ amount: TSchema.Integer,
192
+ });
193
+ export type TreasuryWithdrawalItem = typeof TreasuryWithdrawalItemSchema.Type;
194
+
195
+ export const TreasuryWithdrawalSchema = TSchema.Struct({
196
+ destination: AddressSchema,
197
+ value: TSchema.Array(TreasuryWithdrawalItemSchema),
198
+ });
199
+ export type TreasuryWithdrawal = typeof TreasuryWithdrawalSchema.Type;
200
+
201
+ const GovRedeemerSchema = TSchema.Union(
202
+ TSchema.Struct(
203
+ {
204
+ CreatePoll: TSchema.Struct(
205
+ {
206
+ currentTime: TSchema.Integer,
207
+ proposalOwner: TSchema.ByteArray,
208
+ content: ProposalContentSchema,
209
+ treasuryWithdrawal: TSchema.NullOr(TreasuryWithdrawalSchema),
210
+ },
211
+ { flatFields: true },
212
+ ),
213
+ },
214
+ { flatInUnion: true },
215
+ ),
216
+ TSchema.Struct(
217
+ {
218
+ WitnessEndPoll: TSchema.Struct(
219
+ {
220
+ currentTime: TSchema.Integer,
221
+ },
222
+ { flatFields: true },
223
+ ),
224
+ },
225
+ { flatInUnion: true },
226
+ ),
227
+ TSchema.Literal('UpgradeGov', { flatInUnion: true }),
228
+ TSchema.Literal('UpgradeVersion', { flatInUnion: true }),
229
+ );
230
+ export type GovRedeemer = typeof GovRedeemerSchema.Type;
231
+
232
+ const GovDatumSchema = TSchema.Struct({
233
+ currentProposal: TSchema.Integer,
234
+ protocolParams: ProtocolParamsSchema,
235
+ currentVersion: TSchema.Integer,
236
+ iassetsCount: TSchema.Integer,
237
+ activeProposals: TSchema.Integer,
238
+ });
239
+ export type GovDatum = typeof GovDatumSchema.Type;
240
+
241
+ export function serialiseGovRedeemer(r: GovRedeemer): string {
242
+ return Data.withSchema(GovRedeemerSchema).toCBORHex(r);
108
243
  }
109
244
 
110
- export function parseUpgradePaths(d: Data): UpgradePaths {
111
- return EvoCore.Data.withSchema(
112
- UpgradePathsSchema,
113
- DEFAULT_SCHEMA_OPTIONS,
114
- ).fromCBORHex(Data.to(d));
245
+ export function serialiseGovDatum(d: GovDatum): string {
246
+ return Data.withSchema(GovDatumSchema).toCBORHex(d);
247
+ }
248
+
249
+ export function parseGovDatum(datum: string): O.Option<GovDatum> {
250
+ try {
251
+ return O.some(
252
+ Data.withSchema(GovDatumSchema, DEFAULT_SCHEMA_OPTIONS).fromCBORHex(
253
+ datum,
254
+ ),
255
+ );
256
+ } catch (_) {
257
+ return O.none;
258
+ }
259
+ }
260
+
261
+ export function parseGovDatumOrThrow(datum: string): GovDatum {
262
+ return F.pipe(
263
+ parseGovDatum(datum),
264
+ O.match(() => {
265
+ throw new Error('Expected a Gov datum.');
266
+ }, F.identity),
267
+ );
115
268
  }
@@ -1,18 +1,5 @@
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;
1
+ import { Data } from '@lucid-evolution/lucid';
2
+ import { AssetClassSchema } from '../../types/generic';
16
3
 
17
4
  const GovParamsSchema = Data.Object({
18
5
  govNFT: AssetClassSchema,
@@ -20,7 +7,9 @@ const GovParamsSchema = Data.Object({
20
7
  upgradeToken: AssetClassSchema,
21
8
  indyAsset: AssetClassSchema,
22
9
  versionRecordToken: AssetClassSchema,
10
+ iassetSymbol: Data.Bytes(),
23
11
  pollManagerValHash: Data.Bytes(),
12
+ iassetValHash: Data.Bytes(),
24
13
  gBiasTime: Data.Integer(),
25
14
  daoIdentityToken: AssetClassSchema,
26
15
  iAssetAuthToken: AssetClassSchema,
@@ -28,62 +17,6 @@ const GovParamsSchema = Data.Object({
28
17
  export type GovParams = Data.Static<typeof GovParamsSchema>;
29
18
  export const GovParams = GovParamsSchema as unknown as GovParams;
30
19
 
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
20
  export function castGovParams(params: GovParams): Data {
88
21
  return Data.castTo(params, GovParams);
89
22
  }
@@ -0,0 +1,172 @@
1
+ import {
2
+ credentialToRewardAddress,
3
+ Data,
4
+ fromHex,
5
+ LucidEvolution,
6
+ OutRef,
7
+ scriptHashToCredential,
8
+ slotToUnixTime,
9
+ toHex,
10
+ TxBuilder,
11
+ UTxO,
12
+ } from '@lucid-evolution/lucid';
13
+ import { IAssetPriceInfo } from './types';
14
+ import { matchSingle } from '../../utils/utils';
15
+ import { parsePriceOracleDatum } from '../price-oracle/types-new';
16
+ import { getInlineDatumOrThrow } from '../../utils/lucid-utils';
17
+ import { oracleExpirationAwareValidity } from '../price-oracle/helpers';
18
+ import {
19
+ fromSysParamsDerivedPythPrice,
20
+ fromSystemParamsScriptRef,
21
+ getPythFeedConfig,
22
+ PythConfig,
23
+ } from '../../types/system-params';
24
+ import {
25
+ parsePythStateDatum,
26
+ serialisePythFeedRedeemer,
27
+ serialisePythUpdatesRedeemer,
28
+ } from '../pyth-feed/types';
29
+ import { match, P } from 'ts-pattern';
30
+ import { derivePythPrice } from '../pyth-feed/helpers';
31
+ import { AssetClass } from '@3rd-eye-labs/cardano-offchain-common';
32
+ import * as Core from '@evolution-sdk/evolution';
33
+ import { decodePriceUpdate, decodePythMessage } from '../../utils/pyth';
34
+
35
+ type Interval = {
36
+ validFrom: number;
37
+ validTo: number;
38
+ };
39
+
40
+ export function attachOracle(
41
+ iasset: Uint8Array<ArrayBufferLike>,
42
+ collateralAsset: AssetClass,
43
+ priceInfo: IAssetPriceInfo,
44
+ priceOracleOref: OutRef | undefined,
45
+ pythStateOref: OutRef | undefined,
46
+ pythMessage: string | undefined,
47
+ pythConfig: PythConfig,
48
+ biasTime: bigint,
49
+ currentSlot: number,
50
+ lucid: LucidEvolution,
51
+ tx: TxBuilder,
52
+ ): Promise<{ interval: Interval; referenceInputs: UTxO[] }> {
53
+ return match(priceInfo)
54
+ .returnType<Promise<{ interval: Interval; referenceInputs: UTxO[] }>>()
55
+ .with({ Delisted: P.any }, () => {
56
+ const currentTime = BigInt(
57
+ slotToUnixTime(lucid.config().network!, currentSlot),
58
+ );
59
+
60
+ // We want to make sure the transaction is valid within the bias time for interest tracking AND Pyth
61
+ // For validFrom, we take the higher of the two bias times
62
+ const validFrom = Number(currentTime - BigInt(biasTime / 2n));
63
+ // For validTo, we take the lower of the two bias times
64
+ const validTo = Number(currentTime + BigInt(biasTime / 2n));
65
+
66
+ return Promise.resolve({
67
+ interval: {
68
+ validFrom,
69
+ validTo,
70
+ },
71
+ referenceInputs: [],
72
+ });
73
+ })
74
+ .with({ OracleNft: P.any }, async () => {
75
+ if (!priceOracleOref) throw new Error('Missing price oracle');
76
+
77
+ const priceOracleUtxo = matchSingle<UTxO>(
78
+ await lucid.utxosByOutRef([priceOracleOref]),
79
+ (_) => new Error('Expected a single price oracle UTXO'),
80
+ );
81
+ const priceOracleDatum = parsePriceOracleDatum(
82
+ getInlineDatumOrThrow(priceOracleUtxo),
83
+ );
84
+
85
+ return {
86
+ interval: oracleExpirationAwareValidity(
87
+ currentSlot,
88
+ Number(biasTime),
89
+ Number(priceOracleDatum.expirationTime),
90
+ lucid.config().network!,
91
+ ),
92
+ referenceInputs: [priceOracleUtxo],
93
+ };
94
+ })
95
+ .with(
96
+ { DeferredValidation: { feedValHash: P.select() } },
97
+ async (feedValHash) => {
98
+ if (!pythMessage) throw new Error('Missing Pyth message');
99
+ if (!pythStateOref) throw new Error('Missing pyth state out ref');
100
+
101
+ const pythStateUtxo = matchSingle(
102
+ await lucid.utxosByOutRef([pythStateOref]),
103
+ (_) => new Error('Expected a single pyth state UTXO'),
104
+ );
105
+
106
+ const pythStateDatum = parsePythStateDatum(
107
+ getInlineDatumOrThrow(pythStateUtxo),
108
+ );
109
+
110
+ const pythFeedCfg = getPythFeedConfig(
111
+ pythConfig,
112
+ iasset,
113
+ collateralAsset,
114
+ );
115
+
116
+ // Lookup PFV Validator
117
+ const pfvScriptRefUtxo = matchSingle(
118
+ await lucid.utxosByOutRef([
119
+ fromSystemParamsScriptRef(pythFeedCfg.pythFeedValScriptRef),
120
+ ]),
121
+ (_) =>
122
+ new Error('Expected a single pyth feed validator Ref Script UTXO'),
123
+ );
124
+
125
+ const derivedPythPrice = fromSysParamsDerivedPythPrice(
126
+ pythFeedCfg.params.config,
127
+ );
128
+
129
+ const price = derivePythPrice(derivedPythPrice, pythMessage);
130
+
131
+ const decodedMessage = decodePythMessage(fromHex(pythMessage));
132
+ const pricePayload = decodePriceUpdate(decodedMessage.payload);
133
+ const pythTimestamp = Number(pricePayload.timestampUs) / 1_000;
134
+
135
+ tx.withdraw(
136
+ credentialToRewardAddress(
137
+ lucid.config().network!,
138
+ scriptHashToCredential(toHex(pythStateDatum.withdraw_script)),
139
+ ),
140
+ 0n,
141
+ serialisePythUpdatesRedeemer([fromHex(pythMessage)]),
142
+ );
143
+
144
+ tx.withdraw(
145
+ credentialToRewardAddress(
146
+ lucid.config().network!,
147
+ scriptHashToCredential(toHex(feedValHash)),
148
+ ),
149
+ 0n,
150
+ serialisePythFeedRedeemer({
151
+ price: price,
152
+ auxiliaryData: Core.Data.fromCBORHex(Data.void()),
153
+ }),
154
+ );
155
+
156
+ // 5 minutes - 20 seconds
157
+ const pythMaxDelay = 280 * 1000;
158
+
159
+ const validFrom = Number(pythTimestamp);
160
+ const validTo = Number(pythTimestamp + pythMaxDelay);
161
+
162
+ return {
163
+ interval: {
164
+ validFrom: validFrom,
165
+ validTo: validTo,
166
+ },
167
+ referenceInputs: [pythStateUtxo, pfvScriptRefUtxo],
168
+ };
169
+ },
170
+ )
171
+ .exhaustive();
172
+ }
@@ -0,0 +1,38 @@
1
+ import { applyParamsToScript, SpendingValidator } from '@lucid-evolution/lucid';
2
+ import {
3
+ fromSystemParamsAssetLucid,
4
+ IAssetParamsSP,
5
+ } from '../../types/system-params';
6
+ import { _cdpValidator } from '../../validators/cdp-validator';
7
+ import { _iassetValidator } from '../../validators/iasset-validator';
8
+
9
+ import { Data } from '@lucid-evolution/lucid';
10
+ import { AssetClassSchema } from '../../types/generic';
11
+
12
+ const IAssetScriptParamsSchema = Data.Object({
13
+ upgradeToken: AssetClassSchema,
14
+ versionRecordToken: AssetClassSchema,
15
+ });
16
+
17
+ export type IAssetScriptParams = Data.Static<typeof IAssetScriptParamsSchema>;
18
+ export const IAssetScriptParams =
19
+ IAssetScriptParamsSchema as unknown as IAssetScriptParams;
20
+
21
+ export const mkIAssetValidatorFromSP = (
22
+ params: IAssetParamsSP,
23
+ ): SpendingValidator => {
24
+ return {
25
+ type: 'PlutusV3',
26
+ script: applyParamsToScript(_iassetValidator.cborHex, [
27
+ Data.castTo(
28
+ {
29
+ upgradeToken: fromSystemParamsAssetLucid(params.upgradeToken),
30
+ versionRecordToken: fromSystemParamsAssetLucid(
31
+ params.versionRecordToken,
32
+ ),
33
+ },
34
+ IAssetScriptParams,
35
+ ),
36
+ ]),
37
+ };
38
+ };