diff --git a/src/modules/[chain]/ibc/connStore.ts b/src/modules/[chain]/ibc/connStore.ts new file mode 100644 index 00000000..802dfd72 --- /dev/null +++ b/src/modules/[chain]/ibc/connStore.ts @@ -0,0 +1,59 @@ + +import { defineStore } from 'pinia'; + +import {useBlockchain} from '@/stores' +import ChainRegistryClient from '@ping-pub/chain-registry-client'; +import type { IBCPath } from '@ping-pub/chain-registry-client/dist/types'; +import type { Channel } from '@/types'; +import router from '@/router'; + +export const useIBCModule = defineStore('module-ibc', { + state: () => { + return { + paths: [] as IBCPath[], + connectionId: "", + registryConf: {}, + }; + }, + getters: { + chain() { + return useBlockchain() + }, + commonIBCs() { + return this.paths.filter(x => x.path.search(this.chain.current?.prettyName || this.chain.chainName) > -1) + }, + sourceField() { + return this.registryConf?.chain_1?.chain_name === this.chain.current?.prettyName || this.chain.chainName ? 'chain_1': 'chain_2' + }, + destField() { + return this.registryConf?.chain_1?.chain_name === this.chain.current?.prettyName || this.chain.chainName ? 'chain_2': 'chain_1' + }, + registryChannels() { + return this.registryConf.channels + } + }, + actions: { + load() { + const client = new ChainRegistryClient(); + client.fetchIBCPaths().then(res => { + this.paths = res + }); + }, + fetchConnection(path: string) { + const client = new ChainRegistryClient(); + client.fetchIBCPathInfo(path).then(res => { + const connId = res.chain_1.chain_name === this.chain.current?.prettyName || this.chain.chainName ? + res.chain_1.connection_id : res.chain_2.connection_id + this.registryConf = res + this.showConnection(connId) + }) + }, + showConnection(connId?: string | number) { + if(!connId) { + this.registryConf = {} + } + const path = `/${this.chain.chainName}/ibc/connection/${connId || `connection-${this.connectionId || 0}`}` + router.push(path) + } + }, +}); diff --git a/src/modules/[chain]/ibc/connection.vue b/src/modules/[chain]/ibc/connection.vue index 4f6b8e86..988cc3c8 100644 --- a/src/modules/[chain]/ibc/connection.vue +++ b/src/modules/[chain]/ibc/connection.vue @@ -8,9 +8,11 @@ import { ref } from 'vue'; import ChainRegistryClient from '@ping-pub/chain-registry-client'; import type { IBCPath } from '@ping-pub/chain-registry-client/dist/types'; import router from '@/router'; +import { useIBCModule } from './connStore'; const props = defineProps(['chain']); const chainStore = useBlockchain(); +const ibcStore = useIBCModule() const list = ref([] as Connection[]); const pageRequest = ref(new PageRequest()) const pageResponse = ref({} as Pagination) @@ -18,6 +20,7 @@ const tab = ref('registry'); onMounted(() => { pageload(1) + ibcStore.load() }); function pageload(p: number) { @@ -26,37 +29,11 @@ function pageload(p: number) { list.value = x.connections; pageResponse.value = x.pagination if(x.pagination.total && Number(x.pagination.total) > 0) { - showConnection(0) + ibcStore.showConnection(0) } }); } -// Common IBC connections -const paths = ref([] as IBCPath[]) -const client = new ChainRegistryClient(); -client.fetchIBCPaths().then(res => { - paths.value = res -}); -const connectionId = ref(undefined) -const commonIBCs = computed(() => { - const a = paths.value.filter(x => x.path.search(chainStore.current?.prettyName || chainStore.chainName) > -1) - if (a.length === 0) tab.value === 'favorite' - return a -}) - -function fetchConnection(path: string) { - client.fetchIBCPathInfo(path).then(res => { - const connId = res.chain_1.chain_name === chainStore.current?.prettyName || chainStore.chainName ? - res.chain_1.connection_id : res.chain_2.connection_id - showConnection(connId) - }) -} - -function showConnection(connId?: string | number) { - const path = `/${props.chain}/ibc/connection/${connId || `connection-${connectionId.value || 0}`}` - router.push(path) -} -