1

I've taken the below contract as an example from a tutorial and changed items highlighted in bold, as per IDE errors.

What the contract should do is allocate ownership of the item to the account that deploys the contract, then when 2 ether is paid, change the ownership to the account that made the payment. When I deploy the contract, the ownership isnt allocated to the account, instead it has the address 0x000..., and when i use the buy function, ownership does not transfer and ether is not deducted from the buyers account. Any help would be appreciated.


pragma solidity ^0.4.0;

contract SalesContract {

    address **public** owner;
    bool public isSold = false;
    string public salesDescription = "Honda Civic";
    uint public price = 2 ether;

    function SaleContract() {
        owner = msg.sender;
    }

    function Buy() **public** payable {
        if(msg.value >= price) {
            **owner.transfer(address(this).balance)**;
            owner = msg.sender;
            isSold = true;
        } else {
            revert();
        }
    }

}

Original code from tutorial:

pragma solidity ^0.4.0;

contract SalesContract {
    address public owner;
    bool public sold = false;
    string public salesDescription = 'Volvo V40 HF 56 32';
    uint price = 2 ether;

    function SalesContract() payable {
        owner = msg.sender;
    }

    function buy() payable {
        if(msg.value >= price) {
            owner.transfer(this.balance);
            owner = msg.sender;
            sold = true;
        } else {
            revert();
        }
    }
}
0

Maybe you should check your spelling,I think it should be "SalesContract" rather than "SaleContract",when we use the same name of the contract,we can initialize our contract right.

  • Hi Skyge, I had them the same originally but it wouldnt allow me to have a function named the same as the contract. I've now gotten around this by using 'constructor() public { owner = msg.sender;' – Jim Aug 6 '18 at 1:42
  • This has assigned the account that deploys the account as the owner, but still cannot transfer ownership and no balance transfer when the payment is made. It may be a simple fix but I'm unsure as to where I've gone wrong – Jim Aug 6 '18 at 1:44
  • I change the code "owner.transfer(this.balance);" into "owner.transfer(msg.value);",it works. – skyge Aug 6 '18 at 5:14
  • I tried that before but still no transfer of ownership or ether. – Jim Aug 6 '18 at 12:58
  • I can transfer the ownership,maybe there is something wrong with your operation.First,I use an account to deploy this contract,then I use another account to buy this contract with 3 ether,then,it works. – skyge Aug 7 '18 at 0:57
0

An interesting story on:

contract SalesContract {
    ...
    function SaleContract() {
        ...
    }
    ...
}

A while back, some company deployed a contract with the same typo.

Shortly thereafter, all the money deposited into that contract was stolen.

Why?

The author decided to rename the contract, but forgot to rename the constructor.

So instead of a constructor, there was now a public function available for anyone to invoke.

Switch to Solidity 0.4.21 or later, and declare constructor instead.

It will not solve this specific problem of course, because you may still make the mistake of declaring that public function, but most likely, you will start using the constructor keyword and will not step into this pitfall in the future.

0

Here's the code, upgraded to solidity 5 and confirmed on Remix. Let us know if you have questions.

pragma solidity ^0.5.0;

contract SalesContract {

    address payable public owner;
    bool public isSold = false;
    string public salesDescription = "Honda Civic";
    uint public price = 2 ether;

    constructor() public {
        owner = msg.sender;
    }

    function Buy() public payable {
        if(msg.value >= price) {
            owner.transfer(address(this).balance);
            owner = msg.sender;
            isSold = true;
        } else {
            revert();
        }
    }

}

The differences:

  • pragma version (line 1)
  • address payable (line 5)
  • constructor (line 10)

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.