1

I'm trying to map string to uint but for some reason I'm not able to return the value. However on the demo contract it is working fine. Can somebody help me find the difference?

    // SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.7;

contract Launchpad {

    uint256 projectId;

    // mapping(uint256 => string) public projectNameToProjectId;
    mapping(string => uint) public projectNameToProjectId;

    struct Projects {
        uint projectId;
        string projectName;
    }

    Projects[] public project;

    function addProjectDetails (uint _projectId ,string memory _projectName) public {
        projectNameToProjectId[_projectName] = _projectId;
        project.push(Projects(_projectId, _projectName));
    }

    function getStringToUint(string memory key) public view returns (uint) {
        return projectNameToProjectId[key];
    }
}

contract StringToUintMapping {
    mapping(string => uint) stringToUintMap;

    function setStringToUint(string memory key, uint value) public {
        stringToUintMap[key] = value;
    }

    function getStringToUint(string memory key) public view returns (uint) {
        return stringToUintMap[key];
    }
}
3
  • 1
    What do you mean by " I'm not able to return the value"? It reverts, return an invalid id, it does something else,.. Are you sure the project's name was saved correctly? Perhaps, you are querying before the transaction is mined...
    – Ismael
    Commented Feb 24, 2023 at 15:11
  • @Ismael I'm calling the addProjectDetails function to store the value of the project, now when I try to find the project name by calling getStringToUint in launchpad contract it keeps on returning 0, instead of finding the project id mapped to it
    – psyfi-eth
    Commented Feb 25, 2023 at 10:22
  • 1
    The contract seems fine I don't see an error. If the string wasn't saved then it is likely the transaction saving the name has reverted for some reason. Check if you send enough gas, it has the correct address and contract abi, check the return status of the transaction, etc.
    – Ismael
    Commented Feb 26, 2023 at 0:32

1 Answer 1

0

There is no return value because you are doing the mapping wrong way you need to map a project name against the project id but you are doing it opposite

The correct way of doing it and working code is as follow

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.7;

contract Launchpad {

    uint256 private projectId;

    mapping(string => uint256) public projectNameToProjectId;

    struct Projects {
        uint projectId;
        string projectName;
    }

    Projects[] public project;

    function addProjectDetails (uint _projectId ,string memory _projectName) public {
        projectNameToProjectId[_projectName] = _projectId;
        project.push(Projects(_projectId, _projectName));
    }

    function getStringToUint(string memory key) public view returns (uint) {
        return (projectNameToProjectId[key]);
    }
}

Hope this is what you wanted Good Luck

6
  • @TalhaThis will provide me the name when I enter the equivalent project id, I want project id when I provide equivalent name. I'm looking for the opposite
    – psyfi-eth
    Commented Feb 28, 2023 at 6:41
  • @psyfi-eth Check again ! I've updated answer for that Commented Feb 28, 2023 at 6:48
  • this returns 0 which is actually my problem, I believe your code and my code is same now and the only difference is that you are using datatype as uint256. Please check your code in remix, and let me know if I make sense.
    – psyfi-eth
    Commented Mar 1, 2023 at 5:46
  • Actually, it returns zero when there is no existing ID, it's actually case sensitive for mapping of strings so make sure to enter exact string to get the ID. because its working fine on my end Commented Mar 1, 2023 at 6:06
  • I made sure it is case-sensitive. Please let me know are you calling the mapping array or the function?
    – psyfi-eth
    Commented Mar 1, 2023 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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