web3.js使用bug汇总及解决方法

2020年2月13日23:04:11以太坊评论901阅读模式
摘要

web3.js使用bug解决:More than one instance of bitcore-lib found;BigNumber Error;gasLimit无法解析而报错;js合约方法调用出现undefine;out of gas;Returned error: VM Exception while processing transaction: invalid opcode;Number can only safely store up to 53 bits;Error encountered, bailing. Network state unknown.

web3.js使用bug汇总及解决方法

 

1.More than one instance of bitcore-lib found

More than one instance of bitcore-lib found. Please make sure to require bitcore-lib and check that submodules do not also include their own bitcore-lib dependency.

解决方法:

找到报错的module所在目录,

1)如果是全局安装的bitcore库,则打开

~/.nvm/versions/node/v4.8.3/lib/node_modules/bitcore/node_modules/insight-api/node_modules/bitcore-lib/index.js

修改:

line 7:

bitcore.versionGuard = function(version) {

Change it to:

bitcore.versionGuard = function(version) { return;
2)如果是本项目module中,则修改对应bitcore-lib中index.js中第7行(直接return)
bitcore.versionGuard = function(version) { return;

2.BigNumber Error

错误描述

// 执行下面代码
var tEvent = mxxDataInstance.Transfer({_sender:'0x60718769f87dbc4f4d7f733659d62be715b1c6f6'}, {fromBlock: 'latest', toBlock: 'latest'});
var logRet = tEvent.formatter(receipt2.logs[0]);
console.log(logRet);
​
报错:
{ BigNumber Error: new BigNumber() not a base 16 number:
    at raise (C:\CodeProject\mxx-contractProject\node_modules\bignumber.js\bignumber.js:1177:25)
    at C:\CodeProject\mxx-contractProject\node_modules\bignumber.js\bignumber.js:1165:33
    at new BigNumber (C:\CodeProject\mxx-contractProject\node_modules\bignumber.js\bignumber.js:212:28)
  at SolidityTypeDynamicBytes.formatOutputDynamicBytes [as _outputFormatter] (C:\CodeProject\mxx-contractProject\node_modules\web3\lib\solidity\formatters.js:208:19)
    at C:\CodeProject\mxx-contractProject\node_modules\web3\lib\solidity\type.js:248:25
    at SolidityTypeDynamicBytes.SolidityType.decode (C:\CodeProject\mxx-contractProject\node_modules\web3\lib\solidity\type.js:249:11)
    at C:\CodeProject\mxx-contractProject\node_modules\web3\lib\solidity\coder.js:207:29
    at Array.map (native)
    at SolidityCoder.decodeParams (C:\CodeProject\mxx-contractProject\node_modules\web3\lib\solidity\coder.js:206:26)
    at SolidityEvent.decode (C:\CodeProject\mxx-contractProject\node_modules\web3\lib\web3\event.js:148:31) name: 'BigNumber Error' }

解决方法

暂时不解析logs中的data

进入工程web3源码包mxx-contractProject\node_modules\web3\lib\solidity(或..\node_modules\trufflecontract\node_modules\web3\lib\solidity\type.js)中,修改type.js,242行加上return。

return (function () {
return; //此处
var dynamicOffset = parseInt('0x' + bytes.substr(offset * 2, 64)); // in bytes
var length = parseInt('0x' + bytes.substr(dynamicOffset * 2, 64)); // in bytes
var roundedLength = Math.floor((length + 31) / 32); // in int
var param = new SolidityParam(bytes.substr(dynamicOffset * 2, ( 1 + roundedLength) * 64), 0);
return self._outputFormatter(param, name);
})();

但这样有一个问题:

mxxInst.residents.call(userAddr).then(result =>{

中result中将不能解析出汉字的Nickname名字

3.gasLimit无法解析而报错

错误描述

Number can only safely store up to 53 bits

解决办法

修改nodejs工程目录下库中formatters.js

..\node_modules\web3-core-helpers\src\formatters.js

的236行,修改为如下:

block.gasLimit = block.gasLimit;

4.js合约方法调用出现undefine

var s = 
StandardEvent.methods.bet('0x0bc1dbc3a2941d13248baa12ebbcd002b4585bf1',3).send({from:'0x66e68484a4004ff896e8a994e6efd28fed43d41a',value:web3Client.utils.toWei('2','ether')},function (err, res) {  //方法名或变量后面()括号不可少
console.log(res);
});

5. out of gas问题

后面加“gas:6000000”参数,如下。

StandardEvent.methods.bet(ret1.args._contractAddress,2).send({from:'0x66e68484a4004ff896e8a994e6efd28fed43d41a', value:web3Client.utils.toWei('2','ether'),gas:6000000});

6. 时间问题

require(block.timestamp >= bettingStartTime); //block.timestamp是以秒为单位,而bettingStartTime是以毫秒为单位,故出错
require(block.timestamp < bettingEndTime);
合约中变量时间必须统一

7. Error: Returned error: VM Exception while processing transaction: invalid opcode

解决方法

账户中没有足够的代币,需先使用mint方法发币,再使用方法

8 .Number can only safely store up to 53 bits

解决方法

修改工程目录下

C:\CodeProject\lottery_project\lottery-eth\node_modules\web3-core-helpers\src\formatters.js

236行

// block.gasLimit = utils.hexToNumber(block.gasLimit);
block.gasLimit = 4000000;

9. compilation failed

web3.js使用bug汇总及解决方法

解决方法

将constructor换成function XXX()

10.迁移合约时报错

错误描述

Error encountered, bailing. Network state unknown. Review successful transactions manually.

Error: The contract code couldn't be stored, please check your gas amount.

web3.js使用bug汇总及解决方法

解决方法1

后面进行代码排查,发现只需要把部署合约的is ERC721继承关系去掉,truffle migrate的时候就可以成功。最后发现,原来是继承关系中,必需实现父contract中的接口方法,否则部署的时候就会报错!Mark!为什么编译的时候不报错呢。。)

在逐一实现ERC721的接口方法后,再进行编译部署,此时可成功部署:

解决方法2

弃用私链,使用testrpc

(1)npm install -g ganache-cli

(2)ganache-cli --secure -u 0 -u 1

(3)修改truffle.js

module.exports = {
    networks: {
        development: {
            host: "localhost",//192.168.5.176
            port: 8545,
            network_id: "*", // Match any network id
        }
    }
};

解决方法3(有效)

原因:gas费问题,在调用处指定gas费

{from:'0x1d585f05604a5249520d7f13f3e3e289c6bdf8c1',gas:6000000}