0

I would need to keep a list of addresses (whitelist) and assign a badge/title for to the first 20 users, then another badge/title to the next 100, etc.

e.g.

First 20 users/addresses = Early Adopter
Next 100 users/addresses = Explorer
Future: next n users/addresses different badge/title

Any help, please? Thanks.

1
  • Please share your code so far and where you got stuck.
    – user19510
    Commented Apr 5, 2018 at 2:08

3 Answers 3

1

"Whitelist" makes me think only a trusted user or a server would append to the list. I that case, the msg.sender could actually specify which badge they get and even change it in future.

A variant would be user self-registration.

When you append to an array, you get an unambiguous ordering from the first to the last. The next bit depends on what you're trying to prove. You could.

  • award the badge as a struct property on the way in, proving that the badge was awarded at a certain time, or
  • use the position in the list to determine it. So, by definition, the user at row 18 is an early adopter.

If you want the flexibility to move the thresholds in the future, then the second method. On the other hand, if you need to prove that the contract owner/admin will not tamper with the awards, then the first one and no method of changing the important attribute.

Hope it helps.

1

It depends.

If the whitelist is populated on-the-fly, then as I see it, you have a few optins:

1) Keep only one array (whitelist) of addresses. Whenever someone wants to see their badges, check how many addresses are in the array before the requester and show badges accordingly. If people are never removed from the whitelist this might be an optimal solution

2) Keep a whitelist and a separate counter such as howManyUsers. Whenever a new user joins, increase howManyUsers and assign badge accordingly.

If the whitelist is populated in advance, the second option is much better. IF needed, you can keep a separated array of joiners.

1

You could have a variable

uint userCount = 0;

And maybe an enum or struct for your badge, depending how complex it is.

struct Badge{
    //Whatever makes badges different
}

Then have a mapping of users to badges

mapping(address => Badge) badges;

And whenever someone joins

function theJoiningFunction() public {
    userCount++;
    if(userCount <= 20){
        badges[msg.sender] = Badge(/*early adopter parameters*/);
    }else if(userCount <= 100){
        badges[msg.sender] = Badge(/*explorer parameters*/);
    }else{
       badges[msg.sender] = Badge(/*normal user parameters*/);
    }
2
  • Your solution is simple, but the thing is that in future I will have more titles/badges/ranges to check :S
    – Eusthace
    Commented Apr 5, 2018 at 6:33
  • Well, in stead of hard coding the different ranges, you could create an array with the ranges and iterate through that to find the right badge, and then add to the array as needed. As long as you iterate backwards through the array, and as long as you don't add badges for ranges far beyond your current user count, it should scale fine. Commented Apr 5, 2018 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.