The goal is to create a token that will distribute fees to holders when a user makes a transaction. One way is to individually add the reward to all user’s balance. However, such transaction could easily fail due to network gas limit. A different method is to create a deflationary mechanism so that tokens one holds are worth more.
Over the couple of month, auto-staking (auto-farming) tokens have become a new trend in the DeFi space. Some call this the DeFi 2.0 token.
In traditional farms, users have to stake their tokens manually. For some people, this feels like a two-step process right after supplying liquidity to pools or swapping to native token. Moreover, users have to risk their funds since the staked tokens end up in MasterChef smart contract.
Reflect contracts, or RFI contracts, solve this issue by implementing an auto-staking feature built in to the token. This way, users can safely store their tokens in their wallet while still earning rewards. However, one big difference is that rewards come from transaction fees, not from MasterChef minting new tokens.
Before diving into the contract, a new concept must be introduced: t-space and r-space values, along with tTotal and rTotal. tTotal, which belongs to t-space, represents tokens in circulation or total supply of a token. On the other hand, rTotal, which belongs to r-space, is a reflected value of tTotal. The term “reflected” cannot be easily explained in words but one interpretation of rTotal is token supply in reserve. Furthermore, values in t-space can easily be converted to r-space form, and vice versa using formula (3).
Stakers are users who earn passive income by holding native token. In contrast, non-stakers do not earn rewards. Router contracts, pair contracts, dev wallets are usually excluding from staking in order to fully reward users.
can be further broken down as follows:
From the property of remainders:
Therefore, is a multiple of and it is between and . The value can be any number above , and ~uint256(0) is usually the chosen value.
Any t-space value () can be converted to r-space value () as follows:
Transaction mechanism is illustrated in the figure below:
(1) If sender is excluded from staking (2) If recipient is excluded from staking
• tAmount: token amount that sender pays/transfers including tFee
• tFee: transfer fee (note: 10% was chosen arbitrarily)
• tTransferAmount: tokens that will get transferred to recipient
• tOwned[user]: User’s balance represented in t-space (only used by non-stakers)
• rOwned[user]: User’s balance represented in r-space
As illustrated in the figure above, by decreasing rTotal, a deflationary mechanism is achieved. After the first transaction, a newly generated rate will be used for the next transaction or when viewing user balance. This makes tokens in r-space more valuable.
Relationship of in n+2, n+1, and n-th transaction:
in n-th transaction can be calculated using the following formula:
is 10% of , therefore . This makes tokens in r-space deflationary while maintaining .
It is essential to keep non-stakers out of the equation when calculating rate. Instead of equation (3), which includes non-stakers, rate is calculated as follows:
Dev note: Since this operation involves a loop, non-stakers should be no more than 10 and is recommended to store the summation part in storage variables.
For stakers, balance in t-space is calculated as follows:
After a transaction, a new rate will be applied:
Therefore, the general formula for balance in n-th transaction is:
By combining equations (4) and (8), one can prove that denominator will never go to 0.
For non-stakers, their balance is simply .
As mentioned in the introduction, one way to calculate user’s balance is to simply add reward to their original balance:
Interestingly, this equation looks similar to equation (7).
However, this method quickly becomes an issue after another transaction:
RFI contracts has one fundamental issue over including users into staking. The conversion rate between t-values and r-values is calculated using equation (5). Here, it is excluding non-stakers out of equation in order to fully reward the stakers. However, when a new user gets included into staking, the rate updates, thereby changing all stakers balance. Using this feature, it can be used to rug-pull rewards from stakers by including a whale.
Some projects have expanded the contract to not only reward native tokens but also reward other tokens such as BNB, BTCB, CAKE, etc. This is done by taking portion of the reward, then convert to the other token using the router contract.
However, this can lead the token price to drop since the native token is sold in every transaction.