Treasury Logics

Here the logics used in Treasury contract are explained.

Cumulative rate calculation

Treasury.sol
function _calculateCumulativeRate(uint256 balanceBeforeEvent, Protocol _protocol)
Param Name
Type
Description

balanceBeforeEvent

uint256

Balance of external protocol tokens before deposit or withdraw.

_protocol

enum Protocol

Which external protocol cumulative rate needs to be calculated.

enum Protocol{Aave,Compound}

For the first time deposit, the cumulative rate will be 1.

change = (balanceBeforeEvent - protocolDeposit[_protocol].totalCreditedTokens) / protocolDeposit[_protocol].totalCreditedTokens;
currentCumulativeRate = ((CUMULATIVE_PRECISION + change) * protocolDeposit[_protocol].cumulativeRate);

For further events, calculate the percentage change by dividing the tokens credited between last event and balance before event by total credited tokens till last event.

Then current cumulative rate will be previous cumulative rate multiplied with percentage change plus one.

Reason for adding 1 to percentage change is, converting that into rate.

Functions which are using the above logic

Last updated