I am currently working on a dividend-paying smart contract, and am wondering if there is a more gas-efficient way of performing the action that I require on-chain. The concept of the function is to take a snapshot of the current holders, get a percentage of their total ownership of the token, and then update an internal ledger of pending rewards depending on the month's earnings. So, for example, if a user owns 10% of the tokens and 10k USDT was earned in the last month, their "pending rewards" would update to 1k USDT.
Ideally, I would be able to look at the token holders on-chain and get a percentage of ownership from that, but I'm worried that concept itself is incredibly gas intensive and will take way too much ETH to perform. So I haven't even bothered testing this method because, from previous experience, this is going to be a lot of gas.
// SHOULD BE
function snapshotDividend(uint256 _monthlyEarnings) public {
// Look at all the holders of TOKEN
// Get their percentage ownership
// pendingRewards[_address] += percentage * _monthlyEarnings
// ^ Obviously percentage * _monthlyEarnings is just psuedocode, will need to use integer logic (no floating points)
}
Here's a snippet of a less decentralized but easier to implement idea of what I'm trying to do, but once again this is going to create lots of gas problems if/when the project gets into the thousands of wallets, let alone the hundreds of thousands of wallets.
// dummy way
function snapshotDividendDumb(address[] memory _addrs, uint256[] memory _rewards) public {
require(_addrs.length == _rewards.length, "New rewards and address arrays must be of equal length.");
for (uint i = 0; i < _addrs.length; i++ ) {
pendingRewards[_addrs[i]] += _rewards[i];
}
}
I was just curious as to whether or not anyone here has run into a similar issue dealing with gas for a problem like this. Let me know if I can provide any further information.