1

I am using remix: I have this function in my contract:

contract Control {
address public owner;
address public newOwner;

event OwnershipTransferred(address indexed _from, address indexed _to);

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


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

function withdrawAll() public payable onlyOwner {
    require(payable(msg.sender).send(address(this).balance));
}

}

But I'm getting this error with the withdrawAll function:

test token V2.sol:84:17: ParserError: Expected primary expression. 
require(payable(msg.sender).send(address(this).balance)); ^-----^

did i forget a varible or is this just a syntax error? The script works, but this function I just want to be able to call to withdraw anything that gets stuck at this address.

1 Answer 1

1

In order to make the withdrawAll function work, you have first to call the method Owned to set the owner address.

However, your contract is currently useless. In fact, it cannot receive ethers as it does not include a receive function. Also please note that send only allows to send ethers, and not tokens stucked on the smart contract. Furthermore address(this).balance refers to the contract ether balance.

You can try this code :

pragma solidity ^0.8.0;

contract Control {
address public owner;
address public newOwner;

event OwnershipTransferred(address indexed _from, address indexed _to);

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


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


constructor() payable {
    
}


function withdrawAll() public payable onlyOwner {
    require(payable(msg.sender).send(address(this).balance));
}


receive() external payable { 
    }
    
}

After compiling you can follow these steps in Remix to test the smart contract :

  1. Go to the deployment tab and choose one of the available account.
  2. Fill the field "VALUE" to send some ethers to your contract during the deployment. Note the payable keyword on the constructor which allows us to do this.
  3. Deploy the contract
  4. Call the function Owned to set the owner
  5. Call the function withdrawAll (with the same account) to retrieve all the ethers held by the smart contract.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.