无法使用Web3 1.2.1调用智能合约功能

时间:2019-09-25 14:33:15

标签: node.js blockchain ethereum web3 contract

我在本地私有链上迁移了一个简单的智能合约:

pragma solidity >=0.4.22 <0.6.0;
contract test {
    function say() public pure returns (string memory) {
        return "nihao";
    }
}

,然后编写了一个web3程序来调用此智能合约。 web3的版本是v1.2.1

var express = require("express")
var Web3 = require("web3")
var net = require("net")
var http = require("http")

var web3;
var web3 = new Web3('ws://0.0.0.0:12000');

var abi = ...;
var address = ...;

var t = new web3.eth.Contract(abi, address);
t.methods.say().call().then(console.log);

发生错误,我不知道如何解决。 v1.0.0中似乎有一个错误,我现在不知道它是怎么发生的。

(node:30320) UnhandledPromiseRejectionWarning: Error: Returned values aren't val                                                                             id, did it run Out of Gas?
    at ABICoder.decodeParameters (/home/maxuyang/test/node_modules/web3-eth-abi/                                                                             src/index.js:226:15)
    at Contract._decodeMethodReturn (/home/maxuyang/test/node_modules/web3-eth-c                                                                             ontract/src/index.js:465:22)
    at Method.outputFormatter (/home/maxuyang/test/node_modules/web3-eth-contrac                                                                             t/src/index.js:818:46)
    at Method.formatOutput (/home/maxuyang/test/node_modules/web3-core-method/sr                                                                             c/index.js:163:54)
    at sendTxCallback (/home/maxuyang/test/node_modules/web3-core-method/src/ind                                                                             ex.js:473:33)
    at Object.<anonymous> (/home/maxuyang/test/node_modules/web3-core-requestman                                                                             ager/src/index.js:147:9)
    at /home/maxuyang/test/node_modules/web3-providers-ws/src/index.js:127:44
    at Array.forEach (<anonymous>)
    at W3CWebSocket.WebsocketProvider.connection.onmessage (/home/maxuyang/test/                                                                             node_modules/web3-providers-ws/src/index.js:104:36)
    at W3CWebSocket._dispatchEvent [as dispatchEvent] (/home/maxuyang/test/node_                                                                             modules/yaeti/lib/EventTarget.js:107:17)
    at W3CWebSocket.onMessage (/home/maxuyang/test/node_modules/websocket/lib/W3                                                                             CWebSocket.js:234:14)
    at WebSocketConnection.<anonymous> (/home/maxuyang/test/node_modules/websock                                                                             et/lib/W3CWebSocket.js:205:19)
    at WebSocketConnection.emit (events.js:198:13)
    at WebSocketConnection.processFrame (/home/maxuyang/test/node_modules/websoc                                                                             ket/lib/WebSocketConnection.js:554:26)
    at /home/maxuyang/test/node_modules/websocket/lib/WebSocketConnection.js:323                                                                             :40
    at process._tickCallback (internal/process/next_tick.js:61:11)
(node:30320) 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(). (rejectio                                                                             n id: 1)
(node:30320) [DEP0018] DeprecationWarning: Unhandled promise rejections are depr                                                                             ecated. In the future, promise rejections that are not handled will terminate th                                                                             e Node.js process with a non-zero exit code.

0 个答案:

没有答案