3

Is it possible for Solidity to accept mappings as input parameter in a function? I've been trying to create a function with a second parameter mapping (string => string) aMapping,

pragma solidity ^0.4.6;

contract testInputArray {
    // Events
    event LogFunctionWithMappingAsInput(address from, address to, string message);

    function sendMessage(address to, mapping (string => string) aMapping) {
        LogFunctionWithMappingAsInput(msg.sender, to, aMapping["test1"]);
    }
}

but compiler throws the following error:

Untitled:7:35: Error: Type is required to live outside storage.

According to Can I call mapping as arguments of function? the function must be internal or private to accept a wrapping struct, but I need the function be callable from outside the contract, directly by an Ethereum transaction.

Is there any way to pass a mapping (associative array or dictionary) as an input parameter in Solidity? Thx!

Refs.:

2

You can't pass the whole mapping. You could pass a struct internally. Ideally, you would enumerate the fields you really need to pass so you can create an interface that works with other contracts and clients.

Your example above, seems to pass a single member of the source mapping. I would interpret that roughly like this:

pragma solidity ^0.4.6;

contract testInputArray {

    mapping (string => string) aMapping;

    // Log event to print the message details
    event Log(address from, address to, string message);

    function sendMessage(address to, string key) {
        Log(msg.sender, to, aMapping[key]);
    }
}

Hope it helps.

P.S. It would be remiss to omit the usual preference for bytes32 over string if that's possible in this case. Left it as string for clarity.

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.