From 7ddc3ff024c33d6ee31844d7f641a864cacbb479 Mon Sep 17 00:00:00 2001 From: Micah Zoltu Date: Fri, 8 Nov 2019 08:34:02 +0800 Subject: [PATCH] Adds a `view` method for getting current `chi`. This change makes it so a contract with a `view` function can know what `chi` would be in this block. This is useful for contracts that have view functions that need to return values denominated in DAI when looking at DSR balances. An example use case for this is a token contract (DAI-GoUp) that wraps DAI held in DSR. The `balanceOf` function of such a contract (which is `view`) needs to be able to calculate the current balance of an account denominated in DAI. In order to do this, it must calculate what `chi` _would_ be if `drip` was called this block, but without actually calling `drip` (because that is not a view function). --- src/pot.sol | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/pot.sol b/src/pot.sol index dfdff599..15dd9bd5 100644 --- a/src/pot.sol +++ b/src/pot.sol @@ -139,13 +139,18 @@ contract Pot is LibNote { // --- Savings Rate Accumulation --- function drip() external note returns (uint tmp) { require(now >= rho, "Pot/invalid-now"); - tmp = rmul(rpow(dsr, now - rho, ONE), chi); + tmp = drop(); uint chi_ = sub(tmp, chi); chi = tmp; rho = now; vat.suck(address(vow), address(this), mul(Pie, chi_)); } + function drop() external view returns (uint) { + if (now == rho) return chi; + return rmul(rpow(dsr, now - rho, ONE), chi); + } + // --- Savings Dai Management --- function join(uint wad) external note { require(now == rho, "Pot/rho-not-updated");