Skip to content

Commit

Permalink
Ensure burns cannot occur at the new oracle price
Browse files Browse the repository at this point in the history
  • Loading branch information
ninokeldishvili committed Nov 27, 2024
1 parent c4acd7e commit fe5a936
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions test/src/concrete/vault/ERC20PriceOracleReceiptVault.withdraw.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -334,20 +334,23 @@ contract ERC20PriceOracleReceiptVaultWithdrawTest is ERC20PriceOracleReceiptVaul
uint256 fuzzedKeyAlice,
uint256 priceOne,
uint256 priceTwo,
uint256 aliceDeposit,
uint256 aliceMint
uint256 priceThree,
uint256 aliceDeposit
) external {
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
priceOne = bound(priceOne, 1e18, 100e18);
priceTwo = bound(priceTwo, 1e18, 100e18);
priceThree = bound(priceThree, 1e18, 100e18);
vm.assume(priceTwo != priceOne && priceTwo != priceThree);
vm.assume(priceOne != priceThree);

aliceDeposit = bound(aliceDeposit, 100e18, type(uint128).max);
aliceMint = bound(aliceMint, 100e18, type(uint128).max);

// Start recording logs
vm.recordLogs();
ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, "Alice", "Alice");

ReceiptContract receipt = getReceipt();

// Set initial oracle price and deposit first half
setVaultOraclePrice(priceOne);
vm.startPrank(alice);
Expand All @@ -365,8 +368,8 @@ contract ERC20PriceOracleReceiptVaultWithdrawTest is ERC20PriceOracleReceiptVaul
// Assert receipt balance and vault state after first deposit
uint256 expectedSharesOne = (aliceDeposit / 2).fixedPointMul(priceOne, Math.Rounding.Down);
assertEq(vault.balanceOf(alice), expectedSharesOne);
// Check receipt balance
assertEq(receipt.balanceOf(alice, priceOne), expectedSharesOne);

// Set new oracle price and deposit second half
setVaultOraclePrice(priceTwo);
vm.startPrank(alice);
Expand All @@ -379,29 +382,36 @@ contract ERC20PriceOracleReceiptVaultWithdrawTest is ERC20PriceOracleReceiptVaul
vault.deposit(aliceDeposit / 2, alice, priceTwo, bytes(""));
vm.stopPrank();

// Assert receipt balance and vault state after second deposit
uint256 expectedSharesTwo = (aliceDeposit / 2).fixedPointMul(priceTwo, Math.Rounding.Down);
uint256 totalShares = expectedSharesOne + expectedSharesTwo;
assertEq(vault.balanceOf(alice), expectedSharesOne + expectedSharesTwo);

// Mint additional shares at priceTwo
uint256 assetsRequired = aliceDeposit.fixedPointDiv(priceTwo, Math.Rounding.Up);
vm.startPrank(alice);
vm.mockCall(
address(iAsset),
abi.encodeWithSelector(IERC20.transferFrom.selector, alice, address(vault), assetsRequired),
abi.encode(true)
);

vault.mint(aliceDeposit, alice, priceTwo, bytes(""));
vm.stopPrank();

assertEq(vault.balanceOf(alice), totalShares);
assertEq(vault.balanceOf(alice), expectedSharesOne + expectedSharesTwo + aliceDeposit);

{
// Calculate the required assets for minting the specified shares
uint256 assetsRequired = aliceMint.fixedPointDiv(priceTwo, Math.Rounding.Up);
// Set new oracle price without minting
setVaultOraclePrice(priceOne);
assertEq(vault.balanceOf(alice), expectedSharesOne + expectedSharesTwo + aliceDeposit);

vm.startPrank(alice);
vm.mockCall(
address(iAsset),
abi.encodeWithSelector(IERC20.transferFrom.selector, alice, address(vault), assetsRequired),
abi.encode(true)
);
// Ensure burns cannot occur at the new oracle price
setVaultOraclePrice(priceThree);

uint256 assetsMinted = vault.mint(aliceMint, alice, priceTwo, bytes(""));
vm.stopPrank();
vm.startPrank(alice);
vm.expectRevert("ERC1155: burn amount exceeds balance");
vault.withdraw(1e18, alice, alice, priceThree, bytes(""));

// Assert minting state
assertEq(assetsMinted, assetsRequired);
assertEq(vault.balanceOf(alice), totalShares + aliceMint);
}
vm.expectRevert("ERC1155: burn amount exceeds balance");
vault.redeem(1e18, alice, alice, priceThree, bytes(""));
vm.stopPrank();
}
}

0 comments on commit fe5a936

Please sign in to comment.