1

By factory pattern I mean a contract able to deploy other contracts like in this question: simple bakery factory pattern Each time I trigger

factoryInstance.newContract(args).then(promise);

I get transaction hash after transaction being mined. At this point the newly deployed contract address is already appended to the array inside the Factory contract

address[] public contracts;

Multiple transactions might happen in a short period of time and I want to know, from outside the contract, which one is responsible for a child creation (by its address). But EVM is able to access only the current state of the network (no transaction hashes), and on the front end the transaction receipt is unable to tell which contract has been created by the factory:

contractAddress: null

How do I know which address Y has generated transaction X?

Alternately, is there a way to know which transaction X generated Y? I use ganache-cli and truffle.

  • Could you explain what the Factory contract is? (Perhaps you can share the relevant code.) Reading between the lines, I assume that it has a function called newContract which deploys a new contract and appends its address to an array. Is that right? And you want to find out from outside the contract, for a given contract address, which transaction contained the call to newContract that resulted in that contract being created? – user19510 Dec 12 '17 at 18:03
  • Exactly, I updated the post with a factory example, mine is basically the same. – Maxim Gaina Dec 13 '17 at 7:02
1

You can generate an event with the contract address of the new contract, and inspect the transaction receipt to obtain the address

// Event generated when creating a cookie
event LogNewCookie(address cookie);


function newCookie() public returns (address) {
    Cookie c = new Cookie();
    contracts.push(c);
    LogNewCookie(c);      // <---- generate event
    return c;
}

Now from the client side using truffle you obtain the receipt

const receipt = await factory.newCookie();

// Events generated by the call to newCookie()
const logNewCookie = receipt.logs[0];

// Address is the parameter of the event
const cookieAddress = logNewCookie.args.cookie;
| improve this answer | |
  • It works, now I have to understand how to have the tx hash before being mined, and combined with your answer it is a good way to track the relation (tx.hash, cookieAddress) in Factory pattern. – Maxim Gaina Dec 14 '17 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.