import type Client from '../Client' ; import Validator , { HTTPValidatorObject } from '../models/Validator' ; import ResourceList from '../ResourceList' ; import Account from '../models/Account' ; import Stats from './Stats' ; interface ListParams { cursor ? : string ; } type Context = Account ; export default class Validators { private client ! : Client ; private context ? : Context ; constructor ( client , context ? ) { this . client = client ; this . context = context ; } fromAddress ( address ) { return new Validator ( this . client , { address } ) ; } async search ( term ) { const url = 'validators/name' ; const response = await this . client . get ( url , { search : term } ) ; const { data : { data : validators } , } = response ; const data = validators . map ( ( d ) => new Validator ( this . client , d ) ) ; return new ResourceList ( data , this . list . bind ( this ) ) ; } async list ( params = { } ) { const { validators , cursor } = await this . fetchList ( params ) ; const data = validators . map ( ( d ) => new Validator ( this . client , d ) ) ; return new ResourceList ( data , this . list . bind ( this ) , cursor ) ; } private async fetchList ( params = { } ) { let url = '/validators' ; if ( this . context instanceof Account ) { const account = this . context as Account ; url = `/accounts/${ account . address }/validators` ; } const response = await this . client . get ( url , { cursor : params . cursor } ) ; const { data : { data : validators , cursor } , } = response ; return { validators , cursor } ; } async get ( address ) { const url = `/validators/${ address }` ; const { data : { data : validator } , } = await this . client . get ( url ) ; return new Validator ( this . client , validator ) ; } async elected ( block ? ) { const url = block === undefined ? '/validators/elected' : `/validators/elected/${ block }` ; const response = await this . client . get ( url ) ; const { data : { data : validators } , } = response ; const data = validators . map ( ( h ) => new Validator ( this . client , h ) ) ; return new ResourceList ( data , this . list . bind ( this ) ) ; } public get stats ( ) : Stats { return new Stats ( this . client , 'validators' ) ; } }