Skip to content

Commit

Permalink
get genesis with ApiPromise
Browse files Browse the repository at this point in the history
Signed-off-by: Shreevatsa N <[email protected]>
  • Loading branch information
vatsa287 committed Jun 26, 2024
1 parent 208ce34 commit cc2e01d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
30 changes: 26 additions & 4 deletions packages/utils/src/genesis.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
import { connect, disconnect } from '@cord.network/config'
import { ConfigService, connect, disconnect } from '@cord.network/config'
import { SDKErrors } from '.'
import { ApiPromise } from '@polkadot/api';

/**
* Connects to chain and returnns the genesis hash in hexadecimal format for a given networkAddress.
* Returns the genesis hash in hexadecimal format for a given network object which is a ApiPromise.
*
* @param networkAddress Network Address for which the genesis hash is required.
* @param network network object of type ApiPromise for which the genesis hash is required.
* @returns Returns the genesis hash in Hexadecimal format.
*/
export async function getGenesisHash(
network: ApiPromise,
): Promise<string> {
try {
return network.genesisHash.toHex();
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : JSON.stringify(error)

throw new SDKErrors.CordQueryError(
`Error querying asset entry: ${errorMessage}`
)}
}

/**
* Connects to chain and returns the genesis hash in hexadecimal format for a given networkAddress.
*
* @param networkAddress Network Address for which the genesis hash is required.
* @returns Returns the genesis hash in Hexadecimal format.
*/
export async function getGenesisHashWithConnect(
networkAddress: string,
): Promise<string> {
try {
const connectData = await connect(networkAddress as string);
await connect(networkAddress as string);
const connectData = ConfigService.get('api')
const genesisHash = connectData.genesisHash.toHex();

return genesisHash;
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * as DataUtils from './DataUtils.js'
export * as DecoderUtils from './Decode.js'
export * as SDKErrors from './SDKErrors.js'
export * as JsonSchema from './json-schema/index.js'
export * as Genesis from './genesis.js'
export { ss58Format } from './ss58Format.js'
export { Keyring } from '@polkadot/keyring'
export * as Cbor from 'cbor-x'

0 comments on commit cc2e01d

Please sign in to comment.