Getting the address of a contract deployed by another contract

此生再无相见时 提交于 2019-12-03 03:24:55
Samuel Hawksby-Robinson

Consider the below example. There are a number of ways you can get the address of the contract:

contract Object {

    string name;
    function Object(String _name) {
        name = _name
    }
}

contract ObjectFactory {
    function createObject(string name) returns (address objectAddress) {
        return address(new Object(name));
    }
}

1 Store the Address and Return it:

Store the address in the contract as an attribute and retrieve it using a normal getter method.

contract ObjectFactory {
    Object public theObj;

    function createObject(string name) returns (address objectAddress) {
        theObj = address(new Object(name));
        return theObj;
    }
}

2 Call Before You Make A Transaction

You can make a call before you make a transaction:

var address = web3.eth.contract(objectFactoryAbi)
    .at(contractFactoryAddress)
    .createObject.call("object");

Once you have the address perform the transaction:

var txHash = web3.eth.contract(objectFactoryAbi)
    .at(contractFactoryAddress)
    .createObject("object", { gas: price, from: accountAddress });

3 Calculate the Future Address

Otherwise, you can calculate the address of the future contract like so:

var ethJsUtil = require('ethereumjs-util');
var futureAddress = ethJsUtil.bufferToHex(ethJsUtil.generateAddress(
      contractFactoryAddress,
      await web3.eth.getTransactionCount(contractFactoryAddress)));

We ran across this problem today, and we're solving it as follows:

In the creation of the new contract raise an event.

Then once the block has been mined use the transaction hash and call web3.eth.getTransaction: http://web3js.readthedocs.io/en/1.0/web3-eth.html#gettransaction

Then look at the logs object and you should find the event called by your newly created contract with its address.

Note: this assumes you're able to update the Solidity code for the contract being created, or that it already calls such an event upon creation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!