11import { Address } from './address'
2+ import { bigIntToHex } from './bytes'
23import { TypeOutput , toType } from './types'
34
45import type { AddressLike , BigIntLike } from './types'
@@ -10,6 +11,15 @@ export type WithdrawalData = {
1011 amount : BigIntLike
1112}
1213
14+ export interface JsonRpcWithdrawal {
15+ index : string // QUANTITY - bigint 8 bytes
16+ validatorIndex : string // QUANTITY - bigint 8 bytes
17+ address : string // DATA, 20 Bytes address to withdraw to
18+ amount : string // QUANTITY - bigint amount in wei 32 bytes
19+ }
20+
21+ export type WithdrawalBuffer = [ Buffer , Buffer , Buffer , Buffer ]
22+
1323export class Withdrawal {
1424 constructor (
1525 public readonly index : bigint ,
@@ -33,14 +43,20 @@ export class Withdrawal {
3343 return new Withdrawal ( index , validatorIndex , address , amount )
3444 }
3545
46+ public static fromValuesArray ( withdrawalArray : WithdrawalBuffer ) {
47+ if ( withdrawalArray . length !== 4 ) {
48+ throw Error ( `Invalid withdrawalArray length expected=4 actual=${ withdrawalArray . length } ` )
49+ }
50+ const [ index , validatorIndex , address , amount ] = withdrawalArray
51+ return Withdrawal . fromWithdrawalData ( { index, validatorIndex, address, amount } )
52+ }
53+
3654 /**
3755 * Convert a withdrawal to a buffer array
3856 * @param withdrawal the withdrawal to convert
3957 * @returns buffer array of the withdrawal
4058 */
41- public static toBufferArray (
42- withdrawal : Withdrawal | WithdrawalData
43- ) : [ Buffer , Buffer , Buffer , Buffer ] {
59+ public static toBufferArray ( withdrawal : Withdrawal | WithdrawalData ) : WithdrawalBuffer {
4460 const { index, validatorIndex, address, amount } = withdrawal
4561 const indexBuffer =
4662 toType ( index , TypeOutput . BigInt ) === BigInt ( 0 )
@@ -67,4 +83,13 @@ export class Withdrawal {
6783 raw ( ) {
6884 return Withdrawal . toBufferArray ( this )
6985 }
86+
87+ toJSON ( ) {
88+ return {
89+ index : bigIntToHex ( this . index ) ,
90+ validatorIndex : bigIntToHex ( this . validatorIndex ) ,
91+ address : '0x' + this . address . buf . toString ( 'hex' ) ,
92+ amount : bigIntToHex ( this . amount ) ,
93+ }
94+ }
7095}
0 commit comments