I'm trying to connect local node running on my pc using polkadot.js
library in react.js(browser). I'm pasting my code in below snippet and error that I'm facing in chrome console
Code
// ************************Polkadot****************************
import {WsProvider, ApiPromise} from '@polkadot/api';
import { ContractPromise } from '@polkadot/api-contract';
import { Keyring } from '@polkadot/keyring';
// ************************************************************
import bridgePeer from './MetaData/DevErc20Metadata.json';
import bridgeTxt from './MetaData/PrivErc20Metadata.json';
//Keyring for finding the private and public key pair
const keyring = new Keyring({ type: 'ed25519' });
// Contract address and its host
const txtNode = new WsProvider('ws://127.0.0.1:9944');
const PeerNode = new WsProvider('ws://127.0.0.1:9946');
const ContractAddressGxt = '5GDxmhbSFqoBbsdWC4qd7LLHMZNmW3xX4rLKA7Ucdgb6un4z';
const ContractAddressPeer = '5D6z8T8rWZW1Uy7RwPburZv4wo5tjhCtG8CbfR5pbgxSEY37';
let api1, api2;
(async () => {
api1 = await ApiPromise.create({ provider:txtNode});
api2 = await ApiPromise.create({ provider: PeerNode});
})();
Console
index.js:465 WebSocket connection to 'ws://127.0.0.1:9946/' failed:
connect @ index.js:465
connectWithRetry @ index.js:499
WsProvider @ index.js:411
./src/Common/WasmWasmHelper.js @ WasmWasmHelper.js:15
options.factory @ react refresh:6
_webpack_require_ @ bootstrap:24
fn @ hot module replacement:62
./src/Componets/Swap/Swap.js @ loader.js:10
options.factory @ react refresh:6
_webpack_require_ @ bootstrap:24
fn @ hot module replacement:62
./src/App.js @ classPrivateFieldLooseKey.js:4
options.factory @ react refresh:6
_webpack_require_ @ bootstrap:24
fn @ hot module replacement:62
./src/index.js @ Swap.js:191
options.factory @ react refresh:6
_webpack_require_ @ bootstrap:24
(anonymous) @ startup:7
(anonymous) @ startup:7
logger.js:62 2022-06-10 18:36:55 API-WS: disconnected from ws://127.0.0.1:9946: 1006:: Abnormal Closure
Can anyone tell me why I'm facing this error any suggestion most welcome.
ApiPromise
objects in polkadot-js in React.await ApiPromise.create({provider: ...});
Is this bad practice and should I instead be sharing theApiPromise
object between my classes?