2

This is related to Are Monolithic Smart Contracts Better than Those That Are Decoupled?, where I basically have the following setup:

contract A {  
   int private x;          
   function save(int _x) {
       x = _x;
   }
   function get() returns (int) {
       return x;
   }
}

contract B {    
   A private a;    
   function B(address _a) {
       a = A(_a);
   }    
   function doStuff() {
       a.save(42);
   }
   function getStuff()  returns (int) {
       return a.get();
   }
}

contract C {
   B private b;    
   function C(address _b) {
       b = B(_b);
   }
   function calculateTheMeaning() {
      b.doStuff();
   }
   function getTheAnswer() returns (int) {
      return b.getStuff();
   }
}

That's working, but rather than pass around the addresses, I'd much prefer to instantiate the contracts:

contract A {  
   int private x;          
   function save(int _x) {
       x = _x;
   }
   function get() returns (int) {
       return x;
   }
}

import "A.sol";
contract B {    
   A private a;    
   function B() {
       A a = new A();
   }    
   function doStuff() {
       a.save(42);
   }
   function getStuff()  returns (int) {
       return a.get();
   }
}

import "B.sol";
contract C {
   B private b;    
   function C() {
       B b = new B();
   }
   function calculateTheMeaning() {
      b.doStuff();
   }
   function getTheAnswer() returns (int) {
      return b.getStuff();
   }
}

Trouble is, no matter how I do it (embark blockchain, http://ethereum.github.io/browser-solidity/, testrpc, whatever), I can't get that to deploy properly - I either get invalid jumps or no results whatsoever.

Currently I have all those contracts in separate files and I was hoping that deploying C.sol alone would suffice (since it includes B which includes A). However, it seems not. I'm doing something wrong, so I'd be grateful for any pointers.

  • 1
    I'm not sure if this is your problem or not but I think you need a = new A(); not A a = new A(); – Edmund Edgar Oct 31 '16 at 23:10
  • @EdmundEdgar doh! Good spot! That could well be it, couldn't it! I'll keep you posted. Schoolboy error ;) – glowkeeper Nov 1 '16 at 20:37
  • 1
    Yup - that was it! Doh! I'm off to go stand in the corner with my back to the room :( – glowkeeper Nov 1 '16 at 20:51
  • 1
    Now someone post and answer and accept it ;-) – Waqar Lim Nov 2 '16 at 11:02
2

Eek! So here's where I admit to making a schoolboy error :(

As @EdmundEdgar pointed out, the reason my instantiation of new contracts was failing was because the constructors were declaring those new contracts as local variables. Hence, the class variables were not having the addresses assigned to them correctly, causing "Invalid Jumps". Here's the corrected code:

contract A {  
   int private x;          
   function save(int _x) {
       x = _x;
   }
   function get() returns (int) {
       return x;
   }
}

import "A.sol";
contract B {    
   A private a;    
   function B() {
       a = new A();
   }    
   function doStuff() {
       a.save(42);
   }
   function getStuff()  returns (int) {
       return a.get();
   }
}

import "B.sol";
contract C {
   B private b;    
   function C() {
       b = new B();
   }
   function calculateTheMeaning() {
      b.doStuff();
   }
   function getTheAnswer() returns (int) {
      return b.getStuff();
   }
}

Doh!

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.