1

I would like an array of mappings, is this possible?

mapping (address >= value) sentValue;

sentValue[] public sentValues;

I want to record amounts sent by address, and I want each send event to be unique with the ability for one address to send multiple different amounts and have each tracked in the array individually? The end result would be the ability to iterate through this array and show in order amounts sent by user.

How do I define a new mapping each time a user sends some amount, and what is the code to push that new mapping on the end of the array?

  • Is it really true that I must use a mapping(uint => struct) with the struct containing address and amount members, and then push and pop the uint from the array as a unique key? Doesn't this seem overly complex for such a simple operation? Please help someone... – SkyTemple Jul 23 at 21:21
  • Also if that is truly the case then wont memory grow to an insane amount... I don't necessarily want to store this data forever but if I must use this proxy storage then when I pop the id from the array only the value in the array will be deleted, and the entry in the mapping(uint => struct) will become a permanent ghost. This seems terrible to me? – SkyTemple Jul 23 at 21:26
  • Looks like "Iterable Mapping" is what you are looking for. – hrkrshnn Jul 26 at 17:57
2

What you are searching for is a mapping of mapping. I created the following smart contract that I think achieve what you described.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.8.0;

contract myContract {
    
    mapping (address => mapping (uint => uint)) public payments;
    mapping (address => uint) public lastPayment;

    function sendMoney() public payable {
        require(msg.value > 0);
        payments[msg.sender][lastPayment[msg.sender]] = msg.value;
        lastPayment[msg.sender]++;
    }

}

Please note that using public for payments and lastPayment variables tells the compiler to create two smart contract functions to read those values.

Here the gist, and here the link to try by yourself with remix.

| improve this answer | |

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.