繁体   English   中英

智能合约方法不是 web3 中的功能

[英]Smart contract method is not a function in web3

我正在尝试遵循 web3 上的旧教程,但出现错误,我认为是由于 Solidity 正在更新。 我有下面显示的以下代码

var express = require("express"),
    Web3 = require("web3"),
    web3;    
if (typeof web3 !== 'undefined') {
  web3 = new Web3(web3.currentProvider);
} else {
  // set the provider you want from Web3.providers
  web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}

web3.eth.defaultAccount = web3.eth.accounts[0];

//define contract variable using ABI from compiled Remix tab.
var myContract = new web3.eth.Contract([abi_data]);

myContract.options.address = 'contract_address';

myContract.methods.totalSupply(function(err,res){
    if(!err){
        console.log(res);
    } else {
        console.log(err);
    }
})

其中 abi_data 是我的合约的 abi 数据,contract_address 是我的合约在 Roptsen 测试网络中的实际地址,totalSupply() 是我在 Ropsten 测试网络上的 Solidity 智能合约中的方法,该方法返回合约中引用的代币的总供应量. 当使用node app.js测试它以查看它是否正确记录时,会返回此错误...

/home/ubuntu/workspace/node_modules/web3-eth-contract/src/index.js:693
    throw errors.InvalidNumberOfParams(args.length, this.method.inputs.length, this.method.name);
    ^

Error: Invalid number of parameters for "totalSupply". Got 1 expected 0!
    at Object.InvalidNumberOfParams (/home/ubuntu/workspace/node_modules/web3-core-helpers/src/errors.js:32:16)
    at Object._createTxObject (/home/ubuntu/workspace/node_modules/web3-eth-contract/src/index.js:693:22)
    at Object.<anonymous> (/home/ubuntu/workspace/client/app.js:290:25)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)

并且 totalSupply 在 Soldiity 中定义如下...

function totalSupply() constant returns (uint256 totalSupply){
    return _totalSupply;
}

使用 Adam 的修复程序,我仍然收到以下错误...

Error: Invalid JSON RPC response: ""
at Object.InvalidResponse (/home/ubuntu/workspace/node_modules/web3-core-helpers/src/errors.js:42:16)
at XMLHttpRequest.request.onreadystatechange (/home/ubuntu/workspace/node_modules/web3-providers-http/src/index.js:73:32)
at XMLHttpRequestEventTarget.dispatchEvent (/home/ubuntu/workspace/node_modules/xhr2/lib/xhr2.js:64:18)
at XMLHttpRequest._setReadyState (/home/ubuntu/workspace/node_modules/xhr2/lib/xhr2.js:354:12)
at XMLHttpRequest._onHttpRequestError (/home/ubuntu/workspace/node_modules/xhr2/lib/xhr2.js:544:12)
at ClientRequest.<anonymous> (/home/ubuntu/workspace/node_modules/xhr2/lib/xhr2.js:414:24)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at Socket.socketErrorListener (_http_client.js:310:9)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1277:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)

仔细看看用于调用方法的web3 1.0文档 (它与0.2x API非常不同)。 要调用契约方法,您需要首先使用contractInstance.methods.methodName()创建方法对象,将“methodName”替换为您要调用的合同中的方法。 您还需要将contract函数的参数传递给此方法(此处不会传递回调)。 使用方法对象,您可以使用call方法(用于constant函数)或send (用于事务)。 totalSupply应该是一个常量函数,所以你的代码应该是:

myContract.methods.totalSupply().call(function(err,res){
    if(!err){
        console.log(res);
    } else {
        console.log(err);
    }
);

或者您可以使用返回的Promise而不是传入回调:

myContract.methods.totalSupply().call().then(function(res){
    console.log(res);
}).catch(function(err) {
    console.log(err);
});

发送事务类似,但使用事件发送器来接收事务哈希,收据等。

实际上有一个简单的解决方法:

我一直在使用express和web3,但web3 1.0.0-beta才是问题所在。 您只需安装稳定版本,如0.19.0或0.20.0

npm install web3@^0.19.0 --save

然后它会工作。

这对我来说是一种新语言,所以当我将 abi 复制到我的变量中并粘贴了一组额外的“[]”时出现了错误,因此它看起来像:

const abi = [[...]];

如果我是对的,应该是:

const abi = [...];

只有一组最外面的方括号。 对于像我这样仍在学习这种语言语法的人来说,这可能只是一个错误。

对于其他任何挣扎的人,请尝试检查您的 ABI json 是否确实包含您尝试调用的方法。 可能是您复制了错误合约的 ABI,因为 Remix 会默认列出默认模板中的第一个合约,而不是您新创建的合约。

暂无
暂无

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

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