2

I'm following cryptozombies solidity tutorial. In lesson 2, chapter 11 it explains how to create an interface for CryptoKitties as follows:

contract KittyInterface {
  function getKitty(uint256 _id) external view returns (
    bool isGestating,
    bool isReady,
    uint256 cooldownIndex,
    uint256 nextActionAt,
    uint256 siringWithId,
    uint256 birthTime,
    uint256 matronId,
    uint256 sireId,
    uint256 generation,
    uint256 genes
  );
}

then it initializes it in a contract:

contract ZombieFeeding is ZombieFactory {

  address ckAddress = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
  KittyInterface kittyContract = KittyInterface(ckAddress); // this initialization is what I don't get

  function feedAndMultiply(uint _zombieId, uint _targetDna) public {
    require(msg.sender == zombieToOwner[_zombieId]);
    Zombie storage myZombie = zombies[_zombieId];
    _targetDna = _targetDna % dnaModulus;
    uint newDna = (myZombie.dna + _targetDna) / 2;
    _createZombie("NoName", newDna);
  }    
}

I don't get how the interface can be initialized using when no constructor is defined in KittyInterface.

I saw that usually the keyword interface is used to define interfaces (instead of contract) and I was thinking maybe the compiler knows that an interface can be initialized using an adress, but that's is not the case here.

If someone could explain how this works, it would be wonderful.

1
address ckAddress = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
KittyInterface kittyContract = KittyInterface(ckAddress); // this initialization is what I don't get

That line isn't doing any sort of "initalization." It's just casting the address. It means that kittyContract is now a contract of type KittyInterface, and that when you call functions on that contract, they should be sent to the address ckAddress.

  • This makes sense, I didn't know you could do that, thanks a lot :-) – gionni Jan 31 '18 at 11:15
2

An interface explains to the compiler what functions are available to be called on an external contract without requiring the full source code of that contract to be imported.

In this case, the kittyContract has been deployed completely separately to this zombie contract. So on the ethereum blockchain this kittyContract exists and now this new, unrelated contract wants to be able to make calls to the functions of that kittyContract. For this to work, ZombieFeeding needs to know which functions are available in the external contract so that it can ensure the correct data is being passed etc.

When defining an interface you don't have to define all the functions or public variables in an external contract, you only need to define the ones you'll be interacting with in your contract.

  • Ok, I get that, what I don't understand is how the compiler knows that the contract called KityInterface is tied to the CryptoKittie contract. Nowhere have we told the compiler that the contract is an interface (unless it reads it from the name of the contract) and neither have we defined a constructor for such contract; so, how can it know how to initialize the contract? Is it some built in feature of Solidity? – gionni Jan 31 '18 at 11:07
  • The names are irrelevant, we could call it PotatoInterface in our contract and it would still work just the same, it's simply a variable name for our own usage. The only thing that matters is the address 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d which points to the external contract. We are not instatiating any version of this contract, we are simply making remote procedure calls to the functions (or public data) of this external contract that has already been deployed to the blockchain, identified by that address. – Nanolucas Jan 31 '18 at 11:12
  • Thanks a lot I think I get it now. I got confused because I never saw a similar syntax. – gionni Jan 31 '18 at 11:15

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.