Remix

儘管契約沒有錯誤,但重新混合“尚未編譯契約”

  • July 7, 2021

我試圖在混音上執行程式碼。但它顯示“尚未編譯契約”,儘管契約沒有錯誤

我的契約——

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;
}

}

你應該打開合約文件並點擊編譯 在此處輸入圖像描述

我無法準確地說出“為什麼”,但我知道如何解決它。問題是使用new uint[]()初始化結構值時。您只需要替換registerUser()以下功能即可;

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;
}

解決方法基本上是僅初始化結構的相關值,而不是分配包括空數組在內的所有值。

引用自:https://ethereum.stackexchange.com/questions/94328