Multiple Contracts

Since the beginning of the course, you've been writing one contract at a time. The contracts you've engineered starts to get bigger and bigger.

Declaration

To that end, Solidity allows you to write different contracts in the same file and make them interact with each other. You just have to declare the second contract below the first one.

Exercise
Correct!
False!

Loading...

Declare SpaceCookie below SpaceMuffin
pragma solidity ^0.4.24; contract SpaceMuffin { /* No code in here */ } // Declare `SpaceCookie` here

Hint

Both contracts don't have to do anything

Variables

Interestingly, contract can be stored as variables. Once declared, method can be called on them. It can be done as follow

contract Position {
  uint public x = 0;

  function plusOne() view public returns (uint) {
    return x + 1;
  }
}

contract Map {
  Position origin = new Position();

  function plusOne() view public returns (uint) {
    return origin.plusOne();
  }
}

SpaceCookie industry would like to spy on the number of SpaceMuffin fans. Help them by completing the following exercise.

Exercise
Correct!
False!

Loading...

Within SpaceCookie, declare muffin of type SpaceMuffin as public. Then call getFans on it in function spy.
pragma solidity ^0.4.24; contract SpaceMuffin { uint private fans = 1; function getFans() view public returns (uint) { return fans; } } contract SpaceCookie { // Declare `muffin` of type `SpaceMuffin` function spy() view public returns (uint) { return // number of fans the muffin has } }

Hint

  • Don't forget new keyword
  • Is your variable name muffin

Your help to SpaceCookie industry has been greatly appreciated by their supporters. Let's see if you have what it takes to go away from the one and only SpaceMuffin.