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

additional smart-wallet YieldDistributionTokenScenarioTest tests #82

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
11 changes: 11 additions & 0 deletions smart-wallets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ $ forge compile
```bash
$ forge test
$ forge coverage --ir-minimum

# for generating an detailed easy-to-read report on coverage
$ forge coverage --ir-minimum --report lcov
$ genhtml -o report --branch-coverage lcov.info  ✔  08:29:50 
swimricky marked this conversation as resolved.
Show resolved Hide resolved
# if genhtml gives you an error like this:
# Reading tracefile lcov.info.
# genhtml: ERROR: (corrupt) unable to read trace file 'lcov.info': genhtml: ERROR: (inconsistent) "src/token/YieldDistributionToken.sol":62: function YieldDistributionToken._getYieldDistributionTokenStorage found on line but no corresponding 'line' coverage data point. Cannot derive function end line. See lcovrc man entry for 'derive_function_end_line'.
# (use "genhtml --ignore-errors inconsistent ..." to bypass this error)
# then use this to generate the report
$ genhtml -o report --branch-coverage --ignore-errors inconsistent --ignore-errors corrupt lcov.info
# open up report/index.html in your editor and open the preview to navigate through the coverage report
```

### Deploy
Expand Down
12 changes: 12 additions & 0 deletions smart-wallets/src/token/YieldDistributionToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";

import { IYieldDistributionToken } from "../interfaces/IYieldDistributionToken.sol";
import { Deposit, UserState } from "./Types.sol";
import { console2 } from "forge-std/console2.sol";

/**
* @title YieldDistributionToken
Expand Down Expand Up @@ -327,6 +328,7 @@ abstract contract YieldDistributionToken is ERC20, Ownable, IYieldDistributionTo
uint256 lastDepositIndex = userState.lastDepositIndex;
uint256 amountSecondsAccrued;

console2.log("[Before]lastDepositIndex: %d, currentDepositIndex: %d", lastDepositIndex, currentDepositIndex);
if (lastDepositIndex != currentDepositIndex) {
Deposit memory deposit;

Expand All @@ -337,17 +339,24 @@ abstract contract YieldDistributionToken is ERC20, Ownable, IYieldDistributionTo
// all variables in `userState` are updated until `lastDepositIndex`
while (lastDepositIndex != currentDepositIndex) {
++lastDepositIndex;
console2.log("lastDepositIndex: %d", lastDepositIndex);

deposit = $.deposits[lastDepositIndex];
console2.log("\tbalanceOf(user): %d", balanceOf(user));
console2.log("\tdeposit.timestamp: %d", deposit.timestamp);
console2.log("\tuserState.lastUpdate: %d", userState.lastUpdate);

amountSecondsAccrued = balanceOf(user) * (deposit.timestamp - userState.lastUpdate);
console2.log("\tamountSecondsAccrued %d at %d", amountSecondsAccrued, lastDepositIndex);

userState.amountSeconds += amountSecondsAccrued;

if (userState.amountSeconds > userState.amountSecondsDeduction) {
uint256 yieldAccruedBefore = userState.yieldAccrued;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some way to enable / disable the console logs depending on whether we're debugging or pushing production code ? Remove yieldAccruedBefore and console2.log("\tyieldAccrued: %d", deposit.scaledCurrencyTokenPerAmountSecond.mulDiv(userState.amountSeconds - userState.amountSecondsDeduction, SCALE))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i tried to look into this but didn't seem like there was anything. the console logs were just meant to be temporary while I was doing the tests but I'll dig into it a bit more

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like there isn't a way to do this with forge, only hardhat. I'll manually remove them before the final review

userState.yieldAccrued += deposit.scaledCurrencyTokenPerAmountSecond.mulDiv(
userState.amountSeconds - userState.amountSecondsDeduction, SCALE
);
console2.log("\tyieldAccrued: %d", (userState.yieldAccrued - yieldAccruedBefore));

// the `amountSecondsDeduction` is updated to the value of `amountSeconds`
// of the last yield accrual - therefore for the current yield accrual, it is updated
Expand All @@ -363,11 +372,14 @@ abstract contract YieldDistributionToken is ERC20, Ownable, IYieldDistributionTo
// of the deposit timestamp is equal to the users last update, meaning yield has already been accrued
// the check ensures that the process terminates early if there are no more deposits from which to
// accrue yield
console2.log("\tamountSecondsAccrued: %d", amountSecondsAccrued);
if (amountSecondsAccrued == 0) {
console2.log("\tamountSecondsAccrued == 0 at %d. EARLYBREAK\n\n\n", lastDepositIndex);
userState.lastDepositIndex = currentDepositIndex;
break;
}

console2.log("\tgasLeft %d at %d", gasleft(), lastDepositIndex);
if (gasleft() < 100_000) {
break;
}
Expand Down
25 changes: 25 additions & 0 deletions smart-wallets/test/harness/YieldDistributionTokenHarness.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import { UserState } from "../../src/token/Types.sol";
import { YieldDistributionToken } from "../../src/token/YieldDistributionToken.sol";
import { IERC20 } from "@openzeppelin/contracts/interfaces/IERC20.sol";
import { console2 } from "forge-std/console2.sol";

contract YieldDistributionTokenHarness is YieldDistributionToken {

Expand Down Expand Up @@ -32,11 +34,34 @@ contract YieldDistributionTokenHarness is YieldDistributionToken {
_depositYield(currencyTokenAmount);
}

function exposed_getTotalAmountSeconds() external view returns (uint256) {
return _getYieldDistributionTokenStorage().totalAmountSeconds;
}

// silence warnings
function requestYield(
address
) external override {
++requestCounter;
}

function logUserState(address user, string memory prelog) external view {
UserState memory userState = this.getUserState(user);
console2.log("\n%s", prelog);
console2.log("{");
console2.log("\tamountSeconds:", userState.amountSeconds);
console2.log("\tamountSecondsDeduction:", userState.amountSecondsDeduction);
console2.log("\tlastUpdate:", userState.lastUpdate);
console2.log("\tlastDepositIndex:", userState.lastDepositIndex);
console2.log("\tyieldAccrued:", userState.yieldAccrued);
console2.log("\tyieldWithdrawn:", userState.yieldWithdrawn);
console2.log("}");
}

function logTokenStorage() external view {
console2.log("\nToken Storage");
console2.log("\ttotalAmountSeconds:", _getYieldDistributionTokenStorage().totalAmountSeconds);
console2.log("\tlastSupplyUpdate:", _getYieldDistributionTokenStorage().lastSupplyUpdate);
}

}
Loading