How to debug Parser Error in solidity in my smart contract - constructor

So i am trying to create a smart contract for decentralized movie ratings using remix IDE. Here are the things I want to achieve:
Create a mapping field to store the movie reviews
Use an array to store the list of movies
Create a function that returns the total ratings a movie has received so far
Develop a constructor that will be called once you deploy the contract on Blockchain
// SPDX-License-Identifier: MIT
pragma solidity ^ 0.8.10;
contract Rating {
mapping (bytes32 => uint8) public ratingsReceived;
bytes32 [] public movieList;
constructor Rating(bytes32[]movieNames) public {
movieList = movieNames;
}
function totalVotesFor(bytes32 movie) view public returns (uint8) {
return ratingsReceived[movie];
}
function voteForMovie(bytes32 movie) public {
ratingsReceived[movie];
}
}

Please find below code if it helps for movie rating contract with constructor and used mapping to store ratings and also we can add more movies as we required.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract Rating{
string[] moviesList;
mapping(string => uint8) movieRatings;
constructor() {
moviesList = ["movie1","movie2","movie3"]; // this is default list of movies you can add in constructor
}
// add movies
function addMovie(string memory movie) public returns(string memory){
moviesList.push(movie);
return movie;
}
// get list of movies
function getList() public view returns(string[] memory){
return moviesList;
}
// rate movie with rating
function rateMovie(string memory movie,uint8 rating) public {
require(rating >=0 && rating <= 10,"rating is out of range");
movieRatings[movie] = rating;
}
// get movie rating by passing movie name
function getMovieRating(string memory movie) public view returns(uint8){
return movieRatings[movie];
}
}

Related

Ordinal Number Function in Remix Solidity

I want to create a new function based on these codes below, that it can just fill in the ordinal number (first student is started by 1) to track out the struct student info in this case.
That's mean after I give the data of this first student to the struct by addInfo function:["Simon", 20, "CA USA", 10] Then my expected new function is that I just fill in this unit: 1, then it will appear this student detail
\`
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ReferenceTypes {
address public owner;
struct student {
string Name;
uint Age;
string BirthPlace;
uint8 Marks;
}
constructor() {
owner = msg.sender;
}
mapping (address => student) public Info;
address[] public student_Info;
function addInfo(student memory _student, address _address) public {
require(owner == msg.sender, "Only admin can add Info!!!");
Info[_address] = _student;
student_Info.push(_address);
}
function count() public view returns(uint) {
return student_Info.length;
}
}
\`
Thanks so much guys
I tried a new mapping but failed
You can make another mapping that leads to your mapping Info.
uint public studentsCounter;
mapping (uint => mapping (address => student)) public Info;
function addInfo(student memory _student, address _address) public returns(uint){
require(owner == msg.sender, "Only admin can add Info!!!");
Info[studentsCounter][_address] = _student;
studentsCounter++;
student_Info.push(_address);
return studentsCounter-1;
}
Thus you can reach your struct by passing studentId(studentCounter) which addInfo returns and students address.
Another approach is to remake you array of addresses.
student[] public student_Info;
And push struct in this array.
function addInfo(student memory _student, address _address) public {
require(owner == msg.sender, "Only admin can add Info!!!");
Info[_address] = _student;
student_Info.push(_student);
}
So now you can display studentsInfo buy passing index in student_Info array.

Member "push" not found or not visible after argument-dependent lookup

Issue
There seems to be some issue with this line here family.People.push(people[x]);
I keep getting Member "push" not found or not visible after argument-dependent lookup when I try to compile with brownie.
What have I tried
I saw a few SO posts with similar exceptions but it was related to type casting. I did try to cast my incoming array to its type but it just resulted in more exceptions.
Code
pragma solidity ^0.8.9;
contract Person{
string public FirstName;
string public LastName;
}
contract Family{
Person[] public People;
}
contract FamilyManager{
Family[] Families;
function AddFamily(Person[] memory people) public {
Family family = new Family();
for(uint x; x < people.length; x++){
family.People.push(people[x]);
}
Families.push(family);
}
function GetFamilies() public view returns (Family[] memory){
return Families;
}
}
Can anyone spot what I'm doing wrong here or link to an article that can lead to an answer?
I think it is related to access modifiers. Using the public modifier for your array only generates the getter function for it and not the setter.
As a result, you cannot directly push to an array from another contract. I created a public function to add elements to the array as follows:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
contract Person{
string public FirstName;
string public LastName;
}
contract Family{
Person[] public People;
function addPerson(Person person) public {
People.push(person);
}
}
contract FamilyManager{
Family[] Families;
function AddFamily(Person[] memory people) public {
Family family = new Family();
for(uint x; x < people.length; x++) {
family.addPerson(people[x]);
}
Families.push(family);
}
function GetFamilies() public view returns (Family[] memory){
return Families;
}
}

Solidity CRUD trying to call All data return instead of one by one using ID

I have created a solidity that record data CRUD like where I can create read update delete but I want to create a readAll function wondering how can I write in solidity since I write like below it does not work. For calling with id it return the correct but not readAll. Looking forward for your help <3
example
function readAllTask() public view returns (uint, uint256, uint256) {
return (tasks.id, tasks.stackAddress, tasks.nftId); <============= return everything
}
pragma solidity ^0.8.6;
contract RecordData {
struct Task {
uint id;
uint256 stackAddress;
uint256 nftId;
}
Task[] tasks;
uint nextId; // default value 0, add public to see the value
function createTask(uint256 _stackAddress, uint256 _nftId) public {
tasks.push(Task(nextId, _stackAddress, _nftId));
nextId++;
}
function findIndex(uint _id) internal view returns (uint) {
for (uint i = 0; i < tasks.length; i++) {
if (tasks[i].id == _id) {
return i;
}
}
revert("Task not found");
}
function updateTask(uint _id, uint256 _stackAddress, uint256 _nftId) public {
uint index = findIndex(_id);
tasks[index].stackAddress = _stackAddress;
tasks[index].nftId = _nftId;
}
function readTask(uint _id) public view returns (uint, uint256, uint256) {
uint index = findIndex(_id);
return (tasks[index].id, tasks[index].stackAddress, tasks[index].nftId);
}
function deleteTask(uint _id) public {
uint index = findIndex(_id);
delete tasks[index];
}
}
You can return an array of structs:
function readAllTask() public view returns (Task[] memory) {
return tasks;
}
I think you have reason to do it, but just a remind that saving the data into blockchain is not a good deal, due to the high cost.
If you would like to return all the task, then simply make the tasks public. Solidity automatically assign a get function for it.
If you would like to get some specific content of task struct, then consider something like this:
function readAllTask() public view returns (uint, uint256, uint256 [] memory) {
// something
}

DeclarationError: Identifier not found or not unique. mapping(uint => product) public products;

below is my code I am not sure why give me this error.
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.16;
// creating the contract
contract Rating {
// creating structure to model the product
struct Product {
uint id;
string name;
uint RatingCount;
}
// use mapping to get or fetch the contestant details
mapping(uint => product) public products;
// add a public state variable to keep track of product count
uint public productsCount;
constructor () public {
addProduct("Nike");
addProduct("Adidas");
}
// add a function to add product
// for private variable we use underscore in the start of variable _name
function addProduct(string memory _name) private {
productsCount++;
products[productsCount] = Product(productsCount, _name, 0);
}
}
Solidity is case-sensitive.
You have defined a struct named Product (uppercase P), but your mapping is using product (lowercase p).
Solution: Use the correct form Product
mapping(uint => Product) public products;
sorry I found the answer the problem is in mapping
mapping(uint => product) public products;
I mistakenly user small p instead of capital P. This gives me an error while migrating my smart contract because my struct name starts from Capital P (Product).
so instead of small p just change the p to P.
mapping(uint => Product) public products;

I get a declaration error in solidity when I create a variable with "var"

I'm trying to create a variable "project" to store data from a mapping but I get "Decalration error, undefined identifier" on project = projects[addr]
function getProjectInfo(address addr) public view returns (string memory name, string memory url, uint funds){
var project = projects[addr];
}```
Use explicit variable type definition:
pragma solidity ^0.5.8;
contract Test
{
struct Project
{
bytes32 name ;
}
mapping (address => Project) projects ;
constructor () public {
}
function getProjectInfo(address addr) public view returns (string memory name, string memory url, uint funds)
{
Project memory project = projects[addr];
// ...
}
}