2

I'm a little bit confused by the following code. Specifically, what the empty token contract does, and why it helps to create it. Is there no way to create an instance of a contract just by giving an address and then using the functions of that contract? Why do you need to create this empty code?

contract token { function give(address giveToAddress, uint amount) {} }

contract CrowdFund{

    token public rewardToken;

    function CrowdFund (
        string _name, 
        address _sendTo, 
        uint _durationInHours, 
        uint _priceInEther, 
        uint _minimum,
        token _rewardToken
    ){
        startTime = now;
        endTime = now + _durationInHours*60 minutes;
        name = _name;
        destination = _sendTo;
        tokenPriceInEther = _priceInEther*1 ether;
        minimum = minimumSend;
        rewardToken = _rewardToken;
    }

}
bGe
  • 71
  • 2

1 Answers1

1

The page that this code is taken from (www.ethereum.org/crowdsale) has a section called Code Highlights that details what the token contract is for.

The below is taken from that page. Note the part in bold type.


The following line will instantiate a contract at a given address:

tokenReward = token(addressOfTokenUsedAsReward);

Notice that the contract understands what a token is because we defined it earlier by starting the code with:

contract token { function transfer(address receiver, uint amount){  } }

This doesn't fully describe how the contract works or all the functions it has, but describes only the ones this contract needs: a token is a contract with a transfer function, and we have one at this address.


So this tutorial gives you a framework only. It's up to the developer to flesh it out and add code in the way they want.

(And remember, you should never use code on the main network without knowing exactly what it does, lest you send real money to it.)

Richard Horrocks
  • 37,835
  • 13
  • 87
  • 144
  • So if I actually wanted to perform a function from one contract that is already deployed (let's say the token contract) on a contract I am currently deploying, how would I go about doing that? For example, if I have deployed a token, and I then program a crowdfund for it. Or would I have to code and deploy both of them at the same time? – bGe Jul 21 '17 at 19:13
  • You'd need to make an external call. Have a look at https://ethereum.stackexchange.com/questions/2070/how-to-make-external-contract-function-calls-from-one-contract-to-another or https://ethereum.stackexchange.com/questions/10482/how-to-call-a-contract-from-an-existing-contract or the documentation at https://solidity.readthedocs.io/en/latest/control-structures.html?highlight=extends#external-function-calls – Richard Horrocks Jul 21 '17 at 19:18