I am building a function that will payout dividends based on the percentage of tokens owned.
function simulateRent() public payable{
uint256 rent_received = msg.value;
for (uint i = 0; i < ownersCount; i++) {
uint256 calculateRent = balanceOf(address(owners[i]))*(rent_received)/_totalSupply;
payable(address(owners[i])).transfer(calculateRent);
}
}
This function seems to work when I have simple numbers, e.g. if total supply is 1M and each person owns half, then the msg.amount
will be split in half and divided equally. However, I encounter problems when dealing with more than 2 owners and especially when their number of tokens owned isn't easily divisible.
I was wondering how can I make this calculation?
For context: Total supply is 1M*e18 (18 decimals). Incoming payable amount will be around 1 ETH (it can be less than 1 ETH).