Skip to content

Commit 4c62f10

Browse files
Merge pull request #12 from makerdao/pothelper
Add PotHelper with view function for simulating chi if dripped now.
2 parents 0bb8384 + 3c7c5a2 commit 4c62f10

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed

src/Interfaces.sol

+2
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ import { PotAbstract } from "./dss/PotAbstract.sol";
2828
import { SpotAbstract } from "./dss/SpotAbstract.sol";
2929
import { VatAbstract } from "./dss/VatAbstract.sol";
3030
import { VowAbstract } from "./dss/VowAbstract.sol";
31+
32+
import { PotHelper } from "./dss/PotHelper.sol";

src/dss/PotAbstract.sol

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ import { VatAbstract } from "./VatAbstract.sol";
55
// https://github.com/makerdao/dss/blob/master/src/pot.sol
66
contract PotAbstract {
77
// mapping (address => uint256) public wards;
8-
function wards(address) public returns (uint256);
8+
function wards(address) public view returns (uint256);
99
function rely(address) external;
1010
function deny(address) external;
1111
// mapping (address => uint256) public pie; // user Savings Dai
12-
function pie(address) public returns (uint256);
12+
function pie(address) public view returns (uint256);
1313
// uint256 public Pie; // total Savings Dai
14-
function Pie() public returns (uint256);
14+
function Pie() public view returns (uint256);
1515
// uint256 public dsr; // the Dai Savings Rate
16-
function dsr() public returns (uint256);
16+
function dsr() public view returns (uint256);
1717
// uint256 public chi; // the Rate Accumulator
18-
function chi() public returns (uint256);
18+
function chi() public view returns (uint256);
1919
// VatAbstract public vat; // CDP engine
20-
function vat() public returns (VatAbstract);
20+
function vat() public view returns (VatAbstract);
2121
// address public vow; // debt engine
22-
function vow() public returns (address);
22+
function vow() public view returns (address);
2323
// uint256 public rho; // time of last drip
24-
function rho() public returns (uint256);
24+
function rho() public view returns (uint256);
2525
// uint256 public live; // Access Flag
26-
function live() public returns (uint256);
26+
function live() public view returns (uint256);
2727
function file(bytes32, uint256) external;
2828
function file(bytes32, address) external;
2929
function cage() external;

src/dss/PotHelper.sol

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
pragma solidity ^0.5.12;
2+
3+
import { PotAbstract } from "./PotAbstract.sol";
4+
5+
// https://github.com/makerdao/dss/blob/master/src/pot.sol
6+
contract PotHelper {
7+
8+
PotAbstract pa;
9+
10+
constructor(address pot) public {
11+
pa = PotAbstract(pot);
12+
}
13+
14+
// https://github.com/makerdao/dss/blob/master/src/pot.sol#L79
15+
uint256 constant ONE = 10 ** 27;
16+
17+
function mul(uint x, uint y) internal pure returns (uint z) {
18+
require(y == 0 || (z = x * y) / y == x);
19+
}
20+
21+
function rmul(uint x, uint y) internal pure returns (uint z) {
22+
z = mul(x, y) / ONE;
23+
}
24+
25+
function rpow(uint x, uint n, uint base) internal pure returns (uint z) {
26+
assembly {
27+
switch x case 0 {switch n case 0 {z := base} default {z := 0}}
28+
default {
29+
switch mod(n, 2) case 0 { z := base } default { z := x }
30+
let half := div(base, 2) // for rounding.
31+
for { n := div(n, 2) } n { n := div(n,2) } {
32+
let xx := mul(x, x)
33+
if iszero(eq(div(xx, x), x)) { revert(0,0) }
34+
let xxRound := add(xx, half)
35+
if lt(xxRound, xx) { revert(0,0) }
36+
x := div(xxRound, base)
37+
if mod(n,2) {
38+
let zx := mul(z, x)
39+
if and(iszero(iszero(x)), iszero(eq(div(zx, x), z))) { revert(0,0) }
40+
let zxRound := add(zx, half)
41+
if lt(zxRound, zx) { revert(0,0) }
42+
z := div(zxRound, base)
43+
}
44+
}
45+
}
46+
}
47+
}
48+
49+
// View function for calculating value of chi iff drip() is called in the same block.
50+
function drop() external view returns (uint256) {
51+
if (now == pa.rho()) return pa.chi();
52+
return rmul(rpow(pa.dsr(), now - pa.rho(), ONE), pa.chi());
53+
}
54+
55+
// Pass the Pot Abstract for additional operations
56+
function pot() external view returns (PotAbstract) {
57+
return pa;
58+
}
59+
}

0 commit comments

Comments
 (0)