7

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.:

1
  • 1
    You cannot pass a mapping because they can grow to such a large size that it would be infeasible to copy from one contract storage to another in the same transaction. ethereum.stackexchange.com/a/24519/3573
    – paulhauner
    Commented Aug 17, 2017 at 3:43

1 Answer 1

5

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.

1
  • You can pass a storage pointer to a mappjng but only internally. Commented Mar 9, 2021 at 22:13

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.