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.