Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PotHelper with view function for simulating chi if dripped now. #12

Merged
merged 3 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Interfaces.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ import { PotAbstract } from "./dss/PotAbstract.sol";
import { SpotAbstract } from "./dss/SpotAbstract.sol";
import { VatAbstract } from "./dss/VatAbstract.sol";
import { VowAbstract } from "./dss/VowAbstract.sol";

import { PotHelper } from "./dss/PotHelper.sol";
18 changes: 9 additions & 9 deletions src/dss/PotAbstract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ import { VatAbstract } from "./VatAbstract.sol";
// https://github.com/makerdao/dss/blob/master/src/pot.sol
contract PotAbstract {
// mapping (address => uint256) public wards;
function wards(address) public returns (uint256);
function wards(address) public view returns (uint256);
function rely(address) external;
function deny(address) external;
// mapping (address => uint256) public pie; // user Savings Dai
function pie(address) public returns (uint256);
function pie(address) public view returns (uint256);
// uint256 public Pie; // total Savings Dai
function Pie() public returns (uint256);
function Pie() public view returns (uint256);
// uint256 public dsr; // the Dai Savings Rate
function dsr() public returns (uint256);
function dsr() public view returns (uint256);
// uint256 public chi; // the Rate Accumulator
function chi() public returns (uint256);
function chi() public view returns (uint256);
// VatAbstract public vat; // CDP engine
function vat() public returns (VatAbstract);
function vat() public view returns (VatAbstract);
// address public vow; // debt engine
function vow() public returns (address);
function vow() public view returns (address);
// uint256 public rho; // time of last drip
function rho() public returns (uint256);
function rho() public view returns (uint256);
// uint256 public live; // Access Flag
function live() public returns (uint256);
function live() public view returns (uint256);
function file(bytes32, uint256) external;
function file(bytes32, address) external;
function cage() external;
Expand Down
59 changes: 59 additions & 0 deletions src/dss/PotHelper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
pragma solidity ^0.5.12;

import { PotAbstract } from "./PotAbstract.sol";

// https://github.com/makerdao/dss/blob/master/src/pot.sol
contract PotHelper {

PotAbstract pa;

constructor(address pot) public {
pa = PotAbstract(pot);
}

// https://github.com/makerdao/dss/blob/master/src/pot.sol#L79
uint256 constant ONE = 10 ** 27;

function mul(uint x, uint y) internal pure returns (uint z) {
require(y == 0 || (z = x * y) / y == x);
}

function rmul(uint x, uint y) internal pure returns (uint z) {
z = mul(x, y) / ONE;
}

function rpow(uint x, uint n, uint base) internal pure returns (uint z) {
assembly {
switch x case 0 {switch n case 0 {z := base} default {z := 0}}
default {
switch mod(n, 2) case 0 { z := base } default { z := x }
let half := div(base, 2) // for rounding.
for { n := div(n, 2) } n { n := div(n,2) } {
let xx := mul(x, x)
if iszero(eq(div(xx, x), x)) { revert(0,0) }
let xxRound := add(xx, half)
if lt(xxRound, xx) { revert(0,0) }
x := div(xxRound, base)
if mod(n,2) {
let zx := mul(z, x)
if and(iszero(iszero(x)), iszero(eq(div(zx, x), z))) { revert(0,0) }
let zxRound := add(zx, half)
if lt(zxRound, zx) { revert(0,0) }
z := div(zxRound, base)
}
}
}
}
}

// View function for calculating value of chi iff drip() is called in the same block.
function drop() external view returns (uint256) {
if (now == pa.rho()) return pa.chi();
return rmul(rpow(pa.dsr(), now - pa.rho(), ONE), pa.chi());
}

// Pass the Pot Abstract for additional operations
function pot() external view returns (PotAbstract) {
return pa;
}
}