ParserError: Function, variable, struct or modifier declaration expected(Truffle test) - solidity

I am currently learning solidity.
I have written the code as it appears in "Hands-On Smart Contract Development With Solidity and Ethereum" (O'Reilly/Japanese edition), but when I run TruffleTest, I get the following error
CompileError: project:/contracts/Fundraiser.sol:9:1: ParserError: Function, variable, struct or modifier declaration expected.
Here is the code.
https://github.com/okahijiki/fundraiser
I would appreciate any advice you can give me.
My library's version
Truffle
v5.5.31 (core: 5.5.31)/
Ganache v7.4.3/
Solidity v0.5.16 (solc-js)/
Node v16.15.1/
Web3.js v1.7.4

Here's the linked code:
pragma solidity >0.4.23 <0.7.0;
contract Fundraiser{
string public name;
constructor(string memory _name)public{
name = _name;
}
Count the number of opening braces - and the number of closing braces. You are correctly opening and closing the contract, but the constructor is missing its closing brace.
The solution is simple - close the constructor block with a brace as well.
contract Fundraiser{
string public name;
constructor(string memory _name)public{
name = _name;
} // closing the constructor here
}

Related

Using interface name as Variable type

In solidity, see below code ..... How is that the interface name is being used as variable type ?
See the comments in below code
pragma solidity 0.8.11;
import "../interfaces/ISpool.sol";
// ... other imports
abstract contract VaultBase is IVaultBase, VaultImmutable, SpoolOwnable, SpoolPausable, BaseConstants {
using Bitwise for uint256;
using SafeERC20 for IERC20;
/* ========== STATE VARIABLES ========== */
/// #notice The central Spool contract
ISpool internal immutable spool; // ISpool is interface name and it is
// being used as variable type. What this means ?
}
ISpool interface:
interface ISpool is ISpoolExternal, ISpoolReallocation, ISpoolDoHardWork, ISpoolStrategy, ISpoolBase {}
How is that the interface name is being used as variable type ?
Interface type variables work the same way as Contract type variables.
They serve as as pointer to the specified address, assuming that there is a contract deployed on this address that implements this interface.
Example: If the ISpool interface declares a function foo(uint256) external returns (bool), your contract can call this function and assign the return value directly:
spool = ISpool(address(0x123));
bool result = spool.foo(1);
If the specified address doesn't implement this specific function that you're trying to call, the call fails with an exception.

parsing error in smartcontract constructor

parsing error in smartcontract constructor, can some one please help me out and show me what I am doing wring and why this error is even thought everytrhing is looking fine from outside.
Constructor ERC721 need two string arguments(name and symbol) so you need to write like this
ERC721("Cryptopost","post")
so replace line 16 by this :
constructor(string memory token, string memory symbol)ERC721("Cryptopost","post")
Are you importing the erc721 openzeppelin file before creating the smart contract? For example:
//Import goes here
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
//this is the constructor function
constructor() public ERC721("Name", "symbol") {}

Erorr after truffle compile in Erc721 openzeppelin contract

im doing step by step of this article and i had a problem on truffle compile part.
I've got this error in cmd:
Error parsing #openzeppelin/contracts/token/ERC721/ERC721.sol: ParsedContract.sol:51:72: ParserError: Expected '{' but got reserved keyword 'override'
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
^------^
my contract :
pragma solidity ^0.6.0;
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
import "#openzeppelin/contracts/utils/Counters.sol";
contract Uniken is ERC721{
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
mapping(string => uint8) hashes;
constructor() public ERC721("Uniken", "Ukn") {
}
function awardItem(address recipient, string memory hash, string memory metadata)
public
returns (uint256)
{
require(hashes[hash] != 1);
hashes[hash] = 1;
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, metadata);
return newItemId;
}
}
I'd be thankfull if anyone tell me whatis the problem?
It seems like it isn't seeing the ERC165 contract that ERC721 extends from. That function that it is getting stuck on should be overriding a function of the same name in ERC165, but the truffle compiler doesn't see a function named supportsInterface() in a class that ERC721 inherits from. So I would check to make sure everything imported in the ERC721 smart contract is in the right place in your folder structures, and that ERC721 is correctly inheriting ERC165.
After made some research , I was in truffle version 5.0 and update to the latest version of truffle -> v5.4.6, I am now able to compile

TypeError: Data location must be "memory" for parameter in function, but none was given

I tried to compile my code, but I got the following error:
TypeError: Data location must be "memory" for parameter in function,
but none was given
my code:
pragma solidity ^0.5.0;
contract memeRegistry {
string url;
string name;
uint timestamp;
function setmeme(string _url,string _name, uint _timestamp) public{
url = _url;
name = _name;
timestamp = _timestamp;
}
}
Explicit data location for all variables of struct, array or mapping types is now mandatory. This is also applied to function parameters and return variables.
Add memory after string
function setmeme(string memory _url, string memory _name, uint _timestamp) public{
check here for Solidity 0.5.0. changes https://solidity.readthedocs.io/en/v0.5.0/050-breaking-changes.html
//The version I have used is 0.5.2
pragma solidity ^0.5.2;
contract Inbox{
string public message;
//**Constructor** must be defined using “constructor” keyword
//**In version 0.5.0 or above** it is **mandatory to use “memory” keyword** so as to
//**explicitly mention the data location**
//you are free to remove the keyword and try for yourself
constructor (string memory initialMessage) public{
message=initialMessage;
}
function setMessage(string memory newMessage)public{
message=newMessage;
}
function getMessage()public view returns(string memory){
return message;
}
}
It's working for me.
string memory firstname
Select a different version of the solidity compiler. ^0.4.25 works for me.
The version of the solidity compiler has to be set both on the file and in the compile tab on remix(it is a drop-down menu).
You need to make the return parameters explicitly memory:
Thus,
function setmeme(string _url,string _name, uint _timestamp) public
Becomes
function setmeme(string memory _url, string memory _name, uint _timestamp) public{

How solidity make function signature with tuple(nested abi)?

struct Test {
uint ui;
string s;
}
function test(Test t) public {
emit Log(t.ui, t.s);
}
I have some knowledge about ABI. I made this contract with experimental ABIEncoderV2 option. In conclusion, this function's signature is 0x6056f4cc, I found this value in opcode. I tried some case test(uint256,string), test(tuple(uint256,string)), test(tuple), test(tuple[uint256,string])) with sha3... but no one make correct signature. How solidity make function signature with tuple?
You're close with the first one. The actual encoding is done from test((uint256,string)).
bytes4(keccak256("test((uint256,string))"): 6056f4cc