Defichain WhaleApiClient function call gives TypeError: fn is not a function

Defichain WhaleApiClient function call gives TypeError: fn is not a function

我有一个名为 defichain.js 的文件,我正尝试在我的项目中通过 OCEAN REST API 与 defichain 区块链进行通信。我正在使用 Node.js 版本 16.14.2。我是 运行 带有最新版 truffle 的 .js 脚本。

我已经设法在控制台中打印出 WhaleApiClient 对象以检查其可用功能,因此至少它可以识别客户端。

无论如何,当我尝试 get the current stats of the DeFi-Chain Blockchain(例如主节点的数量)时,我得到 TypeError: fn is not a function.
很明显,我在这里没有调用任何名为 fn 的函数。

const { WhaleApiClient } = require('@defichain/whale-api-client')

const client = new WhaleApiClient({
    url: 'https://ocean.defichain.com',
    timeout: 60000,
    version: 'v0',
    network: 'mainnet'
});

console.log(client); // prints client object with all of its categories

console.log(client.stats.get()) // prints Promise { <pending> }

// it seems like something is wrong here :(
client.stats.get().then((data) => {
  console.log(data)
})

我期待控制台中的输出类似于:

{
   "data":{
      "count":{
         "blocks":1831387,
         "prices":97,
         "tokens":146,
         "masternodes":11044
      },
      "burned":{
         "address":156009915.966001,
         "fee":237127,
         "auction":473292.86501403,
         "payback":49335487.46135226,
         "emission":82354933.12182175,
         "total":288423830.22026604
      },
      "tvl":{
         "dex":874830839.7451664,
         "masternodes":956478286.6812595,
         "loan":260332219.5450214,
         "total":2091641345.9714475
      },
      "price":{
         "usd":4.164032593301086,
         "usdt":4.164032593301086
      },
      "masternodes":{
         "locked":[
            {
               "weeks":0,
               "count":7435,
               "tvl":655918414.0967871
            },
            {
               "weeks":520,
               "count":2859,
               "tvl":238099383.6849561
            },
            {
               "weeks":260,
               "count":750,
               "tvl":62460488.89951629
            }
         ]
      },
      "loan":{
         "count":{
            "collateralTokens":6,
            "loanTokens":28,
            "openAuctions":8,
            "openVaults":9915,
            "schemes":6
         },
         "value":{
            "collateral":260332219.5450214,
            "loan":136835351.84522375
         }
      },
      "emission":{
         "masternode":84.5329931,
         "dex":64.54739497,
         "community":12.45295518,
         "anchor":0.05072487,
         "burned":92.04027360305092,
         "total":253.62434172305092
      },
      "net":{
         "version":2070000,
         "subversion":"/DeFiChain:2.7.0/",
         "protocolversion":70028
      },
      "blockchain":{
         "difficulty":22297887949.45695
      }
   }
}

有关 Ocean REST API、Defichain 及其 Jellyfish 库的更多信息:

Ocean REST API 是一个鲜为人知的项目,仍处于早期阶段。 Ocean REST API 是由 DeFiChain 托管的全球基础设施项目,旨在简化去中心化轻型应用程序的构建。在 Jellyfish 生态系统的支持下,Ocean 节点分布在世界各地,任何 API 请求都由距离请求者最近的节点提供服务,并具有自动故障转移功能。

https://jellyfish.defichain.com/ocean/

头痛了半天自己解决了。错误显示如下:

TypeError: fn is not a function
at Object.exec (/usr/local/lib/node_modules/truffle/build/webpack:/packages/require/require.js:127:1)
at node:internal/util:360:7
at new Promise (<anonymous>)
at bound exec (node:internal/util:346:12)
at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/exec.js:75:1)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at Command.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/command.js:183:1)

查看第一行,我认为问题可能出在 node_modules/truffle/ 中,与我最初的 rest-api 调用无关。

我发现 truffle exec 没有正确处理 example_script 中本地节点模块的 require 语句,这是 2016 年的一个常见错误(参见: https://github.com/trufflesuite/truffle/issues/255)

然而,在深入的讨论中,我从 Tim Coulter

找到了答案

“由于技术原因,Truffle 2.0 中的 truffle exec 要求您的脚本输出一个已传递回调函数的模块。”

在最初的 post 中,他指的是一个 post,该 post 指向 truffle 的文档站点,该站点今天不再可用。

我在这里 link 找到了这个:

https://trufflesuite.com/docs/truffle/getting-started/writing-external-scripts/

为了使外部脚本 运行 正确,Truffle 希望它们导出一个接受单个参数作为回调的函数:

module.exports = function(callback) {
  // TODO: implement your actions

  // invoke callback
  callback();
}

您可以在此脚本中做任何您想做的事情,只要在脚本完成时调用回调即可。回调接受一个错误作为它的第一个也是唯一的参数。如果提供错误,执行将停止并且进程将 return 一个 non-zero 退出代码。