0

I have two contracts. #1 creates tokens and sends them to my address. #2 does some other stuff.

'#1 contains a reference to, say, 0xMyAddress (known in advance, of course) in order to grant it special permission for dealing with the tokens. It also needs to contain a similar reference to the address of contact #2.

Contract #2 needs to contain a reference to the address of contract #1, in order to know what tokens it's dealing with.

How do I reconcile this? I have to deploy each contract before I find out what its address will be, so I can't include #1's address in #2, while also including #2's address in #1.

Am I thinking along the right lines here:

Choose which contract will be deployed first, and in that one, include:

function set_otherContractAddress(address _address) public{
    require(msg.sender == 0xMyAddress);
    otherContractAddress = newVal;

}

Then I can deploy that contract, find out it's address, hard code that address into the other contract, deploy it and find out it's address, and then go back and add it to the first contract with that function ^

I hope that makes sense!

2

That solution seems like a good one to me.

Two other options:

  1. Have one contract deploy the other: FooContract does addr = new BarContract(). Inside BarContract, you can just refer to msg.sender to know FooContract's address.
  2. You can actually predict the address of a contract before deployment. It's just the hash of the deployer's address and nonce. So you could actually hardcode both addresses in advance if you wanted to. (This seems like the worst approach.)
  • Thanks, I'll try it. Also, interesting that the address is deterministic like that - I didn't know! Is my code literally correct, from what you can see? Especially "(address _address)" - that's how I write it? – John Mar 15 '18 at 12:09
  • Your parameter is named _address, but then you're using newVal in the body. Either change it to address newVal or do otherContractAddress = _address. – smarx Mar 15 '18 at 12:38

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.