【问题标题】:Web3 1.0: What does `web3.eth.call(tx)` return for a contract creation?Web3 1.0:`web3.eth.call(tx)` 为合约创建返回什么?
【发布时间】:2018-07-28 14:45:59
【问题描述】:

使用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'

这个字节串中编码了什么信息?我可以恢复要部署的合约地址吗?

【问题讨论】:

  • 我不是 100% 确定,但我猜它会返回结果字节码(将驻留在新地址的代码)。如果你只是想在部署合约之前提前知道地址,你可以自己根据发送者地址和 nonce 的哈希计算。
  • 谢谢!这是一个合理的猜测,我会检查是否是这种情况。能否给出提前计算合约地址的代码示例(使用web3 1.0)?
  • 我不确定仅使用 web3.js 是否可以轻松完成。但它是(伪代码)keccak256(rlp([senderAddress, nonce]))。最后 20 个字节是地址。
  • 使用 rlpkeccak 节点模块,我认为这可行:keccak('keccak256').update(rlp.encode([sender, nonce])).digest('hex').substring(24);
  • nonce 的结果web3.eth.getTransactionCount(sender)?或web3.eth.getTransactionCount(sender) + 1

标签: javascript blockchain ethereum smartcontracts web3


【解决方案1】:

返回的字节串是合约的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))

【讨论】:

    猜你喜欢
    • 2018-04-25
    • 2018-12-28
    • 2020-08-25
    • 2020-06-01
    • 2020-01-17
    • 2020-08-26
    • 2019-04-15
    • 2019-06-26
    • 2018-08-18
    相关资源
    最近更新 更多