diff --git a/src/libs/client.ts b/src/libs/client.ts index 926176a2..b3f78029 100644 --- a/src/libs/client.ts +++ b/src/libs/client.ts @@ -193,13 +193,17 @@ export class CosmosRestClient extends BaseRestClient { async getBaseNodeInfo() { return this.request(this.registry.base_tendermint_node_info, {}); } - async getBaseValidatorsetAt(height: string | number) { + async getBaseValidatorsetAt(height: string | number, offset: number) { + console.log("offset", offset) + const query = `?pagination.limit=100&pagination.offset=${offset}` return this.request(this.registry.base_tendermint_validatorsets_height, { height, - }); + }, query); } - async getBaseValidatorsetLatest() { - return this.request(this.registry.base_tendermint_validatorsets_latest, {}); + async getBaseValidatorsetLatest(offset: number) { + console.log(offset) + const query = `?pagination.limit=100&pagination.offset=${offset}` + return this.request(this.registry.base_tendermint_validatorsets_latest, {}, query); } // tx async getTxsBySender(sender: string) { diff --git a/src/modules/[chain]/staking/index.vue b/src/modules/[chain]/staking/index.vue index a6938922..a3547e99 100644 --- a/src/modules/[chain]/staking/index.vue +++ b/src/modules/[chain]/staking/index.vue @@ -16,45 +16,69 @@ const tab = ref('active'); const unbondList = ref([] as Validator[]); const base = useBaseStore(); onMounted(() => { - fetchChange(0); staking.fetchInacitveValdiators().then((x) => { unbondList.value = x; }); }); -function fetchChange(offset: number) { - const base = useBaseStore(); - const diff = 86400000 / base.blocktime; - // base.fetchAbciInfo().then(h => { - // // console.log('block:', h) - // base.fetchValidatorByHeight(h.lastBlockHeight, offset).then(x => { - // x.validators.forEach(v => { - // if(v.pubkey) latest.value[pubkeyToAddress(v.pubkey.algorithm, v.pubkey.data)] = Number(v.votingPower) - // }) - // }) - // const height = Number(h.lastBlockHeight) - diff - // base.fetchValidatorByHeight(height > 0 ? height : 1, offset).then(old => { - // old.validators.forEach(v => { - // if(v.pubkey) yesterday.value[pubkeyToAddress(v.pubkey.algorithm, v.pubkey.data)] = Number(v.votingPower) - // }) - // // console.log(Object.keys(yesterday.value).map(x => x.toUpperCase())) - // }) - // }) +async function fetchChange() { + console.log('fetch changes') + let page = 0 + + let height = Number(base.latest?.block?.header?.height || 0) + if (height > 14400) { + height -= 14400 + } else { + height = 1 + } + // voting power in 24h ago + while(page < staking.validators.length && height > 0) { + await base.fetchValidatorByHeight(height, page).then(x => { + x.validators.forEach(v => { + yesterday.value[v.pub_key.key] = Number(v.voting_power) + }) + }) + page += 100 + } + + page = 0 + // voting power for now + while(page < staking.validators.length) { + await base.fetchLatestValidators(page).then(x => { + x.validators.forEach(v => { + latest.value[v.pub_key.key] = Number(v.voting_power) + }) + }) + page += 100 + } } +fetchChange(); + +const changes = computed(() => { + const changes = {} as Record + Object.keys(latest.value).forEach(k => { + const l = latest.value[k] || 0 + const y = yesterday.value[k] || 0 + changes[k] = l - y + }) + return changes +}) + const change24 = (key: Key) => { // console.log('hex key:', consensusPubkeyToHexAddress(key)) const txt = key.key; - const n: number = latest.value[txt]; - const o: number = yesterday.value[txt]; - // console.log( txt, n, o) - return n > 0 && o > 0 ? n - o : 0; + // const n: number = latest.value[txt]; + // const o: number = yesterday.value[txt]; + // // console.log( txt, n, o) + // return n > 0 && o > 0 ? n - o : 0; + return changes.value[txt] }; const change24Text = (key?: Key) => { if (!key) return ''; const v = change24(key); - return v !== 0 ? format.numberAndSign(v) : ''; + return v !== 0 ? format.showChanges(v) : ''; }; const change24Color = (key?: Key) => { diff --git a/src/stores/useBaseStore.ts b/src/stores/useBaseStore.ts index c66acb95..ee7cc1eb 100644 --- a/src/stores/useBaseStore.ts +++ b/src/stores/useBaseStore.ts @@ -56,10 +56,10 @@ export const useBaseStore = defineStore('baseStore', { }, async fetchValidatorByHeight(height?: number, offset = 0) { - return this.blockchain.rpc.getBaseValidatorsetAt(String(height)); + return this.blockchain.rpc.getBaseValidatorsetAt(String(height), offset); }, async fetchLatestValidators(offset = 0) { - return this.blockchain.rpc.getBaseValidatorsetLatest(); + return this.blockchain.rpc.getBaseValidatorsetLatest(offset); }, async fetchBlock(height?: number) { return this.blockchain.rpc.getBaseBlockAt(String(height)); diff --git a/src/stores/useFormatter.ts b/src/stores/useFormatter.ts index 2e8fa8b7..065710ed 100644 --- a/src/stores/useFormatter.ts +++ b/src/stores/useFormatter.ts @@ -167,6 +167,7 @@ export const useFormatter = defineStore('formatter', { ); return validator?.description?.moniker; }, + // find validator by operator address validatorFromBech32(address: string) { if (!address) return address; const validator = this.staking.validators.find(