Reflect finance variables?

Hi, I am trying to create a deflationary token for that I analysing Reflect. Finance code , while analysing I have some doubts about this variable listed below,

mapping (address => uint256) public _rOwned;
mapping (address => uint256) public _tOwned;
uint256 private _rTotal;
uint256 rAmount;
uint256 tAmount;
uint256 rTransferAmount;
uint256 tTransferAmount;

my doubt is what is the exact usage of these in the contract. if anyone knows the exact mechanism of RFI and functionality flow of these variables in the RFI contract, can you please help me to understand the functionality of the variable used in RFI ,and here is the full code of RFI.

Hey, welcome!

Sorry, I do not know what is the Reflect. Finance, I think maybe you can ask their developers for help, they must really understand the whole contracts.

Cause the decimals of the RFI is 9, so I think _tTotal = 10 * 10**6 * 10**9; means token total supply. As for _rTotal = (MAX - (MAX % _tTotal));, if MAX = 123 and _tTotal = 45, so

_rTotal = (MAX - (MAX % _tTotal));
        = (123 - 123 % 45)
        = 90 (_tTotal * 2)

so I think _rTotal means in the range of [0, MAX], the maximum multiple integer of _rTotal, I hope I make myself clear.

1 Like

I have this same question in my topic. I’ve personally tried to reach out to the RFI devs for answers to no avail.

I can understand that _rTotal has a very large number attached. I think it’s supposed to stand for “Reflection Totals”

You can actually see it in one of the functions requirements

function tokenFromReflection(uint256 rAmount) public view returns (uint256){        // no idea what this does
        require(rAmount <= _rTotal, "Amount must be less than total reflections");
        uint256 currentRate = getReflectRate();
        return rAmount.div(currentRate);        // gets the amount of the reflection
    }

I think what it’s doing is somehow calculating the total POSSIBLE reflections that can occur within the token.

However, in another function

function takeReflectFee(uint256 reflectFee, uint256 taxFee) private {
        _rTotal = _rTotal.sub(reflectFee);      // subtracts the fee from the reflect totals
        totalFeeAmount = totalFeeAmount.add(taxFee);    // adds to the toal fee amount
    }

You can see that it’s reducing the rTotal amount. Which is odd to me. Is there a limited number of reflections that can happen per token? Why would that keep getting reduced? It doesn’t make sense to me.

function deliverReflectTokens(uint256 tAmount) public {   
        address sender = _msgSender();          
        require(!isAccountExcludedFromReward[sender],"Excluded addresses cannot call this function");
        (uint256 rAmount, , , , , ) = getTaxAndReflectionValues(tAmount);
        reflectTokensOwned[sender] = reflectTokensOwned[sender].sub(rAmount);
        _rTotal = _rTotal.sub(rAmount);
        totalFeeAmount = totalFeeAmount.add(tAmount);
    }

There is also this function, which isn’t called from within it’s code, but instead it’s called manually from outside the contract.

This doesn’t make a lot of sense to me, how does this all work? I have no clue, only that it “works” as shown from all the clones that use RFI.

If you figure it out I would appreciate you adding me on Discord (in my profile) to explain it to me haha.

I’d also like to state that RFI code was written in a very smelly way. I’ve rewritten part of RFI to reduce the code base. You can reduce their nasty if/else. What I’m trying to say is that if you had the time you could probably rewrite RFI to be way better and to make more logical sense. I will probably end up rewriting RFI in a future project.

1 Like

Thanks for the response @Skyge.

Thanks, @Yoshiko. I have the same doubts you have. As you said, it works functionally when we clone and deploy it on the testnet. Still, the code is a bit complex for me to understand what exactly happens in the function(like what value passes on rAmount,tAmount,tTransferAmount,rTransferAmount and mapping variable like _rOwned,_tOwned…).

Hey I have the sam problem with this protocol. Can you show me your RFI rewritten part? maybe I can understand in a better way

This is unaudited code and hasn’t been used in production. You can see my screenshots of what was changed, then copy and paste the code over that. It’s rewriting the transfer portion, removing repetitive code and following DRY.

On rinkeby the fee is being taken still depending on if its excluded or not, so I believe that it is working correctly. You should double check this yourself before use and confirm that it makes logical sense and that it works.

Old:

New:

function transferTokens(address sender, address recipient, uint256 transferAmount, bool takeFee) private {
    if (!takeFee) {
        removeAllFee();
    }
    
    (uint256 reflectAmount, uint256 reflectTransferAmount,uint256 reflectFee,uint256 taxTransferAmount,uint256 taxFee,uint256 taxLiquidity) = getTaxAndReflectionValues(transferAmount);

    if(isAccountExcludedFromReward[sender]){    // is the sender address excluded from Reward?
        totalTokensOwned[sender] = totalTokensOwned[sender].sub(transferAmount);
    }

    reflectTokensOwned[sender] = reflectTokensOwned[sender].sub(reflectAmount);

    if(isAccountExcludedFromReward[recipient]){    // is the sender address excluded from Reward?
        totalTokensOwned[recipient] = totalTokensOwned[recipient].add(taxTransferAmount);
    }

    reflectTokensOwned[recipient] = reflectTokensOwned[recipient].add(reflectTransferAmount);

    _takeLiquidity(taxLiquidity);     

    takeTeamFee(taxTeamFee);      

    takeReflectFee(reflectFee, taxFee);

    amountTransferedWithinOneDay[sender] += transferAmount;

    emit Transfer(sender, recipient, taxTransferAmount);

    if (!takeFee){
        restoreAllFee();
    } 
}

The order might matter with the if statements, (idk why), so if you plan to reduce it further, just know it could have unintended side effects.

1 Like

thank you ASAP I will check it.
Btw do you know which is the function that give the fees to the holders?

Not sure, but here's my train of thought.

When it takes the reflect fee during a transfer....

function takeReflectFee(uint256 reflectFee, uint256 taxFee) private {
        _rTotal = _rTotal.sub(reflectFee);      // subtracts the fee from the reflect totals
        totalFeeAmount = totalFeeAmount.add(taxFee);    // adds to the toal fee amount
    }

It subtracts from the rTotal and increases the totalFeeAmount.

If we look for totalFeeAmount we find it being used in one other function ......

function deliverReflectTokens(uint256 tAmount) public {   
        address sender = _msgSender();          
        require(!isAccountExcludedFromReward[sender],"Excluded addresses cannot call this function");
        (uint256 rAmount, , , , , ,) = getTaxAndReflectionValues(tAmount);
        reflectTokensOwned[sender] = reflectTokensOwned[sender].sub(rAmount);
        _rTotal = _rTotal.sub(rAmount);
        totalFeeAmount = totalFeeAmount.add(tAmount);
    }

This implies that in order to get the Reflect Tokens then the user needs to call this function.

But I don't get how that actually delivers it nor how the function actually works.

1 Like

exactly this is the point. I can’ t figure out how this algo works. This is employed by safemoon too but I can’t get it

This is a high level idea that might help you understand what’s going on behind the codes: https://wd666.medium.com/what-the-hell-is-rtotal-and-ttotal-4e2ec90466f7

I did a little bit of math:

rTotal = MAX - (MAX % tTotal )
= MAX - (MAX - Q * tTotal)
= Q * tTotal (Q ∈ ℕ , 1 <=Q<=MAX)

0 <= MAX % tTotal < tTotal
0 <= MAX - Q * tTotal < tTotal
-MAX <= -Q * tTotal < tTotal - MAX
MAX - tTotal < Q * tTotal <= MAX
MAX - tTotal < rTotal <= MAX