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.
Loading...
pragma solidity ^0.4.24; contract SpaceMuffin { /* No code in here */ } // As simple as it can be contract SpaceCookie { /* Still, no code required */ }
pragma solidity ^0.4.24; // No validation required
[]
38
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.
Loading...
pragma solidity ^0.4.24; contract SpaceMuffin { uint private fans = 1; function getFans() view public returns (uint) { return fans; } } contract SpaceCookie { // You're doing such an evil work SpaceMuffin muffin = new SpaceMuffin(); function spy() view public returns (uint) { // A truly evil work return muffin.getFans(); } }
pragma solidity ^0.4.24; // TODO: validation
[]
39
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
.