# 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
}


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 {
require(!isAccountExcludedFromReward[sender],"Excluded addresses cannot call this function");
(uint256 rAmount, , , , , ) = getTaxAndReflectionValues(tAmount);
reflectTokensOwned[sender] = reflectTokensOwned[sender].sub(rAmount);
_rTotal = _rTotal.sub(rAmount);
}


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?
}

_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
}


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 {
require(!isAccountExcludedFromReward[sender],"Excluded addresses cannot call this function");
(uint256 rAmount, , , , , ,) = getTaxAndReflectionValues(tAmount);
reflectTokensOwned[sender] = reflectTokensOwned[sender].sub(rAmount);
_rTotal = _rTotal.sub(rAmount);
}


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