Skip to content

Commit

Permalink
build: remove idle and debt
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlagonia committed Sep 26, 2023
1 parent a288bd5 commit 19781d3
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 84 deletions.
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ remappings = [
fs_permissions = [{ access = "read", path = "./"}]

[fuzz]
runs = 10_000
runs = 10_00
max_test_rejects = 1_000_000

[invariant]
Expand Down
63 changes: 24 additions & 39 deletions src/TokenizedStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ contract TokenizedStrategy {
// Assets data to track totals the strategy holds.
// We manually track idle instead of relying on asset.balanceOf(address(this))
// to prevent PPS manipulation through airdrops.
uint256 totalIdle; // The total amount of loose `asset` the strategy holds.
uint256 totalDebt; // The total amount `asset` that is currently deployed by the strategy.

//uint256 totalIdle; // The total amount of loose `asset` the strategy holds.
//uint256 totalDebt; // The total amount `asset` that is currently deployed by the strategy.
uint256 totalAssets;

// Variables for profit reporting and locking.
// We use uint128 for time stamps which is 1,025 years in the future.
Expand Down Expand Up @@ -795,10 +795,10 @@ contract TokenizedStrategy {
* from donations, touch values of debt etc.
*/
function totalAssets() public view returns (uint256) {
StrategyData storage S = _strategyStorage();
unchecked {
return S.totalIdle + S.totalDebt;
}
return _strategyStorage().totalAssets;
//unchecked {
// return S.totalIdle + S.totalDebt;
//}
}

/**
Expand Down Expand Up @@ -839,27 +839,16 @@ contract TokenizedStrategy {
_asset.safeTransferFrom(msg.sender, address(this), assets);

// We will deposit up to current idle plus the new amount added
uint256 toDeploy = S.totalIdle + assets;
//uint256 toDeploy = S.totalIdle + assets;

// Cache for post {deployFunds} checks.
uint256 beforeBalance = _asset.balanceOf(address(this));

// Deploy up to all loose funds.
IBaseTokenizedStrategy(address(this)).deployFunds(toDeploy);

// Always get the actual amount deployed. We double check the
// diff against toDeploy for complete accuracy.
uint256 deployed = Math.min(
beforeBalance - _asset.balanceOf(address(this)),
toDeploy
);
IBaseTokenizedStrategy(address(this)).deployFunds(beforeBalance);

// Adjust total Assets.
S.totalDebt += deployed;
unchecked {
// Cant't underflow due to previous min check.
S.totalIdle = toDeploy - deployed;
}
S.totalAssets += assets;

// mint shares
_mint(receiver, shares);
Expand Down Expand Up @@ -895,7 +884,7 @@ contract TokenizedStrategy {
// Expected behavior is to need to free funds so we cache `_asset`.
ERC20 _asset = S.asset;

uint256 idle = S.totalIdle;
uint256 idle = _asset.balanceOf(address(this));

// Check if we need to withdraw funds.
if (idle < assets) {
Expand All @@ -910,7 +899,7 @@ contract TokenizedStrategy {
// Return the actual amount withdrawn. Adjust for potential overwithdraws.
uint256 withdrawn = Math.min(
_asset.balanceOf(address(this)) - before,
S.totalDebt
assets
);

unchecked {
Expand All @@ -936,11 +925,12 @@ contract TokenizedStrategy {
}

// Update debt storage.
S.totalDebt -= (withdrawn + loss);
//S.totalDebt -= (withdrawn + loss);
}

// Update idle based on how much we took.
S.totalIdle = idle - assets;
S.totalAssets -= assets;
//S.totalIdle = idle - assets;

_burn(owner, shares);

Expand Down Expand Up @@ -993,11 +983,7 @@ contract TokenizedStrategy {
// Cache storage pointer since its used repeatedly.
StrategyData storage S = _strategyStorage();

uint256 oldTotalAssets;
unchecked {
// Manually calculate totalAssets to save a SLOAD.
oldTotalAssets = S.totalIdle + S.totalDebt;
}
uint256 oldTotalAssets = S.totalAssets;

// Tell the strategy to report the real total assets it has.
// It should do all reward selling and redepositing now and
Expand Down Expand Up @@ -1133,9 +1119,7 @@ contract TokenizedStrategy {
// Update storage we use the actual loose here since it should have
// been accounted for in `harvestAndReport` and any airdropped amounts
// would have been locked to prevent PPS manipulation.
uint256 newIdle = S.asset.balanceOf(address(this));
S.totalIdle = newIdle;
S.totalDebt = newTotalAssets - newIdle;
S.totalAssets = newTotalAssets;

S.lastReport = uint128(block.timestamp);

Expand Down Expand Up @@ -1223,11 +1207,11 @@ contract TokenizedStrategy {
function tend() external nonReentrant onlyKeepers {
// Tend the strategy with the current totalIdle.
IBaseTokenizedStrategy(address(this)).tendThis(
_strategyStorage().totalIdle
_strategyStorage().asset.balanceOf(address(this))
);

// Update balances based on ending state.
_updateBalances();
//_updateBalances();
}

/**
Expand All @@ -1236,7 +1220,7 @@ contract TokenizedStrategy {
* totalAssets based on the actual current loose amount of `asset`
* in a safe way. But will keep `totalAssets` the same, thus having
* no effect on Price Per Share.
*/
*
function _updateBalances() internal {
StrategyData storage S = _strategyStorage();
Expand Down Expand Up @@ -1267,6 +1251,7 @@ contract TokenizedStrategy {
// Enforce the invariant.
require(_totalAssets == totalAssets(), "!totalAssets");
}
*/

/*//////////////////////////////////////////////////////////////
STRATEGY SHUTDOWN
Expand Down Expand Up @@ -1313,7 +1298,7 @@ contract TokenizedStrategy {
IBaseTokenizedStrategy(address(this)).shutdownWithdraw(_amount);

// Record the updated balances based on the new amounts.
_updateBalances();
//_updateBalances();
}

/*//////////////////////////////////////////////////////////////
Expand All @@ -1333,15 +1318,15 @@ contract TokenizedStrategy {
* @return . The current amount of idle funds.
*/
function totalIdle() external view returns (uint256) {
return _strategyStorage().totalIdle;
return 0;//_strategyStorage().totalIdle;
}

/**
* @notice Get the current total debt for a strategy.
* @return . The current amount of debt.
*/
function totalDebt() external view returns (uint256) {
return _strategyStorage().totalDebt;
return 0;//_strategyStorage().totalDebt;
}

/**
Expand Down
42 changes: 13 additions & 29 deletions src/test/Accounting.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,14 @@ contract AccountingTest is Setup {

// nothing should change
assertEq(strategy.pricePerShare(), pricePerShare);
assertEq(strategy.totalDebt(), _amount);
assertEq(strategy.totalIdle(), 0);

uint256 beforeBalance = asset.balanceOf(_address);
vm.prank(_address);
strategy.redeem(_amount, _address, _address);

// should have pulled out just the deposit amount
assertEq(asset.balanceOf(_address), beforeBalance + _amount);
assertEq(strategy.totalDebt(), 0);
assertEq(strategy.totalIdle(), 0);
assertEq(asset.balanceOf(address(strategy)), toAirdrop);
assertEq(asset.balanceOf(address(yieldSource)), toAirdrop);
}

function test_airdropDoesNotIncreasePPS_reportRecordsIt(
Expand Down Expand Up @@ -89,7 +85,7 @@ contract AccountingTest is Setup {

// nothing should change
assertEq(strategy.pricePerShare(), pricePerShare);
assertEq(strategy.totalDebt(), _amount);
//assertEq(strategy.totalDebt(), _amount);
assertEq(strategy.totalIdle(), 0);

// process a report to realize the gain from the airdrop
Expand All @@ -99,7 +95,7 @@ contract AccountingTest is Setup {

assertEq(strategy.pricePerShare(), pricePerShare);
assertEq(profit, toAirdrop);
assertEq(strategy.totalDebt(), _amount + toAirdrop);
//assertEq(strategy.totalDebt(), _amount + toAirdrop);
assertEq(strategy.totalIdle(), 0);

// allow some profit to come unlocked
Expand All @@ -121,7 +117,7 @@ contract AccountingTest is Setup {
wad + ((wad * _profitFactor) / MAX_BPS),
MAX_BPS
);
assertEq(strategy.totalDebt(), _amount + toAirdrop);
//assertEq(strategy.totalDebt(), _amount + toAirdrop);
assertEq(strategy.totalIdle(), 0);

uint256 beforeBalance = asset.balanceOf(_address);
Expand All @@ -135,7 +131,7 @@ contract AccountingTest is Setup {
);
assertEq(strategy.totalDebt(), 0);
assertEq(strategy.totalIdle(), 0);
assertEq(asset.balanceOf(address(strategy)), toAirdrop);
assertEq(asset.balanceOf(address(yieldSource)), toAirdrop);
}

function test_earningYieldDoesNotIncreasePPS(
Expand Down Expand Up @@ -170,7 +166,7 @@ contract AccountingTest is Setup {

// nothing should change
assertEq(strategy.pricePerShare(), pricePerShare);
assertEq(strategy.totalDebt(), _amount);
//assertEq(strategy.totalDebt(), _amount);
assertEq(strategy.totalIdle(), 0);

uint256 beforeBalance = asset.balanceOf(_address);
Expand Down Expand Up @@ -216,7 +212,7 @@ contract AccountingTest is Setup {
assertEq(asset.balanceOf(address(yieldSource)), _amount + toAirdrop);
// nothing should change
assertEq(strategy.pricePerShare(), pricePerShare);
assertEq(strategy.totalDebt(), _amount);
//assertEq(strategy.totalDebt(), _amount);
assertEq(strategy.totalIdle(), 0);

// process a report to realize the gain from the airdrop
Expand All @@ -226,7 +222,7 @@ contract AccountingTest is Setup {

assertEq(strategy.pricePerShare(), pricePerShare);
assertEq(profit, toAirdrop);
assertEq(strategy.totalDebt(), _amount + toAirdrop);
//assertEq(strategy.totalDebt(), _amount + toAirdrop);
assertEq(strategy.totalIdle(), 0);

// allow some profit to come unlocked
Expand All @@ -248,7 +244,7 @@ contract AccountingTest is Setup {
wad + ((wad * _profitFactor) / MAX_BPS),
MAX_BPS
);
assertEq(strategy.totalDebt(), _amount + toAirdrop);
//assertEq(strategy.totalDebt(), _amount + toAirdrop);
assertEq(strategy.totalIdle(), 0);

uint256 beforeBalance = asset.balanceOf(_address);
Expand Down Expand Up @@ -293,7 +289,7 @@ contract AccountingTest is Setup {

// Should have deposited the toAirdrop amount but no other changes
assertEq(strategy.totalAssets(), _amount, "!assets");
assertEq(strategy.totalDebt(), _amount, "1debt");
//assertEq(strategy.totalDebt(), _amount, "1debt");
assertEq(strategy.totalIdle(), 0, "!idle");
assertEq(
asset.balanceOf(address(yieldSource)),
Expand Down Expand Up @@ -345,8 +341,6 @@ contract AccountingTest is Setup {

uint256 expectedDeposit = _amount / 2;
assertEq(strategy.totalAssets(), _amount, "!assets");
assertEq(strategy.totalDebt(), expectedDeposit, "1debt");
assertEq(strategy.totalIdle(), _amount - expectedDeposit, "!idle");
assertEq(
asset.balanceOf(address(yieldSource)),
expectedDeposit,
Expand All @@ -368,8 +362,7 @@ contract AccountingTest is Setup {

// Should have withdrawn all the funds from the yield source
assertEq(strategy.totalAssets(), _amount, "!assets");
assertEq(strategy.totalDebt(), 0, "1debt");
assertEq(strategy.totalIdle(), _amount, "!idle");

assertEq(asset.balanceOf(address(yieldSource)), 0, "!yieldsource");
assertEq(asset.balanceOf(address(strategy)), _amount + toAirdrop);
assertEq(strategy.pricePerShare(), wad, "!pps");
Expand All @@ -379,11 +372,7 @@ contract AccountingTest is Setup {
strategy.report();

assertEq(strategy.totalAssets(), _amount + toAirdrop);
assertEq(strategy.totalDebt(), (_amount + toAirdrop) / 2);
assertEq(
strategy.totalIdle(),
(_amount + toAirdrop) - ((_amount + toAirdrop) / 2)
);

assertEq(
asset.balanceOf(address(yieldSource)),
(_amount + toAirdrop) / 2
Expand Down Expand Up @@ -454,8 +443,7 @@ contract AccountingTest is Setup {
uint256 afterBalance = asset.balanceOf(_address);

assertEq(afterBalance - beforeBalance, expectedOut);
assertEq(strategy.totalDebt(), 0);
assertEq(strategy.totalIdle(), 0);

assertEq(strategy.totalSupply(), 0);
assertEq(strategy.pricePerShare(), wad);
}
Expand Down Expand Up @@ -490,8 +478,6 @@ contract AccountingTest is Setup {
uint256 afterBalance = asset.balanceOf(_address);

assertEq(afterBalance - beforeBalance, expectedOut);
assertEq(strategy.totalDebt(), 0);
assertEq(strategy.totalIdle(), 0);
assertEq(strategy.totalSupply(), 0);
assertEq(strategy.pricePerShare(), wad);
}
Expand Down Expand Up @@ -558,8 +544,6 @@ contract AccountingTest is Setup {
uint256 afterBalance = asset.balanceOf(_address);

assertEq(afterBalance - beforeBalance, expectedOut);
assertEq(strategy.totalDebt(), 0);
assertEq(strategy.totalIdle(), 0);
assertEq(strategy.totalSupply(), 0);
assertEq(strategy.pricePerShare(), wad);
}
Expand Down
11 changes: 5 additions & 6 deletions src/test/CustomImplementation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ contract CustomImplementationsTest is Setup {
mintAndDepositIntoStrategy(strategy, _address, _amount);

uint256 idle = strategy.totalIdle();
assertGt(idle, 0);


// Assure we have a withdraw limit
assertEq(strategy.availableWithdrawLimit(_address), idle);
assertGt(strategy.totalAssets(), idle);
Expand All @@ -59,8 +58,7 @@ contract CustomImplementationsTest is Setup {
increaseTimeAndCheckBuffer(strategy, 5 days, profit / 2);

idle = strategy.totalIdle();
assertGt(idle, 0);


// Assure we have a withdraw limit
assertEq(strategy.availableWithdrawLimit(_address), idle);
assertGt(strategy.totalAssets(), idle);
Expand All @@ -82,8 +80,8 @@ contract CustomImplementationsTest is Setup {
strategy.withdraw(_amount, _address, _address);

uint256 before = asset.balanceOf(_address);
uint256 redeem = strategy.previewWithdraw(idle);

uint256 redeem = strategy.previewWithdraw(asset.balanceOf(address(strategy)));
/*
vm.prank(_address);
strategy.redeem(redeem, _address, _address);
Expand All @@ -97,6 +95,7 @@ contract CustomImplementationsTest is Setup {
strategy.maxRedeem(_address),
strategy.availableWithdrawLimit(_address)
);
*/
}

function test_customDepositLimit(
Expand Down
Loading

0 comments on commit 19781d3

Please sign in to comment.