0

Im trying to run code on remix. But it's showing "no contract compiled yet" although contract is error free

My contract-

pragma solidity ^0.8.0;

contract Dwitter
{
    //state variables
    uint public dweetCounter;
    uint userCounter;
    address owner;
    string dweetContent;//for showing dweet content
    uint public userIdCounter;
    Dweet[] dweetArray;
    mapping (address => Dweet) dweetToUser;
    mapping (address => User) users; //for map user with address for attach dweets
    mapping ( uint => User) userById;
    mapping (uint => Dweet) dweetById;

//constructor
constructor()
{
    owner=msg.sender;
}

//structs
struct Dweet
{
    uint dweetId;
    string content;
    uint liked;
    uint disliked;
    uint reported;
    bool deleted; 
    uint[] whoLikedMe;
    uint[] whoDislikedMe;
    uint whoLikedMeCounter;
    uint whoDislikedMeCounter;
}

struct User
{
    uint userId;
    string name;
    bool isRegistered;
    
    uint followers;
    uint following;
    
    uint[] whoFollowMe;
    uint[] iFollowWhom;
    uint[] likedDweets;
    
    uint[] dislikedDweets;
    Dweet[] UserAllDweets;
    uint userDweetCounter;
    uint followIndex;
}

//functions
function registerUser(string memory _name) public
{
    require ( msg.sender!=owner,"Owner cant register");
    require ( users[msg.sender].isRegistered==false,"You are already registered! ");
    userIdCounter++;
    User memory user = User(userIdCounter, _name, true, 0, 0,
                            new uint[](0),new uint[](0),
                            new uint [](0),new uint[](0),new Dweet[](0),0, 0);
    users[msg.sender]=user;
    userById[userIdCounter]=user;
}

function dweeet(string memory _content) public
{
    require(msg.sender!=owner,"Owner cant dweeet");//should not be owner
    require ( users[msg.sender].isRegistered==true,"User is not registered yet");//user should exist
    dweetCounter++;
    Dweet memory dweet = Dweet ( dweetCounter, _content ,0, 0, 0, false, new uint[](0), new uint[](0), 0, 0);//creating dweet instance
    users[msg.sender].userDweetCounter++;
    users[msg.sender].UserAllDweets[users[msg.sender].userDweetCounter];//storing dweet in resp. user's array
    dweetById[dweetCounter]=dweet;
}

function searchUser( uint _userId ) public view returns(User memory)
{
    User memory u =userById[_userId];
    return u;
}

function followUser( uint  _userId   ) public
{
    require ( userById[_userId].userId != users[msg.sender].userId ,"You cant follow yourself ");
    require(msg.sender!=owner,"You are owner you cant use this function");
    require(users[msg.sender].isRegistered == true,"First register ! ");
    require(userById[_userId].isRegistered==true, " User doesn't exist" );
    
    searchUser(_userId).followers++;
    users[msg.sender].following++;
    
    userById[_userId].followIndex++;
    userById[_userId].whoFollowMe[ userById[_userId].followIndex ] = users[msg.sender].userId;
    
    users[msg.sender].followIndex++;
    users[msg.sender].iFollowWhom[ users[msg.sender].followIndex ] = userById[_userId].userId;
    
}



function unfollowUser( uint _userId) public
{
    require ( userById[_userId].userId != users[msg.sender].userId ,"You cant unfollow yourself ");
    require(msg.sender!=owner,"You are owner you cant use this function");
    require(users[msg.sender].isRegistered == true,"First register ! ");
    require(userById[_userId].isRegistered==true, " User doesn't exist" );
    
    searchUser(_userId).followers--;
    users[msg.sender].following--;
    
    //for getting indexed
    for(uint i=0;i<userById[_userId].followIndex;i++)
    {
        if(userById[_userId].whoFollowMe[i] == users[msg.sender].userId )
        {
            delete userById[_userId].whoFollowMe[i];
        }
        
    }
    userById[_userId].followIndex--;
    
    for(uint i=0;i<users[msg.sender].followIndex;i++)
    {
        if(users[msg.sender].iFollowWhom[i] == userById[_userId].userId )
        {
            delete users[msg.sender].iFollowWhom[i];
        }
        
    }
    users[msg.sender].followIndex--;
    users[msg.sender].iFollowWhom.push(userById[_userId].userId);
    users[msg.sender].following--;

} 

function likeDweet( uint _DweetId) public
{
    require(dweetById[_DweetId].deleted!=true,"Dweet is deleted, cant like");
    require(msg.sender!=owner,"You are owner you cant use this function");
    require(users[msg.sender].isRegistered == true,"First register ! ");
    //for checking if user liking dweet again.
    for(uint i=0;i< dweetById[_DweetId].whoLikedMeCounter;i++)
    {
        if(dweetById[_DweetId].whoLikedMe[i] == users[msg.sender].userId)
        {
            require( false, "You already liked this dweet, Can't like again! ");
        }
    }
    // if user liking disliked tweet. 
    for(uint i=0;i< dweetById[_DweetId].whoDislikedMeCounter;i++)
    {
        if(dweetById[_DweetId].whoDislikedMe[i] == users[msg.sender].userId)
        {
            require( false, "You already disliked this dweet, Can't like now ! ");
        }
    }
            
    
    
    dweetById[_DweetId].liked++;
    users[msg.sender].likedDweets.push(_DweetId);
    
    dweetById[_DweetId].whoLikedMeCounter++;
    dweetById[_DweetId].whoLikedMe[ dweetById[_DweetId].whoLikedMeCounter ]=users[msg.sender].userId;
    
}

function dislikeDweet( uint _DweetId) public
{
    require(dweetById[_DweetId].deleted!=true,"Dweet is deleted, cant like");
    require(msg.sender!=owner,"You are owner you cant use this function");
    require(users[msg.sender].isRegistered == true,"First register ! ");
    //for checking if user disliking dweet again.
    for(uint i=0;i< dweetById[_DweetId].whoDislikedMeCounter;i++)
    {
        if(dweetById[_DweetId].whoDislikedMe[i] == users[msg.sender].userId)
        {
            require( false, "You already diliked this dweet, Can't like again! ");
        }
    }
    
    
    // if user disliking liked tweet. 
    for(uint i=0;i< dweetById[_DweetId].whoLikedMeCounter;i++)
    {
        if(dweetById[_DweetId].whoLikedMe[i] == users[msg.sender].userId)
        {
            require( false, "You already liked this dweet, Can't dislike now ! ");
        }
    }
    
    dweetById[_DweetId].disliked++;
    users[msg.sender].dislikedDweets.push(_DweetId);
    
    dweetById[_DweetId].whoDislikedMeCounter++;
    dweetById[_DweetId].whoDislikedMe[ dweetById[_DweetId].whoDislikedMeCounter ]=users[msg.sender].userId;
    
}


function deleteDweet(uint _dweetId) public
{
    require(dweetById[_dweetId].deleted!=true,"Dweet is already deleted");
    dweetById[_dweetId].deleted=true;
    dweetCounter--;
    
    
}

function reportDweet( uint _DweetId) public
{
    require(dweetById[_DweetId].deleted!=true,"Dweet is deleted");
    
    if(dweetById[_DweetId].reported==15)
    {
        deleteDweet(_DweetId);
    }
    dweetById[_DweetId].reported++;
}

function fetchDweet( uint _dweetId) public
{
    require (dweetById[_dweetId].deleted==false,"This dweet is deleted! ");
    require ( _dweetId<=dweetCounter,"Invalid dweet Id");
    dweetContent=dweetById[_dweetId].content;

}

function showDweet() public view returns(string memory)
{
    return dweetContent;
}

function userDweetCount(uint _userId) public view returns(uint)
{
    uint e=userById[_userId].userDweetCounter;
    return e;
}

function seeFollowers() public view returns( uint)
{
    uint w=users[msg.sender].followers;
    return w;
}

function seeFollowing() public view returns( uint)
{
    uint w=users[msg.sender].following;
    return w;
}

// for knowing whose account is it (test purpose) 
function showMyName() public view returns(string memory)
{
    string memory a =users[msg.sender].name;
    return a;
}

function totalLikes(uint _dweetId) public view returns(uint )
{
     return dweetById[_dweetId].whoLikedMeCounter;
}
function totalDisikes(uint _dweetId) public view returns(uint )
{
     return dweetById[_dweetId].whoDislikedMeCounter;
}

}

0

you should open the contract file and klick on compile enter image description here

0

I am not able to precise "the why", but I know how to fix it. Problem is when using new uint[]() to initialise a struct value. You just need to replace function registerUser() for the one below;

function registerUser(string memory _name) public
{
    require(msg.sender != owner, "Owner cant register");
    require(users[msg.sender].isRegistered == false, "You are already registered! ");
    userIdCounter++;
    User memory user;
    // Initialise only with the relevant fields
    user.userId = userIdCounter;
    user.name = _name;
    user.isRegistered = true;
    users[msg.sender] = user;
    userById[userIdCounter] = user;
}

The workaround is basically initialising only the relevant values of the struct, instead of assigning all values including the empty arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.