Addresses

Let's get back to the code you've written just before

contract SpaceMuffin {}

contract SpaceCookie {
  SpaceMuffin muffin = new SpaceMuffin();
}

From a reader perspective, you are declaring a *new* SpaceMuffin. Therefore the number of fans you retrieve doesn't come from the one and only SpaceMuffin.

Addresses to contract

When you deploy your contract with Metamask, it is publish at a given address. Your SpacecryptFactory has one, SpaceCookie has one, and SpaceMuffin has one. This address is unique. Anyone is able to see what's in there but noone is able to overwrite it. It's an identity for the contract. If you communicate with your bank, you know they are located in city centre. If the exact same office was to open in a random place you won't trust them for distant communication.

An Ethereum address looks like this: 0xe1a15b265e58b7be2e8a626aee8e27b5d288a1b7. It is string of length 42, starting with 0x followed by letters and number. Because it is complex to type, Solidity enforced it to be checksumed. Previous address looks like 0xe1A15B265e58B7bE2E8A626AeE8e27b5d288a1B7 when checksumed. It provides a better guarantee the address has been typed correctly.

This address is actually not a random one. It is the address of the one and only SpaceMuffin. To associate muffin variable to this address, you simply have to do the following:

contract SpaceMuffin {}

contract SpaceCookie {
  // no `new` keyword, it has been deployed before
  SpaceMuffin muffin = SpaceMuffin(0xe1A15B265e58B7bE2E8A626AeE8e27b5d288a1B7);
}

Contract to addresses

Because SpaceMuffin is unique, your SpaceCookie wants to be unique too. In order to do it, it should be able to share its unique address with the world. To access contract address, you simply need to type address(this) from within the contract.

this is a keyword accessible from within the contract. It is the public representation of the contract. Therefore when converting it to an address it gives the contract public address.

Exercise
Correct!
False!

Loading...

Write getAddress function. It should return your Spacecookie's address
pragma solidity ^0.4.24; contract SpaceCookie { function getAddress() view public returns (address) { // I told you everything, it's your time to shine now } }

Wow! SpaceMuffin starts to have competition. Or maybe not. SpaceCookie is a pale copy.