I'm writing a smart contract function that rewards users some rate of interest/reward for holding my stablecoin over owning competitors (Dai, Tether, etc). What is the most efficient manner to calculate this in a collect_interest function? I've studied how the Dai Savings Rate (DSR) and compound.finance work, and I think my implementation would need to be slightly different because users don't deposit the stablecoin into a contract like DSR or compound. They simply earn interest/rewards for owning the stablecoin in their address. The amount of stablecoin they own in their address can vary across time so their reward can vary as a result, and the function would have to check which windows of time the user has already collected interest/rewards so they don't collect interest more than once for each period.

Is this too complicated or too inefficient/costly? Should I just replicate compound/DSR and have holders of my stablecoin deposit it into an interest_rate smart contract and receive a 1:1 interest_rate_ERC20 token like compound? Or is there a simple way to do this that I'm not seeing? Thanks in advance, even just pointing me to an example link would help.

1 Answer 1


You can compute the interest based on the last collected interest date:

function collect_interest() public {
    uint256 daysSinceLastCollect = now.sub(lastCollectionTime).div(1 day);
    uint256 newInterests = daysSinceLastCollect.mul(interestRatePerDay).mul(balanceOf(msg.sender));

    _mint(msg.sender, newInterests);

    lastCollectionTime = now;

The only thing to be aware of is when tokens are being transferred. However, you can avoid issues by adding a collect_interest() call before each token transfer.

  • Thanks for this, if I understand properly, this function should be called any time when there is a change in balance of the stablecoin since it doesn't keep track of changing balances across time of a specific address, so in order to mitigate for that case, it should just be added to the transferFrom and transfer functions of the ERC20 token, right? So essentially, any transferring of stablecoins will automatically payout interest to the transferer or if they wish to collect interest sooner, they can just call this manually themselves?
    – EazyC
    Commented Apr 14, 2020 at 2:28
  • Also, is dividing by 1 day necessary if paying out interest per second in terms of epoch time is desired? I assume you divided by 1 day because this function pays daily interest rather than per second interest.
    – EazyC
    Commented Apr 14, 2020 at 2:32
  • 1
    @EazyC Everything you said is correct. The 1 day is just an example, yes you could do a rate per second as well. Just be aware of Solidity not supporting float numbers, so either way you probably want to divide it by something. Commented Apr 14, 2020 at 8:27
  • Thank you, really appreciate it.
    – EazyC
    Commented Apr 14, 2020 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.