简体   繁体   中英

Web3 1.0: What does `web3.eth.call(tx)` return for a contract creation?

When using web3.eth.call(tx) for a method call, the return value is a byte string that can be decoded by using web3.eth.abi.decodeParameters(abi.outputs, bytestring) . But for a contract creation transaction, we don't have abi.outputs .

Running some test code I can see that web3 is returning some bytestring:

The transaction:

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

The returned bytestring:

'0x60806040526004361061004b5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166370a082318114610050578063a9059cbb14610090575b600080fd5b34801561005c57600080fd5b5061007e73ffffffffffffffffffffffffffffffffffffffff600435166100d5565b60408051918252519081900360200190f35b34801561009c57600080fd5b506100c173ffffffffffffffffffffffffffffffffffffffff600435166024356100e7565b604080519115158252519081900360200190f35b60006020819052908152604090205481565b3360009081526020819052604081205482111561010357600080fd5b336000818152602081815260408083208054879003905573ffffffffffffffffffffffffffffffffffffffff871680845292819020805487019055805186815290519293927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929181900390910190a3506001929150505600a165627a7a72305820329f975019ded1c786284c0fa829c72d65b4e50562f4d0d31707e0140d200c030029'

What information is encoded in this bytestring? And can I recover the to-be-deployed contract address?

The returned bytestring is the deployedBytecode of the contract, ie it's what is returned by web3.eth.getCode(contractAddress) .

To get the to-be-deployed contract address use the following code:

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM