0

I want to write a generic getData function like this below. But I can only return string data or bytes32 data. When I return bytes32 string type data got vanished. When I return string type data address type data doesn't fit it. So I want to write a tuple for that. To return tuple I need to assign returndatacopy memory into a variable. I have tried mload and store but couldn't manage to assign data into my desired variable. Can anybody help me with this?

function getData(string _key) public returns (string _dataString) {

    string memory _methodName = _delegate.getDataType(_key); // which will return a name from my own internal function. please igrone this.

    bool callSuccess = deployedEternalStorages.call(bytes4(keccak256(abi.encodePacked("get",_methodName,"(bytes32)"))),keccak256(_key)); // getting data from external storage by a call/delegatecall
    //string memory _tempString;

    if (callSuccess) {
        assembly{
            returndatacopy(0x0, 0x0, returndatasize)

            return(0x0, returndatasize)

            //_tempString := mload(0x0)
            //mstore(add(_dataString,0), _tempString)
        }
    } else {
        revert();
    }
}

I have data type in _key variable so, I want to decide what kind of variable will be filled by a condition. I don't want to use fallback function to return data from this function. I need a function for this.

At this point, somebody can show me any process to assign data in a variable from solidity assebbly memory?

Edit: According to the answer from @Ismael I'm clarifying more here. I requirement is to return any kind of data, so I want to return a full set like this below function. To fill those data type I might need to do a conversion. Is there any other way rather than converting data like this below function? or in 5.0.2 can I return data in any other way? string can be directly cast from bytes but how about other datatypes. Can you help me with this?

function getData(string memory _key) public returns (string memory _stringData, address _addressData, uint _uintData, bytes memory _bytesData, bool _boolData) {

    Delegate _delegate = Delegate(MAN.getLatestDelegateContract());
    // Retrieve method name
    string memory _methodName = _delegate.getDataType(_key);
    // Prepare input call
    bytes memory _input = abi.encodePacked(bytes4(keccak256(abi.encodePacked("get",_methodName,"(bytes32)"))),keccak256(abi.encodePacked(_key)));
    // Make the call
    (bool success, bytes memory ret) = address(deployedEternalStorages).call(_input);

    if (success) {

        // Return data as string

        if(keccak256(abi.encodePacked(_methodName)) == keccak256("address")){
            // make necessary change to return ret as address.
        }
        else if(keccak256(abi.encodePacked(_methodName)) == keccak256("uint")){
            // make necessary change to return ret as uint.
        }
        else if(keccak256(abi.encodePacked(_methodName)) == keccak256("bytes")){
            _bytesData = bytes(ret);
        }
        else if(keccak256(abi.encodePacked(_methodName)) == keccak256("bool")){
            // make necessary change to return ret as bool.
        }
        else{
            _stringData = string(ret);
        }
        //return string(ret);
    } else {
        // Call failed revert
        revert();
    }
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.