Skip to content

Commit

Permalink
Remove dependencies (#59)
Browse files Browse the repository at this point in the history
Reduced the non-dev dependencies by changing `getSchemaId` to take the
genesis hash instead of an `ApiPromise`

Problem
=======

Keeping the Polkadot/api libraries in version sync is difficult and
issues arise with dependencies in typescript with the additional
requirement that `@frequency-chain/api-augment` versions align.

Solution
========
Removed the dependency need.
  • Loading branch information
wilwade authored Oct 3, 2024
1 parent bfc36da commit a28ca1a
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 124 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { dsnp } from "@dsnp/frequency-schemas";
import { ApiPromise } from "@polkadot/api";

const api = ApiPromise.create(/* ... */);
console.log(await dsnp.getSchemaId(api, "broadcast"));
console.log(dsnp.getSchemaId("broadcast", "1.3", api.genesisHash.toString()));
```

The API connection is used only to identify the chain by its genesis hash.
Expand All @@ -40,7 +40,7 @@ dsnp.setSchemaMapping(api.genesisHash.toString(), {
// ...
});
console.log(await dsnp.getSchemaId(api, "broadcast")); // yields 67
console.log(dsnp.getSchemaId("broadcast", "1.2", api.genesisHash.toString())); // yields 67
```

### With Parquet Writer
Expand Down
21 changes: 7 additions & 14 deletions dsnp/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ApiPromise } from "@polkadot/api";

import type { Schema } from "avsc";
import { DSNPParquetSchema } from "@dsnp/schemas/types/dsnp-parquet.js";
import {
Expand Down Expand Up @@ -283,19 +281,14 @@ chainMapping["default"] = {
};

/**
* Gets the schemaId from the Frequency instance configured for
* apiPromise for the given DSNP type and version. If version is
* unspecified, the latest version is returned. (You probably only
* need version if you're migrating.)
* Gets the schemaId for for given DSNP type and version and genesis hash.
* If version is unspecified, the latest version is returned. (You probably
* only need version if you're migrating.)
* If genesis hash is unspecified, the mainnet genesis hash and schema
* numbers will be used.
*/
export const getSchemaId = async (
apiPromise: Promise<ApiPromise>,
schemaName: SchemaName,
dsnpVersion?: DSNPVersion,
): Promise<number> => {
const api = await apiPromise;
const genesisHash = api.genesisHash.toString();
let mapping = chainMapping[genesisHash];
export const getSchemaId = (schemaName: SchemaName, dsnpVersion?: DSNPVersion, genesisHash?: string): number => {
let mapping = chainMapping[genesisHash || GENESIS_HASH_MAINNET];
// If we don't recognize this chain, use default mapping
if (!mapping) mapping = chainMapping["default"];
const versions = mapping[schemaName];
Expand Down
Loading

0 comments on commit a28ca1a

Please sign in to comment.