@1coinnetwork/node-types 中文文档教程
coinnet-node-types
Usage
这个 repo 包含自定义 coinnet-node 模块的 Typescript 绑定。
为了针对 1COIN Network 使用标准 API,您必须按如下方式初始化 API 的选项对象:
import { spec } from '@1coinnetwork/node-types';
const options: ApiOptions = {
provider : new WsProvider('ws://localhost:9944'),
...spec,
};
const api = new ApiPromise(options);
您还需要更新项目的 tsconfig.json
以包含以下内容:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@polkadot/api/augment": ["./node_modules/@1coinnetwork/node-types/interfaces/augment-api.d.ts"],
"@polkadot/types/augment": ["./node_modules/@1coinnetwork/node-types/interfaces/augment-types.d.ts"],
}
}
}
Dependencies
您项目的 @polkadot- js 依赖版本必须匹配本项目中使用的版本,包括@polkadot/api
、@polkadot/keyring
,以及所有相关的包,作为以及 rxjs
(如果使用 ApiRx
而不是 ApiPromise
)和 typescript
。 要验证所需的版本,请检查 @polkadot package.json 文件以了解此项目的 package.json 中指定的版本。
Building
这个项目依赖于 @polkadot/typegen 项目,它有一个构建此项目的分步指南。
1COIN 特定版本如下:
确保 package.json
@polkadot/api
设置为正确的版本,并且 < code>@polkadot/typegen 设置为与@polkadot/api
相同的版本。 如果需要发布,请增加@1coinnetwork/node-types
版本。如果任何 1coin 网络模块发生变化,请确保
src/interfaces
中的所有definitions.ts
文件都更新为每种类型的最新版本。在本地主机上运行 coinnet-node 链。
您会注意到 lint 命令的输出包含一个错误。 这是因为 typegen 脚本不能很好地处理相互依赖的自定义模块的相对路径。 您必须手动更改 signaling/types.ts 如下:
- import { VoteStage } from './voting';
+ import { VoteStage } from '../voting';
升级所有 spec文件 根据 @polkadot-js 更新日志 和链的底层 Substrate 版本。
要将 Typescript 编译为 Javascript 以用于 npm 发布,请运行
yarn build
。 此命令不应产生任何错误,并将构建文件输出到 dist 目录。使用
yarn verify [mainnet | 对实时 coinnet 节点运行新构建的 API 的简单测试 测试网]。
使用
npm publish
发布新版本。
coinnet-node-types
Usage
This repo contains Typescript bindings for custom coinnet-node modules.
In order to use the standard API against 1COIN Network you must initialize the API's options object as follows:
import { spec } from '@1coinnetwork/node-types';
const options: ApiOptions = {
provider : new WsProvider('ws://localhost:9944'),
...spec,
};
const api = new ApiPromise(options);
You will also need to update the tsconfig.json
of your project to include the following:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@polkadot/api/augment": ["./node_modules/@1coinnetwork/node-types/interfaces/augment-api.d.ts"],
"@polkadot/types/augment": ["./node_modules/@1coinnetwork/node-types/interfaces/augment-types.d.ts"],
}
}
}
Dependencies
Your project's @polkadot-js dependency versions must match the versions used in this project, including @polkadot/api
, @polkadot/keyring
, and all associated packages, as well as rxjs
(if using ApiRx
instead of ApiPromise
) and typescript
. To verify the versions required, inspect the @polkadot package.json files for the version specified in this project's package.json.
Building
This project depends on the @polkadot/typegen project, which has a step-by-step guide to building this project.
The 1COIN-specific version is as follows:
Ensure in package.json that
@polkadot/api
is set to the correct version, and that@polkadot/typegen
is set to the same version as@polkadot/api
. Increase the@1coinnetwork/node-types
version if necessary for publication.Ensure that all the
definitions.ts
files insrc/interfaces
are updated to the latest versions of each type, if any 1coin network modules changed.Run an coinnet-node chain on localhost.
You will notice that the output of the lint command contains an error. This is because the typegen script does not handle relative paths well for custom modules that depend on each other. You will have to make a manual change to signaling/types.ts as follows:
- import { VoteStage } from './voting';
+ import { VoteStage } from '../voting';
Upgrade all spec files with any changes necessary as per the @polkadot-js changelog and the underlying Substrate version of the chain.
To compile the Typescript to Javascript for npm publication, run
yarn build
. This command should not produce any errors and will output build files to the dist directory.Run a simple test of the newly-built API against a live coinnet node with
yarn verify [mainnet | testnet]
.Publish the new version with
npm publish
.