0

I've just recently started developing in solidity and was wondering if I was using the ERC20 standard correctly. When I attempt to test the contract on remix i get "Error: gas required exceeds allowance or always failing transaction" but on truffle, my tests seem to pass. Any thoughts?

Note: I dont actually have any contract address filled in when referring to the token or lottery contract I just used filler variable names.

pragma solidity ^0.4.17;

contract ERC20Interface {
    function totalSupply() public constant returns (uint);
    function balanceOf(address tokenOwner) public constant returns (uint balance);
    function allowance(address tokenOwner, address spender) public constant 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);

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

contract Lottery {
    address public manager;
    address[] public players;
    // the address of the token that is used in the lottery
    address tokenAddress;

    function Lottery() public {
        manager = msg.sender;
    }

    function enter() public payable {
        require(msg.value > .01 ether);

        players.push(msg.sender);
    }

    function enter() external payable {
        uint amount = msg.value;
        ERC20Interface tokenContract = ERC20Interface(tokenAddress);
        require(tokenContract.transferFrom(msg.sender, this, amount));
        //Add person to the contract if they send tokens
        players.push(msg.sender);
    }

    function random() private view returns (uint) {
        return uint(keccak256(block.difficulty, now, players));
    }

    function pickWinner() public restricted {
        ERC20Interface tokenContract = ERC20Interface(tokenAddress);
        // gets token balance of the lottery contract
        uint contractBalance = tokenContract.balanceOf(address(this));
        uint index = random() % players.length;
        //gets psuedo random number and sends to the player at that index
        tokenContract.transfer( players[index], contractBalance / 2 );
        // sends the remaining balance to a treasury
        address treasury;
        tokenContract.transfer( treasury, contractBalance / 2 );
        //resets player list
        players = new address[](0);
    }

    modifier restricted() {
        require(msg.sender == manager);
        _;
    }

    function getPlayers() public view returns (address[]) {
        return players;
    }
}
1
  • My first thought is that your code contains a whole bunch of variables which are not declared anywhere, and therefore yields multiple compilation errors. So it might be wise to fix those before posting it here, in order to make it easier for anyone who is trying to help out. Jun 27 '18 at 6:44
-1

I think that there exists some problem with your implementation. I have slightly modified your code, and deployed to private network using geth 1.6.7

Interface is the same so I did not include it.

contract Lottery {
    address public manager;
    address[] public players;
    address tokenAddress;

    function Lottery() public {
        manager = msg.sender;
    }

    function enter() public payable {
        require(msg.value > .01 ether);

        players.push(msg.sender);
    }

    function provideService() external payable {
        uint amount = msg.value;
        ERC20Interface tokenContract = ERC20Interface(tokenAddress);
        require(tokenContract.transferFrom(msg.sender, this, amount));
        //Add person to the contract if the send Aether
        players.push(msg.sender);
    }

    function random() private view returns (uint) {
        return uint(keccak256(block.difficulty, now, players));
    }

    function pickWinner() public restricted {
        ERC20Interface tokenContract = ERC20Interface(tokenAddress);
        // gets token balance of the lottery contract
        uint contractBalance = tokenContract.balanceOf(address(this)); 
        uint index = random() % players.length;
        //gets psuedo random number and sends to the player at that index
        tokenContract.transfer( players[index], contractBalance / 2 );
        // sends the remaining balance to a treasury
        address treasury;
        tokenContract.transfer( treasury, contractBalance / 2 );
        //resets player list
        players = new address[](0);
    }

    modifier restricted() {
        require(msg.sender == manager);
        _;
    }

    function getPlayers() public view returns (address[]) {
        return players;
    }
} 

Hope, it works

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.