Send reward to a token holders in smart contracts

Hello every one
How can I send tokens to a token holders from inside a smart contract with solidity?
It means how can send reward to token holders?

You could keep track of the holders by using an array of address or a mapping. Then when you want to distribute a reward, you can loop through the array and give out the tokens.

contract Rewarder {

    address token;

    address[] holders;

    uint256 rewardAmount = 10;

    constructor(address _token) {
        token = _token;
    }

    function addHolder() public payable {
        require(msg.value > 0);
        holders.push(msg.sender);
        IERC20(token).transfer(msg.sender, msg.value);
    }

    function rewardHolders() public {
        for (uint256 i = 0; i < holders.length; i++) {
            IERC20(token).transfer(holders[i], rewardAmount);
        }
    }
}

You would probably want to restrict access to who can call the reward function or maybe call it after a certain time passes. Hope this helps.

1 Like

Hello friend
Do you think this will not cost much?
I mean much gas or energy.

This is not doable if the aim is to rewards all the holders. You will get an out of gas error.

1 Like

Do you want to reward in native token or in another token like busd for instance

1 Like

I want to reward with Native token

Ok then I suggest to take a look at reflective tokens (ie. safemoon). Pay attention, I am not saying their code is good or you must copy it, but just check how they distribute rewards

1 Like

what if you were wanting to send a token to a holder of another token?
for instance, pancakceswap sending cake for staking BNB.
I assume it would some how check the holders of LP token or maybe even check the smart contract some how. Im new to this but it seems right.

What you mentioned is called staking, or, Farm. It can work in different ways. In this case, when user deposit BNB, the staking contract record the user address, the amount and some other data that will be used to calculate the rewards.
You can't know all the holders of a token using a smart contract, this is not doable onchain.

TY.
I am a web dev student and im working to self learn block chain.
here is what im wanting to do.
I created a limited number of nfts on chain (550 total).
My goal is to find a way to use them to stake or farm for holding the item.
Then reward them in a token i am building.
Is it somehting I would manually have to look up all of the addresses each time then?