import { Request , Response , NextFunction } from 'express' ; import { validator , LiskValidationError } from '@liskhq/lisk-validator' ; import { BaseChannel , PluginCodec } from 'lisk-framework' ; import { paginateList } from '../utils' ; const getDelegatesQuerySchema = { type : 'object' , properties : { limit : { type : 'string' , format : 'uint32' , description : 'Number of delegates to be returned' , } , offset : { type : 'string' , format : 'uint32' , description : 'Offset to get delegates after a specific length in a delegates list' , } , } , } ; export const getDelegates = ( channel , codec ) => async ( req , res , next ) : Promise < void > => { const errors = validator . validate ( getDelegatesQuerySchema , req . query ) ; if ( errors . length ) { res . status ( 400 ) . send ( { errors : [ { message : new LiskValidationError ( [ ... errors ] ) . message } ] , } ) ; return ; } const { limit = 100 , offset = 0 } = req . query ; try { const registeredDelegates = await channel . invoke ( 'dpos:getAllDelegates' ) ; const encodedDelegateAccounts = await channel . invoke ( 'app:getAccounts' , { address : registeredDelegates . map ( d => d . address ) , } ) ; const decodedDelegateAccounts = encodedDelegateAccounts . map ( ( d ) => codec . decodeAccount ( Buffer . from ( d , 'hex' ) ) ) ; res . status ( 200 ) . json ( { meta : { count : decodedDelegateAccounts . length , limit : + limit , offset : + offset } , data : paginateList ( decodedDelegateAccounts , + limit , + offset ) , } ) ; } catch ( err ) { next ( err ) ; } } ;