Skip to content

Commit

Permalink
add getVaultInfo, improve logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dfkadyr committed Sep 17, 2024
1 parent a0b80ed commit 789f32d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 21 deletions.
20 changes: 15 additions & 5 deletions src/parser/depositDataParser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type { FileItem, SupportedNetworks, ParserInput, ParserOutput } from './types'
import { ErrorTypes, mockData, ParserError } from './helpers'
import { ErrorTypes, mockData, ParserError, requests } from './helpers'
import { networkNames } from './verifySignature'
import { depositDataParser } from './index'


type MockVaultInfo = jest.MockedFunction<typeof requests.getVaultInfo>

jest.mock('./helpers/requests/getVaultInfo')

const testData = mockData[0]

const validInput: ParserInput = {
depositDataRoot: '0x406de60516154112c876f7250d8b289d4e3d840074e8cf755922dd',
vaultAddress: '0x9b6a6867d222d62dc301528190e3984d60adb06b',
network: 'holesky',
data: mockData,
Expand All @@ -21,6 +24,9 @@ const validOutput: ParserOutput = {
}

describe('depositDataParser',() => {
afterEach(() => {
jest.clearAllMocks()
})

it('processes valid input without throwing errors ', async() => {
await expect(depositDataParser(validInput)).resolves.toEqual(validOutput)
Expand Down Expand Up @@ -97,11 +103,15 @@ describe('depositDataParser',() => {
})

it('throws ParserError if the deposit data file has already been uploaded', async () => {

(requests.getVaultInfo as MockVaultInfo).mockResolvedValue({
isRestake: false,
depositDataRoot: '0x406de60516154112c876f7250d8b289d4e3d840074e8cf755922dd5d3c75d1c0',
})

const errorText = new ParserError(ErrorTypes.DUPLICATE_DEPOSIT_DATA)

await expect(depositDataParser({
...validInput, depositDataRoot: '0x406de60516154112c876f7250d8b289d4e3d840074e8cf755922dd5d3c75d1c0',
})).rejects.toThrow(errorText)
await expect(depositDataParser(validInput)).rejects.toThrow(errorText)
})

})
Expand Down
1 change: 1 addition & 0 deletions src/parser/getDepositData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { pubkey } = mockData[0]

const mockInput = {
pubkey,
isRestake: false,
vaultAddress: '0x9b6a6867d222d62dc301528190e3984d60adb06b',
}

Expand Down
8 changes: 3 additions & 5 deletions src/parser/getDepositData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
prefix0x,
ErrorTypes,
ParserError,
requests,
getBytes,
getAmount,
getEigenPodAddress,
Expand All @@ -13,17 +12,16 @@ import {

export type DepositDataInput = {
pubkey: string
isRestake: boolean
vaultAddress: string
withdrawalAddress?: string
network: SupportedNetworks
}

const getDepositData = async (values: DepositDataInput): Promise<DepositData> => {
const { pubkey, vaultAddress, withdrawalAddress, network } = values
const { pubkey, vaultAddress, isRestake, withdrawalAddress, network } = values

const isRestakeVault = await requests.checkIsRestakeVault(vaultAddress, network)

const withdrawalCredentialAddress = isRestakeVault
const withdrawalCredentialAddress = isRestake
? await getEigenPodAddress({ vaultAddress, withdrawalAddress, network })
: vaultAddress

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import gqlRequest from './gqlRequest'
import { SupportedNetworks } from '../../types'


const checkIsRestakeVault = async (vaultId: string, network: SupportedNetworks) => {
const query = `query Vault($vaultId: ID!) { vault(id: $vaultId) { isRestake }}`
const getVaultInfo = async (vaultId: string, network: SupportedNetworks) => {
const query = `query Vault($vaultId: ID!) { vault(id: $vaultId) { isRestake depositDataRoot }}`

const variables = { vaultId: vaultId.toLowerCase() }

try {
const data = await gqlRequest({ query, variables }, network)

return data?.vault?.isRestake
return data?.vault
}
catch (error) {
console.error('Error fetching isRestake:', error)
console.error('Error fetching Vault info:', error)
}
}


export default checkIsRestakeVault
export default getVaultInfo
2 changes: 1 addition & 1 deletion src/parser/helpers/requests/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default as getEigenPods } from './getEigenPods'
export { default as checkIsRestakeVault } from './checkIsRestakeVault'
export { default as getVaultInfo } from './getVaultInfo'
20 changes: 16 additions & 4 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ParserError, ErrorTypes } from './helpers'
import { ParserError, ErrorTypes, requests } from './helpers'

import initBls from './initBls'
import getTreeLeaf from './getTreeLeaf'
Expand All @@ -11,21 +11,28 @@ import verifySignature from './verifySignature'


export const depositDataParser = async (input: ParserInput) => {
const { vaultAddress, depositDataRoot, network, data, onProgress } = input
const { vaultAddress, network, data, onProgress } = input

const bls = await initBls()
const parsedFile = validateJson({ data })

const pubkeySet = new Set<string>()
const treeLeaves: Uint8Array[] = []

const vaultInfo = await requests.getVaultInfo(vaultAddress, network)

for (let index = 0; index < parsedFile.length; index++) {
const item = parsedFile[index]
const { pubkey, signature, withdrawal_address } = item

validateFields({ item })

const depositData = await getDepositData({ pubkey, vaultAddress, withdrawalAddress: withdrawal_address, network })
const depositData = await getDepositData({
network,
pubkey, vaultAddress,
isRestake: vaultInfo?.isRestake,
withdrawalAddress: withdrawal_address,
})

verifySignature({ bls, pubkey, signature, depositData, network })

Expand All @@ -46,5 +53,10 @@ export const depositDataParser = async (input: ParserInput) => {
throw new ParserError(ErrorTypes.DUPLICATE_PUBLIC_KEYS)
}

return getPostMessage({ pubkeySet, parsedFile, treeLeaves, depositDataRoot })
return getPostMessage({
pubkeySet,
parsedFile,
treeLeaves,
depositDataRoot: vaultInfo?.depositDataRoot,
})
}
1 change: 0 additions & 1 deletion src/parser/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export type DepositData = {
export type ParserInput = {
data: DepositDataFile
vaultAddress: string
depositDataRoot: string
network: SupportedNetworks
onProgress?: (progress: Progress) => void
}
Expand Down

0 comments on commit 789f32d

Please sign in to comment.