What the hell is _rTotal and _tTotal

Darth Jaja
2 min readMay 25, 2021

--

TL;DR: In Reflection contracts like Safemoon, _rTotal is like the money shown in the balance sheets of the central bank, while _tTotal is like the currency in circulation. When someone sells and a fee is to be distributed to all holders, instead of doing it with a for loop, _rTotal is shrunk so everyone’s money is worth more, just like deflation.

Today I’ve been trying to understand the contract behind Safemoon — well actually, Reflect contract. This article shows my understanding from a high level.

The contract is written in an ugly way, without many comments so it took me quite a lot of time to understand _rTotal and _tTotal.

As a result, I find it a rather clever implementation. Think about the two scenarios below:

If you know Physics, think about how people solve Maxwell equations: They bring E and B to complex space, do calculations easily with complex numbers, and transform the result back to real space.

If you know Finance, you must be familiar with the deposit reserve ratio, which can be used to roughly derive the currency in circulation from the central bank money supply, and vice versa.

You see the *supply*here — familiar? It’s not exactly the same thing in Reflection contracts (For example, the currency in circulation is always much more than in the central bank’s balance sheets, opposite to _rTotal and _tTotal in Reflection contracts), but it’s pretty much the same mechanism.

In Reflect, you can understand _rTotal as the total money supply in the central bank, and _tTotal as the dollars in your pocket + you deposit to commercial bank +, etc. The _getRate() function actually gets you a ratio similar to the deposit reserve ratio.

Why is it clever

Imagine you are the creator of the Reflection contract. Now you want to distribute fees to everybody when someone sells. What comes right out of your mind might be using a for loop to add to each owner's balance. But a second thought might be, instead of looping through all the holders, let’s build a deflation mechanism so that the tokens you hold are worth more.

This is done by changing the _tTotal and _rTotal in _reflectFee function, which is called every time there is a fee. For example, in _transferStandard. This way, the next time there’s a transaction when you call the _getRate() function, a different rate will be generated, thus the tokens you hold is worth more/less. The mechanism is illustrated in the figure below:

This is just a rough idea, it’s not rigorous. I don’t want to navigate thru the contract code here but when you do it, hope this idea can help you.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Responses (4)

Write a response