import type { Idl } from "@project-serum/anchor" ; import { AccountsCoder } from "@project-serum/anchor" ; import camelCase from "lodash.camelcase" ; /**
 * Parsers associated with an IDL.
 */ export type AccountParsers < M > = { [ K in keyof M ] : ( data : Buffer ) => M [ K ] ; } ; param idl export const generateAccountParsers = < M extends Record < string , object > > ( idl ) : AccountParsers < M > => { const coder = new AccountsCoder ( idl ) ; return generateAccountParsersFromCoder ( idl . accounts ?. map ( ( a ) => a . name ) , coder ) ; } ; param idl export const generateAccountParsersFromCoder = < M > ( accountNames , coder ) : AccountParsers < M > => { return ( accountNames ?? [ ] ) . reduce ( ( parsers , account ) => { parsers [ camelCase ( account ) as keyof M ] = ( data ) => coder . decode < M [ keyof M ] > ( account , data ) ; return parsers ; } , { } as AccountParsers < M > ) ; } ;