try/catch for rest

This commit is contained in:
Pham Tu 2024-01-22 11:48:12 +07:00
parent 59ce9de395
commit 1649ffaad1
No known key found for this signature in database
GPG Key ID: 7460FD99133ADA1C
4 changed files with 58 additions and 37 deletions

View File

@ -1,5 +1,5 @@
import { DEFAULT, fetchData } from '@/libs';
import { PageRequest } from '@/types';
import type { PageRequest } from '@/types';
import {
setupWasmExtension,
type WasmExtension,
@ -380,9 +380,13 @@ export class CosmosRestClient extends BaseRestClient<RequestRegistry> {
return res;
}
async getBankSupplyByDenom(denom: string) {
const res = await this.queryClient.bank.supplyOf(denom);
console.log(res);
return res;
try {
const res = await this.queryClient.bank.supplyOf(denom);
console.log(res);
return res;
} catch (ex) {
console.log(ex);
}
}
// Distribution Module
async getDistributionParams() {
@ -489,31 +493,35 @@ export class CosmosRestClient extends BaseRestClient<RequestRegistry> {
status: ProposalStatus,
page?: PageRequest
): Promise<ExtraQueryProposalsResponse> {
if (!page) page = new PageRequest();
page.reverse = true;
try {
// v1 for sdk version > 0.45
if (semver.gt(this.version, DEFAULT_SDK_VERSION)) {
const resV1 = await this.queryClient.extra.proposalsV1(status, page);
return {
// @ts-ignore
proposals: resV1.proposals.map((v) => {
return {
...v,
proposalId: v.id,
};
}),
pagination: resV1.pagination,
};
} else {
const res = await this.queryClient.extra.proposals(status, page);
res?.proposals.forEach((item) => {
if (item.content) {
Object.assign(item, TextProposal.decode(item.content.value));
}
});
// v1 for sdk version > 0.45
if (semver.gt(this.version, DEFAULT_SDK_VERSION)) {
const resV1 = await this.queryClient.extra.proposalsV1(status, page);
return res as ExtraQueryProposalsResponse;
}
} catch (ex) {
console.log(ex);
return {
// @ts-ignore
proposals: resV1.proposals.map((v) => {
return {
...v,
proposalId: v.id,
};
}),
pagination: resV1.pagination,
proposals: [],
};
} else {
const res = await this.queryClient.extra.proposals(status, page);
res?.proposals.forEach((item) => {
if (item.content) {
Object.assign(item, TextProposal.decode(item.content.value));
}
});
return res as ExtraQueryProposalsResponse;
}
}
async getGovProposal(proposal_id: string) {
@ -613,9 +621,13 @@ export class CosmosRestClient extends BaseRestClient<RequestRegistry> {
return res;
}
async getStakingParams() {
const res = await this.queryClient.staking.params();
console.log(res);
return res;
try {
const res = await this.queryClient.staking.params();
console.log(res);
return res;
} catch (ex) {
console.log(ex);
}
// return this.request(this.registry.staking_params, {});
}
async getStakingPool() {
@ -630,9 +642,13 @@ export class CosmosRestClient extends BaseRestClient<RequestRegistry> {
// 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;
try {
const res = await this.queryClient.staking.validators(status);
console.log(status, res);
return res;
} catch (ex) {
console.log(ex);
}
// return this.request(this.registry.staking_validators, { status, limit });
}
async getStakingValidator(validator_addr: string) {

View File

@ -14,6 +14,7 @@ import type { Block, Commit } from '@/types';
import { consensusPubkeyToHexAddress, valconsToBase64 } from '@/libs';
import type { SigningInfo } from '@/types/slashing';
import { CosmosRestClient } from '@/libs/client';
import type { ValidatorSigningInfo } from 'cosmjs-types/cosmos/slashing/v1beta1/slashing';
const props = defineProps(['chain']);
@ -28,7 +29,7 @@ const local = ref(
{ name: string; address: string }[]
>
);
const signingInfo = ref({} as Record<string, SigningInfo[]>);
const signingInfo = ref({} as Record<string, ValidatorSigningInfo[]>);
const selected = ref([] as string[]);
const selectChain = ref(chainStore.chainName);
const validators = ref(stakingStore.validators);
@ -93,7 +94,7 @@ function add() {
const newList = [] as { name: string; address: string }[];
selected.value.forEach((x) => {
const validator = validators.value.find(
(v) => consensusPubkeyToHexAddress(v.consensus_pubkey) === x
(v) => consensusPubkeyToHexAddress(v.consensusPubkey) === x
);
if (validator)
newList.push({
@ -115,6 +116,7 @@ async function changeChain() {
const client = CosmosRestClient.newDefault(endpoint);
const x = await client.getStakingValidators('BOND_STATUS_BONDED');
if (!x) return;
validators.value = x.validators;
const vals = local.value[selectChain.value];
@ -283,7 +285,7 @@ function color(v: string) {
<tbody>
<tr v-for="(v, i) in filterValidators">
<td>
<label :for="v.operator_address"
<label :for="v.operatorAddress"
><div class="w-full">
{{ i + 1 }}. {{ v.description.moniker }}
</div></label
@ -291,11 +293,11 @@ function color(v: string) {
</td>
<td>
<input
:id="v.operator_address"
:id="v.operatorAddress"
v-model="selected"
class="checkbox"
type="checkbox"
:value="consensusPubkeyToHexAddress(v.consensus_pubkey)"
:value="consensusPubkeyToHexAddress(v.consensusPubkey)"
/>
</td>
</tr>

View File

@ -109,6 +109,7 @@ export const useParamStore = defineStore('paramstore', {
},
async handleStakingParams() {
const res = await this.getStakingParams();
if (!res) return;
const bond_denom = res?.params.bondDenom;
this.staking.items = Object.entries(res.params)
.map(([key, value]) => ({ subtitle: key, value: value }))

View File

@ -169,6 +169,7 @@ export const useStakingStore = defineStore('stakingStore', {
);
// provider validators
const validatorsRes = await client.getStakingValidators(status);
if (!validatorsRes) return [];
const proVals = validatorsRes.validators.sort(
(a, b) => Number(b.delegatorShares) - Number(a.delegatorShares)
);
@ -182,6 +183,7 @@ export const useStakingStore = defineStore('stakingStore', {
const validatorsRes = await this.blockchain.rpc?.getStakingValidators(
status
);
if (!validatorsRes) return [];
const vals = validatorsRes.validators.sort(
(a, b) => Number(b.delegatorShares) - Number(a.delegatorShares)
);