cosmos-explorer/src/stores/useBlockchain.ts

142 lines
3.9 KiB
TypeScript
Raw Normal View History

2023-05-04 16:28:27 +00:00
import { defineStore } from 'pinia';
import {
useDashboard,
type ChainConfig,
type Endpoint,
EndpointType,
} from './useDashboard';
import type { VerticalNavItems } from '@/@layouts/types';
import { useRouter } from 'vue-router';
import { CosmosRestClient } from '@/libs/client';
import {
useBankStore,
useBaseStore,
useGovStore,
useMintStore,
useStakingStore,
} from '.';
import { useBlockModule } from '@/modules/[chain]/block/block';
import { DEFAULT } from '@/libs';
2023-04-03 09:08:02 +00:00
2023-05-04 16:28:27 +00:00
export const useBlockchain = defineStore('blockchain', {
2023-04-03 09:08:02 +00:00
state: () => {
return {
status: {} as Record<string, string>,
rest: '',
2023-05-04 16:28:27 +00:00
chainName: '',
2023-04-03 09:08:02 +00:00
endpoint: {} as {
2023-05-04 16:28:27 +00:00
type?: EndpointType;
address: string;
provider: string;
2023-04-03 09:08:02 +00:00
},
2023-05-04 16:28:27 +00:00
connErr: '',
};
2023-04-03 09:08:02 +00:00
},
getters: {
2023-05-04 16:28:27 +00:00
current(): ChainConfig | undefined {
return this.dashboard.chains[this.chainName];
2023-04-03 09:08:02 +00:00
},
logo(): string {
2023-05-04 16:28:27 +00:00
return this.current?.logo || '';
2023-04-03 09:08:02 +00:00
},
2023-05-10 03:12:06 +00:00
defaultHDPath(): string {
const cointype = this.current?.coinType || "118"
// if(cointype === "60") {
// return `m/44'/${cointype}`
// }
// return `m/44'/${cointype}/0'/0/0`
return "connected-wallet"
},
2023-04-03 09:08:02 +00:00
dashboard() {
2023-05-04 16:28:27 +00:00
return useDashboard();
2023-04-03 09:08:02 +00:00
},
computedChainMenu() {
2023-05-04 16:28:27 +00:00
let currNavItem: VerticalNavItems = [];
2023-04-03 09:08:02 +00:00
2023-05-04 16:28:27 +00:00
const router = useRouter();
const routes = router?.getRoutes() || [];
if (this.current && routes) {
currNavItem = [
{
title: this.current?.prettyName || this.chainName || '',
icon: { image: this.current.logo, size: '22' },
i18n: false,
children: routes
.filter((x) => x.meta.i18n)
.map((x) => ({
title: `module.${x.meta.i18n}`,
to: { path: x.path.replace(':chain', this.chainName) },
icon: { icon: 'mdi-chevron-right', size: '22' },
i18n: true,
}))
.sort((a, b) => a.to.path.length - b.to.path.length),
},
];
2023-04-03 09:08:02 +00:00
}
// compute favorite menu
2023-05-04 16:28:27 +00:00
const favNavItems: VerticalNavItems = [];
Object.keys(this.dashboard.favoriteMap).forEach((name) => {
const ch = this.dashboard.chains[name];
if (ch && this.dashboard.favoriteMap?.[name]) {
2023-04-03 09:08:02 +00:00
favNavItems.push({
2023-05-04 16:28:27 +00:00
title: ch.prettyName || ch.chainName || name,
to: { path: `/${ch.chainName || name}` },
icon: { image: ch.logo, size: '22' },
});
}
});
2023-04-03 09:08:02 +00:00
// combine all together
2023-05-04 16:28:27 +00:00
return [
...currNavItem,
{ heading: 'Ecosystem' },
{
title: 'Favorite',
children: favNavItems,
badgeContent: favNavItems.length,
badgeClass: 'bg-primary',
i18n: true,
icon: { icon: 'mdi-star', size: '22' },
},
{
title: 'All Blockchains',
to: { path: '/' },
badgeContent: this.dashboard.length,
badgeClass: 'bg-primary',
i18n: true,
icon: { icon: 'mdi-grid', size: '22' },
},
];
2023-04-03 09:08:02 +00:00
},
},
actions: {
async initial() {
2023-05-04 16:28:27 +00:00
await this.randomSetupEndpoint();
await useStakingStore().init();
useBankStore().initial();
useBaseStore().initial();
useGovStore().initial();
useMintStore().initial();
useBlockModule().initial();
2023-04-03 09:08:02 +00:00
},
async randomSetupEndpoint() {
2023-05-04 16:28:27 +00:00
const all = this.current?.endpoints?.rest;
if (all) {
const rn = Math.random();
const endpoint = all[Math.floor(rn * all.length)];
await this.setRestEndpoint(endpoint);
}
2023-04-03 09:08:02 +00:00
},
2023-05-04 16:28:27 +00:00
2023-04-03 09:08:02 +00:00
async setRestEndpoint(endpoint: Endpoint) {
2023-05-04 16:28:27 +00:00
this.connErr = '';
this.endpoint = endpoint;
this.rpc = new CosmosRestClient(endpoint.address, DEFAULT);
2023-04-03 09:08:02 +00:00
},
setCurrent(name: string) {
2023-05-04 16:28:27 +00:00
this.chainName = name;
2023-04-03 09:08:02 +00:00
},
2023-05-04 16:28:27 +00:00
},
});