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

Update to Eip 4626 #34

Merged
merged 14 commits into from
Mar 25, 2022
66 changes: 32 additions & 34 deletions src/DssDirectDepositHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ interface DssDirectDepositPoolLike {
function getMaxBar() external view returns (uint256);
function validTarget() external view returns (bool);
function calcSupplies(uint256) external view returns (uint256, uint256);
function supply(uint256) external;
function deposit(uint256) external;
function withdraw(uint256) external;
function collect(address[] memory, uint256) external returns (uint256);
function gemBalanceOf() external view returns(uint256);
function getNormalizedBalanceOf() external view returns(uint256);
function getNormalizedAmount(uint256) external view returns(uint256);
function transferShares(address, uint256) external returns (bool);
function assetBalance() external returns (uint256);
function shareBalance() external returns (uint256);
function convertToShares(uint256) external view returns(uint256);
function maxWithdraw() external view returns(uint256);
function cage() external;
}

Expand All @@ -87,7 +89,6 @@ contract DssDirectDepositHub {

struct Ilk {
DssDirectDepositPoolLike pool;
TokenLike gem;
uint256 tau; // Time until you can write off the debt [sec]
uint256 culled;
uint256 tic; // Timestamp when the pool is caged
Expand Down Expand Up @@ -153,9 +154,7 @@ contract DssDirectDepositHub {
// --- Administration ---
function file(bytes32 ilk_, bytes32 what, uint256 data) external auth {
Ilk storage ilk = ilks[ilk_];
if (what == "bar") {

} else if (what == "tau" ) {
if (what == "tau" ) {
require(live == 1, "DssDirectDepositHub/hub-not-live");
require(ilk.tic == 0, "DssDirectDepositHub/join-not-live");

Expand All @@ -170,7 +169,6 @@ contract DssDirectDepositHub {
require(ilks[ilk].tic == 0, "DssDirectDepositHub/pool-not-live");

if (what == "pool") ilks[ilk].pool = DssDirectDepositPoolLike(data);
else if (what == "gem") ilks[ilk].gem = TokenLike(data);
else revert("DssDirectDepositHub/file-unrecognized-param");
emit File(ilk, what, data);
}
Expand All @@ -195,16 +193,16 @@ contract DssDirectDepositHub {

require(int256(amount) >= 0, "DssDirectDepositHub/overflow");

uint256 scaledPrev = pool.getNormalizedBalanceOf();
uint256 sharesPrev = pool.shareBalance();

vat.slip(ilk, address(pool), int256(amount));
vat.frob(ilk, address(pool), address(pool), address(pool), int256(amount), int256(amount));
// normalized debt == erc20 DAI (Vat rate for this ilk fixed to 1 RAY)
pool.supply(amount);
pool.deposit(amount);

// Verify the correct amount of gem shows up
uint256 scaledAmount = pool.getNormalizedAmount(amount);
require(pool.getNormalizedBalanceOf() >= _add(scaledPrev, scaledAmount), "DssDirectDepositHub/no-receive-gem-tokens");
uint256 newShares = pool.convertToShares(amount);
require(pool.shareBalance() >= _add(sharesPrev, newShares), "DssDirectDepositHub/no-receive-shares");

emit Wind(ilk, amount);
}
Expand All @@ -215,7 +213,7 @@ contract DssDirectDepositHub {
// This module will have an unintended behaviour if rate is changed to some other value.

address end;
uint256 gemBalance = pool.gemBalanceOf();
uint256 assetBalance = pool.assetBalance();
uint256 daiDebt;
if (mode == Mode.NORMAL) {
// Normal mode or module just caged (no culled)
Expand Down Expand Up @@ -244,15 +242,15 @@ contract DssDirectDepositHub {
supplyReduction,
availableLiquidity
),
gemBalance
assetBalance
),
daiDebt
);

// Determine the amount of fees to bring back
uint256 fees = 0;
if (gemBalance > daiDebt) {
fees = gemBalance - daiDebt;
if (assetBalance > daiDebt) {
fees = assetBalance - daiDebt;

if (_add(amount, fees) > availableLiquidity) {
// Don't need safe-math because this is constrained above
Expand Down Expand Up @@ -295,7 +293,7 @@ contract DssDirectDepositHub {
function exec(bytes32 ilk_) external {
Ilk memory ilk = ilks[ilk_];

uint256 availableLiquidity = dai.balanceOf(address(ilk.gem));
uint256 availableAssets = ilk.pool.maxWithdraw();

if (vat.live() == 0) {
// MCD caged
Expand All @@ -305,7 +303,7 @@ contract DssDirectDepositHub {
ilk_,
ilk.pool,
type(uint256).max,
availableLiquidity,
availableAssets,
Mode.MCD_CAGED
);
} else if (live == 0) {
Expand All @@ -314,23 +312,23 @@ contract DssDirectDepositHub {
ilk_,
ilk.pool,
type(uint256).max,
availableLiquidity,
availableAssets,
ilk.culled == 1
? Mode.MODULE_CULLED
: Mode.NORMAL
);
} else {
// Normal path
(uint256 supplyAmount, uint256 targetSupply) = ilk.pool.calcSupplies(availableLiquidity);
(uint256 totalAssets, uint256 targetAssets) = ilk.pool.calcSupplies(availableAssets);

if (targetSupply > supplyAmount) {
_wind(ilk_, ilk.pool, targetSupply - supplyAmount);
} else if (targetSupply < supplyAmount) {
if (targetAssets > totalAssets) {
_wind(ilk_, ilk.pool, targetAssets - totalAssets);
} else if (targetAssets < totalAssets) {
_unwind(
ilk_,
ilk.pool,
supplyAmount - targetSupply,
availableLiquidity,
totalAssets - targetAssets,
availableAssets,
Mode.NORMAL
);
}
Expand All @@ -344,13 +342,13 @@ contract DssDirectDepositHub {
require(vat.live() == 1, "DssDirectDepositHub/no-reap-during-shutdown");
require(live == 1, "DssDirectDepositHub/no-reap-during-cage");

uint256 gemBalance = ilk.pool.gemBalanceOf();
uint256 assetBalance = ilk.pool.assetBalance();
(, uint256 daiDebt) = vat.urns(ilk_, address(ilk.pool));
if (gemBalance > daiDebt) {
uint256 fees = gemBalance - daiDebt;
uint256 availableLiquidity = dai.balanceOf(address(ilk.gem));
if (fees > availableLiquidity) {
fees = availableLiquidity;
if (assetBalance > daiDebt) {
uint256 fees = assetBalance - daiDebt;
uint256 availableAssets = ilk.pool.maxWithdraw();
if (fees > availableAssets) {
fees = availableAssets;
}
ilk.pool.withdraw(fees);
vat.move(address(ilk.pool), address(chainlog.getAddress("MCD_VOW")), _mul(RAY, fees));
Expand All @@ -371,7 +369,7 @@ contract DssDirectDepositHub {
require(wad <= 2 ** 255, "DssDirectDepositHub/overflow");
vat.slip(ilk_, msg.sender, -int256(wad));
Ilk memory ilk = ilks[ilk_];
require(ilk.gem.transferFrom(address(ilk.pool), usr, wad), "DssDirectDepositHub/failed-transfer");
require(ilk.pool.transferShares(usr, ilk.pool.convertToShares(wad)), "DssDirectDepositHub/failed-transfer");
}

// --- Shutdown ---
Expand Down Expand Up @@ -446,7 +444,7 @@ contract DssDirectDepositHub {
Ilk memory ilk = ilks[ilk_];

// Send all gem in the contract to who
require(ilk.gem.transferFrom(address(ilk.pool), who, ilk.pool.gemBalanceOf()), "DssDirectDepositHub/failed-transfer");
require(ilk.pool.transferShares(who, ilk.pool.shareBalance()), "DssDirectDepositHub/failed-transfer");

if (ilk.culled == 1) {
// Culled - just zero out the gems
Expand Down
63 changes: 30 additions & 33 deletions src/DssDirectDepositHub.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ contract DssDirectDepositHubTest is DSTest {

testGem = new DssDirectDepositTestGem(18);
directDepositHub = new DssDirectDepositHub(address(chainlog));
directDepositHub.file(ilk, "gem", address(testGem));

rewardsClaimer = new DssDirectDepositTestRewards(address(testGem));
directDepositTestPool = new DssDirectDepositTestPool(
Expand All @@ -113,7 +112,7 @@ contract DssDirectDepositHubTest is DSTest {
// Test Target Setup
testGem.rely(address(directDepositTestPool));
directDepositTestPlan.file("maxBar_", type(uint256).max);
directDepositTestPool.file("gem", address(testGem));
directDepositTestPool.file("share", address(testGem));
directDepositTestPool.file("isValidTarget", true);
testGem.giveAllowance(
address(dai),
Expand Down Expand Up @@ -212,8 +211,8 @@ contract DssDirectDepositHubTest is DSTest {

function _windSystem() internal {
directDepositTestPool.file("bar", 10);
directDepositTestPlan.file("supplyAmount", 50 * WAD);
directDepositTestPlan.file("targetSupply", 100 * WAD);
directDepositTestPlan.file("totalAssets", 50 * WAD);
directDepositTestPlan.file("targetAssets", 100 * WAD);
directDepositHub.exec(ilk);

(uint256 ink, uint256 art) = vat.urns(
Expand All @@ -240,10 +239,10 @@ contract DssDirectDepositHubTest is DSTest {
// }

function test_can_file_tau() public {
(, , uint256 tau, , ) = directDepositHub.ilks(ilk);
(, uint256 tau, , ) = directDepositHub.ilks(ilk);
assertEq(tau, 7 days);
directDepositHub.file(ilk, "tau", 1 days);
(, , tau, , ) = directDepositHub.ilks(ilk);
(, tau, , ) = directDepositHub.ilks(ilk);
assertEq(tau, 1 days);
}

Expand All @@ -255,7 +254,7 @@ contract DssDirectDepositHubTest is DSTest {

function testFail_hub_not_live_tau_file() public {
directDepositHub.file(ilk, "tau", 1 days);
(, , uint256 tau, , ) = directDepositHub.ilks(ilk);
(, uint256 tau, , ) = directDepositHub.ilks(ilk);
assertEq(tau, 1 days);

// Cage Join
Expand All @@ -266,7 +265,7 @@ contract DssDirectDepositHubTest is DSTest {

function testFail_pool_not_live_tau_file() public {
directDepositHub.file(ilk, "tau", 1 days);
(, , uint256 tau, , ) = directDepositHub.ilks(ilk);
(, uint256 tau, , ) = directDepositHub.ilks(ilk);
assertEq(tau, 1 days);

// Cage Join
Expand Down Expand Up @@ -306,15 +305,13 @@ contract DssDirectDepositHubTest is DSTest {
// }

function test_can_file_pool() public {
(DssDirectDepositPoolLike pool, , , , ) = directDepositHub.ilks(
ilk
);
(DssDirectDepositPoolLike pool, , , ) = directDepositHub.ilks(ilk);

assertEq(address(pool), address(directDepositTestPool));

directDepositHub.file(ilk, "pool", address(this));

(pool, , , , ) = directDepositHub.ilks(ilk);
(pool, , , ) = directDepositHub.ilks(ilk);
assertEq(address(pool), address(this));
}

Expand Down Expand Up @@ -348,20 +345,20 @@ contract DssDirectDepositHubTest is DSTest {
directDepositHub.file(ilk, "pool", address(123));
}

function testFail_hub_not_live_gem_file() public {
// Cage Pool
directDepositHub.cage(ilk);
// function testFail_hub_not_live_gem_file() public {
// // Cage Pool
// directDepositHub.cage(ilk);

directDepositHub.file(ilk, "gem", address(123));
}
// directDepositHub.file(ilk, "gem", address(123));
// }

function testFail_unknown_address_file() public {
directDepositHub.file(ilk, "unknown", address(123));
}

function testFail_vat_not_live_address_file() public {
directDepositHub.file(ilk, "pool", address(this));
(DssDirectDepositPoolLike pool, , , ,) = directDepositHub.ilks(ilk);
(DssDirectDepositPoolLike pool, , ,) = directDepositHub.ilks(ilk);

assertEq(address(pool), address(this));

Expand All @@ -374,8 +371,8 @@ contract DssDirectDepositHubTest is DSTest {

function test_wind_amount_less_target() public {
directDepositTestPool.file("bar", 10);
directDepositTestPlan.file("supplyAmount", 50 * WAD);
directDepositTestPlan.file("targetSupply", 100 * WAD);
directDepositTestPlan.file("totalAssets", 50 * WAD);
directDepositTestPlan.file("targetAssets", 100 * WAD);

(uint256 pink, uint256 part) = vat.urns(
ilk,
Expand Down Expand Up @@ -458,8 +455,8 @@ contract DssDirectDepositHubTest is DSTest {
assertEq(pink, 50 * WAD);
assertEq(part, 50 * WAD);

directDepositTestPlan.file("targetSupply", 75 * WAD);
directDepositTestPlan.file("supplyAmount", 100 * WAD);
directDepositTestPlan.file("targetAssets", 75 * WAD);
directDepositTestPlan.file("totalAssets", 100 * WAD);

directDepositHub.exec(ilk);

Expand Down Expand Up @@ -611,14 +608,14 @@ contract DssDirectDepositHubTest is DSTest {
directDepositHub.cage();
}

function test_cage_ppol() public {
(, , , , uint256 tic) = directDepositHub.ilks(ilk);
function test_cage_pool() public {
(, , , uint256 tic) = directDepositHub.ilks(ilk);
assertEq(tic, 0);
assertEq(directDepositTestPool.live(), 1);

directDepositHub.cage(ilk);

(, , , , tic) = directDepositHub.ilks(ilk);
(, , , tic) = directDepositHub.ilks(ilk);
assertEq(tic, block.timestamp);
assertEq(directDepositTestPool.live(), 0);
}
Expand All @@ -629,7 +626,7 @@ contract DssDirectDepositHubTest is DSTest {
}

function test_cage_pool_invalid_target() public {
(, , , , uint256 tic) = directDepositHub.ilks(ilk);
(, , , uint256 tic) = directDepositHub.ilks(ilk);
assertEq(tic, 0);
assertEq(directDepositTestPool.live(), 1);

Expand All @@ -640,13 +637,13 @@ contract DssDirectDepositHubTest is DSTest {

directDepositHub.cage(ilk);

(, , , , tic) = directDepositHub.ilks(ilk);
(, , , tic) = directDepositHub.ilks(ilk);
assertEq(tic, block.timestamp);
assertEq(directDepositTestPool.live(), 0);
}

function test_cage_pool_hub_caged() public {
(, , , , uint256 tic) = directDepositHub.ilks(ilk);
(, , , uint256 tic) = directDepositHub.ilks(ilk);
assertEq(tic, 0);
assertEq(directDepositTestPool.live(), 1);

Expand All @@ -656,7 +653,7 @@ contract DssDirectDepositHubTest is DSTest {

directDepositHub.cage(ilk);

(, , , , tic) = directDepositHub.ilks(ilk);
(, , , tic) = directDepositHub.ilks(ilk);
assertEq(tic, block.timestamp);
assertEq(directDepositTestPool.live(), 0);
}
Expand Down Expand Up @@ -688,7 +685,7 @@ contract DssDirectDepositHubTest is DSTest {
uint256 gemAfter = vat.gem(ilk, address(directDepositTestPool));
assertEq(gemAfter, 50 * WAD);
assertEq(sinBefore + 50 * RAD, vat.sin(vow));
(, , , uint256 culled, ) = directDepositHub.ilks(ilk);
(, , uint256 culled, ) = directDepositHub.ilks(ilk);
assertEq(culled, 1);
}

Expand Down Expand Up @@ -722,7 +719,7 @@ contract DssDirectDepositHubTest is DSTest {
uint256 gemAfter = vat.gem(ilk, address(directDepositTestPool));
assertEq(gemAfter, 50 * WAD);
assertEq(sinBefore + 50 * RAD, vat.sin(vow));
(, , , uint256 culled, ) = directDepositHub.ilks(ilk);
(, , uint256 culled, ) = directDepositHub.ilks(ilk);
assertEq(culled, 1);
}

Expand Down Expand Up @@ -783,7 +780,7 @@ contract DssDirectDepositHubTest is DSTest {
uint256 gemBefore = vat.gem(ilk, address(directDepositTestPool));
assertEq(gemBefore, 50 * WAD);
uint256 sinBefore = vat.sin(vow);
(, , , uint256 culled, ) = directDepositHub.ilks(ilk);
(, , uint256 culled, ) = directDepositHub.ilks(ilk);
assertEq(culled, 1);

vat.cage();
Expand All @@ -799,7 +796,7 @@ contract DssDirectDepositHubTest is DSTest {
assertEq(gemAfter, 0);
// Sin should not change since we suck before grabbing
assertEq(sinBefore, vat.sin(vow));
(, , , culled, ) = directDepositHub.ilks(ilk);
(, , culled, ) = directDepositHub.ilks(ilk);
assertEq(culled, 0);
}

Expand Down
Loading