2

When i try to run the below code in remix it works fine but as var keyword is deprecated so i tried to used an alternative way to fix the warning but it resulted in wrong output.

I was trying to achieve:

1) to add a user

namestring - some random name like random

linkstring - some random link like "https://www.google.com"

docConventionstring - some random convention like random1docconvention1

on moving the user will have 10 points in struct. If again i move the same user into struct it will update the totalpoints to 20. Here the issue is var keyword is depricated and when i use this same contract in truffle it is not returning the output as expected.

function AddUser(string namestring,string linkstring,string docConventionstring) public{                          
    bytes32 name=keccak256(abi.encodePacked(namestring));
    bytes32 link=keccak256(abi.encodePacked(linkstring));
    bytes32 docConvention=keccak256(abi.encodePacked(docConventionstring));
    uint32 existingTotalPoints=userTotalPoints(name);
     var user=bytesMappingToken[name];
    user.orginalName=name;
    user.originalKshoplink=link;
    user.validated=false;
    user.Points=firstTimeUserPoints;
    user.totalPoints=existingTotalPoints+firstTimeUserPoints;
    updateUserTotalPoints(name,user.totalPoints);
    user.orginalDocumentConvention=docConvention;
    struUsers.push(user);             
}

2) so I tried to change the add user function like below without using var:

function AddUser(string namestring,string linkstring,string docConventionstring) public{                
    bytes32 name=keccak256(abi.encodePacked(namestring));
    bytes32 link=keccak256(abi.encodePacked(linkstring));
    bytes32 docConvention=keccak256(abi.encodePacked(docConventionstring));
    uint32 existingTotalPoints=userTotalPoints(name);
    uint32 totalPoints=existingTotalPoints+firstTimeUserPoints;
    struUsers.push(struUser(name,link,docConvention, false,firstTimeUserPoints,totalPoints));
}

When i use the above add user function i don't the output as i expect. Below is the whole contract.

pragma solidity ^0.4.22;
contract sample{
address owner=0x1234;    
uint32 firstTimeUserPoints=10;
struct struUser{
    bytes32 orginalName;
    bytes32 originalKshoplink;
    bytes32 orginalDocumentConvention;
    bool validated;
    uint32 Points;
    uint32 totalPoints;
}
struct struUserTotalPoints{
    bytes32 orginalName;
    uint32 totalPoints;
}
//mapping(string=>struUser)MappingToken;
mapping(bytes32=>struUser)bytesMappingToken;
struUser[] struUsers;
struUserTotalPoints[] struArrayUserTotalPoints;
string[] docConventionMappers;
string[] orginalName;
event printAddUserSuccess(string message);
event viewUser(string username, uint32 totalpoints);

modifier isOwner() {
    // if a condition is not met then throw an exception
    require (msg.sender == owner) ;
    _;
}

//TO INSERT A RECORD WITH SOME DATA

function AddUser(string namestring,string linkstring,string docConventionstring) public{

    /*
    bytes32 name=keccak256(abi.encodePacked(namestring));
    bytes32 link=keccak256(abi.encodePacked(linkstring));
    bytes32 docConvention=keccak256(abi.encodePacked(docConventionstring));
    uint32 existingTotalPoints=userTotalPoints(name);
    uint32 totalPoints=existingTotalPoints+firstTimeUserPoints;
    struUsers.push(struUser(name,link,docConvention, false,firstTimeUserPoints,totalPoints));
    */


    bytes32 name=keccak256(abi.encodePacked(namestring));
    bytes32 link=keccak256(abi.encodePacked(linkstring));
    bytes32 docConvention=keccak256(abi.encodePacked(docConventionstring));
    uint32 existingTotalPoints=userTotalPoints(name);
     var user=bytesMappingToken[name];
    user.orginalName=name;
    user.originalKshoplink=link;
    user.validated=false;
    user.Points=firstTimeUserPoints;
    user.totalPoints=existingTotalPoints+firstTimeUserPoints;
    updateUserTotalPoints(name,user.totalPoints);
    user.orginalDocumentConvention=docConvention;
    struUsers.push(user);
    //docConventionMappers.push(docConvention);

    //struUsers.push(struUser({orginalName:name,originalKshoplink:link, 
    //validated:user.validated,Points:user.Points,totalPoints:user.totalPoints,
    //orginalDocumentConvention:docConvention}));

    //struUser memory user=struUser(name,link,docConvention, false,firstTimeUserPoints,totalPoints);

    //struUsers.push(struUser);
    emit printAddUserSuccess("user added successfully!");

}


 function userTotalPoints(bytes32 name) public returns(uint32){  //FOR FETCHING TOTAL POINTS OF A USER

     bool isUserTotalPointsCalculated=false;

        for(uint j=0;j<struArrayUserTotalPoints.length;j++){
            //if(keccak256(struArrayUserTotalPoints[j].orginalName)==keccak256(name)){                    
            if(comparebytes(struArrayUserTotalPoints[j].orginalName,name)){
                isUserTotalPointsCalculated=true;
                return struArrayUserTotalPoints[j].totalPoints;
            }
        }
        if(!isUserTotalPointsCalculated){
            struArrayUserTotalPoints.push(struUserTotalPoints({orginalName:name,totalPoints:firstTimeUserPoints}));
            return 0;                
        }  
}

function userexists(string namestring) view public returns(uint32){
    bytes32 name=keccak256(abi.encodePacked(namestring));
     for(uint j=0;j<struArrayUserTotalPoints.length;j++){
            if(comparebytes(struArrayUserTotalPoints[j].orginalName,name)){
               return bytesMappingToken[name].Points;
            }
        }
        return 5;
}

   function comparebytes (bytes32 a, bytes32 b) pure public returns (bool){
   //return keccak256(a) == keccak256(b);
   return keccak256(abi.encodePacked(a))==keccak256(abi.encodePacked(b));
   }


function viewuser(string name)public returns(string,uint32,uint32){ //
    bytes32 bytesname=keccak256(abi.encodePacked(name));
    uint32 totalpoints=userTotalPoints(bytesname);
    return (name,bytesMappingToken[bytesname].Points,totalpoints);
}

function updateUserTotalPoints(bytes32 name,uint32 totalPoints) public{
    for(uint j=0;j<struArrayUserTotalPoints.length;j++){
            if(comparebytes(struArrayUserTotalPoints[j].orginalName,name)){
                struArrayUserTotalPoints[j].totalPoints=totalPoints;
            }
        }
}


function () public payable {

}
}

How should i remove var keyword and use the alternative solution to get the expected output. Thanks in advance.

5

You have this:

var user=bytesMappingToken[name];

The fix is to use an explicit type. In this case, the type is struUser:

struUser user=bytesMappingToken[name];

EDIT

Per comments below, using the explicit storage keyword will silence the warning:

struUser storage user = bytesMappingToken[name];
  • Thank you Smarx! I tried it earlier but it says to use storage keyword to silence the warning. I am newbie to this could you let me know how to silence the warning. Thanks in anticipation – user2865883 May 17 '18 at 13:46
  • How about this? struUser storage user=bytesMappingToken[name]; – Henk May 17 '18 at 13:53
  • Yes, that's right. I'll edit my answer to include that. – user19510 May 17 '18 at 13:54

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.