Skip to content

Commit

Permalink
Reorder ws urls for Karura and Acala for better reliability ⚙️
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldev5 authored and dudo50 committed Dec 12, 2023
1 parent ad98b29 commit ea35c50
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/nodes/ParachainNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
generateAddressPayload,
getFees,
createHeaderPolkadotXCM,
createCurrencySpecification
createCurrencySpecification,
getAllNodeProviders
} from '../utils'
import { constructRelayToParaParameters } from '../pallets/xcmPallet/utils'

Expand Down Expand Up @@ -137,6 +138,10 @@ abstract class ParachainNode {
parameters: constructRelayToParaParameters(options, Version.V3)
}
}

getProvider(): string {
return getAllNodeProviders(this.node)[0]
}
}

export default ParachainNode
6 changes: 6 additions & 0 deletions src/nodes/supported/Acala.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Version,
type XTokensTransferInput
} from '../../types'
import { getAllNodeProviders } from '../../utils'
import ParachainNode from '../ParachainNode'
import XTokensTransferImpl from '../XTokensTransferImpl'

Expand All @@ -21,6 +22,11 @@ class Acala extends ParachainNode implements IXTokensTransfer {
currencyID !== undefined ? { ForeignAsset: currencyID } : { Token: currency }
return XTokensTransferImpl.transferXTokens(input, currencySelection)
}

getProvider(): string {
// Return the second WebSocket URL because the first one is sometimes unreliable.
return getAllNodeProviders(this.node)[1]
}
}

export default Acala
6 changes: 6 additions & 0 deletions src/nodes/supported/Karura.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type Extrinsic,
type TSerializedApiCall
} from '../../types'
import { getAllNodeProviders } from '../../utils'
import ParachainNode from '../ParachainNode'
import XTokensTransferImpl from '../XTokensTransferImpl'

Expand All @@ -21,6 +22,11 @@ class Karura extends ParachainNode implements IXTokensTransfer {
currencyID !== undefined ? { ForeignAsset: currencyID } : { Token: currency }
return XTokensTransferImpl.transferXTokens(input, currencySelection)
}

getProvider(): string {
// Return the second WebSocket URL because the first one is sometimes unreliable.
return getAllNodeProviders(this.node)[1]
}
}

export default Karura
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,13 @@ export const getNodeEndpointOption = (node: TNode): any => {

export const getAllNodeProviders = (node: TNode): string[] => {
const { providers } = getNodeEndpointOption(node) ?? {}
if (providers.length < 1) {
throw new Error(`Node ${node} does not have any providers.`)
}
return Object.values(providers ?? [])
}

export const getNodeProvider = (node: TNode): string | null => {
const providers = getAllNodeProviders(node)
return providers.length > 0 ? providers[0] : null
}
export const getNodeProvider = (node: TNode): string => getNode(node).getProvider()

export const createApiInstance = async (wsUrl: string): Promise<ApiPromise> => {
const wsProvider = new WsProvider(wsUrl)
Expand Down

0 comments on commit ea35c50

Please sign in to comment.