trUSD Calculations

Term Discount

Each term token (trUSD) comes with a cash flow rate, as well as a yield (discount rate), the protocol defines for newly issued trUSD. For a rUSD holder the cost for purchasing a term is:

c(1+r)np1+c(1+r)np2+...+Bi(1+r)n\frac{c}{(1 + r)^{n - p_{1}}} + \frac{c}{(1 + r)^{n - p_{2}}} + . . . + \frac{B_{i}}{(1 + r)^{n}}

Here BiB_{i} is the trUSD balance for index ii as described in the trUSD section, rr is the discount rate, nn is the number of days until maturity, pjp_{j} is the number of days until maturity for the jjth cash flow date, and cc is the cash flow payment determined as a fixed percentage of the trUSD balance, BiB_{i}. We discount the balance daily, which makes for a large exponent. To calculate this in Solidity it helps to convert the discount formula into a Taylor series:

(1+r)n1+nr+n(n1)2r2(1 + r)^{n} \approx 1 + n r + \frac{n (n - 1)}{2} r^{2}

Then standardizing to 18 decimals, the formula is converted to:

11018+nr1018+n(n1)2r21018=11018+n(r1018)+n(n1)2(r365109)2\begin{align*}1 * 10^{18} & + n r * 10^{18} + \frac{n (n - 1)}{2} r^{2} * 10^{18} \\ & = 1 * 10^{18} + n * \left( r * 10^{18} \right) + \frac{n (n - 1)}{2} * \left(\frac{r}{365} * 10^{9} \right)^{2} \end{align*}

This enables an easy way to set a discount rate in a normal form as an APR such as 0.04 and have the Solidity math work out, replacing rr with (0.04109)/365(0.04 * 10^9) / 365.

Term Cash Flows

The calculation for the total cash flow discount value can be converted into a closed formula. This avoids a loop, so the smart contract can support an arbitrary number of cash payments for any date of a maturing term, as well as minimizes gas costs overall. Since the time gap between coupon payments is fixed, each pj=δjp_{j} = \delta * j, where δ\delta is a fixed constant. For, kk cash flow payments, the total cost is:

c(1+r)np1+c(1+r)np2+...+c(1+r)npk=c(1+r)nδ1+c(1+r)nδ2+...+c(1+r)nδk=((1+r)δ1+(1+r)δ2+...(1+r)δk)c(1+r)n\begin{align*} \frac{c}{(1 + r)^{n - p_{1}}} + \frac{c}{(1 + r)^{n - p_{2}}} + & . . . + \frac{c}{(1 + r)^{n - p_{k}}} \\ & = \frac{c}{(1 + r)^{n - \delta * 1}} + \frac{c}{(1 + r)^{n - \delta * 2}} + . . . + \frac{c}{(1 + r)^{n - \delta * k}} \\ & = \left((1 + r)^{\delta * 1} + (1 + r)^{\delta * 2} + . . . (1 + r)^{\delta * k} \right) \frac{c}{(1 + r)^{n}} \end{align*}

Now this formula can be converted into a geometric series form to apply the simplification. Take, x=(1+r)δx = (1 + r)^{\delta}, a=c/(1+r)na = c / (1 + r)^{n}, and the equation above becomes:

ai=1k+1xi1=aa(1xk+1)(1x)=axk+1x1xa - \sum_{i = 1}^{k + 1} x^{i - 1} = a - \frac{a (1 - x^{k + 1})}{(1 - x)} = a \frac{x^{k + 1} - x}{1 - x}

Last updated