0

I'm writing my first contract and my withdraw function doesnt work. Eth doesnt move to my _withdrawAddr.

Also can somebody explain why my function getBalanceThis returns 0 instead of amount I send to contract and my other GETTER functions getBalanceOwner, getBalanceBuyer and getBalanceWithdrawAddr all return value..

pragma solidity ^0.5.0;

contract Test{

    address payable owner;
    address payable buyerAddr;
    address payable withdrawAddr;

    mapping(address => uint) balances;

    constructor(address payable _buyerAddr, address payable _withdrawAddr) payable public {
        owner = msg.sender;
        buyerAddr = _buyerAddr;
        withdrawAddr = _withdrawAddr;
    }

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

    modifier onlyBuyer(){
        require(msg.sender == buyerAddr);
        _;
    }

    function Send() payable public onlyBuyer{
        owner.transfer(msg.value);
    }

    function getBalanceOwner() view public returns(uint){
        return owner.balance;
    }

    function getBalanceBuyer() view public returns(uint){
        return buyerAddr.balance;
    }

    function getBalanceWithdrawAddr() view public returns(uint){
        return withdrawAddr.balance;
    }

    function getBalanceThis() view public returns(uint){
        return address(this).balance;
    }

    function Withdraw() onlyOwner public returns(bool success)  {
        uint wd = 5 ether;
        withdrawAddr.transfer(wd);
        return true;
    }



}
0

The ether doesn't move in your Withdraw() function because you do the transfer in the Send() function, so there is no ether to transfer. For that reason there is no ether in the contract, so the function getBalanceThis() will return 0.

Alternative solution:

pragma solidity ^0.5.0;

contract Test{

    address payable owner;
    address payable buyerAddr;
    address payable withdrawAddr;

    mapping(address => uint) balances;

    constructor(address payable _buyerAddr, address payable _withdrawAddr) payable public {
        owner = msg.sender;
        buyerAddr = _buyerAddr;
        withdrawAddr = _withdrawAddr;
    }

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

    modifier onlyBuyer(){
        require(msg.sender == buyerAddr);
        _;
    }

    function Send() payable public onlyBuyer{
    }

    function getBalanceOwner() view public returns(uint){
        return owner.balance;
    }

    function getBalanceBuyer() view public returns(uint){
        return buyerAddr.balance;
    }

    function getBalanceWithdrawAddr() view public returns(uint){
        return withdrawAddr.balance;
    }

    function getBalanceThis() view public returns(uint){
        return address(this).balance;
    }

    function Withdraw() onlyOwner public returns(bool success)  {
        uint256 amount = address(this).balance;
        withdrawAddr.transfer(amount);
        return true;
    }
}
  • Oh well apparently I got this all wrong. I see what I did there. I thought I was supposed to send the ether to contract with transfer but transfer is the function to send the money out of contract to address. Thank you for help. – Rin Jun 18 at 19:50

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.