Let's say I'm doing a crowdsale, but instead of selling a set amount of tokens I'm just giving tokens away at a pre-set price. In other words, my token has the following functoin:
function give(address giveToAddress,uint amount)onlyPermittedContracts{
balanceOf[giveToAddress]+= amount;
}
Now I only want permitted contracts to execute this function. So I add the following modifier:
modifier onlyPermittedContracts{
if(permissibleContracts[msg.sender]!=true){
throw;
}
else if(permissibleContracts[msg.sender]==true){
_;
}
throw;
}
And a way for the owner to add permitted contracts:
function setPermissibleContract(address _contactAddress)onlyOwner{
if (permissibleContracts[_contactAddress] == true){
permissibleContracts[_contactAddress]==false;
}
else{
permissibleContracts[_contactAddress]==true;
}
}
Then, in my crowdsale contract I add the following code:
token public rewardToken = myTokenAddress; //(this is set in constructor)
function () payable{
if (saleClosed)
{throw;}
else{
uint amount = msg.value;
balanceOf[msg.sender] = amount;
amountRaised += amount;
rewardToken.give(msg.sender, amount / tokenPriceInEther)
}
}
My question is whether this is the proper way to interact between tokens and contracts.
Additionally, when I create "token public tokenReward" how does ethereum know that my token is a token as opposed to any other type of contract?