Home > Enterprise >  Distribute fees evenly to many addresses(pull method) in Solidity
Distribute fees evenly to many addresses(pull method) in Solidity

Time:05-08

I want to distribute a fee from every transaction to a mapping(address=>uint) of 3000 addresses, evenly.

Now, that is an issue because the function will run out of gas, so I've heard, that instead of a push method, a pull method should be able to pull it off.

So instead, pool all fees together under one single uint and then let each of every 3k address pull their own share.

Now that brings new issues because the pool uint is forever increasing and decreasing (when people take their share out and new incoming fees from transactions) and how can I control one who may only take their share once but still continuously & evenly distributed?

Some direction here would be greatly appreciated on how to solve those distribution issues because my math is far from the strongest asset I possess.

CodePudding user response:

Solved it by having a mapping to store for every user what was the total incoming deposits last time they claimed their share and deduct that from the current total incoming deposits and give their % cut based on the difference if any.

Psuedo Solidity (excluding proper checks & interactions):

uint256 totalRevShare = onDeposit() pub payable {... =msg.value}

//..in Withdraw Function
...
uint256 unclaimedScope = totalRevShare - LastTotalRevShare[user];
LastTotalRevShare[user] = totalRevShare;
uint256 _userUnclaimedCut = unclaimedScope / totalReceivers;
...
msg.sender.call{value:_userUnclaimedCut}("");

Hope it helps you to move from push to pull functionality.

  • Related