When ever I try to compile the contract I keep getting a error on this line - solidity

In remix this line gives me a error "ParserError: Expected '(' but got identifier"
function setIsPublicMintEnabled(bool is PublicMintEnabled_) external onlyOwner {
isPublicMintEnabled=isPublicMintEnabled;
}

There is an syntax error around is keyword. Refactor your function in following way:
function setIsPublicMintEnabled(bool isPublicMintEnabled_) external onlyOwner {
isPublicMintEnabled = isPublicMintEnabled_;
}
is keyword is used when contract inheritance is being defined. Here is an detailed explanation how to use inheritance and the is keyword in practice.

Related

How to write custom errors and return messages with error keyword Solidity

I tried to make a custom error but it doesn't return a message to revert
How could I make it return a message?
/// custom error
error MyCustomError(address _address);
if user { revert MyCustomError({address: msg.sender}) }
I got this error
Runtime error: revert
There are two errors:
1- if statement should be inside a function
2- when you defined the custom error,you defined the named parameter as _address
contract Test {
address public user;
/// custom error
error MyCustomError(address _address);
function revertError() public view {
// I just had to pass a valid condition. address(0)=0x0000000000000000000000000000000000000000
// if(user) would give this error: "Type address is not implicitly convertible to expected type bool"
if(user!=address(0))
{
revert MyCustomError({_address: msg.sender});
}
}
}

TypeError: Type is not callable - on compile

I have created a game where I have an interface and a contract with a Game function
But when I compile it throws an exception:
TypeError: Type is not callable
myGameContract.depositPlayer(0, msg.value)();
It is clear that it refers to the fallback after: msg.value)();
But, I don't know how to fix the interface.
interface Game{
function depositPlayer(uint256 _pid, uint256 _amount) external payable;
function myGame(address _reciever) external payable {
addressBoard = payable(_reciever);
myGameContract= Game(addressBoard );
myGameContract.depositPlayer(0, msg.value)();
I need in this case it to contain a fallback
();
More bellow:
For more clarification, comment as the answer indicates, only the call
function contains a fallback
You can execute an external function without the empty parentheses.
Example that executes the external depositPlayer function:
myGameContract.depositPlayer(0, msg.value); // removed the `()`
You can execute the fallback function by sending an empty data field.
address(myGameContract).call("");
But the fallback function is executed when no suitable function (specified in the data field) is found. It's not executed after each function. So you can't execute both depositPlayer and fallback in the same call (except for executing depositPlayer from the fallback).
This is an easy error to make when you accidentally make an argument shadow a function. Here's an easy to understand place this error would pop up:
constructor(address owner, address[] memory defaultOperators) ERC777("Shipyard", "SHIP", defaultOperators) {
transferOwnership(owner);
ico = address(new ShipICO(this));
_mint(owner(), INITIAL_SUPPLY, "", "");
_pause();
}
Note that the argument owner has the same name as the function called in the _mint(owner()) line. Because of the argument having the same name, you're now trying to call the argument owner, which is of type address, as if it were a function. (Note that in this case, owner() is a function inherited from the OZ Ownable contract, making it especially easy to miss.
Easy, common convention is to add an underscore. In this case, _owner may already be taken, so you may add an underscore to the end (owner_ ) for the argument.
As to the OP's question:
This means that myGameContract.depositPlayer is not a function. Go figure out why you think it is, and the compiler thinks it isn't.

I have tried to compile this code unfortunately i usually get error

This is the code: function () external payable {
The error when compiling:
ParserError: Expected a state variable declaration. If you intended this as a fallback function or a function to handle plain ether transactions, use the "fallback" keyword or the "receive" keyword instead. --> mytoken.sol:145:32: | 145 | function () external payable { | ^
This error occurs because you're using syntax from Solidity 0.5 or older in version 0.6 or newer.
In Solidity 0.6, the fallback function was changed, and the new syntax is
pragma solidity ^0.6;
contract MyContract {
fallback() external payable {
// TODO implement or leave empty
}
}
You can find more info in the docs section breaking changes.

Expected identifier but got '(' in function

When I try to call CustomerFactory function I get
ParserError: Expected identifier but got '('
CustomerFactory(1,28);
I went over the documentation and I didn't see what I did wrong. Can someone advise?
pragma solidity ^0.7.0;
contract Manmade {
struct Customer {
uint customerID;
string nick;
}
Customer[] public customers;
function CustomerFactory(uint _customerID,string memory _nick)public{
customers.push(Customer(_customerID,_nick));
}
CustomerFactory(1,"Peter");
}
Your code CustomerFactory(1,"Peter"); is out of any function body which is not possible with solidity. Put this code into a function and it will work.

Function with return type and "use" keyword

How can you create a function that specifies both the return type and the external variables to use?
Are the examples below wrong or is it not supported (yet)?
<?php
$arr = ['test' => 'value'];
function test1() use ($arr) : string { // Line 5
return $arr['test'];
}
function test2() : string use ($arr) {
return $arr['test'];
}
The error is:
Parse error: syntax error, unexpected 'use' (T_USE), expecting '{' in [PATH]\index.php on line 5
You can run the code here.
As answered by Adam Cameron in the comments, the problem is that the use language construct only applies to closures, not to all functions. Thus, using it on normal functions makes no sense and is as such not understood by the parser.
When the function in the example code would have been a closure, everything would have worked.
$arr = ['test' => 'value'];
$fn = function() use ($arr) : string {
return $arr['test'];
};
echo($fn()); // outputs 'value'
You can run the code here.
Note that your first attempt is correct: first the use statement, then the return type declaration. Trying to reverse these two will result in a parse error.