Skip to content

Commit

Permalink
increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ungaro committed Dec 19, 2024
1 parent 18e9346 commit 9465726
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 49 deletions.
24 changes: 4 additions & 20 deletions smart-wallets/src/token/AssetToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ contract AssetToken is WalletUtils, YieldDistributionToken, IAssetToken {
uint256 totalValue;
/// @dev Mapping of whitelisted users
mapping(address user => bool whitelisted) isWhitelisted;
/// @dev List of all users that have ever held AssetTokens
address[] holders;
/// @dev Mapping of all users that have ever held AssetTokens
mapping(address user => bool held) hasHeld;
}

// keccak256(abi.encode(uint256(keccak256("plume.storage.AssetToken")) - 1)) & ~bytes32(uint256(0xff))
Expand Down Expand Up @@ -120,6 +116,10 @@ contract AssetToken is WalletUtils, YieldDistributionToken, IAssetToken {
uint256 totalValue_,
bool isWhitelistEnabled_
) YieldDistributionToken(owner, name, symbol, currencyToken, decimals_, tokenURI_) {
if (address(currencyToken) == address(0)) {
revert InvalidAddress();
}

AssetTokenStorage storage $ = _getAssetTokenStorage();
$.totalValue = totalValue_;
isWhitelistEnabled = isWhitelistEnabled_;
Expand Down Expand Up @@ -293,22 +293,6 @@ contract AssetToken is WalletUtils, YieldDistributionToken, IAssetToken {
return _getAssetTokenStorage().isWhitelisted[user];
}

/// @notice List of all users that have ever held AssetTokens
function getHolders() external view returns (address[] memory) {
return _getAssetTokenStorage().holders;
}

/**
* @notice Check if the user has ever held AssetTokens
* @param user Address of the user to check
* @return held Boolean indicating if the user has ever held AssetTokens
*/
function hasBeenHolder(
address user
) external view returns (bool held) {
return _getAssetTokenStorage().hasHeld[user];
}

/// @notice Price of an AssetToken based on its total value and total supply
function getPricePerToken() external view returns (uint256) {
return _getAssetTokenStorage().totalValue / totalSupply();
Expand Down
103 changes: 74 additions & 29 deletions smart-wallets/test/AssetToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ contract AssetTokenTest is Test {
address public user2;
address public walletProxyAddress;

// Events for testing
event Deposited(address indexed user, uint256 currencyTokenAmount);

// small hack to be excluded from coverage report
// function test_() public { }

function setUp() public {
owner = ADMIN_ADDRESS;
user1 = address(0x1);
Expand Down Expand Up @@ -116,7 +110,6 @@ contract AssetTokenTest is Test {
assertEq(assetToken.name(), "Asset Token", "Name mismatch");
assertEq(assetToken.symbol(), "AT", "Symbol mismatch");
assertEq(assetToken.decimals(), 18, "Decimals mismatch");
//assertEq(assetToken.tokenURI_(), "http://example.com/token", "TokenURI mismatch");
assertEq(assetToken.totalSupply(), 10_000 * 10 ** 18, "Total supply mismatch");
assertEq(assetToken.getTotalValue(), 10_000 * 10 ** 18, "Total value mismatch");
assertFalse(assetToken.isWhitelistEnabled(), "Whitelist should be enabled");
Expand Down Expand Up @@ -275,7 +268,6 @@ contract AssetTokenTest is Test {
function isWhitelistEnabled() public view returns (bool) {
return assetTokenWhitelisted.isWhitelistEnabled();
}
// Update the test function

function test_AddAndRemoveFromWhitelist() public {
console.log("AssetToken owner:", assetTokenWhitelisted.owner());
Expand Down Expand Up @@ -329,20 +321,6 @@ contract AssetTokenTest is Test {
// You may need to implement a way to verify that the yield was requested
}

function test_GetHoldersAndHasBeenHolder() public {
vm.startPrank(address(testWalletImplementation));
assetToken.addToWhitelist(user1);
assetToken.addToWhitelist(user2);
assetToken.mint(user1, 100 * 10 ** 18);
assetToken.mint(user2, 100 * 10 ** 18);
vm.stopPrank();

address[] memory holders = assetToken.getHolders();
assertEq(holders.length, 3, "Should have 3 holders (owner, user1, user2)");
assertTrue(assetToken.hasBeenHolder(user1), "User1 should be a holder");
assertTrue(assetToken.hasBeenHolder(user2), "User2 should be a holder");
}

function test_GetPricePerToken() public {
uint256 price = assetToken.getPricePerToken();
console.log("price", price);
Expand Down Expand Up @@ -477,10 +455,9 @@ contract AssetTokenTest is Test {
vm.warp(block.timestamp + 1);
assetToken.depositYield(yieldAmount);

// Let yield accrue
vm.warp(block.timestamp + 1 days);
// Let yield accrue for just a few seconds
vm.warp(block.timestamp + 10); // Only 10 seconds instead of 1 hour

// Claim yield for both users
vm.stopPrank();

vm.prank(user1);
Expand All @@ -489,9 +466,16 @@ contract AssetTokenTest is Test {
vm.prank(user2);
(IERC20 token2, uint256 amount2) = assetToken.claimYield(user2);

// Debug logs
console.log("User 1 yield amount:", amount1);
console.log("User 2 yield amount:", amount2);
console.log("Total yield deposited:", yieldAmount);

// Test assumptions:
// 1. Both users should get roughly equal yield (within 1%)
assertApproxEqRel(amount1, amount2, 0.01e18);
// 2. Total claimed yield should not exceed deposited amount
assertLe(amount1 + amount2, yieldAmount);
}

function test_YieldCalculationsWithMultipleDeposits() public {
Expand All @@ -510,24 +494,85 @@ contract AssetTokenTest is Test {
vm.warp(block.timestamp + 1);
assetToken.depositYield(firstYield);

// Second yield deposit
vm.warp(block.timestamp + 1 days);
// Second yield deposit after a short time
vm.warp(block.timestamp + 10); // Only 10 seconds
ERC20Mock(address(currencyToken)).mint(address(testWalletImplementation), secondYield);
currencyToken.approve(address(assetToken), secondYield);
assetToken.depositYield(secondYield);

vm.stopPrank();

// Claim yield
// Claim yield after a short time
vm.startPrank(user1);
vm.warp(block.timestamp + 1 days);
vm.warp(block.timestamp + 10); // Only 10 seconds

(IERC20 token, uint256 claimedAmount) = assetToken.claimYield(user1);

// Debug logs
console.log("Claimed amount:", claimedAmount);
console.log("Total yield deposited:", firstYield + secondYield);

vm.stopPrank();

// Test assumptions:
// 1. Token should be the correct currency token
assertEq(address(token), address(currencyToken));
// 2. Claimed amount should not exceed total deposited yield
assertLe(claimedAmount, firstYield + secondYield);
}

function test_RevertConstructorInvalidAddress() public {
// First test: Invalid owner (should revert with OwnableInvalidOwner)
vm.expectRevert(abi.encodeWithSignature("OwnableInvalidOwner(address)", address(0)));
new AssetToken(
address(0), // Invalid owner address
"Asset Token",
"AT",
ERC20(address(currencyToken)),
18,
"http://example.com/token",
1000 * 10 ** 18,
10_000 * 10 ** 18,
true
);

// Second test: Invalid currency token (should revert with InvalidAddress)
vm.expectRevert(AssetToken.InvalidAddress.selector);
new AssetToken(
address(this), // valid owner
"Asset Token",
"AT",
ERC20(address(0)), // Invalid currency token address
18,
"http://example.com/token",
1000 * 10 ** 18,
10_000 * 10 ** 18,
true
);
}

function test_GetBalanceAvailableWithLockedBalance() public {
uint256 initialBalance = 100 ether;
uint256 lockedBalance = 30 ether;

// Create new MockSmartWallet instance
MockSmartWallet mockWallet = new MockSmartWallet();

vm.startPrank(address(testWalletImplementation));

// Mint some tokens to the mock wallet
assetToken.mint(address(mockWallet), initialBalance);

// Lock some tokens
mockWallet.lockTokens(IAssetToken(address(assetToken)), lockedBalance);

vm.stopPrank();

// Check available balance
uint256 availableBalance = assetToken.getBalanceAvailable(address(mockWallet));

// Available balance should be initial balance minus locked balance
assertEq(availableBalance, initialBalance - lockedBalance);
}

}

0 comments on commit 9465726

Please sign in to comment.