Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ungaro committed Dec 9, 2024
1 parent 5a751b9 commit 28fe0ba
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 4 deletions.
11 changes: 7 additions & 4 deletions nest/script/UpgradepUSD.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ 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";
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;
Expand All @@ -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) {
Expand All @@ -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;
}
}
Expand Down
138 changes: 138 additions & 0 deletions nest/test/AggregateToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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 ",
Expand Down

0 comments on commit 28fe0ba

Please sign in to comment.