'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;
}
}
Solution 1:[1]
There are some errors in your smart contract:
In smart contract's
constructor()you cannot declare a function. In your case you're declaringwithdrawAmount()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 calledwithdrawAmount. 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()hasonlyOwner()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 usingpayable()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;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kerry99 |
