@@ -281,30 +281,9 @@ async function autoValidatorWithdrawals({
281281 // Get beacon chain data
282282 const { stateView } = await getBeaconBlock ( slot ) ;
283283
284- // Iterate over the pending partial withdrawals
285- let totalPendingPartialWithdrawals = BigNumber . from ( 0 ) ;
286- let countPendingPartialWithdrawals = 0 ;
287- for ( let i = 0 ; i < stateView . pendingPartialWithdrawals . length ; i ++ ) {
288- const withdrawal = stateView . pendingPartialWithdrawals . get ( i ) ;
289-
290- if ( validatorIndexes . includes ( withdrawal . validatorIndex ) ) {
291- log (
292- ` Pending partial withdrawal of ${ formatUnits (
293- withdrawal . amount ,
294- 18
295- ) } ETH from validator index ${ withdrawal . validatorIndex } `
296- ) ;
297- totalPendingPartialWithdrawals = totalPendingPartialWithdrawals . add (
298- withdrawal . amount
299- ) ;
300- countPendingPartialWithdrawals ++ ;
301- }
302- }
303- log (
304- `${ countPendingPartialWithdrawals } pending partial withdrawals from beacon chain totalling ${ formatUnits (
305- totalPendingPartialWithdrawals ,
306- 18
307- ) } ETH`
284+ const totalPendingPartialWithdrawals = await totalPartialWithdrawals (
285+ stateView ,
286+ validatorIndexes
308287 ) ;
309288
310289 // 5. Calculate the buffer amount = total assets * buffer in basis points
@@ -444,6 +423,43 @@ async function autoValidatorWithdrawals({
444423 }
445424}
446425
426+ /**
427+ * Sums the pending partial withdrawals for a set of validator indexes
428+ * @param {* } stateView
429+ * @param {* } validatorIndexes array of validator indexes to check for pending partial withdrawals
430+ * @returns the total amount to 18 decimal places
431+ */
432+ async function totalPartialWithdrawals ( stateView , validatorIndexes ) {
433+ // Iterate over the pending partial withdrawals
434+ let totalGwei = BigNumber . from ( 0 ) ;
435+ let count = 0 ;
436+ for ( let i = 0 ; i < stateView . pendingPartialWithdrawals . length ; i ++ ) {
437+ const withdrawal = stateView . pendingPartialWithdrawals . get ( i ) ;
438+
439+ if ( validatorIndexes . includes ( withdrawal . validatorIndex ) ) {
440+ log (
441+ ` Pending partial withdrawal of ${ formatUnits (
442+ withdrawal . amount ,
443+ 9
444+ ) } ETH from validator index ${ withdrawal . validatorIndex } `
445+ ) ;
446+ totalGwei = totalGwei . add ( withdrawal . amount ) ;
447+ count ++ ;
448+ }
449+ }
450+ log (
451+ `${ count } of ${
452+ stateView . pendingPartialWithdrawals . length
453+ } pending partial withdrawals from beacon chain totalling ${ formatUnits (
454+ totalGwei ,
455+ 9
456+ ) } ETH`
457+ ) ;
458+
459+ // Scale up to 18 decimals
460+ return parseUnits ( totalGwei . toString ( ) , 9 ) ;
461+ }
462+
447463async function snapStakingStrategy ( { block } ) {
448464 let blockTag = await getBlock ( block ) ;
449465 // Don't use the latest block as the slot probably won't be available yet
0 commit comments