简体   繁体   English

使用web3.js库调用智能合约方法

[英]Calling a smart contract method using web3.js library

I am new to web3.js and I am trying to call a method from omesigo smart contract but I get a confusing error. 我是web3.js的新手,正在尝试从omesigo智能合约调用方法,但出现一个令人困惑的错误。 I have already read web3 doc and tried it. 我已经阅读了web3文档并进行了尝试。

package.json 的package.json

{
  "name": "web3-learn",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "@babel/runtime": "^7.3.1",
    "babel-cli": "^6.26.0",
    "web3": "^1.0.0-beta.38"
  }
}

node version: 10.15.0

index.js related lines: index.js相关行:

let contract = web3.eth.Contract(abi, address);
contract.methods.name().call((err, res) => {console.log(res)});
/home/amin/Documents/projects/solidity-workshop/solidity-learn/src/web3-learn/node_modules/@babel/runtime/helpers/nonIterableSpread.js:2
  throw new TypeError("Invalid attempt to spread non-iterable instance");
  ^

TypeError: Invalid attempt to spread non-iterable instance
    at _nonIterableSpread (/home/amin/Documents/projects/solidity-workshop/solidity-learn/src/web3-learn/node_modules/@babel/runtime/helpers/nonIterableSpread.js:2:9)
    at _toConsumableArray (/home/amin/Documents/projects/solidity-workshop/solidity-learn/src/web3-learn/node_modules/@babel/runtime/helpers/toConsumableArray.js:8:60)
    at CallContractMethod.set (/home/amin/Documents/projects/solidity-workshop/solidity-learn/src/web3-learn/node_modules/web3-core-method/dist/web3-core-method.cjs.js:405:34)
    at MethodsProxy.executeMethod (/home/amin/Documents/projects/solidity-workshop/solidity-learn/src/web3-learn/node_modules/web3-eth-contract/dist/web3-eth-contract.cjs.js:525:26)
    at Function.anonymousFunction.(anonymous function) [as call] (/home/amin/Documents/projects/solidity-workshop/solidity-learn/src/web3-learn/node_modules/web3-eth-contract/dist/web3-eth-contract.cjs.js:497:27)
    at Object.<anonymous> (/home/amin/Documents/projects/solidity-workshop/solidity-learn/src/web3-learn/index.js:34:25)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

It should print Omisego token name. 它应该显示Omisego令牌名称。 Thanks in advance for your help. 在此先感谢您的帮助。

I was experiencing the same error, and solved it by upgrading the Web3 library to "web3": "^1.0.0-beta.39" 我遇到了同样的错误,并通过将Web3库升级到"web3": "^1.0.0-beta.39"来解决了该错误"web3": "^1.0.0-beta.39"

Looking deeper into the error, it seems related to the deep cloning of contract.method arguments . 深入研究错误,似乎与深度克隆contract.method arguments After a few hours of fruitless debugging, I stumbled upon the Web3 releases and saw that it was a featured release of beta.39 . 经过几个小时的徒劳无功的调试之后,我偶然发现了Web3版本,并发现它是beta.39的功能版本。 (released a day ago) (一天前发布)

I have been facing the same issue and the only difference from your approach is the version of web3 api, which for me is 1.0.0-beta.46. 我一直面临着相同的问题,与您的方法唯一的不同是web3 api的版本,对我来说是1.0.0-beta.46。 The resolution of the issue seems far fetched. 这个问题的解决似乎很牵强。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM