From 28fe0ba0379f0586c2ebddb2a9565bca94bfe341 Mon Sep 17 00:00:00 2001 From: ungaro Date: Mon, 9 Dec 2024 09:07:55 -0500 Subject: [PATCH] formatting --- nest/script/UpgradepUSD.s.sol | 11 ++- nest/test/AggregateToken.t.sol | 138 +++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 4 deletions(-) diff --git a/nest/script/UpgradepUSD.s.sol b/nest/script/UpgradepUSD.s.sol index ce2a86c..f4979c1 100644 --- a/nest/script/UpgradepUSD.s.sol +++ b/nest/script/UpgradepUSD.s.sol @@ -5,7 +5,6 @@ import { UUPSUpgradeable } from "@openzeppelin/contracts-upgradeable/proxy/utils import { ERC1967Utils } from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Utils.sol"; import { Script } from "forge-std/Script.sol"; -import { Test } from "forge-std/Test.sol"; import { console2 } from "forge-std/console2.sol"; import { pUSDProxy } from "../src/proxy/pUSDProxy.sol"; @@ -13,7 +12,7 @@ import { pUSD } from "../src/token/pUSD.sol"; import { IERC20 } from "@openzeppelin/contracts/interfaces/IERC20.sol"; -contract UpgradePUSD is Script, Test { +contract UpgradePUSD is Script { // Constants address private constant ADMIN_ADDRESS = 0xb015762405De8fD24d29A6e0799c12e0Ea81c1Ff; @@ -35,6 +34,9 @@ contract UpgradePUSD is Script, Test { uint256 public currentTotalSupply; bool public isConnected; + // small hack to be excluded from coverage report + function test() public { } + function setUp() public { // Try to read implementation slot from proxy, this only works with RPC try vm.load(PUSD_PROXY, ERC1967Utils.IMPLEMENTATION_SLOT) returns (bytes32 implementation) { @@ -56,12 +58,13 @@ contract UpgradePUSD is Script, Test { console2.log("Vault:", currentVault); console2.log("Total Supply:", currentTotalSupply); } else { - vm.skip(true); + //TODO: Check this again + vm.skip(false); isConnected = false; } } catch { console2.log("No implementation found - skipping"); - vm.skip(true); + vm.skip(false); isConnected = false; } } diff --git a/nest/test/AggregateToken.t.sol b/nest/test/AggregateToken.t.sol index f0e8b62..8a34ad4 100644 --- a/nest/test/AggregateToken.t.sol +++ b/nest/test/AggregateToken.t.sol @@ -38,6 +38,14 @@ contract AggregateTokenTest is Test { event Unpaused(); event ComponentTokenRemoved(IComponentToken indexed componentToken); + event ComponentTokenBuyRequested( + address indexed buyer, IComponentToken indexed componentToken, uint256 assets, uint256 requestId + ); + + event ComponentTokenSellRequested( + address indexed seller, IComponentToken indexed componentToken, uint256 componentTokenAmount, uint256 requestId + ); + function setUp() public { owner = makeAddr("owner"); user1 = makeAddr("user1"); @@ -158,7 +166,137 @@ contract AggregateTokenTest is Test { vm.stopPrank(); } + function testConversion() public { + // Test convertToShares + assertEq(token.convertToShares(2e18), 2e18); // With askPrice = 1e18, 2 assets = 2 shares + + // Test convertToAssets + assertEq(token.convertToAssets(2e18), 2e18); // With bidPrice = 1e18, 2 shares = 2 assets + + // Test with different prices + vm.startPrank(owner); + token.grantRole(token.PRICE_UPDATER_ROLE(), owner); + token.setAskPrice(2e18); // 2:1 ratio + token.setBidPrice(0.5e18); // 1:2 ratio + vm.stopPrank(); + + assertEq(token.convertToShares(2e18), 1e18); // 2 assets = 1 share at 2:1 ratio + assertEq(token.convertToAssets(2e18), 1e18); // 2 shares = 1 asset at 1:2 ratio + } + + function testDeposit() public { + // Test deposit when paused + vm.prank(owner); + token.pause(); + + vm.expectRevert(AggregateToken.DepositPaused.selector); + token.deposit(1e18, address(this), address(this)); + + // Test successful deposit + vm.prank(owner); + token.unpause(); + + vm.startPrank(user1); + usdc.approve(address(token), 1e18); + uint256 shares = token.deposit(1e18, user1, user1); + assertEq(shares, 1e18); + assertEq(token.balanceOf(user1), 1e18); + vm.stopPrank(); + } + + function testRedeem() public { + // Setup: First deposit some tokens + vm.startPrank(user1); + usdc.approve(address(token), 1e18); + token.deposit(1e18, user1, user1); + + // Test redeem + uint256 assets = token.redeem(1e18, user1, user1); + assertEq(assets, 1e18); + assertEq(token.balanceOf(user1), 0); + assertEq(usdc.balanceOf(user1), 1000e6); // Back to original balance + vm.stopPrank(); + } + + function testTotalAssets() public { + assertEq(token.totalAssets(), 0); + + // Deposit some assets + vm.startPrank(user1); + usdc.approve(address(token), 1e18); + token.deposit(1e18, user1, user1); + assertEq(token.totalAssets(), 1e18); + vm.stopPrank(); + } + + function testApproveComponentToken() public { + vm.startPrank(owner); + + // Test ComponentTokenNotListed error + vm.expectRevert( + abi.encodeWithSelector(AggregateToken.ComponentTokenNotListed.selector, IComponentToken(address(newUsdc))) + ); + token.approveComponentToken(IComponentToken(address(newUsdc)), 1e6); + + // Test successful approval + token.addComponentToken(IComponentToken(address(newUsdc))); + token.approveComponentToken(IComponentToken(address(newUsdc)), 1e6); + assertEq(usdc.allowance(address(token), address(newUsdc)), 1e6); + + vm.stopPrank(); + } + + function testComponentTokenOperations() public { + vm.startPrank(owner); + + // Test buyComponentToken + token.addComponentToken(IComponentToken(address(newUsdc))); + vm.expectEmit(address(token)); + emit ComponentTokenBought(owner, IComponentToken(address(newUsdc)), 1e18, 1e18); + token.buyComponentToken(IComponentToken(address(newUsdc)), 1e18); + + // Test sellComponentToken + vm.expectEmit(address(token)); + emit ComponentTokenSold(owner, IComponentToken(address(newUsdc)), 1e18, 1e18); + token.sellComponentToken(IComponentToken(address(newUsdc)), 1e18); + + // Test requestBuyComponentToken + vm.expectEmit(address(token)); + emit ComponentTokenBuyRequested(owner, IComponentToken(address(newUsdc)), 1e18, 0); + token.requestBuyComponentToken(IComponentToken(address(newUsdc)), 1e18); + + // Test requestSellComponentToken + vm.expectEmit(address(token)); + emit ComponentTokenSellRequested(owner, IComponentToken(address(newUsdc)), 1e18, 0); + token.requestSellComponentToken(IComponentToken(address(newUsdc)), 1e18); + + vm.stopPrank(); + } + + function testGetters() public { + // Test getComponentTokenList + IComponentToken[] memory list = token.getComponentTokenList(); + assertEq(list.length, 1); + assertEq(address(list[0]), address(usdc)); + + // Test getComponentToken + assertTrue(token.getComponentToken(IComponentToken(address(usdc)))); + assertFalse(token.getComponentToken(IComponentToken(address(newUsdc)))); + } + /* + function testSupportsInterface() public { + // Test standard interfaces + assertTrue(token.supportsInterface(type(IERC20).interfaceId)); + assertTrue(token.supportsInterface(type(IERC4626).interfaceId)); + assertTrue(token.supportsInterface(type(IAccessControl).interfaceId)); + + // Test custom interfaces + assertTrue(token.supportsInterface(type(IComponentToken).interfaceId)); + assertTrue(token.supportsInterface(type(IAggregateToken).interfaceId)); + } + */ // Helper function for access control error message + function accessControlErrorMessage(address account, bytes32 role) internal pure returns (bytes memory) { return abi.encodePacked( "AccessControl: account ",