remove Buffer

This commit is contained in:
Pham Tu
2024-01-17 10:12:04 +07:00
parent 3bc6a3b660
commit a1fbe82a29
6 changed files with 94 additions and 60 deletions
+1
View File
@@ -13,6 +13,7 @@ const props = defineProps({
}>,
},
});
console.log(props);
const total = computed(() => props.pool?.bonded_tokens);
const format = useFormatter();
const yes = computed(() =>
+8 -14
View File
@@ -46,6 +46,8 @@ 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';
import { fromBase64 } from '@cosmjs/encoding';
export class BaseRestClient<R extends AbstractRegistry> {
endpoint: string;
@@ -176,9 +178,7 @@ export class CosmosRestClient extends BaseRestClient<RequestRegistry> {
// if (!page) page = new PageRequest();
// const query = `?${page.toQueryString()}`;
// return this.request(this.registry.bank_supply, {}, query);
const paginationKey = page?.key
? Buffer.from(page.key, 'base64')
: undefined;
const paginationKey = page?.key ? fromBase64(page.key) : undefined;
const res = await this.queryClient.bank.totalSupply(paginationKey);
console.log(res);
return res;
@@ -291,15 +291,13 @@ export class CosmosRestClient extends BaseRestClient<RequestRegistry> {
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()}`;
// return this.request(this.registry.gov_proposals, { status }, query);
const paginationKey = page?.key
? Buffer.from(page.key, 'base64')
: undefined;
// @ts-ignore
const paginationKey = page?.key ? fromBase64(page.key) : undefined;
const res = this.queryClient.gov.proposals(status, '', '', paginationKey);
console.log(res);
return res;
@@ -344,9 +342,7 @@ export class CosmosRestClient extends BaseRestClient<RequestRegistry> {
// query
// );
const paginationKey = page?.key
? Buffer.from(page.key, 'base64')
: undefined;
const paginationKey = page?.key ? fromBase64(page.key) : undefined;
const res = await this.queryClient.gov.votes(proposal_id, paginationKey);
console.log(res);
return res;
@@ -626,9 +622,7 @@ export class CosmosRestClient extends BaseRestClient<RequestRegistry> {
// {},
// query
// );
const paginationKey = page?.key
? Buffer.from(page.key, 'base64')
: undefined;
const paginationKey = page?.key ? fromBase64(page.key) : undefined;
const res = await this.queryClient.ibc.connection.connections(
paginationKey
);
+57 -28
View File
@@ -22,7 +22,6 @@ import Countdown from '@/components/Countdown.vue';
import PaginationBar from '@/components/PaginationBar.vue';
import { fromBech32, toHex } from '@cosmjs/encoding';
const props = defineProps(['proposal_id', 'chain']);
const proposal = ref({} as GovProposal);
const format = useFormatter();
@@ -36,23 +35,23 @@ store.fetchProposal(props.proposal_id).then((res) => {
// when status under the voting, final_tally_result are no data, should request fetchTally
if (res.proposal?.status === 'PROPOSAL_STATUS_VOTING_PERIOD') {
store.fetchTally(props.proposal_id).then((tallRes) => {
proposalDetail.final_tally_result = tallRes?.tally;
proposalDetail.final_tally_result = tallRes.tally;
});
}
proposal.value = proposalDetail;
// load origin params if the proposal is param change
if(proposalDetail.content?.changes) {
proposalDetail.content?.changes.forEach((item) => {
chainStore.rpc.getParams(item.subspace, item.key).then((res) => {
if(proposal.value.content && res.param) {
if(proposal.value.content.current){
proposal.value.content.current.push(res.param);
} else {
proposal.value.content.current = [res.param];
};
if (proposalDetail.content?.changes) {
proposalDetail.content?.changes.forEach((item) => {
chainStore.rpc.getParams(item.subspace, item.key).then((res) => {
if (proposal.value.content && res.param) {
if (proposal.value.content.current) {
proposal.value.content.current.push(res.param);
} else {
proposal.value.content.current = [res.param];
}
})
})
}
});
});
}
});
@@ -186,17 +185,27 @@ function pageload(p: number) {
});
}
function metaItem(metadata: string|undefined): { title: string; summary: string } {
return metadata ? JSON.parse(metadata) : {}
function metaItem(metadata: string | undefined): {
title: string;
summary: string;
} {
return metadata ? JSON.parse(metadata) : {};
}
</script>
<template>
<div>
<div class="bg-base-100 px-4 pt-3 pb-4 rounded mb-4 shadow">
<h2 class="card-title flex flex-col md:!justify-between md:!flex-row mb-2">
<h2
class="card-title flex flex-col md:!justify-between md:!flex-row mb-2"
>
<p class="truncate w-full">
{{ proposal_id }}. {{ proposal.title || proposal.content?.title || metaItem(proposal?.metadata)?.title }}
{{ proposal_id }}.
{{
proposal.title ||
proposal.content?.title ||
metaItem(proposal?.metadata)?.title
}}
</p>
<div
class="badge badge-ghost"
@@ -214,9 +223,18 @@ function metaItem(metadata: string|undefined): { title: string; summary: string
<div class="">
<ObjectElement :value="proposal.content" />
</div>
<div v-if="proposal.summary && !proposal.content?.description || metaItem(proposal?.metadata)?.summary ">
<div
v-if="
(proposal.summary && !proposal.content?.description) ||
metaItem(proposal?.metadata)?.summary
"
>
<MdEditor
:model-value="format.multiLine(proposal.summary || metaItem(proposal?.metadata)?.summary)"
:model-value="
format.multiLine(
proposal.summary || metaItem(proposal?.metadata)?.summary
)
"
previewOnly
class="md-editor-recover"
></MdEditor>
@@ -272,7 +290,8 @@ function metaItem(metadata: string|undefined): { title: string; summary: string
<div class="flex items-center mb-4 mt-2">
<div class="w-2 h-2 rounded-full bg-error mr-3"></div>
<div class="text-base flex-1 text-main">
{{ $t('gov.submit_at') }}: {{ format.toDay(proposal.submit_time) }}
{{ $t('gov.submit_at') }}:
{{ format.toDay(proposal.submit_time) }}
</div>
<div class="text-sm">{{ shortTime(proposal.submit_time) }}</div>
</div>
@@ -302,7 +321,8 @@ function metaItem(metadata: string|undefined): { title: string; summary: string
<div class="flex items-center">
<div class="w-2 h-2 rounded-full bg-yes mr-3"></div>
<div class="text-base flex-1 text-main">
{{ $t('gov.vote_start_from') }} {{ format.toDay(proposal.voting_start_time) }}
{{ $t('gov.vote_start_from') }}
{{ format.toDay(proposal.voting_start_time) }}
</div>
<div class="text-sm">
{{ shortTime(proposal.voting_start_time) }}
@@ -316,14 +336,16 @@ function metaItem(metadata: string|undefined): { title: string; summary: string
<div class="flex items-center mb-1">
<div class="w-2 h-2 rounded-full bg-success mr-3"></div>
<div class="text-base flex-1 text-main">
{{ $t('gov.vote_end') }} {{ format.toDay(proposal.voting_end_time) }}
{{ $t('gov.vote_end') }}
{{ format.toDay(proposal.voting_end_time) }}
</div>
<div class="text-sm">
{{ shortTime(proposal.voting_end_time) }}
</div>
</div>
<div class="pl-5 text-sm">
{{ $t('gov.current_status') }}: {{ $t(`gov.proposal_statuses.${proposal.status}`) }}
{{ $t('gov.current_status') }}:
{{ $t(`gov.proposal_statuses.${proposal.status}`) }}
</div>
</div>
@@ -373,11 +395,18 @@ function metaItem(metadata: string|undefined): { title: string; summary: string
>
{{ String(item.option).replace('VOTE_OPTION_', '') }}
</td>
<td
v-if="item.options"
class="py-2 text-sm"
>
{{ item.options.map(x => `${x.option.replace('VOTE_OPTION_', '')}:${format.percent(x.weight)}`).join(', ') }}
<td v-if="item.options" class="py-2 text-sm">
{{
item.options
.map(
(x) =>
`${x.option.replace(
'VOTE_OPTION_',
''
)}:${format.percent(x.weight)}`
)
.join(', ')
}}
</td>
</tr>
</tbody>
+2 -3
View File
@@ -4,7 +4,7 @@ import { decodeTxRaw, type DecodedTxRaw } from '@cosmjs/proto-signing';
import dayjs from 'dayjs';
import type { Block } from '@/types';
import { hashTx } from '@/libs';
import { fromBase64 } from '@cosmjs/encoding';
import { fromBase64, toBase64 } from '@cosmjs/encoding';
import { useRouter } from 'vue-router';
import type { BlockResponse } from '@cosmjs/tendermint-rpc';
@@ -101,8 +101,7 @@ export const useBaseStore = defineStore('baseStore', {
if (
this.recents.findIndex(
(x) =>
Buffer.from(x?.blockId?.hash).toString('base64') ===
Buffer.from(this.latest?.blockId?.hash).toString('base64')
toBase64(x?.blockId?.hash) === toBase64(this.latest?.blockId?.hash)
) === -1
) {
if (this.recents.length >= 50) {
+25 -12
View File
@@ -1,9 +1,15 @@
import { defineStore } from 'pinia';
import { decodeTxRaw } from '@cosmjs/proto-signing';
import { useBlockchain } from './useBlockchain';
import type { PageRequest, PaginatedProposals } from '@/types';
import { LoadingStatus } from './useDashboard';
import { useWalletStore } from './useWalletStore';
import { reactive } from 'vue';
import { toBase64 } from '@cosmjs/encoding';
import {
TextProposal,
type ProposalStatus,
} from 'cosmjs-types/cosmos/gov/v1beta1/gov';
export const useGovStore = defineStore('govStore', {
state: () => {
@@ -29,9 +35,9 @@ export const useGovStore = defineStore('govStore', {
initial() {
this.$reset();
this.fetchParams();
this.fetchProposals('2');
this.fetchProposals(2);
},
async fetchProposals(status: string, pagination?: PageRequest) {
async fetchProposals(status: ProposalStatus, pagination?: PageRequest) {
//if (!this.loading[status]) {
this.loading[status] = LoadingStatus.Loading;
const proposals = reactive(
@@ -41,16 +47,20 @@ export const useGovStore = defineStore('govStore', {
//filter spam proposals
if (proposals?.proposals) {
proposals.proposals = proposals.proposals.filter((item) => {
const title = item.content?.title || item.title || '';
return title.toLowerCase().indexOf('airdrop') === -1;
if (item.content) {
const proposal = TextProposal.decode(item.content.value);
Object.assign(item.content, proposal);
return proposal.title.toLowerCase().indexOf('airdrop') === -1;
}
return true;
});
}
if (status === '2') {
if (status === 2) {
proposals?.proposals?.forEach((item) => {
// this.fetchTally(item.proposalId.toString()).then((res) => {
// item.finalTallyResult = res?.tally;
// });
this.fetchTally(item.proposalId.toString()).then((res) => {
item.finalTallyResult = res?.tally;
});
if (this.walletstore.currentAddress) {
try {
this.fetchProposalVotesVoter(
@@ -87,16 +97,19 @@ export const useGovStore = defineStore('govStore', {
return await this.blockchain.rpc.getGovProposalTally(proposalId);
},
async fetchProposal(proposalId: string) {
return this.blockchain.rpc.getGovProposal(proposalId);
return await this.blockchain.rpc.getGovProposal(proposalId);
},
async fetchProposalDeposits(proposalId: string) {
return this.blockchain.rpc.getGovProposalDeposits(proposalId);
return await this.blockchain.rpc.getGovProposalDeposits(proposalId);
},
async fetchProposalVotes(proposalId: string, page?: PageRequest) {
return this.blockchain.rpc.getGovProposalVotes(proposalId, page);
return await this.blockchain.rpc.getGovProposalVotes(proposalId, page);
},
async fetchProposalVotesVoter(proposalId: string, voter: string) {
return this.blockchain.rpc.getGovProposalVotesVoter(proposalId, voter);
return await this.blockchain.rpc.getGovProposalVotesVoter(
proposalId,
voter
);
},
},
});
+1 -3
View File
@@ -105,9 +105,7 @@ export const useStakingStore = defineStore('stakingStore', {
// console.log(signatures)
const key = toBase64(fromHex(valconsToBase64(validatorAddr)));
const exists = signatures.findIndex(
(x) =>
x.validatorAddress &&
Buffer.from(x.validatorAddress).toString('base64') === key
(x) => x.validatorAddress && toBase64(x.validatorAddress) === key
);
if (exists < 0) {
const client = CosmosRestClient.newDefault(