Skip to content

Commit

Permalink
[NES-200] accrue yield in the FakeComponentToken (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
eyqs authored Sep 25, 2024
1 parent b423178 commit f836634
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
4 changes: 2 additions & 2 deletions nest/script/DeployNestContracts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ contract DeployNestContracts is Script {

address private constant ARC_ADMIN_ADDRESS = 0x1c9d94FAD4ccCd522804a955103899e0D6A4405a;
address private constant NEST_ADMIN_ADDRESS = 0xb015762405De8fD24d29A6e0799c12e0Ea81c1Ff;
address private constant USDC_ADDRESS = 0x849c25e6cCB03cdc23ba91d92440dA7bC8486be2;
address private constant P_ADDRESS = 0xEa0c23A2411729073Ed52fF94b38FceffE82FDE3;

function run() external {
vm.startBroadcast(ARC_ADMIN_ADDRESS);

IComponentToken currencyToken = IComponentToken(USDC_ADDRESS);
IComponentToken currencyToken = IComponentToken(P_ADDRESS);

FakeComponentToken fakeComponentToken = new FakeComponentToken();
FakeComponentTokenProxy fakeComponentTokenProxy = new FakeComponentTokenProxy(
Expand Down
46 changes: 35 additions & 11 deletions nest/src/FakeComponentToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ contract FakeComponentToken is
uint8 decimals;
/// @dev Version of the FakeComponentToken
uint256 version;
/// @dev Total amount of yield that has ever been accrued by all users
uint256 totalYieldAccrued;
/// @dev Total amount of yield that has ever been withdrawn by all users
uint256 totalYieldWithdrawn;
/// @dev Total amount of yield that has ever been accrued by each user
mapping(address user => uint256 currencyTokenAmount) yieldAccrued;
/// @dev Total amount of yield that has ever been withdrawn by each user
mapping(address user => uint256 currencyTokenAmount) yieldWithdrawn;
}

// keccak256(abi.encode(uint256(keccak256("plume.storage.FakeComponentToken")) - 1)) & ~bytes32(uint256(0xff))
Expand Down Expand Up @@ -202,8 +210,22 @@ contract FakeComponentToken is
* @param user Address of the user for which to claim yield
*/
function claimYield(address user) external returns (uint256 amount) {
FakeComponentTokenStorage storage $ = _getFakeComponentTokenStorage();
amount = unclaimedYield(user);
_getFakeComponentTokenStorage().currencyToken.transfer(user, amount);
$.currencyToken.transfer(user, amount);
$.yieldWithdrawn[user] += amount;
$.totalYieldWithdrawn += amount;
}

/**
* @notice Accrue yield for the given user
* @dev Anyone can call this function to accrue yield for any user
* @param user Address of the user for which to accrue yield
*/
function accrueYield(address user, uint256 amount) external {
FakeComponentTokenStorage storage $ = _getFakeComponentTokenStorage();
$.yieldAccrued[user] += amount;
$.totalYieldAccrued += amount;
}

// Admin Setter Functions
Expand Down Expand Up @@ -243,36 +265,37 @@ contract FakeComponentToken is
}

/// @notice Total yield distributed to all FakeComponentTokens for all users
function totalYield() public view returns (uint256 amount) {
return 6;
function totalYield() external view returns (uint256 amount) {
return _getFakeComponentTokenStorage().totalYieldAccrued;
}

/// @notice Claimed yield across all FakeComponentTokens for all users
function claimedYield() public view returns (uint256 amount) {
return 4;
function claimedYield() external view returns (uint256 amount) {
return _getFakeComponentTokenStorage().totalYieldWithdrawn;
}

/// @notice Unclaimed yield across all FakeComponentTokens for all users
function unclaimedYield() external view returns (uint256 amount) {
return totalYield() - claimedYield();
FakeComponentTokenStorage storage $ = _getFakeComponentTokenStorage();
return $.totalYieldAccrued - $.totalYieldWithdrawn;
}

/**
* @notice Total yield distributed to a specific user
* @param user Address of the user for which to get the total yield
* @return amount Total yield distributed to the user
*/
function totalYield(address user) public view returns (uint256 amount) {
return 3;
function totalYield(address user) external view returns (uint256 amount) {
return _getFakeComponentTokenStorage().yieldAccrued[user];
}

/**
* @notice Amount of yield that a specific user has claimed
* @param user Address of the user for which to get the claimed yield
* @return amount Amount of yield that the user has claimed
*/
function claimedYield(address user) public view returns (uint256 amount) {
return 2;
function claimedYield(address user) external view returns (uint256 amount) {
return _getFakeComponentTokenStorage().yieldWithdrawn[user];
}

/**
Expand All @@ -281,7 +304,8 @@ contract FakeComponentToken is
* @return amount Amount of yield that the user has not yet claimed
*/
function unclaimedYield(address user) public view returns (uint256 amount) {
return totalYield(user) - claimedYield(user);
FakeComponentTokenStorage storage $ = _getFakeComponentTokenStorage();
return $.yieldAccrued[user] - $.yieldWithdrawn[user];
}

}

0 comments on commit f836634

Please sign in to comment.