Iman
Iman

Reputation: 483

Problem with sending ERC20 tokens from one smart contract to another one

I have an ERC20 token (Cryptos) and want to send some tokens to VulnerableTokenSale smart contract, but it shows zero balance for the token balance (get_balanceToken function). Is it possible to do that? I am using Remix on Ethereum website.

     contract VulnerableTokenSale {
        address public wallet;
        uint256 rate;
        ERC20 public token;
        address public owner;


        mapping(address => uint256) public balances;
        address [] beneficiaries;

        modifier onlyOwner() {
            require(msg.sender == owner, "not owner");
            _;
        }

        constructor(address _wallet, uint256 _rate, ERC20 _token) 
            public {
                wallet = _wallet;
                rate = _rate;
                token = _token;
                owner = msg.sender;
            }

        function sendTokensWithRatio(uint256 _numerator, uint256 _denominator) external onlyOwner {

            require(_numerator <= _denominator);

            for(uint256 i = 0; i < beneficiaries.length; i++){
                address beneficiary = beneficiaries[i];
                uint256 balance = balances[beneficiary];

                if(balance > 0) {
                    uint256 amount = balance * _numerator;
                    amount = amount / _denominator;
                    balances[beneficiary] = balance - amount;
                    token.transfer(beneficiary, amount);
                }
            }
        }

        function purchaseTokens() public payable{
            uint256 weiAmount = msg.value;

            uint256 _tokenAmount = weiAmount * rate;
            beneficiaries.push(msg.sender);
            balances[msg.sender] = balances[msg.sender] + _tokenAmount;
        }

    }

Upvotes: 0

Views: 759

Answers (1)

Over Age
Over Age

Reputation: 21

Think your constructor is slightly wrong, I have never seen anyone take in an ERC token directly.

I have something like this in my contracts:

constructor(address _wallet, uint256 _rate, address _tokenAddress) public {
    wallet = _wallet;
    rate = _rate;
    token = ERC20(_tokenAddress);
    owner = msg.sender;
}

Note you need the ERC20 interface for the above to work:

interface ERC20 {
    function totalSupply() public view returns (uint);
    function balanceOf(address tokenOwner) public view returns (uint balance);
    function allowance(address tokenOwner, address spender) public view returns (uint remaining);
    function transfer(address to, uint tokens) public returns (bool success);
    function approve(address spender, uint tokens) public returns (bool success);
    function transferFrom(address from, address to, uint tokens) public returns (bool success);
    // optional
    function name() external view returns (string);
    function symbol() external view returns (string);
    function decimals() external view returns (string);

    event Transfer(address indexed from, address indexed to, uint tokens);
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}

Upvotes: 1

Related Questions