@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
@@ -0,0 +1,21 @@
1
+ import { fromHex } from '@lucid-evolution/lucid';
2
+ import { ProtocolParams } from '../../src/contracts/gov/types-new';
3
+
4
+ export const protocolParamsMock: ProtocolParams = {
5
+ effectiveDelay: 1_000n,
6
+ expirationPeriod: 360_000n,
7
+ proposalDeposit: 1_000n,
8
+ proposingPeriod: 100_000n,
9
+ votingPeriod: 1000_000n,
10
+ totalShards: 4n,
11
+ minimumQuorum: 100_000n,
12
+ maxTreasuryLovelaceSpend: 10_000_000n,
13
+ maxTreasuryIndySpend: 10_000_000n,
14
+ cdpRedemptionRequiredSignature: null,
15
+ electorate: 35_000_000_000_000n,
16
+ foundationMultisig: {
17
+ Signature: {
18
+ keyHash: fromHex(''),
19
+ },
20
+ },
21
+ };
@@ -0,0 +1,163 @@
1
+ import {
2
+ AssetClass,
3
+ getInlineDatumOrThrow,
4
+ isSameAssetClass,
5
+ } from '@3rd-eye-labs/cardano-offchain-common';
6
+ import { AssetInfo, fromSystemParamsAsset, SystemParams } from '../../src';
7
+ import {
8
+ LucidContext,
9
+ runAndAwaitTx,
10
+ selectWalletByAddress,
11
+ } from '../test-helpers';
12
+ import {
13
+ findAllNecessaryOrefs,
14
+ findPrice,
15
+ findPriceOracleFromCollateralAsset,
16
+ } from '../cdp/cdp-queries';
17
+ import { feedPriceOracleTx } from '../../src/contracts/price-oracle/transactions';
18
+ import { findCollateralAsset } from '../queries/iasset-queries';
19
+ import { unixTimeToSlot } from '@lucid-evolution/lucid';
20
+ import { findPriceOracle } from './price-oracle-queries';
21
+ import { match, P } from 'ts-pattern';
22
+ import { parsePriceOracleDatum } from '../../src/contracts/price-oracle/types-new';
23
+ import { expect } from 'vitest';
24
+ import { Rational } from '../../src/types/rational';
25
+
26
+ /**
27
+ * Selects admin wallet and feeds the price oracle with the given price
28
+ */
29
+ export async function runFeedPriceToOracle(
30
+ context: LucidContext,
31
+ sysParams: SystemParams,
32
+ assetInfo: AssetInfo,
33
+ collateralAsset: AssetClass,
34
+ price: Rational,
35
+ ): Promise<void> {
36
+ const prevAddr = await context.lucid.wallet().address();
37
+
38
+ context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
39
+
40
+ const collateralOut = await findCollateralAsset(
41
+ context.lucid,
42
+ sysParams,
43
+ fromSystemParamsAsset(sysParams.cdpParams.collateralAssetAuthToken),
44
+ assetInfo.iassetTokenNameAscii,
45
+ collateralAsset,
46
+ );
47
+
48
+ const collateralInfo = assetInfo.collateralAssets.find((a) =>
49
+ isSameAssetClass(a.collateralAsset, collateralAsset),
50
+ );
51
+
52
+ if (!collateralInfo) {
53
+ throw new Error('No such collateral asset for the given iasset.');
54
+ }
55
+
56
+ const priceOracleUtxo = await findPriceOracleFromCollateralAsset(
57
+ context.lucid,
58
+ collateralOut,
59
+ );
60
+
61
+ await runAndAwaitTx(
62
+ context.lucid,
63
+ feedPriceOracleTx(
64
+ context.lucid,
65
+ priceOracleUtxo!,
66
+ price,
67
+ collateralInfo.oracleParams!,
68
+ context.emulator.slot,
69
+ ),
70
+ );
71
+
72
+ selectWalletByAddress(context, prevAddr);
73
+ }
74
+
75
+ /**
76
+ * When oracle expired, refreshing updates the expiration leaving the same price.
77
+ */
78
+ export async function refreshPriceOracle(
79
+ context: LucidContext,
80
+ sysParams: SystemParams,
81
+ assetInfo: AssetInfo,
82
+ collateralAsset: AssetClass,
83
+ ): Promise<void> {
84
+ const prevAddr = await context.lucid.wallet().address();
85
+
86
+ context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
87
+
88
+ // Feed price oracle
89
+ const orefs = await findAllNecessaryOrefs(
90
+ context.lucid,
91
+ sysParams,
92
+ assetInfo.iassetTokenNameAscii,
93
+ collateralAsset,
94
+ );
95
+
96
+ const collateralInfo = assetInfo.collateralAssets.find((a) =>
97
+ isSameAssetClass(a.collateralAsset, collateralAsset),
98
+ );
99
+
100
+ if (!collateralInfo) {
101
+ throw new Error('No such collateral asset for the given iasset.');
102
+ }
103
+
104
+ const price = await findPrice(
105
+ context.lucid,
106
+ sysParams,
107
+ assetInfo.iassetTokenNameAscii,
108
+ collateralAsset,
109
+ );
110
+
111
+ const priceOracleUtxo = await findPriceOracleFromCollateralAsset(
112
+ context.lucid,
113
+ orefs.collateralAsset,
114
+ );
115
+
116
+ await runAndAwaitTx(
117
+ context.lucid,
118
+ feedPriceOracleTx(
119
+ context.lucid,
120
+ priceOracleUtxo!,
121
+ price,
122
+ collateralInfo.oracleParams!,
123
+ context.emulator.slot,
124
+ ),
125
+ );
126
+
127
+ selectWalletByAddress(context, prevAddr);
128
+ }
129
+
130
+ export async function waitForOracleExpiration(
131
+ context: LucidContext,
132
+ sysParams: SystemParams,
133
+ iasset: string,
134
+ collateralAsset: AssetClass,
135
+ ): Promise<void> {
136
+ const collateral = await findCollateralAsset(
137
+ context.lucid,
138
+ sysParams,
139
+ fromSystemParamsAsset(sysParams.cdpCreatorParams.collateralAssetAuthTk),
140
+ iasset,
141
+ collateralAsset,
142
+ );
143
+
144
+ await match(collateral.datum.priceInfo)
145
+ .with({ OracleNft: P.select() }, async (nft) => {
146
+ const priceOracle = await findPriceOracle(context.lucid, nft);
147
+
148
+ const oracle = parsePriceOracleDatum(getInlineDatumOrThrow(priceOracle));
149
+
150
+ const targetSlot = unixTimeToSlot(
151
+ context.lucid.config().network!,
152
+ Number(oracle.expirationTime),
153
+ );
154
+
155
+ if (targetSlot > context.emulator.slot) {
156
+ expect(targetSlot).toBeGreaterThan(context.emulator.slot);
157
+
158
+ context.emulator.awaitSlot(targetSlot - context.emulator.slot + 5);
159
+ }
160
+ })
161
+ // otherwise no need to await
162
+ .otherwise(() => {});
163
+ }
@@ -0,0 +1,12 @@
1
+ import { LucidEvolution, UTxO } from '@lucid-evolution/lucid';
2
+ import {
3
+ AssetClass,
4
+ assetClassToUnit,
5
+ } from '@3rd-eye-labs/cardano-offchain-common';
6
+
7
+ export async function findPriceOracle(
8
+ lucid: LucidEvolution,
9
+ oracleNft: AssetClass,
10
+ ): Promise<UTxO> {
11
+ return lucid.utxoByUnit(assetClassToUnit(oracleNft));
12
+ }
@@ -0,0 +1,240 @@
1
+ import {
2
+ addAssets,
3
+ Emulator,
4
+ EmulatorAccount,
5
+ generateEmulatorAccount,
6
+ Lucid,
7
+ paymentCredentialOf,
8
+ } from '@lucid-evolution/lucid';
9
+ import { beforeEach, describe, test } from 'vitest';
10
+ import {
11
+ LucidContext,
12
+ runAndAwaitTx,
13
+ runAndAwaitTxBuilder,
14
+ } from '../test-helpers';
15
+ import { mkLovelacesOf } from '@3rd-eye-labs/cardano-offchain-common';
16
+ import {
17
+ feedPriceOracleTx,
18
+ PriceOracleParams,
19
+ startPriceOracleTx,
20
+ } from '../../src';
21
+ import { expectScriptFailure } from '../utils/asserts';
22
+ import { feedPriceOracleTxStealNft } from './transactions-mutated';
23
+ import * as Core from '@evolution-sdk/evolution';
24
+ import { rationalFromInt } from '../../src/types/rational';
25
+
26
+ type MyContext = LucidContext<{
27
+ admin: EmulatorAccount;
28
+ }>;
29
+
30
+ describe('Rational Price Oracle', () => {
31
+ beforeEach<MyContext>(async (context: MyContext) => {
32
+ context.users = {
33
+ admin: generateEmulatorAccount(
34
+ addAssets(mkLovelacesOf(100_000_000_000_000n)),
35
+ ),
36
+ };
37
+
38
+ context.emulator = new Emulator(Object.values(context.users));
39
+ context.lucid = await Lucid(context.emulator, 'Custom');
40
+ context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
41
+ });
42
+
43
+ test<MyContext>('Start Rational Price Oracle', async (context: MyContext) => {
44
+ const pkh = paymentCredentialOf(context.users.admin.address).hash;
45
+ const params: PriceOracleParams = {
46
+ owner: pkh,
47
+ biasTime: 20_000n,
48
+ expirationPeriod: 1_800_000n,
49
+ };
50
+ const [tx, _] = await startPriceOracleTx(
51
+ context.lucid,
52
+ 'ORACLE_NFT',
53
+ rationalFromInt(1n),
54
+ params,
55
+ context.emulator.slot,
56
+ );
57
+
58
+ await runAndAwaitTxBuilder(context.lucid, tx);
59
+ });
60
+
61
+ test<MyContext>('Feed Rational Price Oracle', async (context: MyContext) => {
62
+ const pkh = paymentCredentialOf(context.users.admin.address).hash;
63
+ const params: PriceOracleParams = {
64
+ owner: pkh,
65
+ biasTime: 20_000n,
66
+ expirationPeriod: 1_800_000n,
67
+ };
68
+ const [startTx, _] = await startPriceOracleTx(
69
+ context.lucid,
70
+ 'ORACLE_NFT',
71
+ rationalFromInt(1n),
72
+ params,
73
+ context.emulator.slot,
74
+ );
75
+
76
+ const startTxHash = await runAndAwaitTxBuilder(context.lucid, startTx);
77
+
78
+ const feedTx = await feedPriceOracleTx(
79
+ context.lucid,
80
+ { txHash: startTxHash, outputIndex: 0 },
81
+ rationalFromInt(2n),
82
+ params,
83
+ context.emulator.slot,
84
+ );
85
+
86
+ await runAndAwaitTxBuilder(context.lucid, feedTx);
87
+ });
88
+
89
+ test<MyContext>('Feed Rational Price Oracle cannot be negative', async (context: MyContext) => {
90
+ const pkh = paymentCredentialOf(context.users.admin.address).hash;
91
+ const params: PriceOracleParams = {
92
+ owner: pkh,
93
+ biasTime: 20_000n,
94
+ expirationPeriod: 1_800_000n,
95
+ };
96
+ const [startTx, _] = await startPriceOracleTx(
97
+ context.lucid,
98
+ 'ORACLE_NFT',
99
+ rationalFromInt(1n),
100
+ params,
101
+ context.emulator.slot,
102
+ );
103
+
104
+ const startTxHash = await runAndAwaitTxBuilder(context.lucid, startTx);
105
+
106
+ await expectScriptFailure(
107
+ 'Price must be positive',
108
+ feedPriceOracleTx(
109
+ context.lucid,
110
+ { txHash: startTxHash, outputIndex: 0 },
111
+ rationalFromInt(-2n),
112
+ params,
113
+ context.emulator.slot,
114
+ ),
115
+ );
116
+ });
117
+
118
+ test<MyContext>('Feed Rational Price Oracle cannot be steal NFT', async (context: MyContext) => {
119
+ const pkh = paymentCredentialOf(context.users.admin.address).hash;
120
+ const params: PriceOracleParams = {
121
+ owner: pkh,
122
+ biasTime: 20_000n,
123
+ expirationPeriod: 1_800_000n,
124
+ };
125
+ const [startTx, _] = await startPriceOracleTx(
126
+ context.lucid,
127
+ 'ORACLE_NFT',
128
+ rationalFromInt(1n),
129
+ params,
130
+ context.emulator.slot,
131
+ );
132
+
133
+ const startTxHash = await runAndAwaitTxBuilder(context.lucid, startTx);
134
+
135
+ await expectScriptFailure(
136
+ 'Wrong continuing output',
137
+ feedPriceOracleTxStealNft(
138
+ context.lucid,
139
+ { txHash: startTxHash, outputIndex: 0 },
140
+ rationalFromInt(2n),
141
+ params,
142
+ context.emulator.slot,
143
+ ),
144
+ );
145
+ });
146
+
147
+ test<MyContext>('Feed Rational Price Oracle cannot have zero denominator', async (context: MyContext) => {
148
+ const pkh = paymentCredentialOf(context.users.admin.address).hash;
149
+ const params: PriceOracleParams = {
150
+ owner: pkh,
151
+ biasTime: 20_000n,
152
+ expirationPeriod: 1_800_000n,
153
+ };
154
+ const [startTx, _] = await startPriceOracleTx(
155
+ context.lucid,
156
+ 'ORACLE_NFT',
157
+ rationalFromInt(1n),
158
+ params,
159
+ context.emulator.slot,
160
+ );
161
+
162
+ const startTxHash = await runAndAwaitTxBuilder(context.lucid, startTx);
163
+
164
+ await expectScriptFailure(
165
+ 'Denominator cannot be zero',
166
+ feedPriceOracleTx(
167
+ context.lucid,
168
+ { txHash: startTxHash, outputIndex: 0 },
169
+ { numerator: 2n, denominator: 0n },
170
+ params,
171
+ context.emulator.slot,
172
+ ),
173
+ );
174
+ });
175
+
176
+ test<MyContext>('Feed Rational Price Oracle with large number', async (context: MyContext) => {
177
+ const pkh = paymentCredentialOf(context.users.admin.address).hash;
178
+ const params: PriceOracleParams = {
179
+ owner: pkh,
180
+ biasTime: 20_000n,
181
+ expirationPeriod: 1_800_000n,
182
+ };
183
+ const [startTx, _] = await startPriceOracleTx(
184
+ context.lucid,
185
+ 'ORACLE_NFT',
186
+ rationalFromInt(1n),
187
+ params,
188
+ context.emulator.slot,
189
+ );
190
+
191
+ const startTxHash = await runAndAwaitTxBuilder(context.lucid, startTx);
192
+
193
+ await runAndAwaitTx(
194
+ context.lucid,
195
+ feedPriceOracleTx(
196
+ context.lucid,
197
+ { txHash: startTxHash, outputIndex: 0 },
198
+ {
199
+ numerator:
200
+ BigInt(Number.MAX_SAFE_INTEGER) * BigInt(Number.MAX_SAFE_INTEGER),
201
+ denominator: BigInt(Number.MAX_SAFE_INTEGER),
202
+ },
203
+ params,
204
+ context.emulator.slot,
205
+ ),
206
+ );
207
+ });
208
+
209
+ test<MyContext>('Feed Rational Price Oracle can change auxiliary data', async (context: MyContext) => {
210
+ const pkh = paymentCredentialOf(context.users.admin.address).hash;
211
+ const params: PriceOracleParams = {
212
+ owner: pkh,
213
+ biasTime: 20_000n,
214
+ expirationPeriod: 1_800_000n,
215
+ };
216
+ const [startTx, _] = await startPriceOracleTx(
217
+ context.lucid,
218
+ 'ORACLE_NFT',
219
+ rationalFromInt(1n),
220
+ params,
221
+ context.emulator.slot,
222
+ );
223
+
224
+ const startTxHash = await runAndAwaitTxBuilder(context.lucid, startTx);
225
+
226
+ await runAndAwaitTx(
227
+ context.lucid,
228
+ feedPriceOracleTx(
229
+ context.lucid,
230
+ { txHash: startTxHash, outputIndex: 0 },
231
+ rationalFromInt(1n),
232
+ params,
233
+ context.emulator.slot,
234
+ Core.Data.fromCBORHex(
235
+ 'd8799f1b0180e51d1ae19514d8799f1a00030d40ff1b00000194ce33c598ff',
236
+ ),
237
+ ),
238
+ );
239
+ });
240
+ });
@@ -0,0 +1,62 @@
1
+ import {
2
+ Data,
3
+ LucidEvolution,
4
+ OutRef,
5
+ slotToUnixTime,
6
+ TxBuilder,
7
+ } from '@lucid-evolution/lucid';
8
+ import {
9
+ serialisePriceOracleDatum,
10
+ serialisePriceOracleRedeemer,
11
+ } from '../../src/contracts/price-oracle/types-new';
12
+ import { PriceOracleParams } from '../../src/contracts/price-oracle/types';
13
+ import { mkPriceOracleValidator } from '../../src/contracts/price-oracle/scripts';
14
+ import { matchSingle } from '../../src/utils/utils';
15
+ import { ONE_SECOND } from '../../src/utils/time-helpers';
16
+ import * as Core from '@evolution-sdk/evolution';
17
+ import { mkLovelacesOf } from '@3rd-eye-labs/cardano-offchain-common';
18
+ import { Rational } from '../../src/types/rational';
19
+
20
+ export async function feedPriceOracleTxStealNft(
21
+ lucid: LucidEvolution,
22
+ oracleOref: OutRef,
23
+ newPrice: Rational,
24
+ oracleParams: PriceOracleParams,
25
+ currentSlot: number,
26
+ ): Promise<TxBuilder> {
27
+ const network = lucid.config().network!;
28
+ const currentTime = BigInt(slotToUnixTime(network, currentSlot));
29
+
30
+ const priceOracleUtxo = matchSingle(
31
+ await lucid.utxosByOutRef([oracleOref]),
32
+ (_) => new Error('Expected a single price oracle UTXO'),
33
+ );
34
+
35
+ const oracleValidator = mkPriceOracleValidator(oracleParams);
36
+
37
+ return lucid
38
+ .newTx()
39
+ .validFrom(Number(currentTime - oracleParams.biasTime) + ONE_SECOND)
40
+ .validTo(Number(currentTime + oracleParams.biasTime) - ONE_SECOND)
41
+ .attach.SpendingValidator(oracleValidator)
42
+ .collectFrom(
43
+ [priceOracleUtxo],
44
+ serialisePriceOracleRedeemer({
45
+ currentTime: currentTime,
46
+ newPrice: newPrice,
47
+ }),
48
+ )
49
+ .pay.ToContract(
50
+ priceOracleUtxo.address,
51
+ {
52
+ kind: 'inline',
53
+ value: serialisePriceOracleDatum({
54
+ price: newPrice,
55
+ expirationTime: currentTime + oracleParams.expirationPeriod,
56
+ auxiliaryData: Core.Data.fromCBORHex(Data.void()),
57
+ }),
58
+ },
59
+ mkLovelacesOf(5_000_000n),
60
+ )
61
+ .addSignerKey(oracleParams.owner);
62
+ }
@@ -0,0 +1,96 @@
1
+ import {
2
+ credentialToAddress,
3
+ credentialToRewardAddress,
4
+ fromHex,
5
+ fromText,
6
+ LucidEvolution,
7
+ validatorToScriptHash,
8
+ } from '@lucid-evolution/lucid';
9
+ import {
10
+ PythStateDatum,
11
+ serialisePythStateDatum,
12
+ } from '../../src/contracts/pyth-feed/types';
13
+ import { alwaysSucceedValidator } from '../always-succeed/script';
14
+ import { runAndAwaitTxBuilder } from '../test-helpers';
15
+ import { AssetClass, mkAssetsOf } from '@3rd-eye-labs/cardano-offchain-common';
16
+ import { mintOneTimeToken } from '../../src';
17
+
18
+ export const TEST_TRUSTED_SIGNER_PRIV_KEY =
19
+ 'a8e6c4d187e6247719cf45112917094c6973d2cf5a5f2e9f3a6802543e523239';
20
+ export const TEST_TRUSTED_SIGNER_PUB_KEY =
21
+ '4a50d7c3d16b2be5c16ba996109a455a34cce08a81b3e15b86ef407e2f72e71f';
22
+
23
+ // For the purpose of testing Pyth in an emulated environment, we need to initialize a Pyth State NFT
24
+ // This NFT will be sent to an always succeeds validator that will have the "Pyth State" datum
25
+ // The important part is that the Pyth State includes an always succeeds withdraw script and a
26
+ // trusted signer that we can use to validate the Pyth Messages we generate.
27
+ export async function initPyth(lucid: LucidEvolution): Promise<AssetClass> {
28
+ const pythStateName = fromText('Pyth State');
29
+ const pythStatePolicyId = await mintOneTimeToken(lucid, pythStateName, 1n);
30
+ const pythStateAsset = {
31
+ currencySymbol: fromHex(pythStatePolicyId),
32
+ tokenName: fromHex(pythStateName),
33
+ };
34
+
35
+ const alwaysSucceedsValidatorHash = validatorToScriptHash(
36
+ alwaysSucceedValidator,
37
+ );
38
+ const pythStateDatum: PythStateDatum = {
39
+ // The governance doesn't matter for the purpose of testing, so we can use any values
40
+ governance: {
41
+ wormhole: fromHex(''),
42
+ emitterChain: 0n,
43
+ emitterAddress: fromHex(''),
44
+ seenSequence: 0n,
45
+ },
46
+ trustedSigners: new Map([
47
+ [
48
+ fromHex(TEST_TRUSTED_SIGNER_PUB_KEY),
49
+ {
50
+ lower_bound: {
51
+ bound_type: {
52
+ _tag: 'NegativeInfinity',
53
+ },
54
+ is_inclusive: true,
55
+ },
56
+ upper_bound: {
57
+ bound_type: {
58
+ _tag: 'PositiveInfinity',
59
+ },
60
+ is_inclusive: true,
61
+ },
62
+ },
63
+ ],
64
+ ]),
65
+ deprecatedWithdrawScripts: new Map(),
66
+ // For the purpose of testing, we use the always succeeds validator to validate the Pyth Messages.
67
+ // This means the message signature checking will always succeed for the purpose of emulation testing.
68
+ withdraw_script: fromHex(alwaysSucceedsValidatorHash),
69
+ };
70
+
71
+ await runAndAwaitTxBuilder(
72
+ lucid,
73
+ lucid.newTx().pay.ToContract(
74
+ credentialToAddress(lucid.config().network!, {
75
+ hash: validatorToScriptHash(alwaysSucceedValidator),
76
+ type: 'Script',
77
+ }),
78
+ { kind: 'inline', value: serialisePythStateDatum(pythStateDatum) },
79
+ mkAssetsOf(pythStateAsset, 1n),
80
+ alwaysSucceedValidator,
81
+ ),
82
+ );
83
+
84
+ // Register the stake key for always succeeds validator.
85
+ await runAndAwaitTxBuilder(
86
+ lucid,
87
+ lucid.newTx().register.Stake(
88
+ credentialToRewardAddress(lucid.config().network!, {
89
+ hash: validatorToScriptHash(alwaysSucceedValidator),
90
+ type: 'Script',
91
+ }),
92
+ ),
93
+ );
94
+
95
+ return pythStateAsset;
96
+ }
@@ -0,0 +1,37 @@
1
+ import { fromHex } from '@lucid-evolution/lucid';
2
+ import { ParsedFeedPayload } from '@pythnetwork/pyth-lazer-sdk';
3
+ import {
4
+ encodePriceUpdate,
5
+ encodePythMessage,
6
+ type PythMessageParts,
7
+ } from '../../src/utils/pyth';
8
+ import {
9
+ TEST_TRUSTED_SIGNER_PRIV_KEY,
10
+ TEST_TRUSTED_SIGNER_PUB_KEY,
11
+ } from './endpoints';
12
+
13
+ /**
14
+ * Creates a signed Pyth message from a list of price feeds.
15
+ * Uses the test trusted signer keys for signing.
16
+ */
17
+ export async function createPythMessage(
18
+ feeds: ParsedFeedPayload[],
19
+ timestampUs: bigint,
20
+ channelId = 0,
21
+ ): Promise<Uint8Array> {
22
+ const noble = await import('@noble/ed25519');
23
+ const payload = encodePriceUpdate({
24
+ timestampUs: timestampUs.toString(),
25
+ channelId,
26
+ priceFeeds: feeds,
27
+ });
28
+ const parts: PythMessageParts = {
29
+ signature: await noble.signAsync(
30
+ payload,
31
+ fromHex(TEST_TRUSTED_SIGNER_PRIV_KEY),
32
+ ),
33
+ publicKey: fromHex(TEST_TRUSTED_SIGNER_PUB_KEY),
34
+ payload,
35
+ };
36
+ return encodePythMessage(parts);
37
+ }