Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the address of a contract deployed by another contract

I am trying to deploy a contract from another factory contract and then return the address of the newly created contract. The address it returns however is the transaction hash not the contract address. I believe this is because the contract is not yet mined when the address is returned. When I deploy a contract using the web3 deploy it seems to wait until the contract is deployed before outputting the address.

The factory contract:

contract Factory {
mapping(uint256 => Contract) deployedContracts;
uint256 numContracts;
function Factory(){
    numContracts = 0;
}

function createContract (uint32 name) returns (address){
    deployedContracts[numContracts] = new Contract(name);
    numContracts++;
    return deployedContracts[numContracts];
}}

This is how I am calling the createContract function.

factory.createContract(2,function(err, res){
        if (err){
            console.log(err)
        }else{
        console.log(res)
        }
    });
like image 825
Joe Avatar asked Feb 14 '17 15:02

Joe


People also ask

Is it possible to deploy one contract by another contract?

Smart contracts are able to create or deploy other contracts. They can also call functions of other smart contracts.

How do you call a contract from another contract?

The easiest way to is to just call it, like A. foo(x, y, z) . Another way to call other contracts is to use the low-level call . This method is not recommended.

Can two smart contracts talk to each other?

They are commonly written in the Solidity language. On the Ethereum blockchain, smart contracts can interact with existing deployed contracts. This also applies to any other EVM-compatible blockchain (any blockchain that was created using the Ethereum virtual machine).


1 Answers

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)));
like image 186
Samuel Hawksby-Robinson Avatar answered Nov 09 '22 12:11

Samuel Hawksby-Robinson