1

I'm trying to implement this pattern:

contract Producer {
  uint8 public SOME_VALUE=12;
}
contract Consumer {
  Producter currentProducer;
  function setProducer(address addr) { currentProducer = Producer(addr); }
  function getSome() returns (uint8) { return currentProducer.SOME_VALUE(); }
}

It compiles, I can deploy my Consumer, and I can it bind a producer.

But I want to ensure addr is really a Producer when I bind it to my Consumer:

function setProducer(address addr) { 
    Producer newProducer = Producer(addr);
    require(newProducer.SOME_VALUE() > 0);
    currentProducer =newProducer; 
}

This way, I'm sure I'm linking a Producer and not anything else (I don't like the way I do this, but that's the best solution I found yet)

But by just adding this require, I'm no more able to deploy. The gas needed is sky rocketing... may be infinite as, no matter how high I put it, the transaction fails after reaching the limit (Same behavior on Remix or on a local testnet). Please note that, a the time of deployment, the setProducer() function is not called.

Is this a bug? Or most probable: am I missing something?

Thanks for your help

2

I implemented your code adding a few details and it works. The only odd thing is that you define the type Producter and is Producer. So the problems is basically a typo. (it happens)

pragma solidity ^0.4.23;

contract Producer {
  uint8 public SOME_VALUE=12;
}

contract Consumer {
    Producer currentProducer;

    function setProducer(address addr) public { 
        Producer newProducer = Producer(addr);
        require(newProducer.SOME_VALUE() > 0);
        currentProducer =newProducer; 
    }

    function getSome() public view returns (uint8) {
        return currentProducer.SOME_VALUE(); 

    }
}

Hope this helps

  • Thanks a lot Jaime. My bad: there was a mistake in the sample code I provided. In the original one, the state variable was set in its declaration (I edited my post to correct). Furthermore, I found out that the issue was not in the particular "require()" I pointed out. If I comment out any random line calling the Producer, I can deploy... it fails only when all calls to Producer functions are left uncommented. #headhache – Phililippe May 18 '18 at 15:41
  • Ok. I updated the answer, you have a typo in the code. I implemented and test it using remix. See the detail in the answer. Let me know how it goes. – Jaime May 18 '18 at 21:30

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.