Redemptions can be read about more here. This page will outline one way to conduct a redemption through snowtrace, in addition to how to estimate fees through Tenderly if doing multiple transactions. DISCLAIMER: This is purely a guide and all users should both understand the actions which they are submitting to the avalanche blockchain, and double check all addresses which they are interacting with. All official Yeti Finance contract addresses are listed at this link: https://docs.yeti.finance/other/contract-addresses#core-contracts.
Submitting a Redemption Through Snowtrace
Open snowtrace.io and navigate to the page with TroveManager, address found at the link above. The direct link is here: https://snowtrace.io/address/0x000000000000614c27530d24B5f039EC15A61d8d
Navigate to the "Contract" tab and then to the "Write as Proxy" tab. Connect your metamask wallet, and look for the function "redeemCollateral", which should look as the image below.
The parameter descriptions are as follows:
_YUSDamount: The amount of YUSD you are attempting to redeem, after the fee is deducted.
_YUSDMaxFee: The maximum fee in YUSD which you are willing to redeem.
_firstRedemptionHint: The hint for the address of the first trove to be redeemed in the sortedTrove list.
_upperPartialRedemptionHint: The upper hint for the position of the last partially redeemed trove in the sortedTroves list.
_lowerPartialRedemptionHint: The lower hint for the position of the last partially redeemed trove in the sortedTroves list.
_partialRedemptionHintAICR: The Adjusted Individual Collateral Ratio of the last partially redeemed trove in the sortedTroves list
The maximum number of iterations to perform. If zero, the function will run until it runs out of gas.
The amount of YUSD which are to be taken from your wallet during the transaction are bounded by YUSDAmount and YUSDMaxFee. For example, if you are trying to redeem 1,010 YUSD, and the fee is 1%, then the YUSDAmount will be 1,000 and the YUSDMaxFee will be 10; save these numbers to input into the "RedeemCollateral" function for later. If your redemption amount is small relative to the YUSD supply, the fee will likely be just a bit over 0.5%, so you can estimate that as 0.51% to input. If there has been redemption activity before this, or if you are doing a large amount, and would like to estimate the fee better, see the tenderly section below.
Redemption hints are just precautions to reduce gas, moving computation off chain before doing the transaction. So if these are incorrect the transaction will still likely be able to proceed. To get estimates of these, use the HintHelpers contract, which just fetches some information from chain and does some computation. Check the link above to verify the address, but it will just be a view function here anyway. The direct link is here: https://snowtrace.io/address/0x70273B508F895254553F4096575832DaB49a8B37
Head over to "Contract" and then "Read Contract". Open the function "getRedemptionHints". There are two parameters, which will are the same _YUSDAmount which you have calculated from above, as well as the maxIterations parameter, which you can input some large number like 100,000. An example is as follows, for 1000 YUSD. Remember that 1000 YUSD actually means 1000e18 = 1000000000000000000000 when input into snowtrace, due to the decimal precision of the YUSD token:
With the outputs, you will need to do the following:
firstRedemptionHint is used in the "RedeemCollateral" function call, so save that for later.
partialRedemptionHintAICR is used in the "RedeemCollateral" function call, so save that for later.
If truncatedYUSDAmount does not equal your input YUSD Amount, then increase the input amount by 2000 YUSD. The minimum debt for a trove is 2000 YUSD, so if a partial redemption would put a trove below that amount it will cut it off before the 2000 is reached. For example, if you are trying to redeem 3000 YUSD, and the bottom trove has 4000 YUSD debt, it will cut it off at 2000 YUSD to keep 2000 in the bottom trove's debt. Most users will not encounter this, essentially if your truncatedYUSDAmount equals your YUSDAmount then ignore this.
To calculate the other hints, on the same HintHelpers contract, open the "getApproxHint" function. With the output of the first function, input the following:
_CR: Input "partialRedemptionHintAICR" from the last function call
_numTrials: Input 0
_inputRandomSeed: Input some random number, like 42
Below is an example of the input:
With the output "hintAddress", save this for later to input into "RedeemCollateral" as both "upperPartialRedemptionHint" and "lowerPartialRedemptionHint".
Putting it all together:
Now we have all the inputs needed for the "RedeemCollateral" function, and we are ready to submit to the chain! Head back to the TroveManager address mentioned in the overview, and input the parameters as described.
After submitting the transaction, your address will receive some collaterals which can be then sold for the equivalent of YUSDAmount on various exchanges.
Simulating Redemptions through Tenderly
Tenderly is a platform which you can use to simulate calls to functions on the Avalanche chain, and see the exact output if you were to run it at that block. It also allows forks, which can be used to simulate multiple subsequent transactions. Head over to https://dashboard.tenderly.co and create an account or log in. A free account should be sufficient for these simulations.
Here we will be demonstrating one example of running a particular string of redemptions to figure out the exact fee, as well as a calculator to figure out the value of the collaterals given back.
Set up Tenderly
To make your life easier, go to the "Contracts" page on the left, and add the contracts. For each contract, select the "Avalanche C-Chain". You can verify the contracts are correct on this page: https://docs.yeti.finance/other/contract-addresses. Add the following contracts:
The next step is to create a new Fork, which can be found on the menu on the left. Fork the "Avalanche C-Chain" and select "Use Latest Block". Now we are ready to simulate. Your page should look as below:
On your fork page, click New Simulation. First, YUSD must be approved for the TroveManager contract. Approve the TroveManagerRedemptions address (0x00000000000d9c2f60d8e82F2d1C2bed5008DD7d) for the amount which you are looking for, and submit the transaction. Change the "From" field to the address intended for the simulation. Go ahead and click "Simulate Transaction"
Next, select the button "New Simulation" and choose the TroveManager contract. In the functions list, search for "redeemCollateral". Since this is a current fork of the chain, use the instructions above to gather your parameters. In the future, since the state on the chain will change due to the simulated redemption, hintHelpers will have to be queried through Tenderly. Make sure the parent is "Previous Simulation". Click "Simulate Transaction"
If all has been formatted correctly, it will go through. To parse what happened, check the Events tab, which should look something like the below. It will show the actual YUSD amount redeemed (should be equal to the collateral value), the YUSD fee actually paid, and the collateral received along with the corresponding amounts. See the below section to calculate the value of the collaterals received back from the redemption.
To perform another redemption, continue with the same instructions as above for submitting a redemption, and perform new simulations, making sure to use the previous simulation as the parent. New hints will need to be calculated, and new values will need to be input.
Though more accurate than other forms of estimation, state on chain can change very quickly and unpredictably, so values from Tenderly are not guaranteed to be the same as
Calculating the Value of the Redeemed Collateral
The calculation for redeemed collateral requires multiplying all the collaterals and their prices. There is a spreadsheet here: https://docs.google.com/spreadsheets/d/1Ydo_yaNbrsTOnSpM33n0Ssz244dwnfNngjEFMrqWi_A/edit?usp=sharing
This spreadsheet calculates the estimated collateral value returned from redemptions. Create a copy of the sheet to edit it. The price is hard coded, and to update the values if there is a major discrepancy, check the "Price Feed" contract listed there and confirm it is the same as the one on this site: https://docs.yeti.finance/other/contract-addresses, and use the output from the function "fetchPrice_v" in the "Read Contract" section to update the sheet. The value from this example is used to currently populate the sheet, so with whatever values come from any other simulation, input them into the "Amount received from Redemption" column. It should be very close to equal to the amount redeemed, and small discrepancies come from the price feeds or decimal rounding.
If the transaction does not go through, there could be a host of reasons:
Make sure you did the approval step first if it says "ERC20: transfer amount exceeds allowance"
If it says: "TMR:User must accept fee", then the max fee parameter must be increased. To see what the fee is on some amount of input, make the YUSDMaxFee equal to the YUSDAmount input in the Tenderly simulation.