Defensive Computation
Suggest an editDue to the nature of Web3, programs cannot trust anyone and have to consider the worst-case scenario at any point of time. Because of that, the programs have to do what we call ‘Defensive Computations’: choose the lowest possible boundary to work it. It can be the lowest evaluation of assets’ price when accepting them as collateral, or the highest evaluation of existing debt.
That is, in order to avoid the risk of taking inaccurate data, the data has to be taken into account at the worst possible version.
Examples
Marginfi. The oracle gives a price and a confidence band. To value a user's collateral, the program takes the bottom of the band. To value the debt, the top. When the band widens, the position looks worse from both sides at once: the user can borrow less, and the liquidation line moves closer. The protocol never lends against the optimistic reading. Two limits keep the band reasonable. The widened confidence is capped at 5% of the price and if the widened confidence is above the limit set for the bank (10% of the price by default) the program does not use the price at all and returns an error.
Image above is the example of how lending and perp protocols use two different caps for “user action” and “protocol action”. They have a band for the protocol to make a decision so users could not move borrowed percent and manipulate.
Drift refuses to fill when the band is too wide relative to the price, and rejects prices that are too old. Let's assume the collateral's price jumps for a few seconds, because of a real move or a manipulated one and then the oracle shows the new price at once, but the 5-minute average does not. The deposit is still worth the lower number and the user cannot borrow against the jump until it has lasted long enough to move the average. On the other hand liquidations do not use such strict mode, they utilize the plain oracle price, for the same reason as the two lines above. We want the protocol to reflect any changes faster than the user could manipulate this change.
Kamino rejects a USDC price far from $1, no matter what the oracle says.
Helium prices Data Credits at the bottom of the band. Its a good example of this pattern to show that this is not only about lending. Helium sells the use of its network in Data Credits (1 Data Credit = $0.00001). To get this Data Credits user needs to burn his HNT token (which price can be different). The protocol reads the price from oracle, takes the exponential moving average and subtracts twice its confidence - by this formula they calculate “most conservative price”. Such ways allows Helium creators to be confident that users never get more Data Credits than HNT tokens were burned.