zhuangjinxin / blockchain

区块链
0 stars 0 forks source link

Ethereum: 调用合约时报错 Error: invalid address #14

Closed zhuangjinxin closed 6 years ago

zhuangjinxin commented 6 years ago
> eth.getCode(contractInstance.address)
"0x606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b3415604e57600080fd5b606260048080359060200190919050506078565b6040518082815260200191505060405180910390f35b60006002820290509190505600a165627a7a72305820e8e05128db9d00a453de656e71eec1ceec964fb4f0118acce284a379eeee51070029"
> contractInstance.multiply(2)
Error: invalid address
    at web3.js:3930:15
    at web3.js:3756:20
    at web3.js:5025:28
    at map (<native code>)
    at web3.js:5024:12
    at web3.js:5050:18
    at web3.js:5075:23
    at web3.js:4137:16
    at apply (<native code>)
    at web3.js:4223:16
zhuangjinxin commented 6 years ago

加上call函数即可正确返回:

> contractInstance.multiply.call(2)
4

关于Call()函数 网友的解答:

A. 这里添加call的原因是因为multiply函数没有添加constant;
B. 调用合约中的方法,应该有两种方式进行调用,一是call(),它直接返回结果,不会写进区块链;二是sendTransaction(),它是一笔交易,会写到区块链中,返回值是交易的哈希值。