diff --git a/src/libs/utils.ts b/src/libs/utils.ts index a22c8bfd..d7548afa 100644 --- a/src/libs/utils.ts +++ b/src/libs/utils.ts @@ -14,6 +14,22 @@ export const percent = (num: number) => { return parseFloat((num * 100).toFixed(2)); }; +export function stringToUint8Array(str: string) { + const arr = []; + for (let i = 0, j = str.length; i < j; ++i) { + arr.push(str.charCodeAt(i)); + } + return new Uint8Array(arr); +} + +export function uint8ArrayToString(arr: Uint8Array) { + let str = ''; + for (let i = 0, j = arr.length; i < j; ++i) { + str += String.fromCharCode(arr[i]); + } + return str; +} + const COUNT_ABBRS = [ '', 'K', diff --git a/src/modules/[chain]/staking/[validator].vue b/src/modules/[chain]/staking/[validator].vue index 071d7ddd..f33652f3 100644 --- a/src/modules/[chain]/staking/[validator].vue +++ b/src/modules/[chain]/staking/[validator].vue @@ -17,6 +17,8 @@ import { } from '@/libs'; import { PageRequest, type Coin, type Delegation, type PaginatedDelegations, type PaginatedTxs, type Validator } from '@/types'; import PaginationBar from '@/components/PaginationBar.vue'; +import { fromBase64, toBase64 } from '@cosmjs/encoding'; +import { stringToUint8Array, uint8ArrayToString } from '@/libs/utils'; const props = defineProps(['validator', 'chain']); @@ -189,10 +191,56 @@ const tipMsg = computed(() => { function pageload(p: number) { page.setPage(p); + page.limit = 10; + blockchain.rpc.getStakingValidatorsDelegations(validator, page).then(res => { delegations.value = res }) } + +const events = ref({} as PaginatedTxs) + +enum EventType { + Delegate = 'delegate', + Unbond = 'unbond', +} + +const selectedEventType = ref(EventType.Unbond) + +function loadPowerEvents(p: number, type: EventType) { + selectedEventType.value = type + page.setPage(p); + page.setPageSize(5); + blockchain.rpc.getTxs("?order_by=2&events={type}.validator='{validator}'", { type: selectedEventType.value, validator }, page).then(res => { + events.value = res + }) +} + +function pagePowerEvents(page: number) { + loadPowerEvents(page, selectedEventType.value) +} + + +function mapEvents(events: {type: string, attributes: {key: string, value: string}[]}[]) { + const attributes = events.filter(x => x.type=== selectedEventType.value).filter(x => x.attributes.findIndex(attr => attr.value === toBase64(stringToUint8Array(validator))) > -1).map(x => { + // check if attributes need to decode + if(x.attributes.findIndex(a => a.value === `amount` || a.value === 'delegator_address') > -1) return x.attributes + const output = {} as {[key: string]: string } + x.attributes.forEach(attr => { + output[uint8ArrayToString(fromBase64(attr.key))] = uint8ArrayToString(fromBase64(attr.value)) + }) + return output + }) + + return attributes + +} + +function mapDelegators(messages: any[]) { + if(!messages) return [] + return Array.from(new Set(messages.map(x => x.delegator_address || x.grantee))) +} +