web3 call got 'ERROR: The returned value is not a convertible string:'

web3 call got 'ERROR: The returned value is not a convertible string:'

这是我的合同

https://kovan.etherscan.io/address/0x9c08fb4e6666a796ef1ade3f58cb0a3e3f469e7c#code

我试图通过 web3 调用合约中的函数,例如:

//address and abi are copied from url above
let contractAddr = contract.address 
let contractAbi = contract.abi
let web3 = new Web3(new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/ws'))
if (typeof web3 !== 'undefined') {
  web3 = new Web3(web3.currentProvider)
} else {
  console.log('we need MetaMask')
}

let myContract = new web3.eth.Contract(contractAbi, contractAddr)
myContract.methods.name().call().then(console.log).catch(console.log)

我知道了:

Error: ERROR: The returned value is not a convertible string:

但是,如果我将合同复制到

https://remix.ethereum.org/#optimize=true&version=soljson-v0.4.24+commit.e67f0147.js

并使用ganache。那么我的代码将是:

//address and abi are copied from url above
let contractAddr = contract.address
let contractAbi = contract.abi
let url = contract.url //http://127.0.0.1:7545 provided by ganache
let web3
if (typeof web3 !== 'undefined') {
  // web3 = new Web3(web3.currentProvider)
} else {
  web3 = new Web3(new Web3.providers.HttpProvider(url))
}
let myContract = new web3.eth.Contract(contractAbi, contractAddr)
myContract.methods.name().call().then(console.log).catch(console.log)

在这种情况下,我会得到正确的结果'MOMO'。

我认为 infura 的工作方式与 ganache 相同,我也尝试过其他 infura 网址,但都失败了。

我的 chrome 扩展中有 metaMask 并使用我们 web3@^1.0.0-beta.33.

如何调用

中的函数

https://kovan.etherscan.io/address/0x9c08fb4e6666a796ef1ade3f58cb0a3e3f469e7c#code

就像我在

中所说的那样

https://remix.ethereum.org/#optimize=true&version=soljson-v0.4.24+commit.e67f0147.js

来自 ganache

看起来您连接的是主网而不是 kovan:

let web3 = new Web3(new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/ws'))

应该是:

let web3 = new Web3(new Web3.providers.WebsocketProvider('wss://kovan.infura.io/ws'))