0

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;
    }
2

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;
    }
  • I had tried the order as you state, but I missed for plural part of the PaymentDetails. Thanks for spotting that. – Trevor Lee Oakley Sep 13 '18 at 7:19
1

You declared the mapping with name PaymentDetails but you are trying to access the contents of struct type PaymentDetail, which does not make sense. I bet that's a typo. Also, as Ha ĐANG noted, you are accessing the mapping in a wrong way.

Change

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

to

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

1

You're using wrong position for the keys of the mapping. It should be as below to matching with you declaration

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

Further, you'd better to use address for wallet, should not use bytes32 for the address/wallet.

  • 1
    Comments are not for extended discussion; this conversation has been moved to chat. – eth Sep 14 '18 at 9:59

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.