Skip to content

Commit

Permalink
feat: add chainSpec_v1 group of functions (#820)
Browse files Browse the repository at this point in the history
* feat: add chainSpec_v1 group of functions

* throw error if genesis is null
  • Loading branch information
voliva authored Sep 15, 2024
1 parent 2e33fd6 commit e244e91
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/core/src/rpc/rpc-spec/chainSpec_v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ChainProperties } from '../../index.js'
import { Handler, ResponseError } from '../shared.js'
import { HexString } from '@polkadot/util/types'

export const chainSpec_v1_chainName: Handler<[], string> = async (context) => {
return context.chain.api.getSystemChain()
}

export const chainSpec_v1_genesisHash: Handler<[], HexString> = async (context) => {
const genesisHash = await context.chain.api.getBlockHash(0)
if (genesisHash === null) {
throw new ResponseError(1, 'Unexpected null genesis hash')
}
return genesisHash
}

export const chainSpec_v1_properties: Handler<[], ChainProperties> = async (context) => {
return context.chain.api.getSystemProperties()
}
4 changes: 3 additions & 1 deletion packages/core/src/rpc/rpc-spec/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as ChainHeadV1RPC from './chainHead_v1.js'
import * as ChainSpecV1RPC from './chainSpec_v1.js'
import * as TransactionV1RPC from './transaction_v1.js'

export { ChainHeadV1RPC, TransactionV1RPC }
export { ChainHeadV1RPC, TransactionV1RPC, ChainSpecV1RPC }

const handlers = {
...ChainHeadV1RPC,
...TransactionV1RPC,
...ChainSpecV1RPC,
}

export default handlers
23 changes: 23 additions & 0 deletions packages/e2e/src/__snapshots__/rpc-spec.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`chainSpec_v1 > retrieves the chainSpec data 1`] = `
{
"genesisHash": "0xfc41b9bd8ef8fe53d58c7ea67c794c7ec9a73daf05e6d54b14ff6342c99ba64c",
"name": "Acala",
"properties": {
"ss58Format": 10,
"tokenDecimals": [
12,
12,
10,
10,
],
"tokenSymbol": [
"ACA",
"AUSD",
"DOT",
"LDOT",
],
},
}
`;
6 changes: 6 additions & 0 deletions packages/e2e/src/rpc-spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ describe('transaction_v1', async () => {
})
})

describe('chainSpec_v1', () => {
it('retrieves the chainSpec data', async () => {
expect(await testApi.substrateClient.getChainSpecData()).toMatchSnapshot()
})
})

const INITIAL_ACCOUNT_VALUE = 100_000_000_000_000n
async function prepareChainForTx() {
const api = await ApiPromise.create({
Expand Down

0 comments on commit e244e91

Please sign in to comment.