【问题标题】:Polkadot-js Babel error when add custom @polkadot/types添加自定义 @polkadot/types 时 Polkadot-js Babel 错误
【发布时间】:2019-07-05 07:16:58
【问题描述】:

我正在使用 @polkadot-js 设置 Nuxt.js 应用程序。当我使用@polkadot/types 请求自定义基板运行时模块时 - 我收到此错误Class constructor Struct cannot be invoked without 'new'

这是一个带有官方设置 typescript 的 Nuxt.js 应用程序。过去,我尝试使用干净的 Nuxt.js 和 Vue 设置它,但总是出现相同的错误。仅当我设置干净的 NodeJS(带或不带 typescript)或使用 @polkadot react 应用程序时,它才能正常工作。

我创建了一个repository 来尝试其他一些方法。

API 调用:

class VecU32 extends Vector.with(u32) {}
class Kind extends Struct {
  constructor(value) {
    super({
        stuff: VecU32
    }, value);
  }
}

const Alice = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";

const provider = new WsProvider("ws://127.0.0.1:9944");
const typeRegistry = getTypeRegistry();
typeRegistry.register({ Kind });
const api = await ApiPromise.create(provider);
// With types providede in create function - works well
// types: {
//   Kind: {
//     stuff: "Vec<u32>"
//   }
// }
const res = await api.query.template.kinds(Alice);
console.log(res);

我期望空(或一些值,取决于区块链中的内容)结果输出,但实际输出是错误,Class constructor Struct cannot be invoked without 'new'

【问题讨论】:

    标签: javascript typescript vue.js babeljs substrate


    【解决方案1】:

    简答:

    代替这个const typeRegistry = getTypeRegistry();,做:

    const typeRegistry.register({
              Kind: {
                'stuff': 'Vec<u32>'
              }
            });
    

    更长的答案

    当您调用typeRegistry.register({ Kind }); 时,您尝试将 Typescript 类注册为注册表中的自定义类型,但您需要传递给 API 的类型注册表的类型与您的 Typescript 类型无关,这两者没有直接关联。

    如果您要编写纯 Javascript,则需要在 Polkadot-JS API 中注册您的自定义 Substrate 类型。

    传递给 API 的类型用于解码和编码您发送到/从底层节点接收的数据。它们符合 SCALE 编解码器,该编解码器也在 Substrate 核心 Rust 代码中实现。使用这些类型可以确保数据可以在不同的环境和不同的语言中正确解码和编码。

    您可以在此处阅读更多信息:https://substrate.dev/docs/en/overview/low-level-data-format

    这些类型的 Javascript 表示在 Polkadot-JS 文档中被列为“编解码器类型”: https://polkadot.js.org/api/types/#codec-types

    您在 Polkadot-JS 文档中找到的所有其他类型都是这些低级编解码器类型的扩展。

    您需要传递给 JS-API 的是您所有自定义底层模块的所有自定义类型,以便 API 知道如何对您的数据进行解码和编码,因此在您的情况下,您声明了 here in Rust

    pub struct Kind {
        stuff: Vec<u32>,
    }
    

    需要在Javascript中这样注册:

    const typeRegistry.register({
              Kind: {
                'stuff': 'Vec<u32>'
              }
            });
    

    另一方面,您的 Typescript 类型用于确保您在前端处理客户端以 typescript 编写的数据具有正确的类型。

    只有 Typescript 需要它们,并且它们增加了额外的安全层,但类型本身不需要与 API 通信。不过,您的数据肯定需要具有正确的格式以防止错误。

    您可以将https://www.npmjs.com/package/@polkadot/types 视为https://github.com/DefinitelyTyped/DefinitelyTyped 的 Substrate/Polkadot 特定版本

    但即使您不使用 Typescript,https://polkadot.js.org/api/types/ 仍然是 100% 的首选参考。

    【讨论】:

    • 好吧,我问题中的注释代码表明我以前做过,但我想在javascript中将Substrate类型注册为类对象(例如here),而不是普通对象。
    • 问题是,类型注册表将很快被弃用,您在这方面学习/编写的所有内容都将在几周内(希望)过时。
    • 目前正在努力自动生成类型声明并将它们自动添加到元数据中。在基板方面,您可以阅读更多关于它的信息hereherehere。它已经在智能合约模块中实现了。请参阅 this PRthis issue。因此,我们强烈建议采用文档中描述的标准方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 2021-05-25
    • 2017-03-14
    • 2022-05-25
    相关资源
    最近更新 更多