[Ethereum] How are double mappings indexed

solidity

Spec:

  1. Wallets have sums paid to them for different tokens.

Design

  1. Wallet address maps to totalpaid by token.

Implementation

  1. Two mappings, one for wallet address, one for a token label (designed to be an integer based on a second struct)

Error

  1. Integer constant expected

This line fails –

 uint _TotalPaid = PaymentDetail[_TokenIndex][_Wallet].TotalPaid;

Has anyone any ideas about how to solve this error?

Here is the code –

    struct PaymentDetail {
        uint TotalPaid;
    }

    mapping (bytes32 => mapping (uint => PaymentDetail[]) ) public PaymentDetails; 

    function addPaymentDetail (bytes32 _Wallet, uint _TokenIndex, uint _Payment) public {
        uint _TotalPaid = PaymentDetail[_TokenIndex][_Wallet].TotalPaid;
        _TotalPaid=_TotalPaid+_Payment;
        PaymentDetail[_Wallet][_TokenIndex].TotalPaid=_TotalPaid;
    }

Best Answer

As per Documentation . Mapping types are declared as mapping(_KeyType => _ValueType). Here _KeyType can be almost any type except for a mapping, a dynamically sized array, a contract, an enum and a struct. _ValueType can actually be any type, including mappings. It not possible directly make an array of struct as value type. But it is possible for other data types, like :

 mapping (bytes32 => mapping (uint => arrayofdata[index_of_array]) ) public PaymentDetails;

Here I changed the code as solidity support for struct as value type in mapping.

struct PaymentDetail {  
        uint TotalPaid;  
        }

    mapping (bytes32 => mapping (uint => PaymentDetail) ) public PaymentDetails; 

    function addPaymentDetail (bytes32 _Wallet, uint _TokenIndex, uint _Payment) public {
        uint _TotalPaid = PaymentDetails[_Wallet][_TokenIndex].TotalPaid;
        _TotalPaid=_TotalPaid+_Payment;
        PaymentDetails[_Wallet][_TokenIndex].TotalPaid=_TotalPaid;
    }
Related Topic