1

我尝试使用 polkadot-js libray 运行一个简单的应用程序,用于在 plasm 上的 Dusty 帐户之间传输令牌。运行代码片段的源代码和 package.json 可以在这个 git repo上找到

节点版本: v12.8.2
纱线版本: 1.22.5



const { ApiPromise, WsProvider } = require('@polkadot/api')
const { Keyring } = require('@polkadot/keyring')
const plasmDefinitions = require('@plasm/types/interfaces/definitions');
const jsonDefinitions = require('./types.js')
const fs = require('fs')
const BN = require('bn.js')

startChain()

async function startChain() {
  console.log("trying connection to ", process.env.RPC_URL)
  const targetAddress = process.env.TARGET_ADDRESS
  const provider = new WsProvider(process.env.RPC_URL)
  const types = Object.values(plasmDefinitions).reduce(
      (res, { types }) => ({ ...res, ...types }),
      {},
  );

  const api = new ApiPromise({
      provider,
      types
  });

  api.on('connected', () => console.log(`Connected to ${process.env.RPC_URL}`));
  api.on('disconnected', () => console.log(`Disconnected from ${process.env.RPC_URL}`));
  await api.isReady;
  const [chain, nodeName, nodeVersion] = await Promise.all([
    api.rpc.system.chain(),
    api.rpc.system.name(),
    api.rpc.system.version()
  ])

  console.log(`You are connected to chain ${chain} using ${nodeName} v${nodeVersion} - ${process.env.RPC_URL}`)

  const keyring = new Keyring({ type: 'sr25519' })
  const fromPair = keyring.addFromUri(process.env.PLASM_MNEMONIC)
  const fromAmountUnits = new BN('1000000000000000')
  const transfer = api.tx.balances.transfer(targetAddress, fromAmountUnits)      
  // send value
  //const nonce = await api.query.system.accountNonce(process.env.FROM_ADDRESS)
  const nonce = await api.rpc.system.accountNextIndex(process.env.FROM_ADDRESS)
  console.log("got nonce", nonce)
  const txHash = await transfer.signAndSend(fromPair, {nonce})

}

堆栈跟踪:

trying connection to wss://rpc.dusty.plasmnet.io/ Connected to wss://rpc.dusty.plasmnet.io/ You are connected to chain Dusty using Plasm Node v1.6.1-cf15b11-x86_64-linux-gnu - wss://rpc.dusty.plasmnet.io/ got nonce Type { negative: 0, words: [ 0 ], length: 1, red: null, registry: TypeRegistry {} } 2021-02-05 21:00:27 RPC-CORE: submitExtrinsic(extrinsic: Extrinsic): Hash:: 1002: Verification Error: Execution: Could not convert parameter tx between node and runtime: Error decoding field CheckMortality.0: RuntimeApi, Execution: Could not convert parametertx between node and runtime: Error decoding field CheckMortality.0 (node:13572) UnhandledPromiseRejectionWarning: Error: 1002: Verification Error: Execution: Could not convert parametertx between node and runtime: Error decoding field CheckMortality.0: RuntimeApi, Execution: Could not convert parametertx between node and runtime: Error decoding field CheckMortality.0 at RpcCoder._checkError (<mydir>\examples\plasm-simple-transfer\node_modules\@polkadot\api\node_modules\@polkadot\rpc-provider\coder\index.js:84:13) at RpcCoder.decodeResponse (<mydir>\examples\plasm-simple-transfer\node_modules\@polkadot\api\node_modules\@polkadot\rpc-provider\coder\index.js:47:10) at WsProvider.value (<mydir>\examples\plasm-simple-transfer\node_modules\@polkadot\api\node_modules\@polkadot\rpc-provider\ws\index.js:214:90) at W3CWebSocket.value [as onmessage] (<mydir>\examples\plasm-simple-transfer\node_modules\@polkadot\api\node_modules\@polkadot\rpc-provider\ws\index.js:194:153) at W3CWebSocket._dispatchEvent [as dispatchEvent] (<mydir>\examples\plasm-simple-transfer\node_modules\yaeti\lib\EventTarget.js:107:17) at W3CWebSocket.onMessage (<mydir>\examples\plasm-simple-transfer\node_modules\websocket\lib\W3CWebSocket.js:234:14) at WebSocketConnection.<anonymous> (<mydir>\examples\plasm-simple-transfer\node_modules\websocket\lib\W3CWebSocket.js:205:19) at WebSocketConnection.emit (events.js:315:20) at WebSocketConnection.processFrame (<mydir>\examples\plasm-simple-transfer\node_modules\websocket\lib\WebSocketConnection.js:554:26) at <mydir>\examples\plasm-simple-transfer\node_modules\websocket\lib\WebSocketConnection.js:323:40 (node:13572) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag--unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:13572) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

此外,当我尝试进行清晰运行(删除 node_modules 并重新安装)时,我收到相同的消息,但字段 CheckMortality 被 CheckNonce 替换。由 plasm-types 库返回的字段可以在此文件中以 json 格式找到。

我试图通过浏览节点模板存储库来找出错误的类型,但我无法弄清楚。如何修复?

4

1 回答 1

0

我不得不对类型定义做一些小改动来规避这个问题。换句话说,替换:


  const types = Object.values(plasmDefinitions).reduce(
      (res, { types }) => ({ ...res, ...types }),
      {},
  );

  const api = new ApiPromise({
      provider,
      types
  });

为了


  const types = Object.values(plasmDefinitions).reduce(
      (res, { types }) => ({ ...res, ...types }),
      {},
  );

  types["Address"] = "IndicesLookupSource";
  types["LookupSource"] =  "IndicesLookupSource";

  const api = await ApiPromise.create({
    provider: provider,
    types: types
  });

可以在此处此处找到有关此问题的更多信息

于 2021-02-06T18:50:18.520 回答