I'm making a contract that makes other contracts. Once the contract is made using address tokenAddress = new MyToken('Name'); I want to save the address of the contract, tokenAddress, in MyStruct. The problem is that myMapping[uuid].tokenAddress is always 0x0000000000000000000000000000000000000000. I'm guessing this is the default.

My question is, how do I get the address of where a contract is deployed?

struct MyStruct {
    string someData;
    address tokenAddress;

mapping (uint256 => MyStruct) myMapping;

function makeContract(uint256 uuid) view public {
    address tokenAddress = new MyToken('Name');
    myMapping[uuid].tokenAddress = tokenAddress;


I changed the signature of makeContract

function makeContract(uint256 uuid) view public returns (address, uint256) {
    address tokenAddress = new MyToken('Name');
    myMapping[uuid].tokenAddress = tokenAddress;
    return (tokenAddress, uuid);

and now it returns an address. So now I'm trying to figure out why myMapping[uuid].tokenAddress isn't working.

One possible thing is that early in the contract I have to initialize some variables:

function setData(uint256 uuid, string someData) public {
    var struct_ = myMapping[uuid];
    struct_.someData= someData;

then later on I call makeContract. Is it possible the modifying the struct is causing the problem?

  • which version of solidity are you using, can you share the token contract? – Jaime Jan 2 '19 at 21:54
  • @Alexis Are you sure your call to makeContract succeeded? Your code should work. Contract creation never returns 0x0 as address. – Jesbus Jan 2 '19 at 21:58
  • Why do you return one of the input parameters? It apparently does not make sense. – Rick Park Jan 2 '19 at 23:35
  • Returning an input variable just for testing/sanity.. Will remove. – Alexis Jan 2 '19 at 23:40

Don’t you face errors from compiler?

Any ‘never created’ and ‘not existent’ mapping element returns zero if you try to read from it.

Consider that this is not a view function, it cannot be because you write in storage.

    myMapping[uuid].tokenAddress = tokenAddress;

This cannot be executed by a view function!

You can paste your code in remix and follow compiler warning and errors. It can help.

I add that the use of ‘new’ you are doing is correct: just be sure to have gas for it.

| improve this answer | |
  • The code is a little bit more complicated so was trying to make it easy with a smaller example. Good pickup. I fixed the code. – Alexis Jan 2 '19 at 23:30
  • pragma solidity ^0.4.23; – Alexis Jan 2 '19 at 23:34
  • I suggest you try to fix the code from a syntactic point of view in order to reflect your aim and use. After it you can resubmit it if it does not work. The use of new you do is correct, just be sure to have enough gas for it. – Rick Park Jan 2 '19 at 23:38
  • Ahhhhh, I see. I think the view was the issue. Am trying it now. – Alexis Jan 2 '19 at 23:38
  • 2
    I suggest hardly to use remix. You should have remix to develop your code and, optionally, you can use truffle+solc to have a test workbench if you encounter some limitation. Remix is grown up very much in the last year. Moreover assist you in avoiding avoidable errors like this and to focus on the code. I work using all the colors (remix, ganache, truffle, Geth, etc etc), but by far my preferite IDE is remix. I must be honest and say that I never tested Vcode for Solidity, even if I use it for JavaScript. Someone claim it is cool. At the moment I have some urgent work to do... next! – Rick Park Jan 2 '19 at 23:57

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.