[Ethereum] How to create withdrawal function from the contract

contract-designcontract-developmentsolidity

I want send ether from my contract to my address(owner). How can I transfer ether from my contract balance(this.balance) to my address(owner)? I use myEthterWallet to do this things. Address of this contract in the Ropsten testnet is 0x614Fe0B3C9E5E2157Ab76F0E91B819279854f709 .

I have contract:

pragma solidity ^0.4.15;


contract WithdrawalContract {

    address public richest;
    address public owner;
    uint public mostSent;

    modifier onlyOwner() {
        require (msg.sender != owner);
        _;

    }

    mapping (address => uint) pendingWithdraws;

    function WithdrawalContract () payable {
        richest = msg.sender;
        mostSent = msg.value;
        owner = msg.sender;
    }

    function becomeRichest() payable returns (bool){
        require(msg.value > mostSent);
        pendingWithdraws[richest] += msg.value;
        richest = msg.sender;
        mostSent = msg.value;
        return true;
    }

    function withdraw(uint amount) onlyOwner returns(bool) {
        // uint amount = pendingWithdraws[msg.sender];
        // pendingWithdraws[msg.sender] = 0;
        // msg.sender.transfer(amount);
        require(amount < this.balance);
        owner.transfer(amount);
        return true;

    }

    function getBalanceContract() constant returns(uint){
        return this.balance;
    }

}

Best Answer

You have a nasty typo here. This allowed me to trigger a funds transfer out of your contract (only 1 Ropsten Wei):

modifier onlyOwner() {
    require (msg.sender != owner);
    _;
}

This needs to be (msg.sender == owner). On the plus side, the Wei only got sent to your owner account (not to me) and I was able to test the process below. But I guess this is not what you intended.

Go to MyEtherWallet's "Contracts" tab. Enter your contract address, and you'll need an ABI. I created the following ABI by pasting your contract into Remix and clicking the Details button after it compiles - what you need to copy is under "INTERFACE - ABI".

[{"constant":true,"inputs":[],"name":"richest","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"amount","type":"uint256"}],"name":"withdraw","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getBalanceContract","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"mostSent","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"becomeRichest","outputs":[{"name":"","type":"bool"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":true,"stateMutability":"payable","type":"constructor"}]

Back in MEW, click Access: now you can interact with your contract.

enter image description here

Choose "withdraw" from the dropdown, enter the Wei amount and click the big "Write" button at the bottom. Then you need to unlock your Ropsten account etc.

Due to the error above, you'll need to trigger the funds withdrawal using an account that is not the owner (ie. not 0xc0b4ec83028307053fbe8d00ba4372384fe4b52b).

Related Topic