web3 调部署上线的合约接口总是失败,本地调就成功

我的合同:

https://kovan.etherscan.io/ad...

我尝试用 web3 调合同里的接口,譬如:

//address and abi 从上面的 URL 里复制过来的
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:

但是如果我把合同拷到 remix 里

https://remix.ethereum.org/#o...

然后用 ganache. 调整代码再在本地调,结果:

//address and abi 从上述 URL 里拷过来的
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'.

所以我的问题是,

我怎么调下面的这个 URL 的接口:

https://kovan.etherscan.io/ad...

就像我调 remix 里的一样

https://remix.ethereum.org/#o...

我尝试过用 infura 的其他节点,也都失败。我用的 chrome 且有 MetaMask 扩展,用的 web3@^1.0.0-beta.33.

阅读 2.8k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题