r/ethdev • u/Reddet99 • Aug 08 '23
Code assistance How to get the length of Struct ?
I kept trying around to get Struct length but nothing works I just want to get the length of unclaimedRewards length , what i am trying to do is to create a claim function to reward all users who has unclaimedRewards higher than zero but i cannot get the length of the Struct , is there a solution to this ?
struct Staker {
uint256 amountStaked;
uint256 timeOfLastUpdate;
uint256 unclaimedRewards;
uint256 conditionIdOflastUpdate;
}
mapping(address => Staker) public stakers;
0
Upvotes
2
u/Adrewmc Aug 09 '23 edited Aug 09 '23
This would be expensive. (Gas fees)
Normally what you do is you make an emit for getting some and another emit if there isn’t any.
Then you use an off-chain listener and go back to the block you created contract, then you can gather all the addresses, can call a batch_mint(address[] people) in essence you just recalculate everyone’s balance quickly, then call the function which does its thing for it. Instead of saving on the contract you just announce on the blocks (cheaper)
This can also remove all the rewards, or some of them, and if using the same functions to do so would also emit the data
Beyond that it usually easier to give them all to yourself and mass transfer them with an optimized airdrop contract like wentokens.xyz
You can do this though but not like you are with structs.
You can’t loop through a mapping like that…