Skip to content

Commit

Permalink
try out different rpc endpoint when error occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengow committed Jun 17, 2022
1 parent c09d097 commit b146f96
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
3 changes: 3 additions & 0 deletions components/settings/SettingChooser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<script lang="ts">
import { Component, Prop, Vue } from 'nuxt-property-decorator'
import AddOption from './AddOption.vue'
import Connector from '@kodadot1/sub-api'
@Component({
components: {
Expand All @@ -40,6 +41,8 @@ export default class SettingChooser extends Vue {
}
set selected(value) {
const { getInstance: Api } = Connector
Api().connect(value)
this.$store.dispatch(this.setter, value)
}
Expand Down
34 changes: 29 additions & 5 deletions pages/transfer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export default class Transfer extends mixins(
}
}
public async submit(): Promise<void> {
public async submit(event: any, usedNodeUrls: string[] = []): Promise<void> {
showNotification(
`${this.$route.query.target ? 'Sent for Sign' : 'Dispatched'}`
)
Expand All @@ -271,7 +271,9 @@ export default class Transfer extends mixins(
this.destinationAddress,
calculateBalance(this.price, this.decimals),
]
if (usedNodeUrls.length < 1) {
throw new Error('test')
}
const tx = await exec(
this.accountId,
'',
Expand Down Expand Up @@ -305,10 +307,32 @@ export default class Transfer extends mixins(
(res) => this.resolveStatus(res.status)
)
)
} catch (e) {
this.$consola.error('[ERR: TRANSFER SUBMIT]', e)
if (e instanceof Error) {
} catch (e: any) {
if (e.message === 'Cancelled') {
showNotification(e.message, notificationTypes.danger)
this.isLoading = false
return
}
const availableNodesByPrefix: { value: string }[] =
this.$store.getters['availableNodesByPrefix']
const availableUrls = availableNodesByPrefix.map((node) => node.value)
if (usedNodeUrls.length === 0) {
usedNodeUrls.push(this.$store.getters.getSettings['apiUrl'])
}
if (usedNodeUrls.length < availableUrls.length) {
const nextTryUrls = availableUrls.filter(
(url) => !usedNodeUrls.includes(url)
)
const { getInstance: Api } = Connector
// try to connect next possible url
await Api().connect(nextTryUrls[0])
await this.$store.dispatch('setApiUrl', nextTryUrls[0])
this.submit(event, [nextTryUrls[0]].concat(usedNodeUrls))
} else {
this.$consola.error('[ERR: TRANSFER SUBMIT]', e)
if (e instanceof Error) {
showNotification(e.message, notificationTypes.danger)
}
}
}
}
Expand Down
18 changes: 1 addition & 17 deletions store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import Connector from '@kodadot1/sub-api'
import correctFormat from '@/utils/ss58Format'
import { GetterTree, MutationTree, Store } from 'vuex'

type VuexAction = {
type: string
payload: string
}

const apiPlugin = (store: Store<any>): void => {
const { getInstance: Api } = Connector

Expand Down Expand Up @@ -50,17 +45,6 @@ const apiPlugin = (store: Store<any>): void => {
})
}

const myPlugin = (store: Store<null>): void => {
const { getInstance: Api } = Connector

store.subscribeAction(({ type, payload }: VuexAction) => {
if (type === 'setApiUrl' && payload) {
store.commit('setLoading', true)
Api().connect(payload)
}
})
}

export const state = () => ({
loading: false,
keyringLoaded: false,
Expand Down Expand Up @@ -100,4 +84,4 @@ export const getters: GetterTree<IndexState, IndexState> = {
},
}

export const plugins = [apiPlugin, myPlugin]
export const plugins = [apiPlugin]

0 comments on commit b146f96

Please sign in to comment.