@indigo-labs/indigo-sdk 0.2.41 → 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 -495
  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,240 @@
1
+ import { beforeEach, describe, test } from 'vitest';
2
+ import {
3
+ loadSystemParamsFromFile,
4
+ mkCDPCreatorValidatorFromSP,
5
+ mkCdpValidatorFromSP,
6
+ mkCollectorValidatorFromSP,
7
+ mkExecuteValidatorFromSP,
8
+ mkRobValidatorFromSP,
9
+ mkPollManagerValidatorFromSP,
10
+ mkPollShardValidatorFromSP,
11
+ mkVersionRegistryValidator,
12
+ } from '../src';
13
+ import { LucidContext } from './test-helpers';
14
+ import {
15
+ Emulator,
16
+ EmulatorAccount,
17
+ Lucid,
18
+ LucidEvolution,
19
+ SpendingValidator,
20
+ TxBuilder,
21
+ credentialToAddress,
22
+ fromHex,
23
+ generateEmulatorAccount,
24
+ } from '@lucid-evolution/lucid';
25
+ import { benchmarkAndAwaitTx } from './utils/benchmark-utils';
26
+ import { mkGovValidatorFromSP } from '../src/contracts/gov/scripts';
27
+ import { mkStabilityPoolValidatorFromSP } from '../src/contracts/stability-pool/scripts';
28
+ import { mkStakingValidatorFromSP } from '../src/contracts/staking/scripts';
29
+ import { mkTreasuryValidatorFromSP } from '../src/contracts/treasury/scripts';
30
+ import { mkStableswapValidatorFromSP } from '../src/contracts/stableswap/scripts';
31
+ import { mkPythFeedValidator } from '../src/contracts/pyth-feed/scripts';
32
+ import { MAINNET_PROTOCOL_PARAMETERS } from './indigo-test-helpers';
33
+
34
+ const systemParams = loadSystemParamsFromFile(
35
+ './tests/data/system-params.json',
36
+ );
37
+
38
+ type TestContext = LucidContext<{
39
+ admin: EmulatorAccount;
40
+ }>;
41
+
42
+ describe('Script Size', () => {
43
+ function initScriptRef(
44
+ lucid: LucidEvolution,
45
+ validator: SpendingValidator,
46
+ ): TxBuilder {
47
+ return lucid.newTx().pay.ToContract(
48
+ credentialToAddress(lucid.config().network!, {
49
+ hash: 'ea84d625650d066e1645e3e81d9c70a73f9ed837bd96dc49850ae744', // always fail validator hash
50
+ type: 'Script',
51
+ }),
52
+ undefined,
53
+ undefined,
54
+ validator,
55
+ );
56
+ }
57
+
58
+ beforeEach<TestContext>(async (context: TestContext) => {
59
+ context.users = {
60
+ admin: generateEmulatorAccount({
61
+ lovelace: BigInt(100_000_000),
62
+ }),
63
+ };
64
+
65
+ context.emulator = new Emulator(
66
+ [context.users.admin],
67
+ MAINNET_PROTOCOL_PARAMETERS,
68
+ );
69
+ context.lucid = await Lucid(context.emulator, 'Custom');
70
+ context.lucid.selectWallet.fromSeed(context.users.admin.seedPhrase);
71
+ });
72
+
73
+ test('CDP Creator', async (context: TestContext) => {
74
+ await benchmarkAndAwaitTx(
75
+ 'Script Size - CDP Creator',
76
+ initScriptRef(
77
+ context.lucid,
78
+ mkCDPCreatorValidatorFromSP(systemParams.cdpCreatorParams),
79
+ ),
80
+ context.lucid,
81
+ context.emulator,
82
+ );
83
+ });
84
+
85
+ test('CDP', async (context: TestContext) => {
86
+ await benchmarkAndAwaitTx(
87
+ 'Script Size - CDP',
88
+ initScriptRef(
89
+ context.lucid,
90
+ mkCdpValidatorFromSP(systemParams.cdpParams),
91
+ ),
92
+ context.lucid,
93
+ context.emulator,
94
+ );
95
+ });
96
+
97
+ test('Collector', async (context: TestContext) => {
98
+ await benchmarkAndAwaitTx(
99
+ 'Script Size - Collector',
100
+ initScriptRef(
101
+ context.lucid,
102
+ mkCollectorValidatorFromSP(systemParams.collectorParams),
103
+ ),
104
+ context.lucid,
105
+ context.emulator,
106
+ );
107
+ });
108
+
109
+ test('Execute', async (context: TestContext) => {
110
+ await benchmarkAndAwaitTx(
111
+ 'Script Size - Execute',
112
+ initScriptRef(
113
+ context.lucid,
114
+ mkExecuteValidatorFromSP(systemParams.executeParams),
115
+ ),
116
+ context.lucid,
117
+ context.emulator,
118
+ );
119
+ });
120
+
121
+ test('Gov', async (context: TestContext) => {
122
+ await benchmarkAndAwaitTx(
123
+ 'Script Size - Gov',
124
+ initScriptRef(
125
+ context.lucid,
126
+ mkGovValidatorFromSP(systemParams.govParams),
127
+ ),
128
+ context.lucid,
129
+ context.emulator,
130
+ );
131
+ });
132
+
133
+ test('ROB', async (context: TestContext) => {
134
+ await benchmarkAndAwaitTx(
135
+ 'Script Size - ROB',
136
+ initScriptRef(
137
+ context.lucid,
138
+ mkRobValidatorFromSP(systemParams.robParams),
139
+ ),
140
+ context.lucid,
141
+ context.emulator,
142
+ );
143
+ });
144
+
145
+ test('Poll Manager', async (context: TestContext) => {
146
+ await benchmarkAndAwaitTx(
147
+ 'Script Size - Poll Manager',
148
+ initScriptRef(
149
+ context.lucid,
150
+ mkPollManagerValidatorFromSP(systemParams.pollManagerParams),
151
+ ),
152
+ context.lucid,
153
+ context.emulator,
154
+ );
155
+ });
156
+
157
+ test('Poll Shard', async (context: TestContext) => {
158
+ await benchmarkAndAwaitTx(
159
+ 'Script Size - Poll Shard',
160
+ initScriptRef(
161
+ context.lucid,
162
+ mkPollShardValidatorFromSP(systemParams.pollShardParams),
163
+ ),
164
+ context.lucid,
165
+ context.emulator,
166
+ );
167
+ });
168
+
169
+ test('Pyth Feed', async (context: TestContext) => {
170
+ await benchmarkAndAwaitTx(
171
+ 'Script Size - Pyth Feed',
172
+ initScriptRef(
173
+ context.lucid,
174
+ mkPythFeedValidator({
175
+ config: { Value: { configuration: { priceFeedId: 0n } } },
176
+ pythStatePolicyId: fromHex(''),
177
+ }),
178
+ ),
179
+ context.lucid,
180
+ context.emulator,
181
+ );
182
+ });
183
+
184
+ test('Stability Pool', async (context: TestContext) => {
185
+ await benchmarkAndAwaitTx(
186
+ 'Script Size - Stability Pool',
187
+ initScriptRef(
188
+ context.lucid,
189
+ mkStabilityPoolValidatorFromSP(systemParams.stabilityPoolParams),
190
+ ),
191
+ context.lucid,
192
+ context.emulator,
193
+ );
194
+ });
195
+
196
+ test('Stableswap', async (context: TestContext) => {
197
+ await benchmarkAndAwaitTx(
198
+ 'Script Size - Stableswap',
199
+ initScriptRef(
200
+ context.lucid,
201
+ mkStableswapValidatorFromSP(systemParams.stableswapParams),
202
+ ),
203
+ context.lucid,
204
+ context.emulator,
205
+ );
206
+ });
207
+
208
+ test('Staking', async (context: TestContext) => {
209
+ await benchmarkAndAwaitTx(
210
+ 'Script Size - Staking',
211
+ initScriptRef(
212
+ context.lucid,
213
+ mkStakingValidatorFromSP(systemParams.stakingParams),
214
+ ),
215
+ context.lucid,
216
+ context.emulator,
217
+ );
218
+ });
219
+
220
+ test('Treasury', async (context: TestContext) => {
221
+ await benchmarkAndAwaitTx(
222
+ 'Script Size - Treasury',
223
+ initScriptRef(
224
+ context.lucid,
225
+ mkTreasuryValidatorFromSP(systemParams.treasuryParams),
226
+ ),
227
+ context.lucid,
228
+ context.emulator,
229
+ );
230
+ });
231
+
232
+ test('Version Registry', async (context: TestContext) => {
233
+ await benchmarkAndAwaitTx(
234
+ 'Script Size - Version Registry',
235
+ initScriptRef(context.lucid, mkVersionRegistryValidator()),
236
+ context.lucid,
237
+ context.emulator,
238
+ );
239
+ });
240
+ });
package/tests/setup.ts ADDED
@@ -0,0 +1,135 @@
1
+ import { BenchmarkResult } from './utils/benchmark-utils';
2
+ import { writeFileSync, readFileSync, existsSync, unlinkSync } from 'fs';
3
+ import { join } from 'path';
4
+
5
+ // File path to persist benchmark results across workers
6
+ const BENCHMARK_RESULTS_FILE = join(
7
+ process.cwd(),
8
+ '.vitest-benchmark-results.json',
9
+ );
10
+
11
+ // Parse command-line arguments for --benchmark-file flag
12
+ function getBenchmarkOutputFile(): string | null {
13
+ const args = process.argv;
14
+ const flagIndex = args.indexOf('--benchmark-file');
15
+ if (flagIndex !== -1 && flagIndex + 1 < args.length) {
16
+ return args[flagIndex + 1];
17
+ }
18
+ return null;
19
+ }
20
+
21
+ // Helper function to read results from file
22
+ function readResultsFromFile(): Record<string, BenchmarkResult> {
23
+ if (existsSync(BENCHMARK_RESULTS_FILE)) {
24
+ try {
25
+ const content = readFileSync(BENCHMARK_RESULTS_FILE, 'utf-8');
26
+ return JSON.parse(content) as Record<string, BenchmarkResult>;
27
+ } catch (error) {
28
+ console.warn('Failed to read benchmark results file:', error);
29
+ return {};
30
+ }
31
+ }
32
+ return {};
33
+ }
34
+
35
+ // Helper function to write results to file
36
+ function writeResultsToFile(results: Record<string, BenchmarkResult>): void {
37
+ try {
38
+ writeFileSync(
39
+ BENCHMARK_RESULTS_FILE,
40
+ JSON.stringify(results, null, 2),
41
+ 'utf-8',
42
+ );
43
+ } catch (error) {
44
+ console.warn('Failed to write benchmark results file:', error);
45
+ }
46
+ }
47
+
48
+ // In-memory store for the current worker/test file
49
+ // Using a Proxy to automatically sync to file when properties are set
50
+ const _BENCHMARK_RESULTS: Record<string, BenchmarkResult> = {};
51
+
52
+ // Create a Proxy that automatically syncs to file on property assignment
53
+ export const BENCHMARK_RESULTS = new Proxy(_BENCHMARK_RESULTS, {
54
+ set(target, property, value: BenchmarkResult) {
55
+ if (Object.prototype.hasOwnProperty.call(target, property)) {
56
+ throw new Error(
57
+ `Benchmark with name "${property as string}" already exists.`,
58
+ );
59
+ }
60
+
61
+ // Set the value in the local object
62
+ target[property as string] = value;
63
+
64
+ // Merge with existing results from file and write back
65
+ const existingResults = readResultsFromFile();
66
+ const mergedResults = { ...existingResults, ...target };
67
+ writeResultsToFile(mergedResults);
68
+
69
+ return true;
70
+ },
71
+ get(target, property) {
72
+ // When reading, first merge with file results to get latest from other workers
73
+ const fileResults = readResultsFromFile();
74
+ Object.assign(target, fileResults);
75
+ return target[property as string];
76
+ },
77
+ });
78
+
79
+ // Vitest global setup (must be default export)
80
+ export default function setup() {
81
+ // Clear any existing results file at the start
82
+ if (existsSync(BENCHMARK_RESULTS_FILE)) {
83
+ unlinkSync(BENCHMARK_RESULTS_FILE);
84
+ }
85
+
86
+ // Return teardown function
87
+ return function teardown() {
88
+ // Read all results from file (accumulated across all workers)
89
+ const results = readResultsFromFile();
90
+
91
+ if (Object.keys(results).length === 0) {
92
+ return;
93
+ }
94
+
95
+ // Sort entries by benchmark name alphabetically
96
+ const sortedEntries = Object.entries(results).sort(([a], [b]) =>
97
+ a.localeCompare(b),
98
+ );
99
+
100
+ const decimalPlaces = 3;
101
+
102
+ console.log('\n=== Benchmark Results ===');
103
+ console.table(
104
+ sortedEntries.map(([name, result]: [string, BenchmarkResult]) => ({
105
+ 'Benchmark Name': name,
106
+ 'Memory Percentage': result.memPercentage.toFixed(decimalPlaces) + '%',
107
+ 'Steps Percentage': result.stepsPercentage.toFixed(decimalPlaces) + '%',
108
+ 'Size Percentage': result.sizePercentage.toFixed(decimalPlaces) + '%',
109
+ })),
110
+ );
111
+
112
+ // Write results to output file if --benchmark-file flag is present
113
+ const outputFile = getBenchmarkOutputFile();
114
+ if (outputFile) {
115
+ try {
116
+ writeFileSync(
117
+ outputFile,
118
+ JSON.stringify(Object.fromEntries(sortedEntries), null, 2),
119
+ 'utf-8',
120
+ );
121
+ console.log(`\nBenchmark results written to: ${outputFile}`);
122
+ } catch (error) {
123
+ console.warn(
124
+ `Failed to write benchmark results to ${outputFile}:`,
125
+ error,
126
+ );
127
+ }
128
+ }
129
+
130
+ // Clean up the results file
131
+ if (existsSync(BENCHMARK_RESULTS_FILE)) {
132
+ unlinkSync(BENCHMARK_RESULTS_FILE);
133
+ }
134
+ };
135
+ }
@@ -0,0 +1,210 @@
1
+ import { AssetClass } from '@3rd-eye-labs/cardano-offchain-common';
2
+ import {
3
+ LucidEvolution,
4
+ TxBuilder,
5
+ unixTimeToSlot,
6
+ } from '@lucid-evolution/lucid';
7
+ import {
8
+ addrDetails,
9
+ adjustSpAccount,
10
+ bigintMax,
11
+ closeSpAccount,
12
+ createE2s2sSnapshots,
13
+ createSpAccount,
14
+ fromSystemParamsAsset,
15
+ openCdp,
16
+ processSpRequest,
17
+ SystemParams,
18
+ } from '../../src';
19
+ import {
20
+ findAllNecessaryOrefs,
21
+ findPriceOracleFromCollateralAsset,
22
+ } from '../cdp/cdp-queries';
23
+ import {
24
+ findE2s2sSnapshots,
25
+ findStabilityPool,
26
+ findStabilityPoolAccount,
27
+ } from '../queries/stability-pool-queries';
28
+ import { LucidContext, runAndAwaitTx } from '../test-helpers';
29
+ import { expect } from 'vitest';
30
+ import { findIAsset } from '../queries/iasset-queries';
31
+ import {
32
+ rationalCeil,
33
+ rationalDiv,
34
+ rationalFromInt,
35
+ rationalMul,
36
+ rationalSub,
37
+ } from '../../src/types/rational';
38
+
39
+ export async function runOpenCdpAndCreateSPAccount(
40
+ context: LucidContext,
41
+ sysParams: SystemParams,
42
+ asset: string,
43
+ /**
44
+ * The collateral asset to back the borrowed iAssets that will be used for stability pool deposit.
45
+ */
46
+ collateralAsset: AssetClass,
47
+ spDepositAmount: bigint,
48
+ ): Promise<TxBuilder> {
49
+ const orefs = await findAllNecessaryOrefs(
50
+ context.lucid,
51
+ sysParams,
52
+ asset,
53
+ collateralAsset,
54
+ );
55
+
56
+ const priceOracleUtxo = await findPriceOracleFromCollateralAsset(
57
+ context.lucid,
58
+ orefs.collateralAsset,
59
+ );
60
+
61
+ const collateral = bigintMax(
62
+ orefs.collateralAsset.datum.minCollateralAmt,
63
+ // collateralisation should be maintenance ratio * 2
64
+ rationalCeil(
65
+ rationalMul(
66
+ rationalFromInt(2n * spDepositAmount),
67
+ orefs.collateralAsset.datum.maintenanceRatio,
68
+ ),
69
+ ),
70
+ );
71
+
72
+ // Mint enough to match the intended SP deposit after paying debt minting fees.
73
+ const mintAmount = rationalCeil(
74
+ rationalDiv(
75
+ rationalFromInt(spDepositAmount),
76
+ rationalSub(rationalFromInt(1n), orefs.iasset.datum.debtMintingFeeRatio),
77
+ ),
78
+ );
79
+
80
+ await runAndAwaitTx(
81
+ context.lucid,
82
+ openCdp(
83
+ collateral,
84
+ BigInt(mintAmount),
85
+ sysParams,
86
+ orefs.cdpCreatorUtxo,
87
+ orefs.iasset.utxo,
88
+ orefs.collateralAsset.utxo,
89
+ priceOracleUtxo,
90
+ orefs.interestOracleUtxo,
91
+ orefs.treasuryUtxo,
92
+ context.lucid,
93
+ context.emulator.slot,
94
+ ),
95
+ );
96
+
97
+ return createSpAccount(asset, spDepositAmount, sysParams, context.lucid);
98
+ }
99
+
100
+ async function waitForAccountCooldown(
101
+ context: LucidContext,
102
+ sysParams: SystemParams,
103
+ asset: string,
104
+ // Request owner
105
+ pkh: string,
106
+ ) {
107
+ const accountUtxo = await findStabilityPoolAccount(
108
+ context.lucid,
109
+ sysParams,
110
+ pkh,
111
+ asset,
112
+ );
113
+
114
+ if (accountUtxo.datum.lastRequestProcessingTime !== 0n) {
115
+ const targetSlot = unixTimeToSlot(
116
+ context.lucid.config().network!,
117
+ Number(accountUtxo.datum.lastRequestProcessingTime) +
118
+ sysParams.stabilityPoolParams.accountProcessingCooldownMs,
119
+ );
120
+
121
+ if (targetSlot > context.emulator.slot) {
122
+ expect(targetSlot).toBeGreaterThan(context.emulator.slot);
123
+
124
+ context.emulator.awaitSlot(targetSlot - context.emulator.slot + 2);
125
+ }
126
+ }
127
+ }
128
+
129
+ export async function runProcessSpRequest(
130
+ context: LucidContext,
131
+ sysParams: SystemParams,
132
+ asset: string,
133
+ // Request owner
134
+ pkh: string,
135
+ ): Promise<TxBuilder> {
136
+ const sp = await findStabilityPool(context.lucid, sysParams, asset);
137
+
138
+ const ia = await findIAsset(
139
+ context.lucid,
140
+ sysParams.validatorHashes.iassetHash,
141
+ fromSystemParamsAsset(sysParams.cdpParams.iAssetAuthToken),
142
+ asset,
143
+ );
144
+
145
+ const accountUtxo = await findStabilityPoolAccount(
146
+ context.lucid,
147
+ sysParams,
148
+ pkh,
149
+ asset,
150
+ );
151
+
152
+ await waitForAccountCooldown(context, sysParams, asset, pkh);
153
+
154
+ return processSpRequest(
155
+ sp.utxo,
156
+ accountUtxo.utxo,
157
+ ia.utxo,
158
+ (await findE2s2sSnapshots(context.lucid, sysParams, asset)).map(
159
+ (res) => res.utxo,
160
+ ),
161
+ sysParams,
162
+ context.lucid,
163
+ context.emulator.slot,
164
+ );
165
+ }
166
+
167
+ export async function runCreateAdjustRequest(
168
+ lucid: LucidEvolution,
169
+ sysParams: SystemParams,
170
+ asset: string,
171
+ adjustment: bigint,
172
+ ): Promise<TxBuilder> {
173
+ const [pkh, _] = await addrDetails(lucid);
174
+
175
+ const accountUtxo = await findStabilityPoolAccount(
176
+ lucid,
177
+ sysParams,
178
+ pkh.hash,
179
+ asset,
180
+ );
181
+
182
+ return adjustSpAccount(adjustment, accountUtxo.utxo, sysParams, lucid);
183
+ }
184
+
185
+ export async function runCreateCloseRequest(
186
+ lucid: LucidEvolution,
187
+ sysParams: SystemParams,
188
+ asset: string,
189
+ ): Promise<TxBuilder> {
190
+ const [pkh, _] = await addrDetails(lucid);
191
+
192
+ const accountUtxo = await findStabilityPoolAccount(
193
+ lucid,
194
+ sysParams,
195
+ pkh.hash,
196
+ asset,
197
+ );
198
+
199
+ return closeSpAccount(accountUtxo.utxo, sysParams, lucid);
200
+ }
201
+
202
+ export async function runCreateE2s2sSnapshots(
203
+ context: LucidContext,
204
+ sysParams: SystemParams,
205
+ asset: string,
206
+ ): Promise<TxBuilder> {
207
+ const spUtxo = await findStabilityPool(context.lucid, sysParams, asset);
208
+
209
+ return createE2s2sSnapshots(spUtxo.utxo, sysParams, context.lucid);
210
+ }