random pick endpoints

This commit is contained in:
liangping 2023-11-10 15:34:06 +08:00
parent 586f819d41
commit ce7abefa84
3 changed files with 19 additions and 9 deletions

View File

@ -41,6 +41,7 @@ Object.values(conf.value).forEach((imported) => {
// continue only if the page is living
if (imported[i].endpoint) {
loadBalances(
imported[i].chainName,
imported[i].endpoint || '',
imported[i].address
).finally(() => resolve());
@ -170,15 +171,17 @@ async function addAddress(acc: AccountEntry) {
}
if (acc.endpoint) {
loadBalances(acc.endpoint, acc.address);
loadBalances(acc.chainName, acc.endpoint, acc.address);
}
localStorage.setItem('imported-addresses', JSON.stringify(conf.value));
}
// load balances for an address
async function loadBalances(endpoint: string, address: string) {
const client = CosmosRestClient.newDefault(endpoint);
async function loadBalances(chainName: string, endpoint: string, address: string) {
const endpointObj = chainStore.randomEndpoint(chainName)
const client = CosmosRestClient.newDefault(endpointObj?.address || endpoint);
await client.getBankBalances(address).then((res) => {
balances.value[address] = res.balances.filter((x) => x.denom.length < 10);
});

View File

@ -4,7 +4,7 @@ import type { Coin, Delegation } from '@/types';
import { ref, watchEffect } from 'vue';
import type { AccountEntry } from './utils';
import { computed } from 'vue';
import { useBaseStore, useFormatter } from '@/stores';
import { useBaseStore, useBlockchain, useFormatter } from '@/stores';
import DonutChart from '@/components/charts/DonutChart.vue';
import ApexCharts from 'vue3-apexcharts';
import { get } from '@/libs';
@ -17,6 +17,7 @@ const conf = ref(
AccountEntry[]
>
);
const chainStore = useBlockchain();
const balances = ref({} as Record<string, Coin[]>);
const delegations = ref({} as Record<string, Delegation[]>);
const tokenMeta = ref({} as Record<string, AccountEntry>);
@ -64,7 +65,8 @@ Object.values(conf.value).forEach((imported) => {
imported.forEach((x) => {
if (x.endpoint && x.address) {
loading.value += 1
const client = CosmosRestClient.newDefault(x.endpoint);
const endpoint = chainStore.randomEndpoint(x.chainName)
const client = CosmosRestClient.newDefault(endpoint?.address || x.endpoint);
client.getBankBalances(x.address).then((res) => {
const bal = res.balances.filter((x) => x.denom.length < 10);
if (bal) balances.value[x.address || ""] = bal;

View File

@ -147,20 +147,25 @@ export const useBlockchain = defineStore('blockchain', {
useBlockModule().initial();
},
async randomSetupEndpoint() {
const end = localStorage.getItem(`endpoint-${this.chainName}`);
randomEndpoint(chainName: string) : Endpoint | undefined {
const end = localStorage.getItem(`endpoint-${chainName}`);
if (end) {
this.setRestEndpoint(JSON.parse(end));
return JSON.parse(end);
} else {
const all = this.current?.endpoints?.rest;
if (all) {
const rn = Math.random();
const endpoint = all[Math.floor(rn * all.length)];
await this.setRestEndpoint(endpoint);
return endpoint
}
}
},
async randomSetupEndpoint() {
const endpoint = this.randomEndpoint(this.chainName)
if(endpoint) await this.setRestEndpoint(endpoint);
},
async setRestEndpoint(endpoint: Endpoint) {
this.connErr = '';
this.endpoint = endpoint;