@indigo-labs/indigo-sdk 0.2.42 → 0.3.1

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 (202) hide show
  1. package/.github/workflows/ci.yml +4 -2
  2. package/README.md +88 -15
  3. package/dist/index.d.mts +3012 -2194
  4. package/dist/index.d.ts +3012 -2194
  5. package/dist/index.js +9849 -6198
  6. package/dist/index.mjs +8733 -4933
  7. package/package.json +14 -3
  8. package/src/contracts/cdp/helpers.ts +68 -72
  9. package/src/contracts/cdp/scripts.ts +50 -13
  10. package/src/contracts/cdp/transactions.ts +841 -546
  11. package/src/contracts/cdp/types-new.ts +256 -0
  12. package/src/contracts/cdp/types.ts +26 -144
  13. package/src/contracts/cdp-creator/scripts.ts +15 -9
  14. package/src/contracts/cdp-creator/types-new.ts +50 -0
  15. package/src/contracts/cdp-creator/types.ts +5 -31
  16. package/src/contracts/collector/scripts.ts +1 -1
  17. package/src/contracts/collector/transactions.ts +23 -13
  18. package/src/contracts/collector/types-new.ts +17 -0
  19. package/src/contracts/execute/scripts.ts +19 -10
  20. package/src/contracts/execute/types-new.ts +44 -0
  21. package/src/contracts/execute/types.ts +5 -38
  22. package/src/contracts/gov/helpers.ts +187 -51
  23. package/src/contracts/gov/scripts.ts +17 -10
  24. package/src/contracts/gov/transactions.ts +599 -271
  25. package/src/contracts/gov/types-new.ts +253 -100
  26. package/src/contracts/gov/types.ts +4 -71
  27. package/src/contracts/iasset/helpers.ts +172 -0
  28. package/src/contracts/iasset/scripts.ts +38 -0
  29. package/src/contracts/iasset/types.ts +154 -0
  30. package/src/contracts/initialize/actions.ts +768 -0
  31. package/src/contracts/initialize/helpers.ts +611 -36
  32. package/src/contracts/initialize/types.ts +102 -28
  33. package/src/contracts/interest-collection/helpers.ts +19 -0
  34. package/src/contracts/interest-collection/scripts.ts +44 -0
  35. package/src/contracts/interest-collection/transactions.ts +436 -0
  36. package/src/contracts/interest-collection/types-new.ts +50 -0
  37. package/src/contracts/interest-collection/types.ts +26 -0
  38. package/src/contracts/interest-oracle/helpers.ts +2 -30
  39. package/src/contracts/interest-oracle/scripts.ts +1 -1
  40. package/src/contracts/interest-oracle/transactions.ts +21 -16
  41. package/src/contracts/interest-oracle/types-new.ts +32 -0
  42. package/src/contracts/interest-oracle/types.ts +1 -40
  43. package/src/contracts/one-shot/transactions.ts +1 -2
  44. package/src/contracts/poll/helpers.ts +5 -23
  45. package/src/contracts/poll/scripts.ts +12 -13
  46. package/src/contracts/poll/types-poll-manager.ts +1 -19
  47. package/src/contracts/poll/types-poll-new.ts +170 -0
  48. package/src/contracts/poll/types-poll-shard.ts +2 -24
  49. package/src/contracts/price-oracle/helpers.ts +1 -4
  50. package/src/contracts/price-oracle/scripts.ts +3 -8
  51. package/src/contracts/price-oracle/transactions.ts +32 -25
  52. package/src/contracts/price-oracle/types-new.ts +50 -0
  53. package/src/contracts/price-oracle/types.ts +2 -36
  54. package/src/contracts/pyth-feed/helpers.ts +58 -0
  55. package/src/contracts/pyth-feed/scripts.ts +15 -0
  56. package/src/contracts/pyth-feed/types.ts +181 -0
  57. package/src/contracts/rob/helpers.ts +405 -0
  58. package/src/contracts/rob/scripts.ts +35 -0
  59. package/src/contracts/rob/transactions.ts +410 -0
  60. package/src/contracts/rob/types-new.ts +128 -0
  61. package/src/contracts/rob/types.ts +16 -0
  62. package/src/contracts/rob-leverage/helpers.ts +424 -0
  63. package/src/contracts/{leverage → rob-leverage}/transactions.ts +68 -48
  64. package/src/contracts/stability-pool/helpers.ts +714 -230
  65. package/src/contracts/stability-pool/scripts.ts +20 -15
  66. package/src/contracts/stability-pool/transactions.ts +628 -496
  67. package/src/contracts/stability-pool/types-new.ts +247 -100
  68. package/src/contracts/stability-pool/types.ts +5 -22
  69. package/src/contracts/stableswap/helpers.ts +22 -0
  70. package/src/contracts/stableswap/scripts.ts +37 -0
  71. package/src/contracts/stableswap/transactions.ts +647 -0
  72. package/src/contracts/stableswap/types-new.ts +131 -0
  73. package/src/contracts/stableswap/types.ts +17 -0
  74. package/src/contracts/staking/helpers.ts +49 -34
  75. package/src/contracts/staking/scripts.ts +1 -1
  76. package/src/contracts/staking/transactions.ts +85 -130
  77. package/src/contracts/staking/types-new.ts +60 -28
  78. package/src/contracts/staking/types.ts +1 -28
  79. package/src/contracts/treasury/helpers.ts +21 -0
  80. package/src/contracts/treasury/scripts.ts +16 -26
  81. package/src/contracts/treasury/transactions.ts +256 -27
  82. package/src/contracts/treasury/types-new.ts +69 -0
  83. package/src/contracts/treasury/types.ts +2 -43
  84. package/src/contracts/version-registry/scripts.ts +2 -2
  85. package/src/contracts/version-registry/types-new.ts +6 -7
  86. package/src/index.ts +37 -20
  87. package/src/scripts/auth-token-policy.ts +3 -2
  88. package/src/scripts/iasset-policy.ts +3 -2
  89. package/src/types/evolution-schema-options.ts +3 -3
  90. package/src/types/generic.ts +17 -89
  91. package/src/types/multisig.ts +48 -0
  92. package/src/types/on-chain-decimal.ts +14 -7
  93. package/src/types/rational.ts +61 -0
  94. package/src/types/system-params.ts +237 -41
  95. package/src/utils/array-utils.ts +70 -1
  96. package/src/utils/bigint-utils.ts +12 -0
  97. package/src/utils/indigo-helpers.ts +8 -10
  98. package/src/utils/lucid-utils.ts +47 -40
  99. package/src/utils/oracle-helpers.ts +62 -0
  100. package/src/utils/pyth/decode.ts +223 -0
  101. package/src/utils/pyth/encode.ts +262 -0
  102. package/src/utils/pyth/index.ts +14 -0
  103. package/src/utils/pyth/types.ts +87 -0
  104. package/src/validators/always-succeed-validator.ts +6 -0
  105. package/src/validators/cdp-creator-validator.ts +2 -2
  106. package/src/validators/cdp-redeem-validator.ts +7 -0
  107. package/src/validators/cdp-validator.ts +2 -2
  108. package/src/validators/collector-validator.ts +2 -2
  109. package/src/validators/execute-validator.ts +2 -2
  110. package/src/validators/governance-validator.ts +2 -2
  111. package/src/validators/iasset-validator.ts +7 -0
  112. package/src/validators/interest-collection-validator.ts +7 -0
  113. package/src/validators/interest-oracle-validator.ts +2 -2
  114. package/src/validators/poll-manager-validator.ts +2 -2
  115. package/src/validators/poll-shard-validator.ts +2 -2
  116. package/src/validators/price-oracle-validator.ts +7 -0
  117. package/src/validators/pyth-feed-validator.ts +7 -0
  118. package/src/validators/rob-validator.ts +7 -0
  119. package/src/validators/stability-pool-validator.ts +2 -2
  120. package/src/validators/stableswap-validator.ts +7 -0
  121. package/src/validators/staking-validator.ts +2 -2
  122. package/src/validators/treasury-validator.ts +2 -2
  123. package/src/validators/version-record-policy.ts +2 -2
  124. package/src/validators/version-registry-validator.ts +2 -2
  125. package/tests/always-succeed/script.ts +7 -0
  126. package/tests/bigint-utils.test.ts +41 -0
  127. package/tests/cdp/actions.ts +610 -0
  128. package/tests/cdp/cdp-helpers.ts +55 -0
  129. package/tests/cdp/cdp-queries.ts +440 -0
  130. package/tests/cdp/cdp.test.ts +6087 -0
  131. package/tests/cdp/transactions-mutated.ts +1729 -0
  132. package/tests/data/system-params.json +177 -34
  133. package/tests/datums.test.ts +209 -210
  134. package/tests/endpoints/initialize.ts +68 -0
  135. package/tests/endpoints/interest-collector.ts +37 -0
  136. package/tests/endpoints/treasury.ts +70 -0
  137. package/tests/gov/actions.ts +406 -0
  138. package/tests/gov/gov.test.ts +4450 -0
  139. package/tests/{queries → gov}/governance-queries.ts +6 -3
  140. package/tests/hash-checks.test.ts +38 -11
  141. package/tests/indigo-test-helpers.ts +100 -0
  142. package/tests/initialize.test.ts +61 -9
  143. package/tests/interest-collection/interest-collection.test.ts +892 -0
  144. package/tests/interest-collection/interest-collector-queries.ts +49 -0
  145. package/tests/interest-collection/transactions-mutated.ts +260 -0
  146. package/tests/interest-oracle.test.ts +43 -35
  147. package/tests/mock/assets-mock.ts +234 -23
  148. package/tests/mock/protocol-params-mock.ts +21 -0
  149. package/tests/price-oracle/actions.ts +163 -0
  150. package/tests/price-oracle/price-oracle-queries.ts +12 -0
  151. package/tests/price-oracle/price-oracle.test.ts +240 -0
  152. package/tests/price-oracle/transactions-mutated.ts +62 -0
  153. package/tests/pyth/endpoints.ts +96 -0
  154. package/tests/pyth/helpers.ts +37 -0
  155. package/tests/pyth/pyth-encoding.test.ts +376 -0
  156. package/tests/pyth/pyth-indigo.test.ts +509 -0
  157. package/tests/pyth/pyth.test.ts +300 -0
  158. package/tests/queries/execute-queries.ts +6 -5
  159. package/tests/queries/iasset-queries.ts +175 -5
  160. package/tests/queries/interest-oracle-queries.ts +4 -2
  161. package/tests/queries/poll-queries.ts +8 -9
  162. package/tests/queries/stability-pool-queries.ts +95 -48
  163. package/tests/queries/staking-queries.ts +4 -2
  164. package/tests/queries/treasury-queries.ts +80 -5
  165. package/tests/rob/actions.ts +58 -0
  166. package/tests/{lrp-leverage.test.ts → rob/rob-leverage.test.ts} +393 -296
  167. package/tests/rob/rob-queries.ts +95 -0
  168. package/tests/rob/rob.test.ts +3762 -0
  169. package/tests/rob/transactions-mutated.ts +853 -0
  170. package/tests/script-size.test.ts +240 -0
  171. package/tests/setup.ts +135 -0
  172. package/tests/stability-pool/actions.ts +220 -0
  173. package/tests/stability-pool.test.ts +6121 -667
  174. package/tests/stableswap/stableswap-actions.ts +84 -0
  175. package/tests/stableswap/stableswap-queries.ts +89 -0
  176. package/tests/stableswap/stableswap.test.ts +3891 -0
  177. package/tests/stableswap/transactions-mutated.ts +348 -0
  178. package/tests/staking.test.ts +82 -99
  179. package/tests/test-helpers.ts +58 -11
  180. package/tests/treasury.test.ts +242 -0
  181. package/tests/utils/asserts.ts +74 -0
  182. package/tests/utils/benchmark-utils.ts +81 -0
  183. package/tests/utils/index.ts +122 -4
  184. package/tsconfig.json +9 -1
  185. package/vitest.config.ts +3 -1
  186. package/src/contracts/collector/types.ts +0 -16
  187. package/src/contracts/initialize/transactions.ts +0 -891
  188. package/src/contracts/leverage/helpers.ts +0 -424
  189. package/src/contracts/lrp/helpers.ts +0 -294
  190. package/src/contracts/lrp/scripts.ts +0 -27
  191. package/src/contracts/lrp/transactions.ts +0 -250
  192. package/src/contracts/lrp/types.ts +0 -131
  193. package/src/contracts/poll/types-poll.ts +0 -88
  194. package/src/contracts/vesting/helpers.ts +0 -218
  195. package/src/utils/value-helpers.ts +0 -37
  196. package/src/validators/lrp-validator.ts +0 -7
  197. package/tests/cdp.test.ts +0 -1528
  198. package/tests/gov.test.ts +0 -2011
  199. package/tests/lrp.test.ts +0 -673
  200. package/tests/queries/cdp-queries.ts +0 -220
  201. package/tests/queries/lrp-queries.ts +0 -76
  202. package/tests/queries/price-oracle-queries.ts +0 -10
@@ -4,21 +4,46 @@ import {
4
4
  LucidEvolution,
5
5
  TxBuilder,
6
6
  } from '@lucid-evolution/lucid';
7
+ import { AssetInfo } from '../src/contracts/initialize/types';
8
+ import { SystemParams } from '../src';
9
+ import { array as A, function as F, option as O } from 'fp-ts';
7
10
 
8
- export type LucidContext<T extends Record<string, EmulatorAccount>> = {
11
+ type EmulatorAccountMap = Record<string, EmulatorAccount> & {
12
+ admin: EmulatorAccount;
13
+ };
14
+
15
+ export type LucidContext<T extends EmulatorAccountMap = EmulatorAccountMap> = {
9
16
  lucid: LucidEvolution;
10
17
  users: T;
11
18
  emulator: Emulator;
12
19
  };
13
20
 
21
+ export type IndigoTestContext = LucidContext<{
22
+ admin: EmulatorAccount;
23
+ user: EmulatorAccount;
24
+ user2: EmulatorAccount;
25
+ withdrawalAccount: EmulatorAccount;
26
+ }> & {
27
+ systemParams: SystemParams;
28
+ assetConfigs: readonly AssetInfo[];
29
+ };
30
+
14
31
  export async function runAndAwaitTx(
15
32
  lucid: LucidEvolution,
16
33
  transaction: Promise<TxBuilder>,
34
+ extraSigners: string[] = [],
17
35
  ): Promise<string> {
18
- const txHash = await transaction
19
- .then((tx) => tx.complete())
20
- .then((tx) => tx.sign.withWallet().complete())
21
- .then((tx) => tx.submit());
36
+ const bTx = await (await transaction).complete();
37
+
38
+ const signatures = [await bTx.partialSign.withWallet()];
39
+ for (const signer of extraSigners) {
40
+ lucid.selectWallet.fromSeed(signer);
41
+ signatures.push(await lucid.fromTx(bTx.toCBOR()).partialSign.withWallet());
42
+ }
43
+
44
+ const signedTx = bTx.assemble(signatures);
45
+
46
+ const txHash = await signedTx.complete().then((tx) => tx.submit());
22
47
 
23
48
  await lucid.awaitTx(txHash);
24
49
  return txHash;
@@ -27,11 +52,19 @@ export async function runAndAwaitTx(
27
52
  export async function runAndAwaitTxBuilder(
28
53
  lucid: LucidEvolution,
29
54
  transaction: TxBuilder,
55
+ extraSigners: string[] = [],
30
56
  ): Promise<string> {
31
- const txHash = await transaction
32
- .complete()
33
- .then((tx) => tx.sign.withWallet().complete())
34
- .then((tx) => tx.submit());
57
+ const bTx = await transaction.complete();
58
+
59
+ const signatures = [await bTx.partialSign.withWallet()];
60
+ for (const signer of extraSigners) {
61
+ lucid.selectWallet.fromSeed(signer);
62
+ signatures.push(await lucid.fromTx(bTx.toCBOR()).partialSign.withWallet());
63
+ }
64
+
65
+ const signedTx = bTx.assemble(signatures);
66
+
67
+ const txHash = await signedTx.complete().then((tx) => tx.submit());
35
68
 
36
69
  await lucid.awaitTx(txHash);
37
70
  return txHash;
@@ -39,9 +72,23 @@ export async function runAndAwaitTxBuilder(
39
72
 
40
73
  export async function repeat(
41
74
  times: number,
42
- action: () => Promise<void>,
75
+ action: (isLast: boolean, idx: number) => Promise<void>,
43
76
  ): Promise<void> {
44
77
  for (let i = 0; i < times; i++) {
45
- await action();
78
+ await action(i === times - 1, i);
46
79
  }
47
80
  }
81
+
82
+ export function selectWalletByAddress(
83
+ context: LucidContext,
84
+ addr: string,
85
+ ): void {
86
+ const account = F.pipe(
87
+ Object.values(context.users),
88
+ A.findFirst((account) => account.address === addr),
89
+ O.getOrElse<EmulatorAccount>(() => {
90
+ throw new Error('No such account with the given address');
91
+ }),
92
+ );
93
+ context.lucid.selectWallet.fromSeed(account.seedPhrase);
94
+ }
@@ -0,0 +1,242 @@
1
+ import { addAssets, Assets, TxBuilder } from '@lucid-evolution/lucid';
2
+ import { assert, beforeEach, describe, test } from 'vitest';
3
+ import { IndigoTestContext } from './test-helpers';
4
+ import { treasuryCollect, treasuryMerge, treasurySplit } from '../src';
5
+ import {
6
+ createIndigoTestContext,
7
+ EXAMPLE_TOKEN_1,
8
+ EXAMPLE_TOKEN_2,
9
+ EXAMPLE_TOKEN_3,
10
+ } from './indigo-test-helpers';
11
+ import { benchmarkAndAwaitTx } from './utils/benchmark-utils';
12
+ import {
13
+ findAllTreasuryUtxos,
14
+ findRandomTreasuryUtxoWithOnlyAda,
15
+ } from './queries/treasury-queries';
16
+ import {
17
+ adaAssetClass,
18
+ mkLovelacesOf,
19
+ } from '@3rd-eye-labs/cardano-offchain-common';
20
+ import { mkAssetsOf } from '@3rd-eye-labs/cardano-offchain-common';
21
+ import { createUtxoAtTreasury } from './endpoints/treasury';
22
+ import { expectScriptFailure } from './utils/asserts';
23
+
24
+ // NOTE: Withdrawing and PrepareWithdrawal from Treasury is tested in gov.test.ts
25
+ describe('Treasury', () => {
26
+ beforeEach<IndigoTestContext>(async (context: IndigoTestContext) => {
27
+ await createIndigoTestContext(context);
28
+ });
29
+
30
+ async function treasuryMergeTx(
31
+ context: IndigoTestContext,
32
+ treasuryOutputs: Assets[],
33
+ ): Promise<TxBuilder> {
34
+ for (const output of treasuryOutputs) {
35
+ await createUtxoAtTreasury(output, context.systemParams, context);
36
+ }
37
+
38
+ const treasuryUtxos = await findAllTreasuryUtxos(
39
+ context.lucid,
40
+ context.systemParams,
41
+ );
42
+
43
+ // Simpler approach: for each expected output in treasuryOutputs, find one UTxO with exactly matching assets and take it
44
+ // Find UTxOs for each expected output, but remove it from the candidate list as we match
45
+ const availableUtxos = [...treasuryUtxos];
46
+ const matchedTreasuryUtxos = treasuryOutputs
47
+ .map((expectedAssets) => {
48
+ const idx = availableUtxos.findIndex((utxo) => {
49
+ const utxoAssets = utxo.assets;
50
+ const keys1 = Object.keys(utxoAssets).sort();
51
+ const keys2 = Object.keys(expectedAssets).sort();
52
+ if (keys1.length !== keys2.length) return false;
53
+ return keys1.every(
54
+ (k, i) => k === keys2[i] && utxoAssets[k] === expectedAssets[k],
55
+ );
56
+ });
57
+ if (idx !== -1) {
58
+ return availableUtxos.splice(idx, 1)[0];
59
+ } else {
60
+ return undefined;
61
+ }
62
+ })
63
+ .filter((utxo) => utxo !== undefined);
64
+
65
+ assert(
66
+ matchedTreasuryUtxos.length === treasuryOutputs.length,
67
+ 'Expected all treasury outputs to be matched',
68
+ );
69
+
70
+ return await treasuryMerge(
71
+ matchedTreasuryUtxos,
72
+ context.lucid,
73
+ context.systemParams,
74
+ );
75
+ }
76
+
77
+ async function treasurySplitTx(
78
+ context: IndigoTestContext,
79
+ treasuryOutput: Assets,
80
+ ): Promise<TxBuilder> {
81
+ await createUtxoAtTreasury(treasuryOutput, context.systemParams, context);
82
+
83
+ const treasuryUtxos = await findAllTreasuryUtxos(
84
+ context.lucid,
85
+ context.systemParams,
86
+ );
87
+
88
+ const matchedTreasuryUtxo = treasuryUtxos.find((utxo) =>
89
+ Object.keys(treasuryOutput).every(
90
+ (key) => utxo.assets[key] === treasuryOutput[key],
91
+ ),
92
+ );
93
+
94
+ assert(
95
+ matchedTreasuryUtxo !== undefined,
96
+ 'Expected a single treasury UTXO',
97
+ );
98
+
99
+ return await treasurySplit(
100
+ matchedTreasuryUtxo,
101
+ context.lucid,
102
+ context.systemParams,
103
+ );
104
+ }
105
+
106
+ test<IndigoTestContext>('Merge (3 lovelace UTxOs)', async (context: IndigoTestContext) => {
107
+ context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
108
+
109
+ await benchmarkAndAwaitTx(
110
+ 'Treasury - Merge (3 lovelace UTxOs)',
111
+ await treasuryMergeTx(context, [
112
+ mkLovelacesOf(50_000_000n),
113
+ mkLovelacesOf(50_000_000n),
114
+ mkLovelacesOf(50_000_000n),
115
+ ]),
116
+ context.lucid,
117
+ context.emulator,
118
+ );
119
+ });
120
+
121
+ test<IndigoTestContext>('Merge (3 INDY UTxOs)', async (context: IndigoTestContext) => {
122
+ context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
123
+
124
+ await benchmarkAndAwaitTx(
125
+ 'Treasury - Merge (3 INDY UTxOs)',
126
+ await treasuryMergeTx(context, [
127
+ addAssets(
128
+ mkLovelacesOf(5_000_000n),
129
+ mkAssetsOf(EXAMPLE_TOKEN_1, 1_000_000_000n),
130
+ ),
131
+ addAssets(
132
+ mkLovelacesOf(5_000_000n),
133
+ mkAssetsOf(EXAMPLE_TOKEN_1, 1_000_000_000n),
134
+ ),
135
+ addAssets(
136
+ mkLovelacesOf(5_000_000n),
137
+ mkAssetsOf(EXAMPLE_TOKEN_1, 1_000_000_000n),
138
+ ),
139
+ ]),
140
+ context.lucid,
141
+ context.emulator,
142
+ );
143
+ });
144
+
145
+ test<IndigoTestContext>('Merge (fail, 1 lovelace, 2 INDY UTxOs)', async (context: IndigoTestContext) => {
146
+ context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
147
+
148
+ await expectScriptFailure(
149
+ 'All inputs have to contain the same merging assets',
150
+ treasuryMergeTx(context, [
151
+ mkLovelacesOf(5_000_000n),
152
+ addAssets(
153
+ mkLovelacesOf(5_000_000n),
154
+ mkAssetsOf(EXAMPLE_TOKEN_1, 1_000_000_000n),
155
+ ),
156
+ addAssets(
157
+ mkLovelacesOf(5_000_000n),
158
+ mkAssetsOf(EXAMPLE_TOKEN_1, 1_000_000_000n),
159
+ ),
160
+ ]),
161
+ );
162
+ });
163
+
164
+ test<IndigoTestContext>('Split (3 assets)', async (context: IndigoTestContext) => {
165
+ context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
166
+
167
+ await benchmarkAndAwaitTx(
168
+ 'Treasury - Split (3 assets)',
169
+ await treasurySplitTx(
170
+ context,
171
+ addAssets(
172
+ mkLovelacesOf(5_000_000n),
173
+ mkAssetsOf(EXAMPLE_TOKEN_1, 1n),
174
+ mkAssetsOf(EXAMPLE_TOKEN_2, 1n),
175
+ mkAssetsOf(EXAMPLE_TOKEN_3, 1n),
176
+ ),
177
+ ),
178
+ context.lucid,
179
+ context.emulator,
180
+ );
181
+ });
182
+
183
+ test<IndigoTestContext>('Split (fail, lovelace only)', async (context: IndigoTestContext) => {
184
+ context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
185
+
186
+ await expectScriptFailure(
187
+ 'All inputs must have more than 1 non-ADA asset or 1 non-ADA asset and more than 2x ADA buffer',
188
+ treasurySplitTx(context, mkLovelacesOf(5_000_000n)),
189
+ );
190
+ });
191
+
192
+ test<IndigoTestContext>('Collect non-positive amount or negative extra lovelaces fails', async (context: IndigoTestContext) => {
193
+ context.lucid.selectWallet.fromSeed(context.users.user.seedPhrase);
194
+
195
+ const treasuryUTxO = await findRandomTreasuryUtxoWithOnlyAda(
196
+ context.lucid,
197
+ context.systemParams,
198
+ );
199
+
200
+ // This tests that the amount to collect cannot be negative.
201
+ await expectScriptFailure(
202
+ 'Collected amount is positive',
203
+ treasuryCollect(
204
+ adaAssetClass,
205
+ -1_000_000n,
206
+ 0n,
207
+ context.lucid,
208
+ context.systemParams,
209
+ treasuryUTxO,
210
+ treasuryUTxO,
211
+ ),
212
+ );
213
+
214
+ // This tests that the amount to collect cannot be zero.
215
+ await expectScriptFailure(
216
+ 'Collected amount is positive',
217
+ treasuryCollect(
218
+ adaAssetClass,
219
+ 0n,
220
+ 0n,
221
+ context.lucid,
222
+ context.systemParams,
223
+ treasuryUTxO,
224
+ treasuryUTxO,
225
+ ),
226
+ );
227
+
228
+ // This tests that the extra lovelaces cannot be negative.
229
+ await expectScriptFailure(
230
+ 'Collected amount is positive',
231
+ treasuryCollect(
232
+ adaAssetClass,
233
+ 1n,
234
+ -1_000_000n,
235
+ context.lucid,
236
+ context.systemParams,
237
+ treasuryUTxO,
238
+ treasuryUTxO,
239
+ ),
240
+ );
241
+ });
242
+ });
@@ -1,5 +1,42 @@
1
+ import {
2
+ isAssetsZero,
3
+ lovelacesAmt,
4
+ negateAssets,
5
+ noAdaValue,
6
+ } from '@3rd-eye-labs/cardano-offchain-common';
7
+ import { addAssets, Assets, TxBuilder } from '@lucid-evolution/lucid';
8
+ import { JSONStringify } from 'json-with-bigint';
9
+ import { match, P } from 'ts-pattern';
1
10
  import { assert, expect } from 'vitest';
2
11
 
12
+ export async function expectScriptFailure(
13
+ /**
14
+ * This doesn't have to be full message, can be just a part of it.
15
+ */
16
+ contains: string,
17
+ tx: Promise<TxBuilder>,
18
+ ): Promise<void> {
19
+ if (contains.length === 0) {
20
+ throw new Error('Expected error message has to be non empty.');
21
+ }
22
+
23
+ const result = await (await tx).completeSafe();
24
+
25
+ const errMsg = match(result)
26
+ .with({ _tag: 'Left', left: P.select() }, (smth) => smth.message)
27
+ .otherwise(() => null);
28
+
29
+ if (!errMsg) {
30
+ throw new Error(`Expected TX to fail, but it succeeded.`);
31
+ }
32
+
33
+ if (!errMsg.includes(contains)) {
34
+ throw new Error(
35
+ `Expected TX to fail with error containing: "${contains}". But got: "${errMsg}"`,
36
+ );
37
+ }
38
+ }
39
+
3
40
  export function assertValueInRange<T extends number | bigint>(
4
41
  val: T,
5
42
  bounds: { min: T; max: T },
@@ -11,3 +48,40 @@ export function assertValueInRange<T extends number | bigint>(
11
48
  `${val} not in range [${bounds.min}, ${bounds.max}]`,
12
49
  ).toBeTruthy();
13
50
  }
51
+
52
+ export function expectValue(
53
+ actual: Assets,
54
+ msg: string,
55
+ ): {
56
+ toEqual: (expected: Assets) => void;
57
+ /**
58
+ * The non ADA part of value has to equal and the lovelace has to be less than or equal than
59
+ * the expected `maxLovelace`.
60
+ */
61
+ toEqualNonAdaAndMaxAda: (constraints: {
62
+ expectedNonAda: Assets;
63
+ maxLovelace: bigint;
64
+ }) => void;
65
+ } {
66
+ return {
67
+ toEqual(expected) {
68
+ expect(
69
+ isAssetsZero(addAssets(actual, negateAssets(expected))),
70
+ `${msg}: Expected ${JSONStringify(actual)} to equal ${JSONStringify(expected)}`,
71
+ ).toBeTruthy();
72
+ },
73
+ toEqualNonAdaAndMaxAda({ expectedNonAda, maxLovelace }) {
74
+ expect(
75
+ isAssetsZero(
76
+ addAssets(noAdaValue(actual), negateAssets(expectedNonAda)),
77
+ ),
78
+ `${msg}: Expected no ADA part of ${JSONStringify(actual)} to equal ${JSONStringify(expectedNonAda)}`,
79
+ ).toBeTruthy();
80
+
81
+ expect(
82
+ lovelacesAmt(actual),
83
+ `${msg}: Lovelace part is more than expected`,
84
+ ).toBeLessThanOrEqual(maxLovelace);
85
+ },
86
+ };
87
+ }
@@ -0,0 +1,81 @@
1
+ import {
2
+ EvalRedeemer,
3
+ LucidEvolution,
4
+ Provider,
5
+ TxBuilder,
6
+ TxSignBuilder,
7
+ } from '@lucid-evolution/lucid';
8
+ import { array as A } from 'fp-ts';
9
+ import { BENCHMARK_RESULTS } from '../setup';
10
+ import { MAINNET_PROTOCOL_PARAMETERS } from '../indigo-test-helpers';
11
+
12
+ type ExUnits = { mem: number; steps: number; txSize: number };
13
+
14
+ export async function calculateTxExUnits(
15
+ provider: Provider,
16
+ tx: TxSignBuilder,
17
+ ): Promise<ExUnits> {
18
+ const cbor = tx.toCBOR();
19
+ const redeemersEval = await provider.evaluateTx(tx.toCBOR());
20
+
21
+ const res = A.reduce<EvalRedeemer, ExUnits>(
22
+ { mem: 0, steps: 0, txSize: 0 },
23
+ (acc, e) => {
24
+ return {
25
+ mem: acc.mem + e.ex_units.mem,
26
+ steps: acc.steps + e.ex_units.steps,
27
+ txSize: 0,
28
+ };
29
+ },
30
+ )(redeemersEval);
31
+
32
+ return { ...res, txSize: cbor.length / 2 };
33
+ }
34
+
35
+ export type BenchmarkResult = ExUnits & {
36
+ memPercentage: number;
37
+ stepsPercentage: number;
38
+ sizePercentage: number;
39
+ };
40
+
41
+ // This function is used to benchmark a transaction and await it's successful execution.
42
+ export async function benchmarkAndAwaitTx(
43
+ name: string,
44
+ tx: TxBuilder,
45
+ lucid: LucidEvolution,
46
+ provider: Provider,
47
+ extraSigners: string[] = [],
48
+ ): Promise<void> {
49
+ // Complete the transaction
50
+ const bTx = await tx.complete();
51
+ const signatures = [await bTx.partialSign.withWallet()];
52
+ for (const signer of extraSigners) {
53
+ lucid.selectWallet.fromSeed(signer);
54
+ signatures.push(await lucid.fromTx(bTx.toCBOR()).partialSign.withWallet());
55
+ }
56
+
57
+ const signedTx = bTx.assemble(signatures);
58
+
59
+ // Calculate execution units
60
+ const exUnits = await calculateTxExUnits(provider, signedTx);
61
+
62
+ // Get protocol parameters for percentages
63
+ const protocolParams = await provider.getProtocolParameters();
64
+
65
+ if (protocolParams.maxTxExMem !== MAINNET_PROTOCOL_PARAMETERS.maxTxExMem) {
66
+ throw new Error('Protocol parameters do not match Mainnet');
67
+ }
68
+
69
+ // Calculate percentages
70
+ const result = {
71
+ ...exUnits,
72
+ memPercentage: (exUnits.mem / Number(protocolParams.maxTxExMem)) * 100,
73
+ stepsPercentage:
74
+ (exUnits.steps / Number(protocolParams.maxTxExSteps)) * 100,
75
+ sizePercentage: (exUnits.txSize / Number(protocolParams.maxTxSize)) * 100,
76
+ };
77
+
78
+ BENCHMARK_RESULTS[name] = result;
79
+
80
+ await lucid.awaitTx(await (await signedTx.complete()).submit());
81
+ }
@@ -1,28 +1,100 @@
1
+ import {
2
+ negateAssets,
3
+ noAdaValue,
4
+ } from '@3rd-eye-labs/cardano-offchain-common';
1
5
  import {
2
6
  addAssets,
3
7
  Assets,
8
+ credentialToAddress,
9
+ getAddressDetails,
4
10
  LucidEvolution,
11
+ paymentCredentialOf,
5
12
  UTxO,
6
13
  } from '@lucid-evolution/lucid';
7
- import { array as A } from 'fp-ts';
8
- import { negateAssets } from '../../src/utils/value-helpers';
14
+ import { array as A, function as F } from 'fp-ts';
15
+ import { zipWith } from 'fp-ts/lib/Array';
16
+ import { runAndAwaitTxBuilder } from '../test-helpers';
9
17
 
18
+ /**
19
+ * In case passing in address with stake credential, the non stake credential address
20
+ * is used as well for collecting inputs but cannot increase its value.
21
+ */
10
22
  export async function getValueChangeAtAddressAfterAction<T>(
11
23
  lucid: LucidEvolution,
12
24
  address: string,
13
25
  action: () => Promise<T>,
14
26
  ): Promise<[T, Assets]> {
27
+ const hasStakeCred = getAddressDetails(address).stakeCredential != null;
28
+
15
29
  const valBefore = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
16
30
  addAssets(acc, utxo.assets),
17
31
  )(await lucid.utxosAt(address));
18
32
 
19
- const res = await action();
33
+ const [res, nonStakeCredValChange] = hasStakeCred
34
+ ? await getValueChangeAtAddressAfterAction(
35
+ lucid,
36
+ credentialToAddress(
37
+ lucid.config().network!,
38
+ paymentCredentialOf(address),
39
+ ),
40
+ action,
41
+ )
42
+ : [await action(), {}];
43
+
44
+ if (
45
+ hasStakeCred &&
46
+ !Object.entries(nonStakeCredValChange).every(([_, amt]) => amt <= 0n)
47
+ ) {
48
+ throw new Error("Can't add value to the address without stake credential");
49
+ }
20
50
 
21
51
  const valAfter = A.reduce<UTxO, Assets>({}, (acc, utxo) =>
22
52
  addAssets(acc, utxo.assets),
23
53
  )(await lucid.utxosAt(address));
24
54
 
25
- return [res, addAssets(valAfter, negateAssets(valBefore))];
55
+ return [
56
+ res,
57
+ addAssets(valAfter, negateAssets(valBefore), nonStakeCredValChange),
58
+ ];
59
+ }
60
+
61
+ export async function getValueChangeAtAddressesAfterAction<T>(
62
+ lucid: LucidEvolution,
63
+ addresses: string[],
64
+ action: () => Promise<T>,
65
+ ): Promise<[T, Assets[]]> {
66
+ const valsBefore = [];
67
+
68
+ for (const address of addresses) {
69
+ const utxos = await lucid.utxosAt(address);
70
+ valsBefore.push(
71
+ F.pipe(
72
+ utxos,
73
+ A.reduce<UTxO, Assets>({}, (acc, utxo) => addAssets(acc, utxo.assets)),
74
+ ),
75
+ );
76
+ }
77
+
78
+ const res = await action();
79
+
80
+ const valsAfter = [];
81
+
82
+ for (const address of addresses) {
83
+ const utxos = await lucid.utxosAt(address);
84
+ valsAfter.push(
85
+ F.pipe(
86
+ utxos,
87
+ A.reduce<UTxO, Assets>({}, (acc, utxo) => addAssets(acc, utxo.assets)),
88
+ ),
89
+ );
90
+ }
91
+
92
+ return [
93
+ res,
94
+ zipWith(valsBefore, valsAfter, (valBefore, valAfter) =>
95
+ addAssets(valAfter, negateAssets(valBefore)),
96
+ ),
97
+ ];
26
98
  }
27
99
 
28
100
  export async function getNewUtxosAtAddressAfterAction<T>(
@@ -48,3 +120,49 @@ export async function getNewUtxosAtAddressAfterAction<T>(
48
120
  ),
49
121
  ];
50
122
  }
123
+
124
+ export async function totalValueAtAddress(
125
+ lucid: LucidEvolution,
126
+ addr: string,
127
+ ): Promise<Assets> {
128
+ const utxos = await lucid.utxosAt(addr);
129
+
130
+ return F.pipe(
131
+ utxos,
132
+ A.reduce<UTxO, Assets>({}, (acc, utxo) => addAssets(acc, utxo.assets)),
133
+ );
134
+ }
135
+
136
+ export async function sendValueTo(
137
+ destinationAddr: string,
138
+ value: Assets,
139
+ lucid: LucidEvolution,
140
+ ): Promise<void> {
141
+ await runAndAwaitTxBuilder(
142
+ lucid,
143
+ lucid.newTx().pay.ToAddress(destinationAddr, value),
144
+ );
145
+ }
146
+
147
+ /**
148
+ * For all the non-ADA assets in current wallet, create a separate UTXo with each of them.
149
+ */
150
+ export async function fragmentWallet(lucid: LucidEvolution): Promise<void> {
151
+ const currentAddr = await lucid.wallet().address();
152
+
153
+ const totalVal = await totalValueAtAddress(lucid, currentAddr);
154
+
155
+ const otherAssets = Object.keys(noAdaValue(totalVal));
156
+
157
+ if (otherAssets.length === 0) return;
158
+
159
+ const tx = lucid.newTx();
160
+
161
+ for (const asset of otherAssets) {
162
+ tx.pay.ToAddress(currentAddr, {
163
+ [asset]: totalVal[asset],
164
+ });
165
+ }
166
+
167
+ await runAndAwaitTxBuilder(lucid, tx);
168
+ }
package/tsconfig.json CHANGED
@@ -7,7 +7,15 @@
7
7
  "moduleResolution": "nodenext",
8
8
  "allowUnreachableCode": true,
9
9
  "strict": true,
10
- "skipLibCheck": true
10
+ "skipLibCheck": true,
11
+ "allowUnusedLabels": false,
12
+ "exactOptionalPropertyTypes": true,
13
+ "noFallthroughCasesInSwitch": true,
14
+ "noImplicitOverride": true,
15
+ "noImplicitReturns": true,
16
+ "noPropertyAccessFromIndexSignature": true,
17
+ "noUnusedLocals": true,
18
+ "noUnusedParameters": true
11
19
  },
12
20
  "ts-node": {
13
21
  "compilerOptions": {
package/vitest.config.ts CHANGED
@@ -2,8 +2,10 @@ import { defineConfig } from 'vitest/config';
2
2
 
3
3
  export default defineConfig({
4
4
  test: {
5
- testTimeout: 120000, // 3 minutes
5
+ hookTimeout: 0,
6
+ testTimeout: 400_000,
6
7
  reporters: 'verbose',
7
8
  include: ['./tests/**/*.test.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
9
+ globalSetup: './tests/setup.ts',
8
10
  },
9
11
  });