简体   繁体   中英

ParserError: Expected '(' but got identifier

When i get compile i get this error =ParserError: Expected '(' but got identifier I am facing such an error, I tried many things but I could not do it. I want to make a small wallet and I want to run a withdraw function. I am waiting for your help. strong text

// SPDX-License-Identifier: UNLICENSED

pragma solidity >= 0.7.0 <0.9.0;

contract Challenge0 {
address public owner;
uint256 userBalance;
uint256 withdrawAmount;
bool public canWithdraw= false;

  
    constructor (address payable _withdrawAmount){
     function withdrawAmount(uint256 amount) onlyOwner payable public {
         require(msg.value == amount);
        require(amount <= getBalance());
         msg.sender.transfer(amount);
  }
    }

    function setUserBalance()external view {

    }
    
function getUserBalance()public returns (uint256){
    return address(this).balance;

    }
  


    }
 

There are some errors in your smart contract:

  • In smart contract's constructor() you cannot declare a function. In your case you're declaring withdrawAmount() function into it. For resolve this, you must to move this function outside constructor;

  • You're using the same name for a withdrawAmount() function and uint256 variabile called withdrawAmount . The compiler give you error because you cannot use the same name for declaring a variabile and a function, thus you must to change one of these name with a different name;

  • withdrawAmount() has onlyOwner() modifier that you didn't declare into your smart contract. To solve this problem you must to declare it. More information about modifiers here ;

  • You didn't declare and implement the getBalance() function in your contract;

  • When use transfer() function you must use it with payable address. In your case you must to cast the msg.sender address using payable() in this way:

    payable(msg.sender).transfer(amount);

I tried to correct your smart contract, you can continue with this model:

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 <0.9.0;

contract Challenge0 {
    address public owner;
    uint256 userBalance;
    uint256 withdrawAmountVariable;
    bool public canWithdraw = false;
  
    constructor() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(owner == msg.sender, "You aren't the smart contact's owner");
        _;
    }

    function getBalance() public returns(uint) {
        // TODO: implement your logic
    }

    function withdrawAmount(uint256 amount) onlyOwner payable public {
        require(msg.value == amount);
        require(amount <= getBalance());
        payable(msg.sender).transfer(amount);
    }

    function setUserBalance()external view {
        // TODO: implement  your logic 
    }
    
    function getUserBalance()public view returns (uint256){
        return address(this).balance;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM