I have a mapping of a mapping that stores 3 things

1 Address of buyer 2 Address of seller 3 Amount spent.

The goal is to store the amount spent by both parties, so that if buyer B something from seller A, the total amount you spent is recorded, and if buyer X buys from Seller A - it will also be recorded on its own. So then at a later point, one party is able to do something with that value.

I'd like to retrieve the amount spent and seller's address - when I ONLY have the buyers (msg.sender) address.

How would the data structure for this look like?

Currently I have something like this - but its not going to work as I need both keys

mapping(address => mapping(address => uint256)) public valueSpent;

    function test(uint256 initialPrice, address seller) public returns (bool){
        valueSpent[msg.sender][seller] = valueSpent[msg.sender][seller].add(initialPrice);
        valueSpent[seller][msg.sender] = valueSpent[msg.sender][seller];
        return true;
    function getData() public {
    //how to get the seller's address with only the buyer's address??


How would you achieve this?

The key here is gas optimization - so instead of just storing multiple values, we reduce gas cost as much as possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.