@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,348 @@
1
+ import {
2
+ addAssets,
3
+ Data,
4
+ fromHex,
5
+ LucidEvolution,
6
+ OutRef,
7
+ sortUTxOs,
8
+ toHex,
9
+ TxBuilder,
10
+ UTxO,
11
+ } from '@lucid-evolution/lucid';
12
+ import {
13
+ parseStableswapOrderDatumOrThrow,
14
+ serialiseStableswapOrderRedeemer,
15
+ StableswapOrderDatum,
16
+ } from '../../src/contracts/stableswap/types-new';
17
+ import {
18
+ fromSystemParamsScriptRef,
19
+ SystemParams,
20
+ treasuryFeeTx,
21
+ } from '../../src';
22
+ import {
23
+ addressToBech32,
24
+ assetClassToUnit,
25
+ getInlineDatumOrThrow,
26
+ lovelacesAmt,
27
+ matchSingle,
28
+ mkAssetsOf,
29
+ mkLovelacesOf,
30
+ } from '@3rd-eye-labs/cardano-offchain-common';
31
+ import { isEmpty } from 'fp-ts/lib/Array';
32
+ import { array as A, function as F } from 'fp-ts';
33
+ import { calculateFeeFromRatio } from '../../src/utils/indigo-helpers';
34
+ import {
35
+ parseStableswapPoolDatumOrThrow,
36
+ serialiseCdpRedeemer,
37
+ serialiseStableswapPoolDatum,
38
+ } from '../../src/contracts/cdp/types-new';
39
+ import { createDestinationDatum } from '../../src/contracts/stableswap/helpers';
40
+ import {
41
+ rationalDiv,
42
+ rationalFloor,
43
+ rationalFromInt,
44
+ rationalMul,
45
+ } from '../../src/types/rational';
46
+
47
+ type StableswapOrderInfo = {
48
+ utxo: UTxO;
49
+ datum: StableswapOrderDatum;
50
+ suppliedIasset: bigint;
51
+ suppliedCollateralAsset: bigint;
52
+ };
53
+ export type MutatedBatchProcessStableswapOrdersType = {
54
+ type: 'exceed-max-execution-fee';
55
+ maxExecutionFee: bigint;
56
+ };
57
+ export async function mutatedBatchProcessStableswapOrders(
58
+ stableswapOrderOrefs: OutRef[],
59
+ stableswapPoolOref: OutRef,
60
+ treasuryOref: OutRef,
61
+ sysParams: SystemParams,
62
+ lucid: LucidEvolution,
63
+ type: MutatedBatchProcessStableswapOrdersType,
64
+ ): Promise<TxBuilder> {
65
+ const stableswapScriptRefUtxo = matchSingle(
66
+ await lucid.utxosByOutRef([
67
+ fromSystemParamsScriptRef(
68
+ sysParams.scriptReferences.stableswapValidatorRef,
69
+ ),
70
+ ]),
71
+ (_) => new Error('Expected a single Stableswap Ref Script UTXO'),
72
+ );
73
+
74
+ const cdpScriptRefUtxo = matchSingle(
75
+ await lucid.utxosByOutRef([
76
+ fromSystemParamsScriptRef(sysParams.scriptReferences.cdpValidatorRef),
77
+ ]),
78
+ (_) => new Error('Expected a single CDP Ref Script UTXO'),
79
+ );
80
+
81
+ const iAssetTokenPolicyRefScriptUtxo = matchSingle(
82
+ await lucid.utxosByOutRef([
83
+ fromSystemParamsScriptRef(
84
+ sysParams.scriptReferences.iAssetTokenPolicyRef,
85
+ ),
86
+ ]),
87
+ (_) => new Error('Expected a single iasset token policy Ref Script UTXO'),
88
+ );
89
+
90
+ if (isEmpty(stableswapOrderOrefs)) {
91
+ throw new Error('At least one order must be provided.');
92
+ }
93
+
94
+ const stableswapOrderUtxos = await lucid.utxosByOutRef(stableswapOrderOrefs);
95
+
96
+ const sortedStableswapOrderUtxos = sortUTxOs(
97
+ stableswapOrderUtxos,
98
+ 'Canonical',
99
+ );
100
+
101
+ if (sortedStableswapOrderUtxos.length !== stableswapOrderOrefs.length) {
102
+ throw new Error('Expected certain number of orders');
103
+ }
104
+
105
+ const mainOrderUtxo = sortedStableswapOrderUtxos[0];
106
+
107
+ const mainOrderDatum = parseStableswapOrderDatumOrThrow(
108
+ getInlineDatumOrThrow(mainOrderUtxo),
109
+ );
110
+
111
+ const iassetAc = {
112
+ currencySymbol: fromHex(
113
+ sysParams.stableswapParams.iassetSymbol.unCurrencySymbol,
114
+ ),
115
+ tokenName: mainOrderDatum.iasset,
116
+ };
117
+
118
+ const collateralAc = mainOrderDatum.collateralAsset;
119
+
120
+ const ordersInfo: StableswapOrderInfo[] = sortedStableswapOrderUtxos.map(
121
+ (orderUtxo) => {
122
+ const orderDatum = parseStableswapOrderDatumOrThrow(
123
+ getInlineDatumOrThrow(orderUtxo),
124
+ );
125
+
126
+ if (
127
+ toHex(orderDatum.iasset) != toHex(mainOrderDatum.iasset) ||
128
+ toHex(orderDatum.collateralAsset.currencySymbol) !=
129
+ toHex(mainOrderDatum.collateralAsset.currencySymbol) ||
130
+ toHex(orderDatum.collateralAsset.tokenName) !=
131
+ toHex(mainOrderDatum.collateralAsset.tokenName)
132
+ ) {
133
+ throw new Error('Wrong batch of orders');
134
+ }
135
+
136
+ const suppliedIasset = orderUtxo.assets[assetClassToUnit(iassetAc)] ?? 0n;
137
+ const suppliedCollateralAsset =
138
+ orderUtxo.assets[assetClassToUnit(collateralAc)] ?? 0n;
139
+
140
+ if (
141
+ (suppliedIasset != 0n && suppliedCollateralAsset != 0n) ||
142
+ (suppliedIasset == 0n && suppliedCollateralAsset == 0n)
143
+ ) {
144
+ throw new Error(
145
+ 'An order must supply either iAsset or collateral asset',
146
+ );
147
+ }
148
+
149
+ return {
150
+ utxo: orderUtxo,
151
+ datum: orderDatum,
152
+ suppliedIasset: suppliedIasset,
153
+ suppliedCollateralAsset: suppliedCollateralAsset,
154
+ };
155
+ },
156
+ );
157
+
158
+ const totalSuppliedAssets = F.pipe(
159
+ ordersInfo,
160
+ A.reduce<StableswapOrderInfo, [bigint, bigint]>(
161
+ [0n, 0n],
162
+ (acc, orderInfo) => [
163
+ acc[0] + orderInfo.suppliedIasset,
164
+ acc[1] + orderInfo.suppliedCollateralAsset,
165
+ ],
166
+ ),
167
+ );
168
+
169
+ const totalSuppliedIasset = totalSuppliedAssets[0];
170
+
171
+ const totalSuppliedCollateralAsset = totalSuppliedAssets[1];
172
+
173
+ const stableswapPoolUtxo = matchSingle(
174
+ await lucid.utxosByOutRef([stableswapPoolOref]),
175
+ (_) => new Error('Expected a single cdp UTXO'),
176
+ );
177
+
178
+ const stableswapPoolDatum = parseStableswapPoolDatumOrThrow(
179
+ getInlineDatumOrThrow(stableswapPoolUtxo),
180
+ );
181
+
182
+ const redemptionFee = calculateFeeFromRatio(
183
+ stableswapPoolDatum.redemptionFeeRatio,
184
+ totalSuppliedIasset,
185
+ );
186
+
187
+ const totalEffectiveSuppliedIasset = totalSuppliedIasset - redemptionFee;
188
+
189
+ const collateralAmtChangePool =
190
+ totalSuppliedCollateralAsset -
191
+ rationalFloor(
192
+ rationalMul(
193
+ rationalFromInt(totalEffectiveSuppliedIasset),
194
+ stableswapPoolDatum.collateralToIassetRatio,
195
+ ),
196
+ );
197
+
198
+ const amountToMint =
199
+ rationalFloor(
200
+ rationalDiv(
201
+ rationalFromInt(totalSuppliedCollateralAsset),
202
+ stableswapPoolDatum.collateralToIassetRatio,
203
+ ),
204
+ ) - totalEffectiveSuppliedIasset;
205
+
206
+ const mintingFee = calculateFeeFromRatio(
207
+ stableswapPoolDatum.mintingFeeRatio,
208
+ amountToMint,
209
+ );
210
+
211
+ const tx = lucid
212
+ .newTx()
213
+ .readFrom([
214
+ stableswapScriptRefUtxo,
215
+ cdpScriptRefUtxo,
216
+ iAssetTokenPolicyRefScriptUtxo,
217
+ ])
218
+ .collectFrom([stableswapPoolUtxo], {
219
+ kind: 'selected',
220
+ makeRedeemer: (inputIndices) =>
221
+ serialiseCdpRedeemer({
222
+ Stableswap: {
223
+ forwardingInputIndex: inputIndices[0],
224
+ },
225
+ }),
226
+ inputs: [mainOrderUtxo],
227
+ })
228
+ .pay.ToContract(
229
+ stableswapPoolUtxo.address,
230
+ {
231
+ kind: 'inline',
232
+ value: serialiseStableswapPoolDatum(stableswapPoolDatum),
233
+ },
234
+ collateralAmtChangePool != 0n
235
+ ? addAssets(
236
+ stableswapPoolUtxo.assets,
237
+ mkAssetsOf(collateralAc, collateralAmtChangePool),
238
+ )
239
+ : stableswapPoolUtxo.assets,
240
+ );
241
+
242
+ const fee = mintingFee + redemptionFee;
243
+
244
+ if (fee > 0) {
245
+ await treasuryFeeTx(
246
+ iassetAc,
247
+ fee,
248
+ 0n,
249
+ lucid,
250
+ sysParams,
251
+ tx,
252
+ stableswapPoolOref,
253
+ treasuryOref,
254
+ );
255
+ }
256
+
257
+ if (amountToMint != 0n) {
258
+ tx.mintAssets(mkAssetsOf(iassetAc, amountToMint), Data.void());
259
+ }
260
+
261
+ F.pipe(
262
+ ordersInfo,
263
+ A.reduce<StableswapOrderInfo, TxBuilder>(tx, (acc, orderInfo) => {
264
+ const iassetSupplied =
265
+ orderInfo.utxo.assets[assetClassToUnit(iassetAc)] ?? 0n;
266
+
267
+ const collateralAssetSupplied =
268
+ orderInfo.utxo.assets[assetClassToUnit(collateralAc)] ?? 0n;
269
+
270
+ const isMinting = iassetSupplied > 0n ? false : true;
271
+
272
+ const iAssetEquivalentSupplied = isMinting
273
+ ? rationalFloor(
274
+ rationalDiv(
275
+ rationalFromInt(collateralAssetSupplied),
276
+ stableswapPoolDatum.collateralToIassetRatio,
277
+ ),
278
+ )
279
+ : iassetSupplied;
280
+
281
+ const effectiveiAssetEquivalentSupplied = isMinting
282
+ ? iAssetEquivalentSupplied -
283
+ calculateFeeFromRatio(
284
+ stableswapPoolDatum.mintingFeeRatio,
285
+ iAssetEquivalentSupplied,
286
+ )
287
+ : iAssetEquivalentSupplied -
288
+ calculateFeeFromRatio(
289
+ stableswapPoolDatum.redemptionFeeRatio,
290
+ iAssetEquivalentSupplied,
291
+ );
292
+
293
+ const amountToReceive = isMinting
294
+ ? effectiveiAssetEquivalentSupplied
295
+ : rationalFloor(
296
+ rationalMul(
297
+ rationalFromInt(effectiveiAssetEquivalentSupplied),
298
+ stableswapPoolDatum.collateralToIassetRatio,
299
+ ),
300
+ );
301
+
302
+ const maxExecutionFee =
303
+ type.type === 'exceed-max-execution-fee'
304
+ ? type.maxExecutionFee
305
+ : orderInfo.datum.maxExecutionFee;
306
+
307
+ return acc
308
+ .collectFrom(
309
+ [orderInfo.utxo],
310
+ orderInfo.utxo == mainOrderUtxo
311
+ ? serialiseStableswapOrderRedeemer('BatchProcessStableswapOrders')
312
+ : {
313
+ kind: 'selected',
314
+ makeRedeemer: (inputIndices: bigint[]) => {
315
+ return serialiseStableswapOrderRedeemer({
316
+ BatchAuxiliary: {
317
+ ownInputIndex: inputIndices[0],
318
+ mainOrderInputIndex: inputIndices[1],
319
+ },
320
+ });
321
+ },
322
+ inputs: [orderInfo.utxo, mainOrderUtxo],
323
+ },
324
+ )
325
+ .pay.ToAddressWithData(
326
+ addressToBech32(orderInfo.datum.destination, lucid.config().network!),
327
+ {
328
+ kind: 'inline',
329
+ value: createDestinationDatum(
330
+ orderInfo.datum.destinationInlineDatum ?? null,
331
+ orderInfo.utxo,
332
+ ),
333
+ },
334
+ addAssets(
335
+ // Currently, we always take the max execution fee from the order utxo.
336
+ // This can be improved so that we take the actual execution fee.
337
+ mkLovelacesOf(
338
+ lovelacesAmt(orderInfo.utxo.assets) - maxExecutionFee,
339
+ ),
340
+ isMinting
341
+ ? mkAssetsOf(iassetAc, amountToReceive)
342
+ : mkAssetsOf(collateralAc, amountToReceive),
343
+ ),
344
+ );
345
+ }),
346
+ );
347
+ return tx;
348
+ }
@@ -1,73 +1,55 @@
1
1
  import { beforeEach, expect, test } from 'vitest';
2
- import { LucidContext, runAndAwaitTx } from './test-helpers';
3
- import { EmulatorAccount, Lucid } from '@lucid-evolution/lucid';
4
- import { Emulator } from '@lucid-evolution/lucid';
5
- import { generateEmulatorAccount } from '@lucid-evolution/lucid';
6
- import { init } from '../src/contracts/initialize/transactions';
2
+ import { IndigoTestContext, runAndAwaitTx } from './test-helpers';
7
3
  import { addrDetails } from '../src/utils/lucid-utils';
8
4
  import { findStakingPosition } from './queries/staking-queries';
9
- import { iusdInitialAssetCfg } from './mock/assets-mock';
10
5
  import {
11
6
  adjustStakingPosition,
12
7
  closeStakingPosition,
13
8
  distributeAda,
14
9
  openStakingPosition,
15
10
  } from '../src/contracts/staking/transactions';
16
- import { collectorFeeTx, fromSystemParamsAsset } from '../src';
11
+ import {
12
+ collectorFeeTx,
13
+ findStakingManager,
14
+ fromSystemParamsAsset,
15
+ } from '../src';
16
+ import { createIndigoTestContext } from './indigo-test-helpers';
17
+ import { benchmarkAndAwaitTx } from './utils/benchmark-utils';
17
18
  import {
18
19
  findAllCollectors,
19
20
  findRandomCollector,
20
21
  } from './queries/collector-queries';
21
- import { findStakingManager } from '../src/contracts/staking/helpers';
22
- import { getValueChangeAtAddressAfterAction } from './utils';
23
- import { lovelacesAmt } from '@3rd-eye-labs/cardano-offchain-common';
24
-
25
- type MyContext = LucidContext<{
26
- admin: EmulatorAccount;
27
- }>;
28
-
29
- beforeEach<MyContext>(async (context: MyContext) => {
30
- context.users = {
31
- admin: generateEmulatorAccount({
32
- lovelace: BigInt(100_000_000_000_000),
33
- }),
34
- };
35
-
36
- context.emulator = new Emulator([context.users.admin]);
22
+ import { getValueChangeAtAddressAfterAction, sendValueTo } from './utils';
23
+ import {
24
+ lovelacesAmt,
25
+ mkAssetsOf,
26
+ } from '@3rd-eye-labs/cardano-offchain-common';
37
27
 
38
- context.lucid = await Lucid(context.emulator, 'Custom');
28
+ beforeEach<IndigoTestContext>(async (context: IndigoTestContext) => {
29
+ await createIndigoTestContext(context);
39
30
  });
40
31
 
41
- test<MyContext>('Staking - Create Position', async ({
32
+ test<IndigoTestContext>('Staking - Create Position', async ({
42
33
  lucid,
43
- users,
44
- }: MyContext) => {
45
- lucid.selectWallet.fromSeed(users.admin.seedPhrase);
46
- const [systemParams, _] = await init(lucid, [iusdInitialAssetCfg]);
47
- const stakingManager = await findStakingManager(systemParams, lucid);
48
-
49
- await runAndAwaitTx(
34
+ emulator,
35
+ systemParams,
36
+ }: IndigoTestContext) => {
37
+ await benchmarkAndAwaitTx(
38
+ 'Staking - Create Position',
39
+ await openStakingPosition(1_000_000n, systemParams, lucid),
50
40
  lucid,
51
- openStakingPosition(1_000_000n, systemParams, lucid, stakingManager.utxo),
41
+ emulator,
52
42
  );
53
43
  });
54
44
 
55
- test<MyContext>('Staking - Adjust Position', async ({
45
+ test<IndigoTestContext>('Staking - Adjust Position', async ({
56
46
  lucid,
57
- users,
58
47
  emulator,
59
- }: MyContext) => {
60
- lucid.selectWallet.fromSeed(users.admin.seedPhrase);
61
- const [systemParams, _] = await init(lucid, [iusdInitialAssetCfg]);
62
-
48
+ systemParams,
49
+ }: IndigoTestContext) => {
63
50
  await runAndAwaitTx(
64
51
  lucid,
65
- openStakingPosition(
66
- 1_000_000n,
67
- systemParams,
68
- lucid,
69
- (await findStakingManager(systemParams, lucid)).utxo,
70
- ),
52
+ openStakingPosition(1_000_000n, systemParams, lucid),
71
53
  );
72
54
 
73
55
  const [pkh, __] = await addrDetails(lucid);
@@ -78,73 +60,73 @@ test<MyContext>('Staking - Adjust Position', async ({
78
60
  pkh.hash,
79
61
  );
80
62
 
81
- await runAndAwaitTx(
82
- lucid,
83
- adjustStakingPosition(
63
+ await benchmarkAndAwaitTx(
64
+ 'Staking - Adjust Position',
65
+ await adjustStakingPosition(
84
66
  myStakingPosition.utxo,
85
67
  1_000_000n,
86
68
  systemParams,
87
69
  lucid,
88
70
  emulator.slot,
89
- (await findStakingManager(systemParams, lucid)).utxo,
90
71
  ),
72
+ lucid,
73
+ emulator,
91
74
  );
92
75
  });
93
76
 
94
- test<MyContext>('Staking - Close Position', async ({
95
- lucid,
96
- users,
97
- emulator,
98
- }: MyContext) => {
99
- lucid.selectWallet.fromSeed(users.admin.seedPhrase);
100
- const [systemParams, _] = await init(lucid, [iusdInitialAssetCfg]);
101
-
102
- await runAndAwaitTx(
103
- lucid,
104
- openStakingPosition(
105
- 1_000_000n,
106
- systemParams,
77
+ // TODO: fails with the Lucid Evolution error: `Error: Not enough ADA leftover to include non-ADA assets in a change address`
78
+ test.todo<IndigoTestContext>(
79
+ 'Staking - Close Position',
80
+ async ({ lucid, emulator, systemParams, users }: IndigoTestContext) => {
81
+ lucid.selectWallet.fromSeed(users.admin.seedPhrase);
82
+
83
+ await sendValueTo(
84
+ users.user2.address,
85
+ mkAssetsOf(
86
+ fromSystemParamsAsset(systemParams.govParams.indyAsset),
87
+ 1_000_000n,
88
+ ),
107
89
  lucid,
108
- (await findStakingManager(systemParams, lucid)).utxo,
109
- ),
110
- );
90
+ );
111
91
 
112
- const [pkh, __] = await addrDetails(lucid);
113
- const myStakingPosition = await findStakingPosition(
114
- lucid,
115
- systemParams.validatorHashes.stakingHash,
116
- fromSystemParamsAsset(systemParams.stakingParams.stakingToken),
117
- pkh.hash,
118
- );
92
+ lucid.selectWallet.fromSeed(users.user2.seedPhrase);
119
93
 
120
- await runAndAwaitTx(
121
- lucid,
122
- closeStakingPosition(
123
- myStakingPosition.utxo,
124
- systemParams,
94
+ await runAndAwaitTx(
125
95
  lucid,
126
- emulator.slot,
127
- (await findStakingManager(systemParams, lucid)).utxo,
128
- ),
129
- );
130
- });
96
+ openStakingPosition(1_000_000n, systemParams, lucid),
97
+ );
98
+
99
+ const [pkh, __] = await addrDetails(lucid);
100
+ const myStakingPosition = await findStakingPosition(
101
+ lucid,
102
+ systemParams.validatorHashes.stakingHash,
103
+ fromSystemParamsAsset(systemParams.stakingParams.stakingToken),
104
+ pkh.hash,
105
+ );
106
+
107
+ await benchmarkAndAwaitTx(
108
+ 'Staking - Close Position',
109
+ await closeStakingPosition(
110
+ myStakingPosition.utxo,
111
+ systemParams,
112
+ lucid,
113
+ emulator.slot,
114
+ ),
115
+ lucid,
116
+ emulator,
117
+ );
118
+ },
119
+ );
131
120
 
132
- test<MyContext>('Staking - Distribute ADA to Stakers', async ({
121
+ test<IndigoTestContext>('Staking - Distribute ADA to Stakers', async ({
133
122
  lucid,
134
123
  users,
135
124
  emulator,
136
- }: MyContext) => {
137
- lucid.selectWallet.fromSeed(users.admin.seedPhrase);
138
- const [systemParams, _] = await init(lucid, [iusdInitialAssetCfg]);
139
-
125
+ systemParams,
126
+ }: IndigoTestContext) => {
140
127
  await runAndAwaitTx(
141
128
  lucid,
142
- openStakingPosition(
143
- 1_000_000n,
144
- systemParams,
145
- lucid,
146
- (await findStakingManager(systemParams, lucid)).utxo,
147
- ),
129
+ openStakingPosition(1_000_000n, systemParams, lucid),
148
130
  );
149
131
 
150
132
  const collectorOref = await findRandomCollector(
@@ -157,19 +139,21 @@ test<MyContext>('Staking - Distribute ADA to Stakers', async ({
157
139
 
158
140
  const collectorUtxo = (
159
141
  await findAllCollectors(lucid, systemParams.validatorHashes.collectorHash)
160
- ).find((utxo) => utxo.assets.lovelace > 100_000_000n);
142
+ ).find((utxo) => utxo.assets['lovelace'] > 100_000_000n);
161
143
  if (!collectorUtxo) {
162
144
  throw new Error('Expected a collector UTXO');
163
145
  }
164
146
 
165
- await runAndAwaitTx(
166
- lucid,
167
- distributeAda(
147
+ await benchmarkAndAwaitTx(
148
+ 'Staking - Distribute ADA to Stakers',
149
+ await distributeAda(
168
150
  (await findStakingManager(systemParams, lucid)).utxo,
169
151
  [collectorUtxo],
170
152
  systemParams,
171
153
  lucid,
172
154
  ),
155
+ lucid,
156
+ emulator,
173
157
  );
174
158
 
175
159
  const [pkh, __] = await addrDetails(lucid);
@@ -183,7 +167,7 @@ test<MyContext>('Staking - Distribute ADA to Stakers', async ({
183
167
  const [____, userValChange] = await getValueChangeAtAddressAfterAction(
184
168
  lucid,
185
169
  users.admin.address,
186
- async () =>
170
+ () =>
187
171
  runAndAwaitTx(
188
172
  lucid,
189
173
  closeStakingPosition(
@@ -191,7 +175,6 @@ test<MyContext>('Staking - Distribute ADA to Stakers', async ({
191
175
  systemParams,
192
176
  lucid,
193
177
  emulator.slot,
194
- (await findStakingManager(systemParams, lucid)).utxo,
195
178
  ),
196
179
  ),
197
180
  );