1

This is my struct:

    struct Token {
        address tokenAddress;
        uint256 minAmount;
        bool emergencyUnlock;
        TokenStatus status;
        string name;
        uint256 decimal;
        string thumbnailUrl;
    }

and a mapping:

    mapping(address => Token) private _token;

A function returns details of the token when I asked given the indexes:

    function getTokens(uint256 start, uint256 length) external view returns(
        address[] memory tokenAddresses,
        uint256[] memory decimals,
        string[] memory name
    )
    {
        tokenAddresses = new address[](length);
        name = new string[](length);
        decimals = new uint256[](length);

        require(start.add(length) <= _tokens.length, "Lock: Invalid input");
        require(length > 0 && length <= 15, "Lock: Invalid length");
        uint256 count = 0;
        for(uint256 i = start; i < start.add(length); i++) {
            tokenAddresses[count] = _tokens[i].tokenAddress;
            count = count.add(1);
            decimals[count] = _tokens[i].decimal;
            name[count] = _tokens[i].name;
        }
        return(
            tokenAddresses,
            decimals,
            name
        );
    }

Now, the problem is that, it asks me that it is not supported to return array of string. Error is below:

TypeError: This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature. string[] memory name

Does anyone know the work around? I know that name will not be more than 20 characters long ever.

4
  • The workaround is to Use "pragma experimental ABIEncoderV2;" to enable the feature. string[] memory name! Jun 5, 2020 at 11:44
  • but experimental ABIEncoderV2 is not recommended for live deployment Jun 5, 2020 at 11:49
  • And so there is no way for you to return an array of strings. Perhaps on solc 0.6, but I doubt it. Jun 5, 2020 at 11:50
  • @FarihaAbbasi It's not experimental anymore: solidity.readthedocs.io/en/v0.6.9/…: 'we kept the same pragma, even though it is not considered experimental anymore'. Jun 6, 2020 at 0:42

1 Answer 1

1

A simple solution to your query is: Rather than including the name of the array in the return parameter. Only write "array_type []" And if it is a string array then, "array_type [] memory"

This Modified Code should work fine in returning the items that you want...

struct Token {
    address tokenAddress;
    uint256 minAmount;
    bool emergencyUnlock;
    TokenStatus status;
    string name;
    uint256 decimal;
    string thumbnailUrl;
}

and a mapping:

mapping(address => Token) private _token;

A function returns details of the token when I asked given the indexes:

function getTokens(uint256 start, uint256 length) external view returns(
    address[] memory,
    uint256[] memory,
    string[] memory
)
{
    tokenAddresses = new address[](length);
    name = new string[](length);
    decimals = new uint256[](length);

    require(start.add(length) <= _tokens.length, "Lock: Invalid input");
    require(length > 0 && length <= 15, "Lock: Invalid length");
    uint256 count = 0;
    for(uint256 i = start; i < start.add(length); i++) {
        tokenAddresses[count] = _tokens[i].tokenAddress;
        count = count.add(1);
        decimals[count] = _tokens[i].decimal;
        name[count] = _tokens[i].name;
    }
    return(
        tokenAddresses,
        decimals,
        name
    );
}

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.