diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ce3fc22..cafa5355 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ Version header format: `[version] Name - year-month-day (entropy-core compatibil ## [UNRELEASED] ### Added +- utility method to return substrate api instance to interact with without needing to instantiate entropy instance (#435)[https://github.com/entropyxyz/sdk/pull/435] ### Fixed diff --git a/src/index.ts b/src/index.ts index e18f9da0..8dbb878c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ -import { ApiPromise, WsProvider } from '@polkadot/api' +import { ApiPromise } from '@polkadot/api' import xtend from 'xtend' -import { isValidSubstrateAddress as isDeployer } from './utils' +import { createSubstrate, isValidSubstrateAddress as isDeployer } from './utils' import RegistrationManager, { RegistrationParams } from './registration' import SignatureRequestManager, { SigOps, SigWithAdaptersOps } from './signing' import { crypto, loadCryptoLib } from './utils/crypto' @@ -96,8 +96,7 @@ export default class Entropy { async #init (opts: EntropyOpts) { this.keyring = opts.keyring - const wsProvider = new WsProvider(opts.endpoint) - this.substrate = new ApiPromise({ provider: wsProvider, noInitWarn: true }) + this.substrate = createSubstrate(opts.endpoint) await this.substrate.isReadyOrError // throws an error if fails this.registrationManager = new RegistrationManager({ diff --git a/src/utils/index.ts b/src/utils/index.ts index ddf413dc..99ae1d7d 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,3 +1,4 @@ +import { WsProvider, ApiPromise } from '@polkadot/api' import { decodeAddress, encodeAddress } from '@polkadot/keyring' import { hexToU8a, isHex } from '@polkadot/util' import Debug from 'debug' @@ -112,7 +113,17 @@ export async function sendHttpPost (url: string, data: any): Promise { return oks } +/** + * Creates substrate api provider without needing to instantiate an entropy instance + * + * @param {string} endpoint - Endpoint for the api + * @returns {ApiPromise} Api tool to interact with protocol + */ +export function createSubstrate (endpoint: string): ApiPromise { + const wsProvider = new WsProvider(endpoint) + return new ApiPromise({ provider: wsProvider, noInitWarn: true }) +} /** * Converts an ArrayBuffer to a hexadecimal string.