api.ts 668 B

12345678910111213141516171819
  1. import { ApiPromise, WsProvider } from '@polkadot/api'
  2. import types from '@joystream/types/augment/all/defs.json'
  3. export default async function createApi(): Promise<ApiPromise> {
  4. // Get URL to websocket endpoint from environment or connect to local node by default
  5. const WS_URL = process.env.WS_PROVIDER_ENDPOINT_URI || 'ws://127.0.0.1:9944'
  6. // explicitely state what RPC we are connecting to
  7. console.error('Connecting to RPC at: ' + WS_URL)
  8. // Initialise the provider
  9. const provider = new WsProvider(WS_URL)
  10. // Create the API and wait until ready
  11. const api = await ApiPromise.create({ provider, types })
  12. await api.isReadyOrError
  13. return api
  14. }