From ce7abefa84bfb0d650aa030ac26b36c00173108e Mon Sep 17 00:00:00 2001
From: liangping <18786721@qq.com>
Date: Fri, 10 Nov 2023 15:34:06 +0800
Subject: [PATCH] random pick endpoints

---
 src/modules/wallet/accounts.vue  |  9 ++++++---
 src/modules/wallet/portfolio.vue |  6 ++++--
 src/stores/useBlockchain.ts      | 13 +++++++++----
 3 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/src/modules/wallet/accounts.vue b/src/modules/wallet/accounts.vue
index a702c4bb..06ac38e2 100644
--- a/src/modules/wallet/accounts.vue
+++ b/src/modules/wallet/accounts.vue
@@ -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);
   });
diff --git a/src/modules/wallet/portfolio.vue b/src/modules/wallet/portfolio.vue
index 5fa01ced..dbceb937 100644
--- a/src/modules/wallet/portfolio.vue
+++ b/src/modules/wallet/portfolio.vue
@@ -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;
diff --git a/src/stores/useBlockchain.ts b/src/stores/useBlockchain.ts
index 3af2e5f7..76f392db 100644
--- a/src/stores/useBlockchain.ts
+++ b/src/stores/useBlockchain.ts
@@ -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;