🏵
rTokens
tokens that represent redirected yield from lending
Author: Saddam Asmatullayev
Source code: https://github.com/scaffold-eth/scaffold-eth-examples/tree/defi-rtokens
Intended audience: Intermediate
Topics: Scaffold-eth basics, ERC-20 token, yield farming
Table of Contents
rTokens are the tokens exchangeable for their underlying ones (e.g. rDai -> Dai). The principle is similar to cTokens. If someone exchanges tokens for rTokens they could be able to earn interest over the time. However, what differentiates rTokens from cTokens is that with rTokens owner could also share the interest with others. Great example of that is rTrees. Here people could contribute part of their interest to plant the real trees!
Here we will do similar thing 😈
Let's start our environment for tinkering and exploring how honeypots work.
- 1.Clone the repo first
git clone https://github.com/scaffold-eth/scaffold-eth-examples.git defi-rtokens
cd defi-rtokens
git checkout defi-rtokens
- 1.Install dependencies
yarn install
- 1.Start your React frontend
yarn start
- 1.
yarn fork
- 1.Deploy your smart contracts to a local blockchain
yarn deploy
This is how it looks like in my terminal:
If everything worked fine, you have to have something like this opened in your browser:
Don't pay attention to not deployed contracts - these are just interfaces we use for some reasons.
Let's navigate to
packages/hardhat/contracts
folder and check out what contracts we have there.IAllocationStrategy.sol
is the interface for the CompoundAllocationStrategy.sol
where some logic regarding allocation of interest is included. Here we will delegate all this logic to cDai token. And will pay more attention to sharing the interest between different hats. (rToken logic)This is the interface for the compond Tokens, e.g. cDai. We will need one of these for our rToken. We could have also used our own cToken, but I chose to use cDai instead to emphasize on rTokens more in the scope of this branch :)
This is the main contract - of our rToken -
rScaffold 🏗
.The logic and implementation contains all the mentioned use cases of rTokens. We will cover the most important of them and then you can fork the branch and play with other functions and features :)
The first one that we need is SnatchDAI button. We can snatch some DAI from local copy of whatever the real account exist on planet. Once you chose that one, you can insert it as the value of
accountToImpersonate
in App.jsx and for the burnerWalletAddress
put your local address of the wallet from upper right corner.So, don't forget to give you some money for gas from faucet and feel free to click the
snatchDai
! Congratulations, now you have few thousands of DAI on your dai account :)In order to mint some Dai to the contract, we need to approve the contract to take some amount from us. For that, we have that button
ApproveDai
. For simplicity and for only educational purpose we approve maximum amount, but the amount is configurable. Again change the value of accountToImpersonate
in App.jsx and set the value of the rTokenAddress
to address of our contract: 
Then click
Approve Dai
and we can go minting :)If you have read the rToken prerequisite article above, you know that there are some different mint methods we can use. We can separately create a new
hat
and then mint to that default one OR we can mintWithNewHat
and do those 2 actions once. Let's do the second one and see what happens.We need to enter an amount of Dai (100) and then multiply it by 10^18, Also we enter the addresses for our hat - where the interest would go to and the percentages how this will be divided. E.g. I want to 70% to go to me aand 30% to some other address, which I have as an incognito user:

recipients: ["0x4cdabEeaC618d5D16c3838572D3c7d3DC502A286", "0x174968d71d91020b4C827A89E08C6b42ad663BeE"]
proportions: [70, 30]
Then click Send! Warning: Processing of the transaction may take a while, so try again if it will be interrupted due to that. But, in most cases it should successfully convert your 100 Dai to 100rDai and create a new hat with 2 recipients for you!
You can now go and check
balanceOf
function by entering your burnerWallet address. This should show you 100 rDai! 
You can also check our new hat if you want - by id (1) or address (your burner wallet address).
The one of the main interesting things is to check the interest.
If we check now the balance, we will see that it is still 100. So what we need to do is to call
payInterest
in order to transfer hat interest to our account. If you do it and see the balance again you should see that some interest is earned already:Similar with the other account :)
Last modified 1yr ago