# What does Reflect.Finance code do: \_rTotal = (MAX - (MAX % totalSupplyOfToken));

**URL:** <https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572>\
**Category:** Support\
**Created:** [March 27, 2021, 3:45pm UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572 "2021-03-27T15:45:43Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![sanar](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@sanar](https://forum.openzeppelin.com/u/sanar)\
**Post date:** [May 31, 2021, 7:47am UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/26 "2021-05-31T07:47:38Z")

</div>

Hi Tsushima - thanks for the reply. i did check the excludedFromReward and if you notice what happens is that the code is doing the following:

```
function excludeFromReward(address account) public onlyOwner() {
        // if the account excluded equals false carry on - else throw error
        require(!_isExcluded[account], "Account is already excluded");
        // if the rewards owned is greater then zero - set the tokens owned to the tokens from reflection
        // using the current rate of reflection.
        if(_rOwned[account] > 0) {
            _tOwned[account] = tokenFromReflection(_rOwned[account]);
        }
        // exclude the account from future token reflections
        _isExcluded[account] = true;
        // remove the account from the excluded array 
        _excluded.push(account);
    }

```

this looks correct as now \_tOwned tracks the tokens owned by the account and it has been removed from \_rOwned.

Anyways it looks like it works - I just thought from comments above that \_tOwned would always reflect the actual tokens owned without reflections.

---

<div class="post-metadata">

**Author:** ![nastari](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/nastari/32/6841_2.png) [@nastari](https://forum.openzeppelin.com/u/nastari)\
**Post date:** [June 16, 2021, 3:29pm UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/27 "2021-06-16T15:29:41Z")

</div>

![Screenshot from 2021-06-16 11-44-09](https://us1.discourse-cdn.com/flex022/uploads/zeppelin/original/2X/2/2d3c78653e11062a9a653f8ce7a6f4e14ed3ef34.png)  
this is the behavior on the graph for the f(x) = ( any\_number - ( any\_number % x))

I couldn’t imagine any real advantage in using this strategy, just more spending on gas.

---

<div class="post-metadata">

**Author:** ![hvquy001](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/hvquy001/32/7170_2.png) [@hvquy001](https://forum.openzeppelin.com/u/hvquy001)\
**Post date:** [June 27, 2021, 6:25pm UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/28 "2021-06-27T18:25:25Z")

</div>

Actually, the `MAX` number is fine.

Take a look at the function `tokenFromReflection` which transforms the balance to `balanceOf`

Balance is calculated by the `rAmount / currentRate`, which `currentRate` is calculated by `_rTotal/_tTotal` (suppose that there are no exclude addresses available)

 ![Screen Shot 2021-06-28 at 01.15.35](https://us1.discourse-cdn.com/flex022/uploads/zeppelin/original/2X/1/1b3dc741c8c54d7262337aa289f92ac79264bcd5.png)

However, `_rTotal ` will be reduced by the fee whenever a transaction is made. That why the user’s balance keeps increasing.

For the question of this topic, I think just for rounding the number more beautify.  
`115792089237316195423570985008687907853269984665640564039457580000000000000000/10000000000000000` would be much human friendly than `115792089237316195423570985008687907853269984665640564039457584007913129639935/10000000000000000`

---

<div class="post-metadata">

**Author:** ![REGO350](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/rego350/32/7220_2.png) [@REGO350](https://forum.openzeppelin.com/u/REGO350)\
**Post date:** [July 15, 2021, 6:51am UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/29 "2021-07-15T06:51:23Z")

</div>

@Yoshiko  
After doing a little bit of math, I think I started to see somthing.

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

```

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

```

rTotal is a multiple of tTotal. Q is equal to the return value from `_getRate()`.  
MAX can be any number since it gets cancelled out. However, in order to avoid MAX - tTotal from becoming negative, they just chose the biggest number that solidity can provide.

---

<div class="post-metadata">

**Author:** ![Cory](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/cory/32/7676_2.png) [@Cory](https://forum.openzeppelin.com/u/Cory)\
**Post date:** [July 17, 2021, 6:19am UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/30 "2021-07-17T06:19:24Z")

</div>

I suggest you see this article in Stack Exchange.

> <https://ethereum.stackexchange.com/questions/98622/binance-smart-chain-tokens-what-are-ttotal-rtotal-tsupply-rsupply-rowned-t>

---

<div class="post-metadata">

**Author:** ![REGO350](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/rego350/32/7220_2.png) [@REGO350](https://forum.openzeppelin.com/u/REGO350)\
**Post date:** [August 21, 2021, 3:25am UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/31 "2021-08-21T03:25:32Z")

</div>

I wrote a technical whitepaper for Reflect.  
Check it out:

> [@A Technical Whitepaper for Reflect Contracts](https://forum.openzeppelin.com/t/a-technical-whitepaper-for-reflect-contracts/14297):
>
> I wrote a technical whitepaper for Reflect Contracts. ↓ [https://reflect-contract-doc.netlify.app/](https://reflect-contract-doc.netlify.app/) In short, reflect contracts are tokens that have auto-staking mechanism built in. Safemoon, Octa, Lunarswap tokens, are some examples. I decided to write the technical paper because there wasn’t one. In fact, if you look at the [program](https://github.com/reflectfinance/reflect-contracts/blob/main/contracts/REFLECT.sol), it is very hard to read and understand. I spent some time digging it to the program, tried to understand how it works, and then finally wrote the paper. I thin…

---

<div class="post-metadata">

**Author:** ![maxareo](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/maxareo/32/11606_2.png) [@maxareo](https://forum.openzeppelin.com/u/maxareo)\
**Post date:** [August 21, 2021, 5:19am UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/32 "2021-08-21T05:19:47Z")

</div>

Does `rTotal` matter in the calculations?

---

<div class="post-metadata">

**Author:** ![REGO350](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/rego350/32/7220_2.png) [@REGO350](https://forum.openzeppelin.com/u/REGO350)\
**Post date:** [August 21, 2021, 8:10am UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/33 "2021-08-21T08:10:08Z")

</div>

Yes.  
Read [my paper](https://www.keepandshare.com/doc15/24269/reflect-technical-paper-pdf-822k?da=y) and you'll understand

---

<div class="post-metadata">

**Author:** ![maxareo](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/maxareo/32/11606_2.png) [@maxareo](https://forum.openzeppelin.com/u/maxareo)\
**Post date:** [August 21, 2021, 9:46am UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/34 "2021-08-21T09:46:45Z")

</div>

You paper does include it in your formulas but didn't explain why it is included. Curious if a large `rTotal` or a small `rTotal` would change the performance of the mechanism. It would be great if it is made more clear.

---

<div class="post-metadata">

**Author:** ![REGO350](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/rego350/32/7220_2.png) [@REGO350](https://forum.openzeppelin.com/u/REGO350)\
**Post date:** [August 21, 2021, 12:26pm UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/35 "2021-08-21T12:26:04Z")

</div>

TL;DR

- The whole purpose of t values and r values is to create a deflationary mechanism.
- rTotal is tTotal in r values.
- Most numbers in Solidity is uint256 so it shouldn't change the performance
- The initial rTotal is between MAX-tTotal and MAX. MAX can be any number above tTotal but many tokens choose the biggest number that Solidity provides.

It is a new concept, it is not easy to explain in simple english.

---

<div class="post-metadata">

**Author:** ![AmirHossein484](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@AmirHossein484](https://forum.openzeppelin.com/u/AmirHossein484)\
**Post date:** [October 1, 2021, 3:31pm UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/37 "2021-10-01T15:31:44Z")

</div>

This article seems to be deleted.  
Do you have the pdf file?

---

<div class="post-metadata">

**Author:** ![maxareo](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/maxareo/32/11606_2.png) [@maxareo](https://forum.openzeppelin.com/u/maxareo)\
**Post date:** [October 1, 2021, 3:39pm UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/38 "2021-10-01T15:39:52Z")

</div>

Sorry, the article was withdrawn for confidentiality reasons. There are great alternative posts on this forum doing a great job explaining `RFI`.

---

<div class="post-metadata">

**Author:** ![AlexMarty](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@AlexMarty](https://forum.openzeppelin.com/u/AlexMarty)\
**Post date:** [October 18, 2021, 8:41pm UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/39 "2021-10-18T20:41:33Z")

</div>

Hey!  
I found this article. I hope it helps 😉  
[\_rTotal](https://wd666.medium.com/what-the-hell-is-rtotal-and-ttotal-4e2ec90466f7)

---

<div class="post-metadata">

**Author:** ![Alexander-Herranz](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/alexander-herranz/32/9387_2.png) [@Alexander-Herranz](https://forum.openzeppelin.com/u/Alexander-Herranz)\
**Post date:** [November 13, 2021, 5:53pm UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/41 "2021-11-13T17:53:56Z")

</div>

And what is the purpose of **tOwned** and **excluded accounts**?  
I see that by default excluded accounts are 2: The contract's itself and the owner's account.

Is it just to avoid the creator (owner) and the contract itself receive rewards, in order to give more to token holders?

---

<div class="post-metadata">

**Author:** ![Omar\_EL\_KHAL](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/omar_el_khal/32/10378_2.png) [@Omar\_EL\_KHAL](https://forum.openzeppelin.com/u/Omar_EL_KHAL)\
**Post date:** [November 14, 2021, 1:21pm UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/42 "2021-11-14T13:21:45Z")

</div>

thank you for the great explanation, what I don't understand is why you subtract \_rFee from \_rTotal?

---

<div class="post-metadata">

**Author:** ![Alexander-Herranz](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/alexander-herranz/32/9387_2.png) [@Alexander-Herranz](https://forum.openzeppelin.com/u/Alexander-Herranz)\
**Post date:** [November 14, 2021, 6:36pm UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/43 "2021-11-14T18:36:20Z")

</div>

> [@Omar\_EL\_KHAL](#):
>
> \_rFee

Because when you decrese **\_rFee** to **\_rTotal** (divisor) each **rOwned** [address] balance gets bigger, in order to give rewards from the transaction fee, because **currentRate** (the divisor obtained from **\_getRate()** function) is getting smaller.

Take a look at how transfer fee rewards are shared and obtained from **balanceOf** (address) function trough **tokenFromReflection** (rAmount) function =\> **rAmount.div(currentRate)**:

```auto
function _getRate() private view returns(uint256) {
       (uint256 rSupply, uint256 tSupply) = _getCurrentSupply();
       return rSupply.div(tSupply);
   }

function balanceOf(address account) public view override returns (uint256) {
    if (_isExcluded[account]) return _tOwned[account];
    return tokenFromReflection(_rOwned[account]);//=> rAmount/currentRate
}

function tokenFromReflection(uint256 rAmount) public view returns(uint256) {
    require(rAmount <= _rTotal, "Amount must be less than total reflections");
    uint256 currentRate = _getRate();
    return rAmount.div(currentRate);
}

```

---

<div class="post-metadata">

**Author:** ![Amin20100](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/amin20100/32/8549_2.png) [@Amin20100](https://forum.openzeppelin.com/u/Amin20100)\
**Post date:** [January 19, 2022, 10:04pm UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/44 "2022-01-19T22:04:15Z")

</div>

Well `_rTotal` is simply `Total Reflections` and `_rOwned` refers to number of reflection a user owns.  
think of this way:  
we could set initial `_rTotal` to `100` to represent 100%  
and if your `_rOwned` value is 10, you own 10% of total reflections.  
in the `constructor` method you see after deployment of the contract, the whole reflections are transfered to the owner of the contract.

```auto
_rOwned[_msgSender()] = _rTotal;

```

using 100 as total reflection has a problem: solidity only supports integers and doesn't support floating point numbers, so we gotta somehow do it another way

If i am right, the reason they set `_rTotal` to a huge number is to make reflection very accurate and support as much floating points as possible (if i am wrong, correct me), so if you own something like 0.00812301283120398912312836% of reflections, it should work accurately.

I hope i could explain it in a simple way

---

<div class="post-metadata">

**Author:** ![v\_lnko99](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/v_lnko99/32/11751_2.png) [@v\_lnko99](https://forum.openzeppelin.com/u/v_lnko99)\
**Post date:** [March 29, 2022, 4:42pm UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/45 "2022-03-29T16:42:38Z")

</div>

You are a f\*cking hero, dude!

---

<div class="post-metadata">

**Author:** ![v\_lnko99](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/v_lnko99/32/11751_2.png) [@v\_lnko99](https://forum.openzeppelin.com/u/v_lnko99)\
**Post date:** [March 30, 2022, 4:00pm UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/46 "2022-03-30T16:00:26Z")

</div>

Guys, why do they assign rTotal, but emit another value tTotal? Thx in advance!

constructor () public {  
\_rOwned[\_msgSender()] = \_rTotal;  
emit Transfer(address(0), \_msgSender(), \_tTotal);  
}

---

<div class="post-metadata">

**Author:** ![Yams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/yams/32/7035_2.png) [@Yams](https://forum.openzeppelin.com/u/Yams)\
**Post date:** [June 22, 2021, 11:44pm UTC](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572/47 "2021-06-22T23:44:08Z")

</div>

I am new to Solidity and contracts. Have a background in JavaScript and been spending hours into learning Solidity and reading multiple contracts.

I was hoping to gain some insight from those with experience. It is a lot of fun to read this and try to piece the puzzle together on how it all works but I was wondering what does an r and t refer to in front of variables?

Example:

```
uint256 private _tTotal = 1000000000 * 10 **6 * 10** 9;
uint256 private _rTotal = (MAX - (MAX % _tTotal));

```

or

```
mapping (address => uint256) private _rOwned;
mapping (address => uint256) private _tOwned;

```

I wish people used the full word in order for those starting can actually understand if t refers to Transfer or something else.

Thank you for everyone who can help me understand this and looking forward to diving into this 😃

[Previous page](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572.md?page=1)

[Next page](https://forum.openzeppelin.com/t/what-does-reflect-finance-code-do-rtotal-max-max-totalsupplyoftoken/6572.md?page=3)
