0

Currently I have quiet a few mappings which I believe overall could be reduced to a struct with internal mappings and one overall mapping, however, I'm not sure how to do this myself and are therefore looking for guidance.

mapping(address => uint256)    public proposedOfferings;
mapping(address => uint256)    public receivedOfferings;
mapping(address => uint256)    public acceptedOfferings;
mapping(address => uint256)    public rejectedOfferings;

How can I re-structure above to below, keeping in mind each mapping consist of two separate uint256 variables (e.g. uint256 amount and uint256 percentageBonus).

struct offerings {
mapping(address=> uint256) proposed;
mapping(address=> uint256) received;
mapping(address=> uint256) accepted;
mapping(address=> uint256) rejected;
}

mapping(address => offerings) offeringData;

also, some information on the related design paradigm would be highly appreciated. thanks!

1
  • Your code still does not reflect that the addresses are mapped to an array of size 2. – Jaime May 4 '18 at 14:22
2
struct param {
    uint256 amount;
    uint256 percentageBonus;
}

struct offerings {
    param proposed;
    param received;
    param accepted;
    param rejected;
}

mapping(address => offerings) public offeringData;

Like this, you will be mapping each address to a structure with four fields (proposed, received... etc) each of these fields have two fields (amount and percentageBonus)

you can use it like this, assume the user address is A:

offeringData[A].proposed.amount will give you the amount proposed by the user with address A.

offeringData[A].proposed.percentageBonus give you the percentage bonus. Same for the other variables.

You can set these doing, for instance,: offeringData[A].proposed.amount = value

Hope this helps.

8
  • Each of the mappings however contain two uint256 variables, so I can't use your approach as far as my knowledge goes. I'll edit my answer for more clarity. – NowsyMe May 4 '18 at 14:17
  • This is not reflected in your question, you have an address mapping to a number. You mean these are uint256 arrays? – Jaime May 4 '18 at 14:19
  • I updated the answer to make every variable in the struct an array of two positions, you will use, for instance, offerings.proposed[0] to access the first element of proposed. – Jaime May 4 '18 at 14:23
  • I think we misunderstand each other, maybe I'm not completely clear in my definitions. I apologise if that's the case. Each mapping consist of two separate uint256 variables (e.g. uint256 amount and uint256 percentageBonus), and not an array of size 2. – NowsyMe May 4 '18 at 14:49
  • 1
    are the names of the 2 variables the same for each mapping? – Jaime May 4 '18 at 15:27
1

Your offering should be a struct:

struct offering {
    uint256 amount;
    uint256 percentageBonus;
}

After this, you can do:

mapping(address => offering) public proposedOfferings;
mapping(address => offering) public receivedOfferings;
mapping(address => offering) public acceptedOfferings;
mapping(address => offering) public rejectedOfferings;

Or even:

struct offerings {  /* please note that is plural */
    mapping(address => offering) proposed;
    mapping(address => offering) received;
    mapping(address => offering) accepted;
    mapping(address => offering) rejected;
}

mapping(address => offerings) offeringData;

It will help to understand more about the business logic behind this, because it seems your offerings can be on different states, and an alternative approach could be to use an enum to describe such states:

enum offeringState { Received, Proposed, Accepted, Rejected }

struct offering {
    uint256 amount;
    uint256 percentageBonus;
    offeringState state;
}

But I'm really taking a wild guess now.

1
  • This could be an alternative approach as well, thanks for insight! – NowsyMe May 4 '18 at 18:07

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.