1

In my contract I have a function that's supposed to move the balance of the contract to its owner. The function is: function moveFunds(){ owner.transfer(this.balance); }

owner is set to be: address owner = msg.sender; in the constructor of the contract.

but it doesn't work. The transaction is completed, but the balance doesn't change in the contract or in the owners wallet. Does anyone have any idea how to solve this? Is there another way to do this? I've tried both in private network and in Rinkeby multiple times. There's also a donate function in the contract that works well, the value input is added to the contracts balance and the same value is subtracted from the sender. (I do this before I try to transfer anything to owner)

Thanks in advance.

Edit: Whole contract below: (formatting in here didn't work out..)

   pragma solidity ^0.4.2;

contract donation {
    address owner;
    modifier onlyowner {
        if (msg.sender == owner) _;
        }
    address[] _giver;

function Donation() public {
        owner = msg.sender;
    }


function donate() public payable {
        if (msg.value == 0) {
        revert();
        }
      addGiver(msg.sender);
    }

   /*
   * TODO: 
   */
function moveFund() public onlyowner{
      owner.transfer(this.balance);
    }


function addGiver(address _a) internal {
        _giver.push(_a);
    }
}
  • May you post your entire contract to make it easier to spot the issue? :) – Crema Oct 30 '17 at 21:02
  • It's added now. Couldn't get the format right, but.. – Cec Oct 30 '17 at 21:35
2

There's a typo, a very critical one indeed.

The contract is named donation (d not capitalized)

The constructor is named Donation (D IS capitalized)

Since the contract and constructor are not named the same, the constructor is never called, so owner is never set.

  • How embarrassing, I didn't see that. It works like a charm now, thanks! – Cec Oct 30 '17 at 22:16
  • Nice catch; I've made this mistake too. Solidity should really have a separate keyword or syntax for constructors in my opinion, e.g.: constructor() { ... } It would prevent this mistake altogether. – Jesse Busman Oct 30 '17 at 22:49
  • Yes, this simple typo could lead to terrible vulnerabilities. Notice that in this case, not only the contract was not working as @Cec expected, but also allowed anyone to call Donation() and become the contract owner :O – pabloruiz55 Oct 30 '17 at 22:55
1
pragma solidity 0.4.24;

contract eventTickets{

address owner;
uint tickets;
string description;
uint constant price = 1 ether;
mapping (address => uint) public purchasers;

constructor (uint _tickets, string _description) public {
    owner = msg.sender;
    description = _description;
    tickets = _tickets;
}

modifier OnlyBy(){
    assert (owner == msg.sender);
    _;
}

function infoEvent() public view returns (address, string, uint, uint) {
    return(owner, description, tickets, price);
}

function buyTickets(uint amount) public payable {
    if (msg.value < (amount * price) || amount > tickets) {
        revert(); 
    }
    if (msg.value > (amount * price)) {

    msg.sender.transfer(msg.value - (amount * price));
    }
    purchasers[msg.sender] += amount;
    tickets -= amount;
    }

    function refund(uint numTickets) public payable {
        if (purchasers[msg.sender] <= numTickets) {
            revert();
    }
    msg.sender.transfer(numTickets * price);
        purchasers[msg.sender] -= numTickets;
        tickets += numTickets;
    }

    function endSale() public payable OnlyBy(){

        owner.transfer(this.balance);
    }

}

I'm getting this error enter image description here

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.