0

Lets assume situation is this. I have a contract where there are some ERC20 tokens are stored. And there are some whitelisted addresses. I want those addresses to be able to withdraw tokens to their addresses.

example code:

     pragma solidity ^0.4.0;
            contract TokenInterface {
                function transfer(address _from, address _to, uint _value) returns (bool success);
            }

        contract Example contract {  

            ...

            TokenInterface private _instance;
            function claimTokens () public onlyWhitelisted returns (bool){
                require(!_investors[msg.sender].claimed);
                uint tokensToBeClaimed =  _investors[msg.sender].invested * ratio;
                if(_instance.transfer(contractAddress, msg.sender, tokensToBeClaimed)){
                     _investors[msg.sender].claimed = true;
                      return true;
                } else {
                    _investors[msg.sender].claimed = false;
                     return false;
                }
            }
            ....
        }

Its not full code, but main thing is here. I am sure this is not the way to do it, and maybe someone will help me?

EDIT.

I've found this line of code.

contract TokenInterface {
    function transferFrom(address from, address to, uint tokens) public returns (bool success);
    function approve(address spender, uint tokens) public returns (bool success);
}

and then in claimTokens function:

_instance.approve(msg.sender, tokensToBeClaimed);
 _instance.transferFrom(contractAddress, msg.sender, tokensToBeClaimed)

can someone approve?

  • Might be of help: ethereum.stackexchange.com/questions/23945/… – Richard Horrocks Apr 9 '18 at 14:27
  • i have whitelist function, everything is fine there, i need token withdrawal function – Lukas Junokas Apr 9 '18 at 14:29
  • Need some explanation. Do you have a token contract and another contract that have a balance of tokens in the token contract, right? What you want to do is to allow people to withdraw tokens from your contract balance? Please give as much information as possible in order to be able to help. – Jaime Apr 9 '18 at 14:57
  • it just simple escrow contract. The token contract is not mine, i am just a middle man. So i need for specific addresses to be able to withdraw tokens from my contract. – Lukas Junokas Apr 9 '18 at 15:01
0

Your original idea will work. When they claim the token your contract will transfer the tokens from its balance to the user that is claiming.

Your contract is missing to define the token contract address to define the instance correctly.

TokenInterface _instance = TokenInterface('tokenContractAddress')

The function will be then:

function claimTokens () public onlyWhitelisted returns (bool){
                require(!_investors[msg.sender].claimed);
                TokenInterface _instance = TokenInterface('the_token_contract_address');
                uint tokensToBeClaimed =  _investors[msg.sender].invested * ratio;
                _investors[msg.sender].claimed = true;                   
                _instance.transfer(contractAddress, msg.sender, tokensToBeClaimed));
                return true;
            }

The conditional is not necessary because if something goes wrong during the transfer the ERC20 contract will throw.

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.