1

My idea for college was to make contract where the total amount from contract is shared equally between 2 partners on testnet I am using Mist and have two wallets (accounts) one is called the MainAcc and other partner1. I can push my contract online, there's no errors in compiler I can push it on the blockchain, but when I go to my contract and call function withdraw and choose an user it starts processing, everything looks fine and transaction is mined, but no ether have been moved from contract to partner or MainAcc.

If i compile it Remix it works i can send money beetween Accounts but have warning --- Gas requirement of function PayCheck.withdraw() high: infinite. If the gas requirement of a function is higher than the block gas limit, it cannot be executed. Please avoid loops in your functions or actions that modify large areas of storage (this includes clearing or copying arrays in storage) --- maybe thats reason why it wont work on TestNet

I am really desperate, have made whole seminar about smart contracts (history, idea, crypto currency, blockchain, benefits etc) and Ethereum as biggest platform and only need this simple contract to finish it with cool example :(

Thanks in advance!

pragma solidity ^0.4.18;

contract PayCheck {

    address[] employees = [0xE83fad0b5EdF2999c29a00199Ca9B773A4627239, 0x118531D9C6C9114c8B999Ac1197778d1669Fc6c6];

    mapping (address => uint) withdrawnAmounts;

    constructor() payable public{
    }

    function () payable public{
    }

    modifier canWithdraw() {
        bool contains = false;

        for(uint i = 0; i < employees.length; i++) {
            if(employees[i] == msg.sender) {
                contains = true;
            }
        }
        require(contains);
        _;
    }

    function withdraw() canWithdraw public{
        uint amountAllocated = address(this).balance/employees.length;
        uint amountWithdrawn = withdrawnAmounts[msg.sender];
        uint amount = amountAllocated - amountWithdrawn;
        withdrawnAmounts[msg.sender] = amountWithdrawn + amount;
        if (amount > 0) {
            msg.sender.transfer(amount);
        }

    }

}
2

It works just fine for me.

Initially I was a bit puzzled as I was testing it in Remix and I couldn't see the Ether being received anywhere. In the end it seems Remix doesn't display anywhere if you receive Ether in the transaction (except maybe in the address balance at the top right).

I ended up adding some logging (and changed addresses) and here you can see it works:

pragma solidity ^0.4.18;

contract PayCheck {

    address[] employees = [0xca35b7d915458ef540ade6068dfe2f44e8fa733c, 0x118531D9C6C9114c8B999Ac1197778d1669Fc6c6];

    mapping (address => uint) withdrawnAmounts;

    constructor() payable public{
    }

    function () payable public{
    }

    modifier canWithdraw() {
        bool contains = false;

        for(uint i = 0; i < employees.length; i++) {
            if(employees[i] == msg.sender) {
                contains = true;
            }
        }
        require(contains);
        _;
    }

    event DebugLog(string, uint256);

    function withdraw() canWithdraw public{
        uint amountAllocated = address(this).balance/employees.length;
        uint amountWithdrawn = withdrawnAmounts[msg.sender];
        uint amount = amountAllocated - amountWithdrawn;
        withdrawnAmounts[msg.sender] = amountWithdrawn + amount;
        if (amount > 0) {
            emit DebugLog("balance before", msg.sender.balance);
            msg.sender.transfer(amount);
            emit DebugLog("balance after", msg.sender.balance);
        }

    }
}

I hope you are also aware that this code is only valid for demonstration purposes - it has lots of underflow/overflow errors and other validation problems.

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.