@obolnetwork/obol-sdk 2.11.3 → 2.11.5-rc.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 (143) hide show
  1. package/dist/cjs/src/index.js +17457 -382
  2. package/dist/cjs/src/index.js.map +1 -0
  3. package/dist/esm/src/index.js +17424 -361
  4. package/dist/esm/src/index.js.map +1 -0
  5. package/dist/types/src/abi/OVM.d.ts +103 -0
  6. package/dist/types/src/bytecodes.d.ts +3 -2
  7. package/dist/types/src/constants.d.ts +1 -2
  8. package/dist/types/src/eoa/eoaHelpers.d.ts +1 -1
  9. package/dist/types/src/exits/verificationHelpers.d.ts +0 -2
  10. package/dist/types/src/incentives/incentiveHelpers.d.ts +1 -1
  11. package/dist/types/src/splits/splitHelpers.d.ts +3 -3
  12. package/dist/types/src/utils.d.ts +2 -2
  13. package/dist/types/src/verification/common.d.ts +0 -2
  14. package/dist/types/src/verification/signature-validator.d.ts +4 -4
  15. package/dist/types/src/verification/v1.10.0.d.ts +1 -1
  16. package/dist/types/src/verification/v1.6.0.d.ts +1 -1
  17. package/dist/types/src/verification/v1.7.0.d.ts +1 -1
  18. package/dist/types/src/verification/v1.8.0.d.ts +1 -1
  19. package/dist/types/test/client/methods.spec.d.ts +17 -0
  20. package/dist/types/tsconfig.types.tsbuildinfo +1 -0
  21. package/dist/types/tsup.config.d.ts +2 -0
  22. package/package.json +79 -54
  23. package/dist/cjs/package.json +0 -139
  24. package/dist/cjs/src/abi/BatchDeposit.js +0 -120
  25. package/dist/cjs/src/abi/MerkleDistributorWithDeadline.js +0 -73
  26. package/dist/cjs/src/abi/Multicall3.js +0 -253
  27. package/dist/cjs/src/abi/OVMFactory.js +0 -620
  28. package/dist/cjs/src/abi/OWR.js +0 -304
  29. package/dist/cjs/src/abi/SplitMain.js +0 -929
  30. package/dist/cjs/src/abi/splitV2FactoryAbi.js +0 -420
  31. package/dist/cjs/src/ajv.js +0 -136
  32. package/dist/cjs/src/base.js +0 -42
  33. package/dist/cjs/src/bytecodes.js +0 -18
  34. package/dist/cjs/src/constants.js +0 -268
  35. package/dist/cjs/src/eoa/eoa.js +0 -132
  36. package/dist/cjs/src/eoa/eoaHelpers.js +0 -82
  37. package/dist/cjs/src/errors.js +0 -11
  38. package/dist/cjs/src/exits/ethUtils.js +0 -57
  39. package/dist/cjs/src/exits/exit.js +0 -502
  40. package/dist/cjs/src/exits/verificationHelpers.js +0 -86
  41. package/dist/cjs/src/incentives/incentiveHelpers.js +0 -39
  42. package/dist/cjs/src/incentives/incentives.js +0 -119
  43. package/dist/cjs/src/schema.js +0 -352
  44. package/dist/cjs/src/services.js +0 -33
  45. package/dist/cjs/src/splits/splitHelpers.js +0 -571
  46. package/dist/cjs/src/splits/splits.js +0 -366
  47. package/dist/cjs/src/types.js +0 -29
  48. package/dist/cjs/src/utils.js +0 -77
  49. package/dist/cjs/src/verification/common.js +0 -355
  50. package/dist/cjs/src/verification/signature-validator.js +0 -73
  51. package/dist/cjs/src/verification/sszTypes.js +0 -69
  52. package/dist/cjs/src/verification/termsAndConditions.js +0 -40
  53. package/dist/cjs/src/verification/v1.10.0.js +0 -146
  54. package/dist/cjs/src/verification/v1.6.0.js +0 -149
  55. package/dist/cjs/src/verification/v1.7.0.js +0 -173
  56. package/dist/cjs/src/verification/v1.8.0.js +0 -196
  57. package/dist/cjs/test/client/ajv.spec.js +0 -320
  58. package/dist/cjs/test/client/methods.spec.js +0 -418
  59. package/dist/cjs/test/eoa/eoa.spec.js +0 -176
  60. package/dist/cjs/test/exit/ethUtils.spec.js +0 -83
  61. package/dist/cjs/test/exit/exit.spec.js +0 -399
  62. package/dist/cjs/test/exit/verificationHelpers.spec.js +0 -68
  63. package/dist/cjs/test/fixtures.js +0 -896
  64. package/dist/cjs/test/incentives/incentives.spec.js +0 -202
  65. package/dist/cjs/test/splits/splits.spec.js +0 -402
  66. package/dist/esm/package.json +0 -139
  67. package/dist/esm/src/abi/BatchDeposit.js +0 -117
  68. package/dist/esm/src/abi/MerkleDistributorWithDeadline.js +0 -70
  69. package/dist/esm/src/abi/Multicall3.js +0 -250
  70. package/dist/esm/src/abi/OVMFactory.js +0 -617
  71. package/dist/esm/src/abi/OWR.js +0 -301
  72. package/dist/esm/src/abi/SplitMain.js +0 -926
  73. package/dist/esm/src/abi/splitV2FactoryAbi.js +0 -417
  74. package/dist/esm/src/ajv.js +0 -129
  75. package/dist/esm/src/base.js +0 -38
  76. package/dist/esm/src/bytecodes.js +0 -15
  77. package/dist/esm/src/constants.js +0 -234
  78. package/dist/esm/src/eoa/eoa.js +0 -128
  79. package/dist/esm/src/eoa/eoaHelpers.js +0 -77
  80. package/dist/esm/src/errors.js +0 -7
  81. package/dist/esm/src/exits/ethUtils.js +0 -52
  82. package/dist/esm/src/exits/exit.js +0 -475
  83. package/dist/esm/src/exits/verificationHelpers.js +0 -81
  84. package/dist/esm/src/incentives/incentiveHelpers.js +0 -34
  85. package/dist/esm/src/incentives/incentives.js +0 -115
  86. package/dist/esm/src/schema.js +0 -349
  87. package/dist/esm/src/services.js +0 -29
  88. package/dist/esm/src/splits/splitHelpers.js +0 -554
  89. package/dist/esm/src/splits/splits.js +0 -362
  90. package/dist/esm/src/types.js +0 -26
  91. package/dist/esm/src/utils.js +0 -68
  92. package/dist/esm/src/verification/common.js +0 -319
  93. package/dist/esm/src/verification/signature-validator.js +0 -64
  94. package/dist/esm/src/verification/sszTypes.js +0 -64
  95. package/dist/esm/src/verification/termsAndConditions.js +0 -33
  96. package/dist/esm/src/verification/v1.10.0.js +0 -140
  97. package/dist/esm/src/verification/v1.6.0.js +0 -142
  98. package/dist/esm/src/verification/v1.7.0.js +0 -166
  99. package/dist/esm/src/verification/v1.8.0.js +0 -189
  100. package/dist/esm/test/client/ajv.spec.js +0 -318
  101. package/dist/esm/test/client/methods.spec.js +0 -393
  102. package/dist/esm/test/eoa/eoa.spec.js +0 -174
  103. package/dist/esm/test/exit/ethUtils.spec.js +0 -81
  104. package/dist/esm/test/exit/exit.spec.js +0 -374
  105. package/dist/esm/test/exit/verificationHelpers.spec.js +0 -66
  106. package/dist/esm/test/fixtures.js +0 -893
  107. package/dist/esm/test/incentives/incentives.spec.js +0 -177
  108. package/dist/esm/test/splits/splits.spec.js +0 -400
  109. package/dist/types/src/abi/OVMFactory.d.ts +0 -662
  110. package/src/abi/BatchDeposit.ts +0 -117
  111. package/src/abi/MerkleDistributorWithDeadline.ts +0 -70
  112. package/src/abi/Multicall3.ts +0 -250
  113. package/src/abi/OVMFactory.ts +0 -617
  114. package/src/abi/OWR.ts +0 -302
  115. package/src/abi/SplitMain.ts +0 -927
  116. package/src/abi/splitV2FactoryAbi.ts +0 -417
  117. package/src/ajv.ts +0 -191
  118. package/src/base.ts +0 -54
  119. package/src/bytecodes.ts +0 -31
  120. package/src/constants.ts +0 -304
  121. package/src/eoa/eoa.ts +0 -153
  122. package/src/eoa/eoaHelpers.ts +0 -112
  123. package/src/errors.ts +0 -8
  124. package/src/exits/ethUtils.ts +0 -49
  125. package/src/exits/exit.ts +0 -661
  126. package/src/exits/verificationHelpers.ts +0 -110
  127. package/src/incentives/incentiveHelpers.ts +0 -55
  128. package/src/incentives/incentives.ts +0 -153
  129. package/src/index.ts +0 -614
  130. package/src/schema.ts +0 -376
  131. package/src/services.ts +0 -24
  132. package/src/splits/splitHelpers.ts +0 -1032
  133. package/src/splits/splits.ts +0 -504
  134. package/src/types.ts +0 -687
  135. package/src/utils.ts +0 -88
  136. package/src/verification/common.ts +0 -532
  137. package/src/verification/signature-validator.ts +0 -103
  138. package/src/verification/sszTypes.ts +0 -88
  139. package/src/verification/termsAndConditions.ts +0 -32
  140. package/src/verification/v1.10.0.ts +0 -245
  141. package/src/verification/v1.6.0.ts +0 -256
  142. package/src/verification/v1.7.0.ts +0 -309
  143. package/src/verification/v1.8.0.ts +0 -340
@@ -1,1032 +0,0 @@
1
- import {
2
- type OWRTranches,
3
- type ClusterValidator,
4
- type ETH_ADDRESS,
5
- type SplitRecipient,
6
- type SignerType,
7
- type SplitV2Recipient,
8
- type OVMArgs,
9
- type ChainConfig,
10
- } from '../types';
11
- import { Contract, Interface, parseEther, ZeroAddress } from 'ethers';
12
- import { OWRContract, OWRFactoryContract } from '../abi/OWR';
13
- import { OVMFactoryContract, OVMContract } from '../abi/OVMFactory';
14
- import { splitMainEthereumAbi } from '../abi/SplitMain';
15
- import { CHAIN_CONFIGURATION, ETHER_TO_GWEI } from '../constants';
16
- import { splitV2FactoryAbi } from '../abi/splitV2FactoryAbi';
17
- import { MultiCall3Contract } from '../abi/Multicall3';
18
-
19
- const splitMainContractInterface = new Interface(splitMainEthereumAbi);
20
- const owrFactoryContractInterface = new Interface(OWRFactoryContract.abi);
21
- const ovmFactoryContractInterface = new Interface(OVMFactoryContract.abi);
22
- const splitV2FactoryInterface = new Interface(splitV2FactoryAbi);
23
-
24
- type Call = {
25
- target: ETH_ADDRESS;
26
- callData: string;
27
- };
28
-
29
- type OWRArgs = {
30
- recoveryAddress: ETH_ADDRESS;
31
- principalRecipient: ETH_ADDRESS;
32
- amountOfPrincipalStake: number;
33
- predictedSplitterAddress: ETH_ADDRESS;
34
- };
35
-
36
- type SplitArgs = {
37
- accounts: ETH_ADDRESS[];
38
- percentAllocations: number[];
39
- distributorFee: number;
40
- controllerAddress: ETH_ADDRESS;
41
- };
42
-
43
- // Helper function to extract common recipient formatting logic
44
- const formatRecipientsCommon = (
45
- recipients: SplitRecipient[] | SplitV2Recipient[],
46
- ): {
47
- sortedRecipients: any[];
48
- getAddress: (item: any) => string;
49
- getPercentAllocation: (item: any) => number;
50
- } => {
51
- const copiedRecipients = [...recipients];
52
-
53
- // Handle both SplitRecipient and SplitV2Recipient types
54
- const getAddress = (item: any): string => item.account || item.address;
55
- const getPercentAllocation = (item: any): number => item.percentAllocation;
56
-
57
- // Has to be sorted when passed
58
- copiedRecipients.sort((a, b) => getAddress(a).localeCompare(getAddress(b)));
59
-
60
- return {
61
- sortedRecipients: copiedRecipients,
62
- getAddress,
63
- getPercentAllocation,
64
- };
65
- };
66
-
67
- export const formatSplitRecipients = (
68
- recipients: SplitRecipient[],
69
- ): { accounts: ETH_ADDRESS[]; percentAllocations: number[] } => {
70
- const { sortedRecipients, getAddress, getPercentAllocation } =
71
- formatRecipientsCommon(recipients);
72
-
73
- const accounts = sortedRecipients.map(item => getAddress(item));
74
- const percentAllocations = sortedRecipients.map(recipient => {
75
- const splitTostring = (getPercentAllocation(recipient) * 1e4).toFixed(0);
76
- return parseInt(splitTostring);
77
- });
78
- return { accounts, percentAllocations };
79
- };
80
-
81
- export const predictSplitterAddress = async ({
82
- signer,
83
- accounts,
84
- percentAllocations,
85
- chainId,
86
- distributorFee,
87
- controllerAddress,
88
- }: {
89
- signer: SignerType;
90
- accounts: ETH_ADDRESS[];
91
- percentAllocations: number[];
92
- chainId: number;
93
- distributorFee: number;
94
- controllerAddress: ETH_ADDRESS;
95
- }): Promise<ETH_ADDRESS> => {
96
- try {
97
- const splitMainContractInstance = new Contract(
98
- getChainConfig(chainId).SPLITMAIN_CONTRACT.address,
99
- splitMainEthereumAbi,
100
- signer,
101
- );
102
-
103
- let predictedSplitterAddress: string;
104
-
105
- if (controllerAddress === ZeroAddress) {
106
- try {
107
- predictedSplitterAddress =
108
- await splitMainContractInstance.predictImmutableSplitAddress(
109
- accounts,
110
- percentAllocations,
111
- distributorFee,
112
- );
113
- } catch (error: any) {
114
- throw new Error(
115
- `Failed to predict immutable splitter address: ${error.message ?? 'Contract call failed'}`,
116
- );
117
- }
118
- } else {
119
- try {
120
- // It throws on deployed Immutable splitter
121
- predictedSplitterAddress =
122
- await splitMainContractInstance.createSplit.staticCall(
123
- accounts,
124
- percentAllocations,
125
- distributorFee,
126
- controllerAddress,
127
- );
128
- } catch (error: any) {
129
- throw new Error(
130
- `Failed to predict mutable splitter address via static call: ${error.message ?? 'Static call failed'}`,
131
- );
132
- }
133
- }
134
-
135
- return predictedSplitterAddress;
136
- } catch (error: any) {
137
- // Re-throw if it's already our custom error
138
- if (error.message.includes('Failed to predict')) {
139
- throw error;
140
- }
141
- // Handle unexpected errors
142
- throw new Error(
143
- `Unexpected error in predictSplitterAddress: ${error.message ?? 'Unknown contract interaction error'}`,
144
- );
145
- }
146
- };
147
-
148
- export const handleDeployOWRAndSplitter = async ({
149
- signer,
150
- isSplitterDeployed,
151
- predictedSplitterAddress,
152
- accounts,
153
- percentAllocations,
154
- etherAmount,
155
- principalRecipient,
156
- chainId,
157
- distributorFee,
158
- controllerAddress,
159
- recoveryAddress,
160
- }: {
161
- signer: SignerType;
162
- isSplitterDeployed: boolean;
163
- predictedSplitterAddress: ETH_ADDRESS;
164
- accounts: ETH_ADDRESS[];
165
- percentAllocations: number[];
166
- etherAmount: number;
167
- principalRecipient: ETH_ADDRESS;
168
- chainId: number;
169
- distributorFee: number;
170
- controllerAddress: ETH_ADDRESS;
171
- recoveryAddress: ETH_ADDRESS;
172
- }): Promise<ClusterValidator> => {
173
- try {
174
- if (isSplitterDeployed) {
175
- let owrAddress: ETH_ADDRESS;
176
- try {
177
- owrAddress = await createOWRContract({
178
- owrArgs: {
179
- principalRecipient,
180
- amountOfPrincipalStake: etherAmount,
181
- predictedSplitterAddress,
182
- recoveryAddress,
183
- },
184
- signer,
185
- chainId,
186
- });
187
- } catch (error: any) {
188
- throw new Error(
189
- `Failed to create OWR contract with existing splitter: ${error.message ?? 'OWR contract creation failed'}`,
190
- );
191
- }
192
- return {
193
- withdrawal_address: owrAddress,
194
- fee_recipient_address: predictedSplitterAddress,
195
- };
196
- } else {
197
- let owrAddress: ETH_ADDRESS;
198
- let splitterAddress: ETH_ADDRESS;
199
- try {
200
- const result = await deploySplitterAndOWRContracts({
201
- owrArgs: {
202
- principalRecipient,
203
- amountOfPrincipalStake: etherAmount,
204
- predictedSplitterAddress,
205
- recoveryAddress,
206
- },
207
- splitterArgs: {
208
- accounts,
209
- percentAllocations,
210
- distributorFee,
211
- controllerAddress,
212
- },
213
- signer,
214
- chainId,
215
- });
216
- owrAddress = result.owrAddress;
217
- splitterAddress = result.splitterAddress;
218
- } catch (error: any) {
219
- throw new Error(
220
- `Failed to deploy both splitter and OWR contracts: ${error.message ?? 'Multicall3 contract deployment failed'}`,
221
- );
222
- }
223
-
224
- return {
225
- withdrawal_address: owrAddress,
226
- fee_recipient_address: splitterAddress,
227
- };
228
- }
229
- } catch (error: any) {
230
- // Re-throw if it's already our custom error
231
- if (error.message.includes('Failed to')) {
232
- throw error;
233
- }
234
- // Handle unexpected errors
235
- throw new Error(
236
- `Unexpected error in handleDeployOWRAndSplitter: ${error.message ?? 'Unknown error during contract deployment orchestration'}`,
237
- );
238
- }
239
- };
240
-
241
- const createOWRContract = async ({
242
- owrArgs,
243
- signer,
244
- chainId,
245
- }: {
246
- owrArgs: OWRArgs;
247
- signer: SignerType;
248
- chainId: number;
249
- }): Promise<ETH_ADDRESS> => {
250
- try {
251
- const OWRFactoryInstance = new Contract(
252
- getChainConfig(chainId).OWR_FACTORY_CONTRACT.address,
253
- OWRFactoryContract.abi,
254
- signer,
255
- );
256
-
257
- let tx;
258
- try {
259
- tx = await OWRFactoryInstance.createOWRecipient(
260
- owrArgs.recoveryAddress,
261
- owrArgs.principalRecipient,
262
- owrArgs.predictedSplitterAddress,
263
- parseEther(owrArgs.amountOfPrincipalStake.toString()),
264
- );
265
- } catch (error: any) {
266
- throw new Error(
267
- `Failed to submit OWR contract creation transaction: ${error.message ?? 'Transaction submission failed'}`,
268
- );
269
- }
270
-
271
- let receipt;
272
- try {
273
- receipt = await tx.wait();
274
- } catch (error: any) {
275
- throw new Error(
276
- `OWR contract creation transaction failed or was reverted: ${error.message ?? 'Transaction execution failed'}`,
277
- );
278
- }
279
-
280
- if (!receipt?.logs?.length) {
281
- throw new Error(
282
- 'OWR contract creation transaction succeeded but no events were emitted - unable to determine contract address',
283
- );
284
- }
285
-
286
- const OWRAddressData = receipt.logs[0]?.topics[1];
287
- if (!OWRAddressData) {
288
- throw new Error(
289
- 'OWR contract creation transaction succeeded but contract address could not be extracted from events',
290
- );
291
- }
292
-
293
- const formattedOWRAddress = '0x' + OWRAddressData.slice(26, 66);
294
-
295
- // Basic address validation
296
- if (
297
- formattedOWRAddress.length !== 42 ||
298
- !formattedOWRAddress.startsWith('0x')
299
- ) {
300
- throw new Error(
301
- `Invalid OWR contract address format: ${formattedOWRAddress}`,
302
- );
303
- }
304
-
305
- return formattedOWRAddress;
306
- } catch (error: any) {
307
- // Re-throw if it's already our custom error
308
- if (
309
- error.message.includes('Failed to') ||
310
- error.message.includes('OWR contract') ||
311
- error.message.includes('Invalid OWR')
312
- ) {
313
- throw error;
314
- }
315
- // Handle unexpected errors
316
- throw new Error(
317
- `Unexpected error in createOWRContract: ${error.message ?? 'Unknown error during OWR contract creation'}`,
318
- );
319
- }
320
- };
321
-
322
- export const deploySplitterContract = async ({
323
- signer,
324
- accounts,
325
- percentAllocations,
326
- chainId,
327
- distributorFee,
328
- controllerAddress,
329
- }: {
330
- signer: SignerType;
331
- accounts: ETH_ADDRESS[];
332
- percentAllocations: number[];
333
- chainId: number;
334
- distributorFee: number;
335
- controllerAddress: ETH_ADDRESS;
336
- }): Promise<ETH_ADDRESS> => {
337
- try {
338
- const splitMainContractInstance = new Contract(
339
- getChainConfig(chainId).SPLITMAIN_CONTRACT.address,
340
- splitMainEthereumAbi,
341
- signer,
342
- );
343
-
344
- let tx;
345
- try {
346
- tx = await splitMainContractInstance.createSplit(
347
- accounts,
348
- percentAllocations,
349
- distributorFee,
350
- controllerAddress,
351
- );
352
- } catch (error: any) {
353
- throw new Error(
354
- `Failed to submit splitter contract creation transaction: ${error.message ?? 'Transaction submission failed'}`,
355
- );
356
- }
357
-
358
- let receipt;
359
- try {
360
- receipt = await tx.wait();
361
- } catch (error: any) {
362
- throw new Error(
363
- `Splitter contract creation transaction failed or was reverted: ${error.message ?? 'Transaction execution failed'}`,
364
- );
365
- }
366
-
367
- if (!receipt?.logs?.length) {
368
- throw new Error(
369
- 'Splitter contract creation transaction succeeded but no events were emitted - unable to determine contract address',
370
- );
371
- }
372
-
373
- const splitterAddressData = receipt.logs[0]?.topics[1];
374
- if (!splitterAddressData) {
375
- throw new Error(
376
- 'Splitter contract creation transaction succeeded but contract address could not be extracted from events',
377
- );
378
- }
379
-
380
- const formattedSplitterAddress = '0x' + splitterAddressData.slice(26, 66);
381
-
382
- // Basic address validation
383
- if (
384
- formattedSplitterAddress.length !== 42 ||
385
- !formattedSplitterAddress.startsWith('0x')
386
- ) {
387
- throw new Error(
388
- `Invalid splitter contract address format: ${formattedSplitterAddress}`,
389
- );
390
- }
391
-
392
- return formattedSplitterAddress;
393
- } catch (error: any) {
394
- // Re-throw if it's already our custom error
395
- if (
396
- error.message.includes('Failed to') ||
397
- error.message.includes('Splitter contract') ||
398
- error.message.includes('Invalid splitter')
399
- ) {
400
- throw error;
401
- }
402
- // Handle unexpected errors
403
- throw new Error(
404
- `Unexpected error in deploySplitterContract: ${error.message ?? 'Unknown error during splitter contract creation'}`,
405
- );
406
- }
407
- };
408
-
409
- export const deploySplitterAndOWRContracts = async ({
410
- owrArgs,
411
- splitterArgs,
412
- signer,
413
- chainId,
414
- }: {
415
- owrArgs: OWRArgs;
416
- splitterArgs: SplitArgs;
417
- signer: SignerType;
418
- chainId: number;
419
- }): Promise<{ owrAddress: ETH_ADDRESS; splitterAddress: ETH_ADDRESS }> => {
420
- const executeCalls: Call[] = [];
421
-
422
- const splitTxData = encodeCreateSplitTxData(
423
- splitterArgs.accounts,
424
- splitterArgs.percentAllocations,
425
- splitterArgs.distributorFee,
426
- splitterArgs.controllerAddress,
427
- );
428
-
429
- const owrTxData = encodeCreateOWRecipientTxData(
430
- owrArgs.recoveryAddress,
431
- owrArgs.principalRecipient,
432
- owrArgs.predictedSplitterAddress,
433
- owrArgs.amountOfPrincipalStake,
434
- );
435
-
436
- executeCalls.push(
437
- {
438
- target: getChainConfig(chainId).SPLITMAIN_CONTRACT.address,
439
- callData: splitTxData,
440
- },
441
- {
442
- target: getChainConfig(chainId).OWR_FACTORY_CONTRACT.address,
443
- callData: owrTxData,
444
- },
445
- );
446
-
447
- const executeMultiCalls = await multicall3(executeCalls, signer, chainId);
448
-
449
- const splitAddressData = executeMultiCalls?.logs[0]?.topics[1];
450
- const formattedSplitterAddress = '0x' + splitAddressData?.slice(26, 66);
451
- const owrAddressData = executeMultiCalls?.logs[1]?.topics[1];
452
- const formattedOwrAddress = '0x' + owrAddressData?.slice(26, 66);
453
-
454
- return {
455
- owrAddress: formattedOwrAddress,
456
- splitterAddress: formattedSplitterAddress,
457
- };
458
- };
459
-
460
- export const getOWRTranches = async ({
461
- owrAddress,
462
- signer,
463
- }: {
464
- owrAddress: ETH_ADDRESS;
465
- signer: SignerType;
466
- }): Promise<OWRTranches> => {
467
- try {
468
- const owrContract = new Contract(owrAddress, OWRContract.abi, signer);
469
-
470
- let res;
471
- try {
472
- res = await owrContract.getTranches();
473
- } catch (error: any) {
474
- throw new Error(
475
- `Failed to call getTranches on OWR contract at ${owrAddress}: ${error.message ?? 'Contract call failed'}`,
476
- );
477
- }
478
-
479
- if (!res) {
480
- throw new Error(
481
- `OWR contract at ${owrAddress} returned empty result for getTranches()`,
482
- );
483
- }
484
-
485
- return {
486
- principalRecipient: res.principalRecipient,
487
- rewardRecipient: res.rewardRecipient,
488
- amountOfPrincipalStake: res.amountOfPrincipalStake,
489
- };
490
- } catch (error: any) {
491
- // Re-throw if it's already our custom error
492
- if (
493
- error.message.includes('Failed to') ||
494
- error.message.includes('OWR contract')
495
- ) {
496
- throw error;
497
- }
498
- // Handle unexpected errors
499
- throw new Error(
500
- `Unexpected error in getOWRTranches: ${error.message ?? 'Unknown error while fetching OWR tranche data'}`,
501
- );
502
- }
503
- };
504
-
505
- export const multicall3 = async (
506
- calls: Call[],
507
- signer: SignerType,
508
- chainId: number,
509
- ): Promise<any> => {
510
- try {
511
- const chainConfig = getChainConfig(chainId);
512
- const multicall3Address = chainConfig.MULTICALL3_CONTRACT.address;
513
- const multiCall3ContractInstance = new Contract(
514
- multicall3Address,
515
- MultiCall3Contract.abi,
516
- signer,
517
- );
518
-
519
- let tx;
520
- try {
521
- tx = await multiCall3ContractInstance.aggregate(calls);
522
- } catch (error: any) {
523
- throw new Error(
524
- `Failed to submit multicall3 transaction: ${error.message ?? 'Transaction submission failed'}`,
525
- );
526
- }
527
-
528
- let receipt;
529
- try {
530
- receipt = await tx.wait();
531
- } catch (error: any) {
532
- throw new Error(
533
- `Multicall3 transaction failed or was reverted: ${error.message ?? 'Transaction execution failed'}`,
534
- );
535
- }
536
-
537
- if (!receipt) {
538
- throw new Error(
539
- 'Multicall3 transaction succeeded but no receipt was returned',
540
- );
541
- }
542
-
543
- return receipt;
544
- } catch (error: any) {
545
- // Re-throw if it's already our custom error
546
- if (
547
- error.message.includes('Failed to') ||
548
- error.message.includes('Multicall3 transaction')
549
- ) {
550
- throw error;
551
- }
552
- // Handle unexpected errors
553
- throw new Error(
554
- `Unexpected error in multicall3: ${error.message ?? 'Unknown error during multicall3 execution'}`,
555
- );
556
- }
557
- };
558
-
559
- const encodeCreateSplitTxData = (
560
- accounts: ETH_ADDRESS[],
561
- percentAllocations: number[],
562
- distributorFee: number,
563
- controller: ETH_ADDRESS,
564
- ): ETH_ADDRESS => {
565
- return splitMainContractInterface.encodeFunctionData('createSplit', [
566
- accounts,
567
- percentAllocations,
568
- distributorFee,
569
- controller,
570
- ]);
571
- };
572
-
573
- const encodeCreateOWRecipientTxData = (
574
- recoveryAddress: ETH_ADDRESS,
575
- principalRecipient: ETH_ADDRESS,
576
- rewardRecipient: ETH_ADDRESS,
577
- amountOfPrincipalStake: number,
578
- ): ETH_ADDRESS => {
579
- return owrFactoryContractInterface.encodeFunctionData('createOWRecipient', [
580
- recoveryAddress,
581
- principalRecipient,
582
- rewardRecipient,
583
- parseEther(amountOfPrincipalStake.toString()),
584
- ]);
585
- };
586
-
587
- // OVM and SplitV2 Helper Functions
588
-
589
- // Helper function to format recipients specifically for SplitV2 (returns SplitV2Recipient[])
590
- export const formatRecipientsForSplitV2 = (
591
- splitRecipients: SplitRecipient[] | SplitV2Recipient[],
592
- ): SplitV2Recipient[] => {
593
- const { sortedRecipients, getAddress, getPercentAllocation } =
594
- formatRecipientsCommon(splitRecipients);
595
-
596
- return sortedRecipients
597
- .filter(item => getAddress(item) !== '')
598
- .map(item => ({
599
- address: getAddress(item),
600
- percentAllocation: parseFloat(getPercentAllocation(item).toString()),
601
- }));
602
- };
603
-
604
- // Helper function to create SplitV2 parameters
605
- const createSplitV2Params = (
606
- recipients: SplitV2Recipient[],
607
- distributorFeePercent: number,
608
- ): {
609
- recipients: string[];
610
- allocations: number[];
611
- totalAllocation: number;
612
- distributionIncentive: number;
613
- } => {
614
- const addresses = recipients.map(r => r.address);
615
- const allocations = recipients.map(r =>
616
- Math.floor(r.percentAllocation * 1e4),
617
- ); // Convert to basis points
618
- const totalAllocation = allocations.reduce(
619
- (sum, allocation) => sum + allocation,
620
- 0,
621
- );
622
-
623
- return {
624
- recipients: addresses,
625
- allocations,
626
- totalAllocation,
627
- distributionIncentive: distributorFeePercent,
628
- };
629
- };
630
-
631
- export const predictSplitV2Address = async ({
632
- splitOwnerAddress,
633
- recipients,
634
- distributorFeePercent,
635
- salt,
636
- signer,
637
- chainId,
638
- }: {
639
- splitOwnerAddress: string;
640
- recipients: SplitV2Recipient[];
641
- distributorFeePercent: number;
642
- salt: `0x${string}`;
643
- signer: SignerType;
644
- chainId: number;
645
- }): Promise<string> => {
646
- try {
647
- const chainConfig = getChainConfig(chainId);
648
- if (!chainConfig?.SPLIT_V2_FACTORY_CONTRACT?.address) {
649
- throw new Error(`SplitV2 Factory not configured for chain ${chainId}`);
650
- }
651
-
652
- const splitV2FactoryContract = new Contract(
653
- chainConfig.SPLIT_V2_FACTORY_CONTRACT.address,
654
- splitV2FactoryAbi,
655
- signer,
656
- );
657
-
658
- const splitParams = createSplitV2Params(recipients, distributorFeePercent);
659
-
660
- const predictedAddress = await splitV2FactoryContract[
661
- 'predictDeterministicAddress((address[],uint256[],uint256,uint16),address,bytes32)'
662
- ](splitParams, splitOwnerAddress, salt);
663
-
664
- return predictedAddress;
665
- } catch (error: any) {
666
- throw new Error(
667
- `Failed to predict SplitV2 address: ${error.message ?? 'SplitV2 contract call failed'}`,
668
- );
669
- }
670
- };
671
-
672
- export const isSplitV2Deployed = async ({
673
- splitOwnerAddress,
674
- recipients,
675
- distributorFeePercent,
676
- salt,
677
- signer,
678
- chainId,
679
- }: {
680
- splitOwnerAddress: string;
681
- recipients: SplitV2Recipient[];
682
- distributorFeePercent: number;
683
- salt: `0x${string}`;
684
- signer: SignerType;
685
- chainId: number;
686
- }): Promise<boolean> => {
687
- try {
688
- const chainConfig = getChainConfig(chainId);
689
- if (!chainConfig?.SPLIT_V2_FACTORY_CONTRACT?.address) {
690
- throw new Error(`SplitV2 Factory not configured for chain ${chainId}`);
691
- }
692
-
693
- const splitV2FactoryContract = new Contract(
694
- chainConfig.SPLIT_V2_FACTORY_CONTRACT.address,
695
- splitV2FactoryAbi,
696
- signer,
697
- );
698
-
699
- const splitParams = createSplitV2Params(recipients, distributorFeePercent);
700
-
701
- const [, exists] = await splitV2FactoryContract.isDeployed(
702
- splitParams,
703
- splitOwnerAddress,
704
- salt,
705
- );
706
-
707
- return exists;
708
- } catch (error: any) {
709
- // If the check fails, assume it's not deployed
710
- return false;
711
- }
712
- };
713
-
714
- export const deployOVMContract = async ({
715
- OVMOwnerAddress,
716
- principalRecipient,
717
- rewardRecipient,
718
- principalThreshold,
719
- signer,
720
- chainId,
721
- }: {
722
- OVMOwnerAddress: string;
723
- principalRecipient: string;
724
- rewardRecipient: string;
725
- principalThreshold: number;
726
- signer: SignerType;
727
- chainId: number;
728
- }): Promise<string> => {
729
- try {
730
- const chainConfig = getChainConfig(chainId);
731
- if (!chainConfig?.OVM_FACTORY_CONTRACT?.address) {
732
- throw new Error(`OVM Factory not configured for chain ${chainId}`);
733
- }
734
-
735
- const ovmFactoryContract = new Contract(
736
- chainConfig.OVM_FACTORY_CONTRACT.address,
737
- OVMFactoryContract.abi,
738
- signer,
739
- );
740
-
741
- const tx = await ovmFactoryContract.createObolValidatorManager(
742
- OVMOwnerAddress,
743
- principalRecipient,
744
- rewardRecipient,
745
- principalThreshold * ETHER_TO_GWEI,
746
- );
747
-
748
- const receipt = await tx.wait();
749
-
750
- // Extract OVM address from logs
751
- const ovmAddressLog = receipt?.logs[1]?.topics[1];
752
- if (!ovmAddressLog) {
753
- throw new Error('Failed to extract OVM address from transaction logs');
754
- }
755
-
756
- const ovmAddress = '0x' + ovmAddressLog.slice(26, 66);
757
- return ovmAddress;
758
- } catch (error: any) {
759
- throw new Error(
760
- `Failed to deploy OVM contract: ${error.message ?? 'OVM deployment failed'}`,
761
- );
762
- }
763
- };
764
-
765
- export const deployOVMAndSplitV2 = async ({
766
- ovmArgs,
767
- rewardRecipients,
768
- isRewardsSplitterDeployed,
769
- distributorFeePercent,
770
- salt,
771
- signer,
772
- chainId,
773
- principalSplitRecipients,
774
- isPrincipalSplitDeployed,
775
- splitOwnerAddress,
776
- }: {
777
- ovmArgs: OVMArgs;
778
- rewardRecipients: SplitV2Recipient[];
779
- isRewardsSplitterDeployed?: boolean;
780
- distributorFeePercent: number;
781
- salt: `0x${string}`;
782
- signer: SignerType;
783
- chainId: number;
784
- principalSplitRecipients?: SplitV2Recipient[];
785
- isPrincipalSplitDeployed?: boolean;
786
- splitOwnerAddress: string;
787
- }): Promise<string> => {
788
- try {
789
- const chainConfig = getChainConfig(chainId);
790
- if (!chainConfig?.OVM_FACTORY_CONTRACT?.address) {
791
- throw new Error(`OVM Factory not configured for chain ${chainId}`);
792
- }
793
-
794
- if (!chainConfig?.SPLIT_V2_FACTORY_CONTRACT?.address) {
795
- throw new Error(`SplitV2 Factory not configured for chain ${chainId}`);
796
- }
797
-
798
- const executeCalls: Call[] = [];
799
-
800
- if (rewardRecipients && !isRewardsSplitterDeployed) {
801
- // Create rewards split call data
802
- const splitParams = createSplitV2Params(
803
- rewardRecipients,
804
- distributorFeePercent,
805
- );
806
- const rewardsSplitTxData = encodeCreateSplitV2DeterministicTxData(
807
- splitParams,
808
- splitOwnerAddress,
809
- salt,
810
- );
811
-
812
- executeCalls.push({
813
- target: chainConfig.SPLIT_V2_FACTORY_CONTRACT.address,
814
- callData: rewardsSplitTxData,
815
- });
816
- }
817
-
818
- // Create principal split call data if needed (for total split scenario)
819
- if (principalSplitRecipients && !isPrincipalSplitDeployed) {
820
- const principalSplitParams = createSplitV2Params(
821
- principalSplitRecipients,
822
- distributorFeePercent,
823
- );
824
- const principalSplitTxData = encodeCreateSplitV2DeterministicTxData(
825
- principalSplitParams,
826
- splitOwnerAddress,
827
- salt,
828
- );
829
- executeCalls.push({
830
- target: chainConfig.SPLIT_V2_FACTORY_CONTRACT.address,
831
- callData: principalSplitTxData,
832
- });
833
- }
834
-
835
- // Create OVM call data
836
- const ovmTxData = encodeCreateOVMTxData(
837
- ovmArgs.OVMOwnerAddress,
838
- ovmArgs.principalRecipient,
839
- ovmArgs.rewardRecipient,
840
- ovmArgs.principalThreshold * ETHER_TO_GWEI,
841
- );
842
-
843
- executeCalls.push({
844
- target: chainConfig.OVM_FACTORY_CONTRACT.address,
845
- callData: ovmTxData,
846
- });
847
-
848
- // Execute multicall3
849
- const executeMultiCalls = await multicall3(executeCalls, signer, chainId);
850
-
851
- // Extract addresses from events
852
- let ovmAddress: string | undefined;
853
- const logsCount = executeMultiCalls?.logs?.length || 0;
854
-
855
- if (logsCount === 2) {
856
- ovmAddress = '0x' + executeMultiCalls?.logs[1]?.topics[1]?.slice(26, 66);
857
- } else if (logsCount === 5) {
858
- ovmAddress = '0x' + executeMultiCalls?.logs[4]?.topics[1]?.slice(26, 66);
859
- } else {
860
- ovmAddress = '0x' + executeMultiCalls?.logs[7]?.topics[1]?.slice(26, 66);
861
- }
862
- if (!ovmAddress) {
863
- throw new Error(
864
- 'Failed to extract contract addresses from multicall3 events',
865
- );
866
- }
867
-
868
- return ovmAddress;
869
- } catch (error: any) {
870
- throw new Error(
871
- `Failed to deploy OVM and SplitV2: ${error.message ?? 'Deployment failed'}`,
872
- );
873
- }
874
- };
875
-
876
- const encodeCreateOVMTxData = (
877
- OVMOwnerAddress: string,
878
- principalRecipient: string,
879
- rewardRecipient: string,
880
- principalThreshold: number,
881
- ): string => {
882
- return ovmFactoryContractInterface.encodeFunctionData(
883
- 'createObolValidatorManager',
884
- [OVMOwnerAddress, principalRecipient, rewardRecipient, principalThreshold],
885
- );
886
- };
887
-
888
- const encodeCreateSplitV2DeterministicTxData = (
889
- splitParams: {
890
- recipients: string[];
891
- allocations: number[];
892
- totalAllocation: number;
893
- distributionIncentive: number;
894
- },
895
- splitOwnerAddress: string,
896
- salt: `0x${string}`,
897
- ): string => {
898
- // creatorAddress can be kept as default https://docs.splits.org/sdk/splits-v2#createsplit
899
- return splitV2FactoryInterface.encodeFunctionData(
900
- 'createSplitDeterministic',
901
- [splitParams, splitOwnerAddress, ZeroAddress, salt],
902
- );
903
- };
904
-
905
- // Helper function to safely get chain configuration
906
- const getChainConfig = (chainId: number): ChainConfig => {
907
- const config = CHAIN_CONFIGURATION[chainId];
908
- if (!config) {
909
- throw new Error(`Chain configuration not found for chain ID ${chainId}`);
910
- }
911
- return config;
912
- };
913
-
914
- /**
915
- * Requests withdrawal from an OVM contract
916
- * @param ovmAddress - The address of the OVM contract
917
- * @param pubKeys - Array of validator public keys in bytes format
918
- * @param amounts - Array of withdrawal amounts in wei (uint64)
919
- * @param signer - The signer to use for the transaction
920
- * @returns Promise that resolves to the transaction hash
921
- */
922
- export const requestWithdrawalFromOVM = async ({
923
- ovmAddress,
924
- pubKeys,
925
- amounts,
926
- withdrawalFees,
927
- signer,
928
- }: {
929
- ovmAddress: string;
930
- pubKeys: string[];
931
- amounts: string[];
932
- withdrawalFees: string;
933
- signer: SignerType;
934
- }): Promise<{ txHash: string }> => {
935
- try {
936
- // Convert string amounts to bigint
937
- const bigintAmounts = amounts.map(amount => BigInt(amount));
938
-
939
- const ovmContract = new Contract(ovmAddress, OVMContract.abi, signer);
940
-
941
- const tx = await ovmContract.requestWithdrawal(pubKeys, bigintAmounts, {
942
- value: BigInt(withdrawalFees),
943
- });
944
- const receipt = await tx.wait();
945
-
946
- return { txHash: receipt.hash };
947
- } catch (error: unknown) {
948
- const errorMessage =
949
- error instanceof Error ? error.message : 'Request withdrawal failed';
950
- throw new Error(`Failed to request withdrawal from OVM: ${errorMessage}`);
951
- }
952
- };
953
-
954
- /**
955
- * Deposits to OVM contract using multicall3 for batch operations
956
- * @param ovmAddress - The address of the OVM contract
957
- * @param deposits - Array of deposit objects with all required parameters
958
- * @param signer - The signer to use for the transaction
959
- * @returns Promise that resolves to an array of transaction hashes
960
- */
961
- export const depositWithMulticall3 = async ({
962
- ovmAddress,
963
- deposits,
964
- signer,
965
- chainId,
966
- }: {
967
- ovmAddress: string;
968
- deposits: Array<{
969
- pubkey: string;
970
- withdrawal_credentials: string;
971
- signature: string;
972
- deposit_data_root: string;
973
- amount: string;
974
- }>;
975
- signer: SignerType;
976
- chainId: number;
977
- }): Promise<{ txHashes: string[] }> => {
978
- try {
979
- const ovmContract = new Contract(ovmAddress, OVMContract.abi, signer);
980
- const chainConfig = getChainConfig(chainId);
981
- const multicall3Address = chainConfig.MULTICALL3_CONTRACT.address;
982
- const multiCall3ContractInstance = new Contract(
983
- multicall3Address,
984
- MultiCall3Contract.abi,
985
- signer,
986
- );
987
-
988
- const BATCH_SIZE = 500;
989
- const txHashes: string[] = [];
990
-
991
- // Process deposits in batches of 500
992
- for (let i = 0; i < deposits.length; i += BATCH_SIZE) {
993
- const batchDeposits = deposits.slice(i, i + BATCH_SIZE);
994
-
995
- // Use Multicall3 aggregate3Value (payable per-call)
996
- const callsWithValue = batchDeposits.map(deposit => ({
997
- target: ovmAddress,
998
- allowFailure: false,
999
- callData: ovmContract.interface.encodeFunctionData('deposit', [
1000
- deposit.pubkey,
1001
- deposit.withdrawal_credentials,
1002
- deposit.signature,
1003
- deposit.deposit_data_root,
1004
- ]),
1005
- value: BigInt(deposit.amount),
1006
- }));
1007
-
1008
- const totalBatchValue = callsWithValue.reduce(
1009
- (sum: bigint, c: { value: bigint }) => sum + c.value,
1010
- BigInt(0),
1011
- );
1012
-
1013
- const tx = await multiCall3ContractInstance.aggregate3Value(
1014
- callsWithValue,
1015
- { value: totalBatchValue },
1016
- );
1017
-
1018
- const receipt = await tx.wait();
1019
- if (receipt?.hash) {
1020
- txHashes.push(receipt.hash);
1021
- }
1022
- }
1023
-
1024
- return { txHashes };
1025
- } catch (error: unknown) {
1026
- const errorMessage =
1027
- error instanceof Error ? error.message : 'Deposit failed';
1028
- throw new Error(
1029
- `Failed to deposit to OVM with multicall3: ${errorMessage}`,
1030
- );
1031
- }
1032
- };