import axios from "axios" ; import { sleep } from "./util" ; export class Subscan { public baseV1Url : string ; public baseV2Url : string ; public denom : number ; public eventsEndpoint = "/events" ; public rewardsEndpoint = "/account/reward_slash" ; public defaultHeader = { "Content-Type" : "application/json" , } ; constructor ( baseV1Url , baseV2Url , denom ) { this . baseV1Url = baseV1Url ; this . baseV2Url = baseV2Url ; this . denom = denom ; } getEraPaid = async ( ) => { const url = this . baseV1Url + this . eventsEndpoint ; const totalList = [ ] ; let page = 0 ; let shouldContinue = true ; let queriedEraPaid = false ; let queriedEraPayout = false ; let call = "erapaid" ; while ( shouldContinue ) { const data = { row : 100 , page : page , module : "staking" , call : call } ; const res = await axios . post ( url , data , { headers : this . defaultHeader } ) ; const status = res . status ; if ( status == 200 ) { const { count , events } = res . data . data ; if ( ! events ) { if ( call == "erapaid" ) { queriedEraPaid = true ; call = "erapayout" ; page = 0 ; } else if ( call == "erapayout" ) { queriedEraPayout = true ; shouldContinue = false ; } } else { const eventPageList = events . map ( ( event ) => { const { event_index , block_num , extrinsic_idx , module_id , event_id , params , event_idx , extrinsic_hash , finalized , block_timestamp , } = event ; const [ eraIndex , validatorReward , remainderReward ] = JSON . parse ( params ) ; return { era : eraIndex . value , blockNumber : block_num , blockTimestamp : block_timestamp , eventIndex : event_index , moduleId : module_id , eventId : event_id , totalValidatorReward : parseFloat ( validatorReward . value ) / this . denom , totalRemainderReward : parseFloat ( remainderReward . value ) / this . denom , } ; } ) ; page ++ ; totalList . push ( ... eventPageList ) ; await sleep ( 2000 ) ; } } else if ( status == 400 ) { } } return totalList ; } ; getRewards = async ( stash ) => { const url = this . baseV2Url + this . rewardsEndpoint ; const totalList = [ ] ; let page = 0 ; let shouldContinue = true ; while ( shouldContinue ) { const data = { row : 100 , page : page , address : stash , is_stash : true } ; const res = await axios . post ( url , data , { headers : this . defaultHeader } ) ; const status = res . status ; if ( status == 200 ) { const { count , list } = res . data . data ; if ( ! list ) { shouldContinue = false ; } else { const rewardList = list . map ( ( reward ) => { const { era , stash , account , validator_stash , amount , block_timestamp , event_index , module_id , event_id , slash_kton , extrinsic_index , } = reward ; return { era , stash , rewardDestination : account , validatorStash : validator_stash , amount : parseFloat ( amount ) / this . denom , blockTimestamp : block_timestamp , blockNumber : event_index ? event_index . split ( "-" ) [ 0 ] : 0 , slashKTon : slash_kton , } ; } ) ; page ++ ; totalList . push ( ... rewardList ) ; await sleep ( 2000 ) ; } } else if ( status == 400 ) { } } return totalList ; } ; }