0

I am trying to create a smart contract which stores a list of users. Further, a joint account can be created between any two users. My code is given below. edge_list maps each of the linked neighbours to the balance on that link.

contract MyContract{
struct User{
    uint user_id;
    string user_name;
    mapping(uint=>uint) edge_list;
}

mapping(uint=>User) users;

function registerUser(uint user_id, string user_name) public{
        users[user_id] = User(user_id, user_name);
}

function createAcc(uint user_id_1, uint user_id_2) public{
        users[user_id_1].edge_list[user_id_2] = 50;
        users[user_id_2].edge_list[user_id_1] = 50;
}

When I create two users with ID 1 and 2, and try to create an account between them, I would expect users[1].edge_list[2]=50 and users[2].edge_list[1]=50. But for some reason the output I see is users[1].edge_list[2]=50 and users[2].edge_list[2]=50. [I am calling registerUser(1), registerUser(2) and createAcc(1,2)] I've been trying to figure it out for the past 6 hours but haven't understood. Is there something I am missing?

enter image description here

3
  • I replicated your code using solc <0 .7.0 (mandatory to be able to make assignement with nested mappings like you do) and saw no strange behavior... users[1][2] == 50, users[1][1] == 0 users[2][1] == 50 and users[2][2] == 0 are all true.
    – hroussille
    Nov 20 at 11:06
  • @hroussille I am using the remix IDE and solidity 0.4.25. Still getting the wrong output. I've added an image to the question. Should I use a different version or something? Nov 20 at 11:14
  • what is allUsers ? it's the name of your mapping in your code maybe ? It doesn't match the code you provided here. I personally added the following function to check : function getUsers() public view returns (uint256, uint256, uint256, uint256) { return (users[1].edge_list[2], users[1].edge_list[1], users[2].edge_list[1], users[2].edge_list[2]); }
    – hroussille
    Nov 20 at 11:34

Your Answer

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

Browse other questions tagged or ask your own question.