2

I'm doing the CryptoZombies tutorial to get a better handle on solidity and in one of the chapters it contains the following code

pragma solidity ^0.4.19;

import "./zombiefactory.sol";

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
  );
}

contract ZombieFeeding is ZombieFactory {

  address ckAddress = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
  KittyInterface kittyContract = KittyInterface(ckAddress);

  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);
  }

  function feedOnKitty(uint _zombieId, uint _kittyId) public {
    uint kittyDna;
    (,,,,,,,,,kittyDna) = kittyContract.getKitty(_kittyId);
    feedAndMultiply(_zombieId, kittyDna);
  }

}

My question is in the KittyInterface kittyContract = KittyInterface(ckAddress) line.

In the local contract ZombieFeeding, there is no reference to KittyInterface, nor is there any context given on why the contract address is passed as a parameter to the contract.

I'm assuming that the address being passing into the contract is allowed because solidity uses this interface to point to some external contract, but it gives no explicit reason why I can reference KittyInterface in my ZombieFeeding contract.

My guess here is that KittyInterface is treated as a global variable, but again, I'm not sure.

1
KittyInterface kittyContract = KittyInterface(ckAddress);

ckAddress is not being passed as a parameter to anything. This is a cast (like uint256(5) or address(0)).

The line is saying "ckAddress is an address of a contract implementing the KittyInterface interface. I would like to reference that contract using the variable kittyContract."

  • Awesome! Thank you so much. I find it strange that they didn't specifically state this in the tutorial, but the way you explained it makes perfect sense. – user8972341 May 29 '18 at 15:12

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.