Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto generated tfchain types to tfchain_client #3020

Merged
merged 8 commits into from
Jul 2, 2024

Conversation

0oM4R
Copy link
Contributor

@0oM4R 0oM4R commented Jun 24, 2024

Description

Integrate chain types in our project provide such better development experience, We can use the generated types and check auto complete via IDE. Polkadot provides typegen to generate pre defined types and chain types, currently, we only need the chain types.

Changes

  • create interface dir to contain the chain types
  • add script to generate types and adjust the created files to pass the build
  • include the created types into tsconfig-*json, to make the compiler aware of it

Details:

  • Generate a chain types requires @polkadot/typegen package of the same version as @polkadot/api.

Generating

First we need to have chain metadata, to generate types based on it, this is done by running the following command:

curl -H "Content-Type: application/json" -d '{"id":"1", "jsonrpc":"2.0", "method": "state_getMetadata", "params":[]}' -o chainMeta.json <<CHAIN_URL>>

The result of this command will be chainMeta.json with a content like

{ "jsonrpc": "2.0", "result": "0x6d6574610b6c185379737....", "id": 29 }

This file will be used in the following two commands:

  • Generate defined chain types and lookup files using polkadot-types-from-defs
    ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --package @threefold/tfchain_client  --input ./src/interfaces/chain --endpoint ./chainMeta.json

    Currently we don't have defined types but this command needed to crate the lookup files, for more details see the documentation

  • Generate types from the chain using polkadot-types-from-chain
     ts-node --skip-project node_modules/.bin/polkadot-types-from-chain --endpoint ./chainMeta.json --output ./src/interfaces/chain

Now types are generated, but we need to let the compiler aware of the generated types, we can do that by add the following paths to tsconfig-*.json inside compilerOptions.paths add those lines:

 "@polkadot/types/lookup": ["src/interfaces/chain/types-lookup.ts"],
 "@polkadot/api-augment*": ["src/interfaces/chain/augment-api.ts"]

Finally we need to export the generated types
in src/interfaces/index.ts

export * from "./chain/augment-api";
export * from "./chain/types-lookup";
export * from "./chain/lookup";

Usage

We need to import the augmented definitions "somewhere" as documentation says, in our case we will import them in src/client.

import "./interfaces";
import "@polkadot/api-augment";

Now we can import any type anywhere in the client as follows:

// e.g SpRuntimeModuleError
import { SpRuntimeModuleError } from "@polkadot/types/lookup";

There are two blockers now from building the project:

  1. As we din't have any defined types src/interfaces/chain/types is empty and will gives an error while build, for now we just need to export an empty object on it so we add export {} to the file.
  2. Types with reserved names:
    In src/interfaces/chain/augment-api-tx.ts we have createFarmingPolicy and updateFarmingPolicy functions in tfgridmodule each of them contains parameter named default this is not acceptable on Typescript as it is a reserved word, so we could replace it with _default.

All the previous steps are collected into scripts/generateTypes.bash
Now everything is ready to go
make sure to build the client after that

Related Issues

Documentation PR

For UI changes, Please provide the Documetation PR on info_grid

Checklist

  • Tests included
  • Build pass
  • Documentation
  • Code format and docstrings
  • Screenshots/Video attached (needed for UI changes)

0oM4R added 6 commits June 24, 2024 16:39
- add polkadot/typegen pachage
- update paths in tsconfig to make the compiler aware of the generated types
- use generateTypes script
- add required types files to the client
- add generated types to chain dir and export them

There are two blockers now from building the project:

1. As we din't have any defined types `src/interfaces/chain/types` is empty and will gives an error while build, for now we just need to export an empty object on it so we add `export {}` to the file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1. As we din't have any defined types `src/interfaces/chain/types` is empty and will gives an error while build, for now we just need to export an empty object on it so we add `export {}` to the file.
1. As we don't have any defined types `src/interfaces/chain/types` is empty and will give an error while build, for now we just need to export an empty object on it so we add `export {}` to the file.

Copy link
Contributor

@zaelgohary zaelgohary left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really good job, @0oM4R. Just a small typo.

@0oM4R 0oM4R requested a review from zaelgohary July 2, 2024 11:50
@0oM4R 0oM4R merged commit 9b27d34 into development_2.6 Jul 2, 2024
3 checks passed
@0oM4R 0oM4R deleted the development_chainTypes branch July 2, 2024 12:12
@xmonader xmonader added this to the 2.6.0 milestone Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants