Skip to content

Commit

Permalink
Merge pull request #18 from risedle/audit-rev
Browse files Browse the repository at this point in the history
✦ Subsquent addSupply
  • Loading branch information
pyk authored Jan 28, 2022
2 parents 941bf45 + b609869 commit 3037a84
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/RisedleVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ contract RisedleVault is ERC20, Ownable, ReentrancyGuard {
/// @notice Lender supplies underlying token into the vault and receives vault tokens in exchange
function addSupply(uint256 amount) external nonReentrant {
accrueInterest(); // Accrue interest
if (maxTotalDeposit != 0) require(IERC20(underlyingToken).balanceOf(address(this)) + amount < maxTotalDeposit, "!MCR"); // Max cap reached
IERC20(underlyingToken).safeTransferFrom(msg.sender, address(this), amount); // Transfer asset from lender to the vault
if (maxTotalDeposit != 0) require(getTotalAvailableCash() + totalOutstandingDebt + amount < maxTotalDeposit, "!MCR"); // Max cap reached
uint256 exchangeRateInEther = getExchangeRateInEther(); // Get the exchange rate
uint256 mintedAmount = (amount * 1 ether) / exchangeRateInEther; // Calculate how much vault token we need to send to the lender
IERC20(underlyingToken).safeTransferFrom(msg.sender, address(this), amount); // Transfer asset from lender to the vault
_mint(msg.sender, mintedAmount); // Send vault token to the lender
emit SupplyAdded(msg.sender, amount, exchangeRateInEther, mintedAmount);
}
Expand Down
30 changes: 30 additions & 0 deletions src/test/RisedleVaultExternal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,36 @@ contract RisedleVaultExternalTest is DSTest {
assertEq(vault.getExchangeRateInEther(), 1 ether);
}

/// @notice Make sure subsquent supply yield the same amount of rvToken
function test_SubsequentSupplyToTheVault() public {
// Create new vault
RisedleVault vault = createNewVault();

// Create new lender
Lender lender = new Lender(vault);

// Set the lender USDC balance
uint256 amount = 1000 * 1e6; // 1000 USDC
hevm.setUSDCBalance(address(lender), amount);

// Lender add supply to the vault
uint256 depositAmount = 500 * 1e6;
lender.lend(depositAmount); // 500 USDC first

// Lender should receive the same amount of vault token
// Because the initial exchange rate is 1:1
uint256 lenderVaultTokenBalance = vault.balanceOf(address(lender));
assertEq(lenderVaultTokenBalance, depositAmount);

// The vault should receive the USDC
assertEq(IERC20(USDC_ADDRESS).balanceOf(address(vault)), depositAmount);

// Lender add supply once again
lender.lend(depositAmount); // 500 USDC second
assertEq(vault.balanceOf(address(lender)), depositAmount * 2);
assertEq(IERC20(USDC_ADDRESS).balanceOf(address(vault)), depositAmount * 2);
}

/// @notice Make sure anyone can remove asset from the vault
function test_AnyoneCanRemoveSupplyFromTheVault() public {
// Create new vault
Expand Down

0 comments on commit 3037a84

Please sign in to comment.