3

I am learning to code with solidity. need help to solve this

I have a membership system where users can have basic, gold, diamond membership types. Users can use coupons every 5 minutes and for basic it is allowed once, for gold 2, for diamond 3.

But issue is this membership is attached to user token balance of some other contract.

For this I already have function to get balance like this

function getERC20Balance(address _address) view public returns (uint) {
    return ERC20Interface(tokenAddress).balanceOf(_address);
}

Now balance is in getERC20Balance()

In constructor I have made this which is definitely for basic membership

constructor { allowedNumberOfTimes = 1; }

But what i want to do is make this dynamic, so if user has balace mroe than 500 then he is in second membership status, and if 1000 then in third, so contract should dynamically set allowedNumberOfTimes = 1 or 2 or 3 depending on membership type

Made this enum also

 enum types["basic","gold","diamond"];
 types type;
 typess constant defaulttype = types.basic;

Now stuck here and need help to make either loop in constructor or some better way to check balance on contract start and assign the appropriate membership automatically in form of allowedNumberOfTimes.

0

You don't have to store it explicitly, you could define it as a function depending on the balance:

function allowedNumberOfTimes(address a) public view returns (uint res) {
    uint b = getERC20Balance(a);

    if(b > 1000) res = 3;
    else if(b > 500) res = 2;
    else res = 1;
}

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.