0

Wasn't quite sure how to word the title, but in the following code:

contract KittyInterface {
...
}

contract ZombieFeeding is ZombieFactory {

KittyInterface kittyContract;

}

what is the declaration KittyInterface kittyContract; creating? I saw that it can be be initialized with KittyInterface kittyContract = KittyInterface(ckAddress); where ckAddress is the address of the interface contract that is being used.

I read the contracts section of the solidity documentation but it hasn't really answered my question. If anyone can forward me to a resource to better understand this I would be very appreciative!

1
  • It declares a storage variable. Commented Jan 12, 2020 at 16:32

2 Answers 2

1

It declares a storage variable of type KittyInterface. You can also define a storage variable to be a contract type and then use it in your contract instance.

An example is given in the solidity docs: https://solidity.readthedocs.io/en/v0.6.1/contracts.html#creating-contracts

contract OwnedToken {
    // `TokenCreator` is a contract type that is defined below.
    // It is fine to reference it as long as it is not used
    // to create a new contract.
    TokenCreator creator;
    address owner;
    bytes32 name;

    // This is the constructor which registers the
    // creator and the assigned name.
    constructor(bytes32 _name) public {

        owner = msg.sender;
        name = _name;

        // We perform an explicit type conversion from `address`
        // to `TokenCreator` and assume that the type of
        // the calling contract is `TokenCreator`, there is
        // no real way to verify that.
        // This does not create a new contract.
        creator = TokenCreator(msg.sender);

    }

    function transfer(address newOwner) public {
        if (msg.sender != owner) return;

        // We ask the creator contract if the transfer
        // should proceed by using a function of the
        // `TokenCreator` contract defined below. If
        // the call fails (e.g. due to out-of-gas),
        // the execution also fails here.
        if (creator.isTokenTransferOK(owner, newOwner))
            owner = newOwner;
    }

    [...rest snipped...]
}
0

The Type includes the functions and a deployment address, once its instantiated. So, instances will have methods that correspond to the contract functions.

For example, a function might take an address for a KittyContract and then use it for something. Since your example stored exactly one, let us say that's part of the constructor.

constructor(address k) public {
  kittyContract = KittyContract(k);
}

Now, we're all set to

kittyContract.anyFunction(args...)

or if it returns something

bool success = kittyContract.anyFunction()...

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.