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:
(1+r)n−p1c+(1+r)n−p2c+...+(1+r)nBi
Here Bi is the trUSD balance for index i as described in the trUSD section, r is the discount rate, n is the number of days until maturity, pj is the number of days until maturity for the jth cash flow date, and c is the cash flow payment determined as a fixed percentage of the trUSD balance, Bi. 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)n≈1+nr+2n(n−1)r2
Then standardizing to 18 decimals, the formula is converted to:
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 r with (0.04∗109)/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=δ∗j, where δ is a fixed constant. For, k cash flow payments, the total cost is:
Now this formula can be converted into a geometric series form to apply the simplification. Take, x=(1+r)δ, a=c/(1+r)n, and the equation above becomes: