ws.ts 750 B

123456789101112131415161718192021222324
  1. // TODO allow alternative backends
  2. import { ApiPromise, WsProvider } from '@polkadot/api'
  3. import { types } from '@joystream/types'
  4. const wsLocation =
  5. 'ws://localhost:9944'
  6. // 'wss://rome-rpc-endpoint.joystream.org:9944'
  7. const connectUpstream = async (): Promise<ApiPromise> => {
  8. try {
  9. //console.debug(`[Joystream] Connecting to ${wsLocation}`)
  10. const provider = new WsProvider(wsLocation)
  11. const api = await ApiPromise.create({ provider, types })
  12. await api.isReady
  13. console.debug(`[Joystream] Connected to ${wsLocation}`)
  14. return api
  15. } catch (e) {
  16. console.error(`[Joystream] upstream connection failed`, e)
  17. throw new Error()
  18. }
  19. }
  20. module.exports = { connectUpstream }