Skip to content

Commit

Permalink
fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
eyqs committed Sep 17, 2024
1 parent 053d649 commit c8b20cf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
9 changes: 6 additions & 3 deletions smart-wallets/src/interfaces/IYieldDistributionToken.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

interface IYieldDistributionToken is IERC20 {

function claimYield() external returns (address currency, uint256 amount);
function processYield(address user) external;
function _depositYield(uint256 timestamp, uint256 amount) internal;
function setTokenURI(string memory tokenURI) external;
function getCurrencyToken() external returns (ERC20 currencyToken);
function getTokenURI() external returns (string memory tokenURI);
function claimYield(address user) external returns (ERC20 currencyToken, uint256 currencyTokenAmount);
function accrueYield(address user) external;

}
46 changes: 28 additions & 18 deletions smart-wallets/src/token/YieldDistributionToken.sol
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import { IYieldDistributionToken } from "../interfaces/IYieldDistributionToken.sol";

/**
* @title YieldDistributionToken
* @author Eugene Y. Q. Shen
* @notice ERC20 token that represents TODO
* @notice ERC20 token that receives yield deposits and distributes yield
* to token holders proportionally based on how long they have held the token
*/
abstract contract YieldDistributionToken is ERC20, IYieldDistributionToken {
abstract contract YieldDistributionToken is ERC20, Ownable, IYieldDistributionToken {

// Types

/**
* @notice Balance of one user at one point in time
* @param amount Amount of tokens held by the user at that time
* @param amount Amount of YieldDistributionTokens held by the user at that time
* @param previousTimestamp Timestamp of the previous balance for that user
*/
struct Balance {
Expand Down Expand Up @@ -68,7 +69,7 @@ abstract contract YieldDistributionToken is ERC20, IYieldDistributionToken {
/// @custom:storage-location erc7201:plume.storage.YieldDistributionToken
struct YieldDistributionTokenStorage {
/// @dev CurrencyToken in which the yield is deposited and denominated
IERC20 currencyToken;
ERC20 currencyToken;
/// @dev Number of decimals of the YieldDistributionToken
uint8 decimals;
/// @dev URI for the YieldDistributionToken metadata
Expand Down Expand Up @@ -152,13 +153,21 @@ abstract contract YieldDistributionToken is ERC20, IYieldDistributionToken {

/**
* @notice Construct the YieldDistributionToken
* @param owner Address of the owner of the YieldDistributionToken
* @param name Name of the YieldDistributionToken
* @param symbol Symbol of the YieldDistributionToken
* @param currencyToken Token in which the yield is deposited and denominated
* @param decimals_ Number of decimals of the YieldDistributionToken
* @param tokenURI URI of the YieldDistributionToken metadata
*/
constructor(string memory name, string memory symbol, ERC20 currencyToken, uint8 decimals_, string memory tokenURI) ERC20(name, symbol) {
constructor(
address owner,
string memory name,
string memory symbol,
ERC20 currencyToken,
uint8 decimals_,
string memory tokenURI
) ERC20(name, symbol) Ownable(owner) {
YieldDistributionTokenStorage storage $ = _getYieldDistributionTokenStorage();
$.currencyToken = currencyToken;
$.decimals = decimals_;
Expand Down Expand Up @@ -227,14 +236,14 @@ abstract contract YieldDistributionToken is ERC20, IYieldDistributionToken {
* @dev Only the owner can call this setter
* @param tokenURI New token URI
*/
function setTokenURI(string memory tokenURI) public onlyRole(DEFAULT_ADMIN_ROLE) {
function setTokenURI(string memory tokenURI) public onlyOwner {
_getYieldDistributionTokenStorage().tokenURI = tokenURI;
}

// Getter View Functions

/// @notice CurrencyToken in which the yield is deposited and denominated
function getCurrencyToken() public view returns (IERC20) {
function getCurrencyToken() public view returns (ERC20) {
return _getYieldDistributionTokenStorage().currencyToken;
}

Expand Down Expand Up @@ -271,7 +280,7 @@ abstract contract YieldDistributionToken is ERC20, IYieldDistributionToken {
if (timestamp == lastTimestamp) {
$.depositHistory.deposits[timestamp].currencyTokenAmount += currencyTokenAmount;
} else {
$.depositHistory[timestamp] = Deposit(currencyTokenAmount, totalSupply(), lastTimestamp);
$.depositHistory.deposits[timestamp] = Deposit(currencyTokenAmount, totalSupply(), lastTimestamp);
$.depositHistory.lastTimestamp = timestamp;
}

Expand Down Expand Up @@ -341,14 +350,14 @@ abstract contract YieldDistributionToken is ERC20, IYieldDistributionToken {

Deposit storage deposit = depositHistory.deposits[depositTimestamp];
Balance storage balance = balanceHistory.balances[balanceTimestamp];
uint256 previousBalanceTimestamp = balance.previousBalanceTimestamp;
uint256 previousBalanceTimestamp = balance.previousTimestamp;
Balance storage previousBalance = balanceHistory.balances[previousBalanceTimestamp];

// Iterate through the balanceHistory list until depositTimestamp >= previousBalanceTimestamp
while (depositTimestamp < previousBalanceTimestamp) {
balanceTimestamp = previousBalanceTimestamp;
balance = previousBalance;
previousBalanceTimestamp = balance.previousBalanceTimestamp;
previousBalanceTimestamp = balance.previousTimestamp;
previousBalance = balanceHistory.balances[previousBalanceTimestamp];
}

Expand All @@ -364,12 +373,12 @@ abstract contract YieldDistributionToken is ERC20, IYieldDistributionToken {
if (balanceTimestamp < depositTimestamp) {
balanceHistory.lastTimestamp = depositTimestamp;
balanceHistory.balances[depositTimestamp].amount = balance.amount;
delete balanceHistory.balances[depositTimestamp].previousBalanceTimestamp;
delete balanceHistory.balances[depositTimestamp].previousTimestamp;
} else if (balanceTimestamp > depositTimestamp) {
if (previousBalanceTimestamp != 0) {
balance.previousTimestamp = depositTimestamp;
balanceHistory.balances[depositTimestamp].amount = previousBalance.amount;
delete balanceHistory.balances[depositTimestamp].previousBalanceTimestamp;
delete balanceHistory.balances[depositTimestamp].previousTimestamp;
}
balance = previousBalance;
balanceTimestamp = previousBalanceTimestamp;
Expand All @@ -386,9 +395,9 @@ abstract contract YieldDistributionToken is ERC20, IYieldDistributionToken {
* This outer loop ends after we go through all deposits or all of the user's balance history.
*/
uint256 yieldAccrued = 0;
uint256 depositAmount = deposit.amount;
uint256 depositAmount = deposit.currencyTokenAmount;
while (depositAmount > 0 && balanceTimestamp > 0) {
uint256 previousDepositTimestamp = deposit.previousDepositTimestamp;
uint256 previousDepositTimestamp = deposit.previousTimestamp;
uint256 timeBetweenDeposits = depositTimestamp - previousDepositTimestamp;

/**
Expand All @@ -411,7 +420,7 @@ abstract contract YieldDistributionToken is ERC20, IYieldDistributionToken {
/ deposit.totalSupply / timeBetweenDeposits;

nextBalanceTimestamp = balanceTimestamp;
balanceTimestamp = balance.previousBalanceTimestamp;
balanceTimestamp = balance.previousTimestamp;
balance = balanceHistory.balances[balanceTimestamp];

/**
Expand All @@ -421,7 +430,7 @@ abstract contract YieldDistributionToken is ERC20, IYieldDistributionToken {
*/
if (nextBalanceTimestamp != preserveBalanceTimestamp) {
delete balanceHistory.balances[nextBalanceTimestamp].amount;
delete balanceHistory.balances[nextBalanceTimestamp].previousBalanceTimestamp;
delete balanceHistory.balances[nextBalanceTimestamp].previousTimestamp;
}
}

Expand All @@ -435,10 +444,11 @@ abstract contract YieldDistributionToken is ERC20, IYieldDistributionToken {

depositTimestamp = previousDepositTimestamp;
deposit = depositHistory.deposits[depositTimestamp];
depositAmount = deposit.amount;
depositAmount = deposit.currencyTokenAmount;
}

$.yieldAccrued[user] += yieldAccrued / _BASE;
emit YieldAccrued(user, yieldAccrued / _BASE);
}

}

0 comments on commit c8b20cf

Please sign in to comment.