From 85ccf923305da62ca2ce3635eb7134bfdbed0649 Mon Sep 17 00:00:00 2001 From: ducphamle2 Date: Tue, 16 Jan 2024 18:02:30 -0800 Subject: [PATCH] chore: update incompatible data --- src/libs/client.ts | 41 +++++++++++++++---------- src/modules/[chain]/block/block.ts | 9 +++--- src/stores/useBaseStore.ts | 20 +++++++++--- src/stores/useGovStore.ts | 49 +++++++++++++++--------------- src/stores/useParamsStore.ts | 2 +- 5 files changed, 71 insertions(+), 50 deletions(-) diff --git a/src/libs/client.ts b/src/libs/client.ts index 0b8277a5..bdea5c27 100644 --- a/src/libs/client.ts +++ b/src/libs/client.ts @@ -46,6 +46,7 @@ import type { SlashingExtension, } from '@cosmjs/stargate/build/modules'; import type { BondStatusString } from '@cosmjs/stargate/build/modules/staking/queries'; +import type { ProposalStatus } from 'cosmjs-types/cosmos/gov/v1beta1/gov'; export class BaseRestClient { endpoint: string; @@ -186,11 +187,11 @@ export class CosmosRestClient extends BaseRestClient { async getBankSupplyByDenom(denom: string) { let supply; try { - supply = this.queryClient?.bank.supplyOf(denom); + supply = await this.queryClient.bank.supplyOf(denom); console.log(supply); } catch (err) { // will move this to sdk version profile later - console.log(err); + console.log('err getting bank supply: ', err); } return supply; } @@ -291,7 +292,7 @@ export class CosmosRestClient extends BaseRestClient { console.log(res); return res; } - async getGovProposals(status: string, page?: PageRequest) { + async getGovProposals(status: ProposalStatus, page?: PageRequest) { if (!page) page = new PageRequest(); page.reverse = true; // const query = `?proposal_status={status}&${page.toQueryString()}`; @@ -300,17 +301,20 @@ export class CosmosRestClient extends BaseRestClient { ? Buffer.from(page.key, 'base64') : undefined; // @ts-ignore - const res = this.queryClient.gov.proposals(status, '', '', paginationKey); + const res = await this.queryClient.gov.proposals( + status, + '', + '', + paginationKey + ); console.log(res); return res; } async getGovProposal(proposal_id: string) { - return this.request(this.registry.gov_proposals_proposal_id, { - proposal_id, - }); + return this.queryClient.gov.proposal(proposal_id); } async getGovProposalDeposits(proposal_id: string) { - return this.request(this.registry.gov_proposals_deposits, { proposal_id }); + return this.queryClient.gov.deposits(proposal_id); } async getGovProposalTally(proposal_id: string) { const res = await this.queryClient.gov.tally(proposal_id); @@ -411,16 +415,21 @@ export class CosmosRestClient extends BaseRestClient { // return this.request(this.registry.staking_params, {}); } async getStakingPool() { - const res = await this.queryClient.staking.pool(); - console.log(res); - return res; - // return this.request(this.registry.staking_pool, {}); + // try { + // // const res = await this.queryClient.staking.pool(); + // const res = await this.request(this.registry.staking_pool, {}); + // console.log(res); + // return res; + // } catch (error) { + // console.log('error staking pool: ', error); + // } + return this.request(this.registry.staking_pool, {}); } async getStakingValidators(status: BondStatusString, limit = 200) { - const res = await this.queryClient.staking.validators(status); - console.log(status, res); - return res; - // return this.request(this.registry.staking_validators, { status, limit }); + // const res = await this.queryClient.staking.validators(status); + // console.log(status, res); + // return res; + return this.request(this.registry.staking_validators, { status, limit }); } async getStakingValidator(validator_addr: string) { // return this.request(this.registry.staking_validators_address, { diff --git a/src/modules/[chain]/block/block.ts b/src/modules/[chain]/block/block.ts index ead62cb3..11761ec7 100644 --- a/src/modules/[chain]/block/block.ts +++ b/src/modules/[chain]/block/block.ts @@ -3,13 +3,14 @@ import { decodeTxRaw, type DecodedTxRaw } from '@cosmjs/proto-signing'; import { useBlockchain } from '@/stores'; import { hashTx } from '@/libs'; import type { Block } from '@/types'; +import type { BlockResponse } from '@cosmjs/tendermint-rpc'; export const useBlockModule = defineStore('blockModule', { state: () => { return { - latest: {} as Block, - current: {} as Block, - recents: [] as Block[], + latest: {} as BlockResponse, + current: {} as BlockResponse, + recents: [] as BlockResponse[], }; }, getters: { @@ -23,7 +24,7 @@ export const useBlockModule = defineStore('blockModule', { txsInRecents() { const txs = [] as { hash: string; tx: DecodedTxRaw }[]; this.recents.forEach((x) => - x.block?.data?.txs.forEach((tx: Uint8Array) => { + x.block?.txs.forEach((tx: Uint8Array) => { if (tx) { try { txs.push({ diff --git a/src/stores/useBaseStore.ts b/src/stores/useBaseStore.ts index 3e5c72d9..b855e467 100644 --- a/src/stores/useBaseStore.ts +++ b/src/stores/useBaseStore.ts @@ -8,6 +8,18 @@ import { fromBase64 } from '@cosmjs/encoding'; import { useRouter } from 'vue-router'; import type { BlockResponse } from '@cosmjs/tendermint-rpc'; +const compareHashEqual = ( + firstHash: Uint8Array, + secondHash: Uint8Array +): boolean => { + for (let i = 0; i < firstHash.length; i++) { + if (firstHash[i] !== secondHash[i]) { + return false; + } + } + return true; +}; + export const useBaseStore = defineStore('baseStore', { state: () => { return { @@ -99,11 +111,9 @@ export const useBaseStore = defineStore('baseStore', { } //check if the block exists in recents if ( - this.recents.findIndex( - (x) => - Buffer.from(x?.blockId?.hash).toString('base64') === - Buffer.from(this.latest?.blockId?.hash).toString('base64') - ) === -1 + this.recents.findIndex((x) => { + return compareHashEqual(x.blockId.hash, this.latest.blockId.hash); + }) === -1 ) { if (this.recents.length >= 50) { this.recents.shift(); diff --git a/src/stores/useGovStore.ts b/src/stores/useGovStore.ts index 9a9f386d..5ec00690 100644 --- a/src/stores/useGovStore.ts +++ b/src/stores/useGovStore.ts @@ -4,6 +4,7 @@ import type { PageRequest, PaginatedProposals } from '@/types'; import { LoadingStatus } from './useDashboard'; import { useWalletStore } from './useWalletStore'; import { reactive } from 'vue'; +import { Proposal, ProposalStatus } from 'cosmjs-types/cosmos/gov/v1beta1/gov'; export const useGovStore = defineStore('govStore', { state: () => { @@ -13,7 +14,7 @@ export const useGovStore = defineStore('govStore', { voting: {}, tally: {}, }, - proposals: {} as Record, + proposals: {} as Record, loading: {} as Record, }; }, @@ -29,9 +30,9 @@ export const useGovStore = defineStore('govStore', { initial() { this.$reset(); this.fetchParams(); - this.fetchProposals('2'); + this.fetchProposals(ProposalStatus.PROPOSAL_STATUS_VOTING_PERIOD); }, - async fetchProposals(status: string, pagination?: PageRequest) { + async fetchProposals(status: ProposalStatus, pagination?: PageRequest) { //if (!this.loading[status]) { this.loading[status] = LoadingStatus.Loading; const proposals = reactive( @@ -46,35 +47,35 @@ export const useGovStore = defineStore('govStore', { }); } - if (status === '2') { + if (status === ProposalStatus.PROPOSAL_STATUS_VOTING_PERIOD) { proposals?.proposals?.forEach((item) => { // this.fetchTally(item.proposalId.toString()).then((res) => { // item.finalTallyResult = res?.tally; // }); - if (this.walletstore.currentAddress) { - try { - this.fetchProposalVotesVoter( - item.proposalId.toString(), - this.walletstore.currentAddress - ) - .then((res) => { - item.status = res?.vote?.option || 'VOTE_OPTION_NO_WITH_VETO'; - // 'No With Veto'; - }) - .catch((reject) => { - item.status = 'VOTE_OPTION_NO_WITH_VETO'; - }); - } catch (error) { - item.status = 'VOTE_OPTION_NO_WITH_VETO'; - } - } else { - item.status = 'VOTE_OPTION_NO_WITH_VETO'; - } + // if (this.walletstore.currentAddress) { + // try { + // this.fetchProposalVotesVoter( + // item.proposalId.toString(), + // this.walletstore.currentAddress + // ) + // .then((res) => { + // item.status = res.vote.options[res.vote.options.length].option || 'VOTE_OPTION_NO_WITH_VETO'; + // // 'No With Veto'; + // }) + // .catch((reject) => { + // item.status = 'VOTE_OPTION_NO_WITH_VETO'; + // }); + // } catch (error) { + // item.status = 'VOTE_OPTION_NO_WITH_VETO'; + // } + // } else { + // item.status = 'VOTE_OPTION_NO_WITH_VETO'; + // } }); } this.loading[status] = LoadingStatus.Loaded; - this.proposals[status] = proposals; + this.proposals[status] = proposals.proposals; //} return this.proposals[status]; }, diff --git a/src/stores/useParamsStore.ts b/src/stores/useParamsStore.ts index d5cf2734..bc073bd5 100644 --- a/src/stores/useParamsStore.ts +++ b/src/stores/useParamsStore.ts @@ -102,7 +102,7 @@ export const useParamStore = defineStore('paramstore', { // this.syncing = false // } // this.latestTime = toDay(res.block.header.time, 'long') - this.latestTime = res.block.header.time; + this.latestTime = res.block.header.time.toDateString(); } catch (error) { console.warn(error); }