Web3 1.0:`web3.eth.call(tx)`返回的合同创建内容是什么?

时间:2018-07-28 14:46:00

标签: javascript blockchain ethereum smartcontracts web3

使用web3.eth.call(tx)进行方法调用时,返回值是一个字节字符串,可以使用web3.eth.abi.decodeParameters(abi.outputs, bytestring)对其进行解码。但是对于合同创建交易,我们没有abi.outputs

运行一些测试代码,我可以看到web3返回了一些字节串:

交易:

{ from: '0x0838ab6597248e7fb1c11e582eaf88550cce5fb6',
        to: undefined,
        data: '608060405234801561001057600080fd5b506040516020806101ec8339810160409081529051336000908152602081905291909120556101a8806100446000396000f30060806040526004361061004b5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166370a082318114610050578063a9059cbb14610090575b600080fd5b34801561005c57600080fd5b5061007e73ffffffffffffffffffffffffffffffffffffffff600435166100d5565b60408051918252519081900360200190f35b34801561009c57600080fd5b506100c173ffffffffffffffffffffffffffffffffffffffff600435166024356100e7565b604080519115158252519081900360200190f35b60006020819052908152604090205481565b3360009081526020819052604081205482111561010357600080fd5b336000818152602081815260408083208054879003905573ffffffffffffffffffffffffffffffffffffffff871680845292819020805487019055805186815290519293927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929181900390910190a3506001929150505600a165627a7a72305820329f975019ded1c786284c0fa829c72d65b4e50562f4d0d31707e0140d200c0300295ded32980000000000000000000000000000000000000000000000000000000000000032',
        value: '0x0',
        gas: '0x33274' }

返回的字节串:

'0x60806040526004361061004b5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166370a082318114610050578063a9059cbb14610090575b600080fd5b34801561005c57600080fd5b5061007e73ffffffffffffffffffffffffffffffffffffffff600435166100d5565b60408051918252519081900360200190f35b34801561009c57600080fd5b506100c173ffffffffffffffffffffffffffffffffffffffff600435166024356100e7565b604080519115158252519081900360200190f35b60006020819052908152604090205481565b3360009081526020819052604081205482111561010357600080fd5b336000818152602081815260408083208054879003905573ffffffffffffffffffffffffffffffffffffffff871680845292819020805487019055805186815290519293927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929181900390910190a3506001929150505600a165627a7a72305820329f975019ded1c786284c0fa829c72d65b4e50562f4d0d31707e0140d200c030029'

此字节串中编码了什么信息?我可以找回要部署的合同地址吗?

1 个答案:

答案 0 :(得分:0)

返回的字节串是合同的deployedBytecode,即web3.eth.getCode(contractAddress)返回的字节串。

要获取要部署的合同地址,请使用以下代码:

const rlp = require('rlp');
const keccak = require('keccak');

const sender = '0x123...' // the address that sends the contract creation tx
const nonce = web3.eth.getTransactionCount(sender)
const contractAddress = web3.utils.toChecksumAddress(keccak('keccak256').update(rlp.encode([sender, nonce])).digest('hex').substring(24))