0

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?

0

In general, the architecture is fine. There are small glitches, probably the most important one is the line:

balanceOf[msg.sender] = amount;

as a user may overwrite his account by performing more that one transaction.

I'd also recommend using a safe math library to check for over and underflows.

This funcion is a bit misleading. It's better to have two separate one to grant and revoke permissions:

function setPermissibleContract(address _contactAddress)onlyOwner{

Your modifier can be simplified:

modifier onlyPermittedContracts{
    if(permissibleContracts[msg.sender]) {
        _;
    } else {
       throw;
    }
}

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?

The usual approach is to define an interface and cast the token address to this interface. If you are interested in the detailed answer how one contract may execute a method from the other one please check this answer.

  • With regards to the "token" question I'm asking more because in my token contract I never specify that it is a token anywhere. So what is the significance behind using a token variable? – bGe Jul 9 '17 at 14:33
  • To answer that correctly I need to see where do you define the identifier "token" in your code. Is it import statement or inline interface? – Jakub Wojciechowski Jul 9 '17 at 19:02
  • Oh I see. It is: contract token { function transfer(address receiver, uint amount){ } } – bGe Jul 10 '17 at 23:36

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.