Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidmeister committed Oct 19, 2024
1 parent fc45600 commit 14a40cb
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,19 @@ contract ERC20PriceOracleReceiptVaultConstructionTest is ERC20PriceOracleReceipt
function testCreatingSeveralVaults(
uint256 fuzzedKeyAlice,
uint256 fuzzedKeyBob,
uint256 timestamp,
string memory assetName,
string memory assetSymbol,
string memory assetNameTwo,
string memory assetSymbolTwo,
uint8 xauDecimals,
uint8 usdDecimals,
uint80 answeredInRound
string memory assetSymbolTwo
) external {
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

// Use common decimal bounds for price feeds
// Use 0-20 so we at least have some coverage higher than 18
usdDecimals = uint8(bound(usdDecimals, 0, 20));
xauDecimals = uint8(bound(xauDecimals, 0, 20));
timestamp = bound(timestamp, 0, type(uint32).max);

// Simulate transaction from alice
vm.prank(alice);

address vaultPriceOracle = address(uint160(uint256(keccak256("twoPriceOracle"))));

ERC20PriceOracleReceiptVault vault = createVault(vaultPriceOracle, assetName, assetSymbol);
ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, assetName, assetSymbol);

assert(address(vault) != address(0));
assertEq(keccak256(bytes(vault.name())), keccak256(bytes(assetName)));
Expand All @@ -103,7 +91,7 @@ contract ERC20PriceOracleReceiptVaultConstructionTest is ERC20PriceOracleReceipt
// Simulate transaction from alice
vm.prank(bob);

ERC20PriceOracleReceiptVault vaultTwo = createVault(vaultPriceOracle, assetNameTwo, assetSymbolTwo);
ERC20PriceOracleReceiptVault vaultTwo = createVault(iVaultOracle, assetNameTwo, assetSymbolTwo);

assert(address(vaultTwo) != address(0));
assertEq(keccak256(bytes(vaultTwo.name())), keccak256(bytes(assetNameTwo)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,29 @@ contract ERC20PriceOracleReceiptVaultDepositTest is ERC20PriceOracleReceiptVault
using LibFixedPointDecimalArithmeticOpenZeppelin for uint256;

/// Test deposit function
function testDeposit(
uint256 fuzzedKeyAlice,
string memory assetName,
uint256 assets,
address vaultOracle,
uint256 oraclePrice
) external {
function testDepositBasic(uint256 fuzzedKeyAlice, string memory assetName, uint256 assets, uint256 oraclePrice)
external
{
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);

oraclePrice = bound(oraclePrice, 0.01e18, 100e18);
setVaultOraclePrice(oraclePrice);

vm.startPrank(alice);
ERC20PriceOracleReceiptVault vault;
{
vault = createVault(vaultOracle, assetName, assetName);
vault = createVault(iVaultOracle, assetName, assetName);

vm.mockCall(address(iAsset), abi.encodeWithSelector(IERC20.totalSupply.selector), abi.encode(1e18));

// Ensure Alice has enough balance and allowance
vm.mockCall(address(iAsset), abi.encodeWithSelector(IERC20.balanceOf.selector, alice), abi.encode(assets));

uint256 totalSupply = iAsset.totalSupply();
// Getting ZeroSharesAmount if bounded from 1
assets = bound(assets, 2, totalSupply);

assets = bound(assets, 1, totalSupply);
vm.assume(assets.fixedPointMul(oraclePrice, Math.Rounding.Down) > 0);

vm.mockCall(
address(iAsset),
Expand All @@ -67,28 +67,31 @@ contract ERC20PriceOracleReceiptVaultDepositTest is ERC20PriceOracleReceiptVault
uint256 fuzzedKeyBob,
string memory assetName,
uint256 assets,
address vaultOracle,
uint256 oraclePrice
) external {
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

oraclePrice = bound(oraclePrice, 0.01e18, 100e18);
setVaultOraclePrice(oraclePrice);

vm.startPrank(alice);

vm.recordLogs();
ERC20PriceOracleReceiptVault vault;
{
vault = createVault(vaultOracle, assetName, assetName);
vault = createVault(iVaultOracle, assetName, assetName);

vm.mockCall(address(iAsset), abi.encodeWithSelector(IERC20.totalSupply.selector), abi.encode(1e18));

// Ensure Alice has enough balance and allowance
vm.mockCall(address(iAsset), abi.encodeWithSelector(IERC20.balanceOf.selector, alice), abi.encode(assets));

uint256 totalSupply = iAsset.totalSupply();
// Getting ZeroSharesAmount if bounded from 1
assets = bound(assets, 20, totalSupply);

assets = bound(assets, 1, totalSupply);
vm.assume(assets.fixedPointMul(oraclePrice, Math.Rounding.Down) > 0);

vm.mockCall(
address(iAsset),
Expand Down Expand Up @@ -123,13 +126,15 @@ contract ERC20PriceOracleReceiptVaultDepositTest is ERC20PriceOracleReceiptVault
string memory assetName,
string memory assetSymbol,
bytes memory data,
address vaultOracle,
uint256 oraclePrice
) external {
oraclePrice = bound(oraclePrice, 0.01e18, 100e18);
setVaultOraclePrice(oraclePrice);

// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);

ERC20PriceOracleReceiptVault vault = createVault(vaultOracle, assetName, assetSymbol);
ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, assetName, assetSymbol);

vm.expectRevert(abi.encodeWithSelector(ZeroAssetsAmount.selector));
vault.deposit(0, alice, oraclePrice, data);
Expand All @@ -142,14 +147,18 @@ contract ERC20PriceOracleReceiptVaultDepositTest is ERC20PriceOracleReceiptVault
string memory assetSymbol,
bytes memory data,
uint256 assets,
address vaultOracle,
uint256 oraclePrice
) external {
oraclePrice = bound(oraclePrice, 0.01e18, 100e18);
setVaultOraclePrice(oraclePrice);

// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);

assets = bound(assets, 1, type(uint256).max);
ERC20PriceOracleReceiptVault vault = createVault(vaultOracle, assetName, assetSymbol);
assets = bound(assets, 1, type(uint128).max);
vm.assume(assets.fixedPointMul(oraclePrice, Math.Rounding.Down) > 0);

ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, assetName, assetSymbol);

vm.expectRevert(abi.encodeWithSelector(MinShareRatio.selector, oraclePrice + 1, oraclePrice));
vault.deposit(assets, alice, oraclePrice + 1, data);
Expand All @@ -161,11 +170,15 @@ contract ERC20PriceOracleReceiptVaultDepositTest is ERC20PriceOracleReceiptVault
string memory assetSymbol,
bytes memory data,
uint256 assets,
address vaultOracle,
uint256 oraclePrice
) external {
assets = bound(assets, 1, type(uint256).max);
ERC20PriceOracleReceiptVault vault = createVault(vaultOracle, assetName, assetSymbol);
oraclePrice = bound(oraclePrice, 0.01e18, 100e18);
setVaultOraclePrice(oraclePrice);

assets = bound(assets, 1, type(uint128).max);
vm.assume(assets.fixedPointMul(oraclePrice, Math.Rounding.Down) > 0);

ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, assetName, assetSymbol);

vm.expectRevert();
vault.deposit(assets, address(0), oraclePrice, data);
Expand All @@ -176,11 +189,15 @@ contract ERC20PriceOracleReceiptVaultDepositTest is ERC20PriceOracleReceiptVault
string memory assetName,
string memory assetSymbol,
uint256 assets,
address vaultOracle,
uint256 oraclePrice
) external {
assets = bound(assets, 1, type(uint256).max);
ERC20PriceOracleReceiptVault vault = createVault(vaultOracle, assetName, assetSymbol);
oraclePrice = bound(oraclePrice, 0.01e18, 100e18);
setVaultOraclePrice(oraclePrice);

assets = bound(assets, 1, type(uint128).max);
vm.assume(assets.fixedPointMul(oraclePrice, Math.Rounding.Down) > 0);

ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, assetName, assetSymbol);

uint256 expectedShares = assets.fixedPointMul(oraclePrice, Math.Rounding.Down);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,27 @@ contract ERC20PriceOracleReceiptVaultreceiptVaultTest is ERC20PriceOracleReceipt
using LibFixedPointDecimalArithmeticOpenZeppelin for uint256;

/// Test vault asset
function testVaultAsset(uint256 fuzzedKeyAlice, string memory assetName, address vaultOracle) external {
function testVaultAsset(uint256 fuzzedKeyAlice, string memory assetName) external {
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);

vm.startPrank(alice);

ERC20PriceOracleReceiptVault vault = createVault(vaultOracle, assetName, assetName);
ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, assetName, assetName);

assertEq(vault.asset(), address(iAsset));
}

/// Test vault totalAssets
function testTotalAssets(uint256 fuzzedKeyAlice, string memory assetName, uint256 assets, address vaultOracle)
external
{
function testTotalAssets(uint256 fuzzedKeyAlice, string memory assetName, uint256 assets) external {
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);

assets = bound(assets, 1, type(uint256).max);

vm.startPrank(alice);

ERC20PriceOracleReceiptVault vault = createVault(vaultOracle, assetName, assetName);
ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, assetName, assetName);

vm.mockCall(
address(iAsset), abi.encodeWithSelector(IERC20.balanceOf.selector, address(vault)), abi.encode(assets)
Expand All @@ -52,13 +50,9 @@ contract ERC20PriceOracleReceiptVaultreceiptVaultTest is ERC20PriceOracleReceipt
}

/// Test convertToAssets
function testConvertToAssets(
uint256 fuzzedKeyAlice,
string memory assetName,
uint256 shares,
uint256 id,
address vaultOracle
) external {
function testConvertToAssets(uint256 fuzzedKeyAlice, string memory assetName, uint256 shares, uint256 id)
external
{
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);

Expand All @@ -67,7 +61,7 @@ contract ERC20PriceOracleReceiptVaultreceiptVaultTest is ERC20PriceOracleReceipt

vm.startPrank(alice);

ERC20PriceOracleReceiptVault vault = createVault(vaultOracle, assetName, assetName);
ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, assetName, assetName);

uint256 expectedAssets = shares.fixedPointDiv(id, Math.Rounding.Down);
uint256 resultAssets = vault.convertToAssets(shares, id);
Expand All @@ -81,8 +75,7 @@ contract ERC20PriceOracleReceiptVaultreceiptVaultTest is ERC20PriceOracleReceipt
uint256 fuzzedKeyBob,
string memory assetName,
uint256 shares,
uint256 id,
address vaultOracle
uint256 id
) external {
// Generate unique addresses
(address alice, address bob) =
Expand All @@ -93,7 +86,7 @@ contract ERC20PriceOracleReceiptVaultreceiptVaultTest is ERC20PriceOracleReceipt

vm.startPrank(alice);

ERC20PriceOracleReceiptVault vault = createVault(vaultOracle, assetName, assetName);
ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, assetName, assetName);

uint256 resultAssetsAlice = vault.convertToAssets(shares, id);
vm.stopPrank();
Expand All @@ -106,13 +99,9 @@ contract ERC20PriceOracleReceiptVaultreceiptVaultTest is ERC20PriceOracleReceipt
}

/// Test convertToShares
function testConvertToShares(
uint256 fuzzedKeyAlice,
string memory assetName,
uint256 assets,
uint256 id,
address vaultOracle
) external {
function testConvertToShares(uint256 fuzzedKeyAlice, string memory assetName, uint256 assets, uint256 id)
external
{
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);

Expand All @@ -121,7 +110,7 @@ contract ERC20PriceOracleReceiptVaultreceiptVaultTest is ERC20PriceOracleReceipt

vm.startPrank(alice);

ERC20PriceOracleReceiptVault vault = createVault(vaultOracle, assetName, assetName);
ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, assetName, assetName);

uint256 expectedShares = assets.fixedPointMul(id, Math.Rounding.Down);
uint256 resultShares = vault.convertToShares(assets, id);
Expand All @@ -135,19 +124,20 @@ contract ERC20PriceOracleReceiptVaultreceiptVaultTest is ERC20PriceOracleReceipt
uint256 fuzzedKeyBob,
string memory assetName,
uint256 assets,
uint256 id,
address vaultOracle
uint256 id
) external {
// Generate unique addresses
(address alice, address bob) =
LibUniqueAddressesGenerator.generateUniqueAddresses(vm, SECP256K1_ORDER, fuzzedKeyAlice, fuzzedKeyBob);

// Bound the ID to a range that could actually be a price.
id = bound(id, 0, type(uint128).max);
id = bound(id, 0.001e18, 100e18);
assets = bound(assets, 1, type(uint128).max);

vm.startPrank(alice);

ERC20PriceOracleReceiptVault vault = createVault(vaultOracle, assetName, assetName);
ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, assetName, assetName);

uint256 resultSharesAlice = vault.convertToShares(assets, id);
vm.stopPrank();

Expand All @@ -159,45 +149,42 @@ contract ERC20PriceOracleReceiptVaultreceiptVaultTest is ERC20PriceOracleReceipt
}

/// Test vault sets correct max deposit
function testMaxDeposit(uint256 fuzzedKeyAlice, string memory assetName, address vaultOracle) external {
function testMaxDeposit(uint256 fuzzedKeyAlice, string memory assetName) external {
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
vm.startPrank(alice);

ERC20PriceOracleReceiptVault vault = createVault(vaultOracle, assetName, assetName);
ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, assetName, assetName);

uint256 maxDeposit = vault.maxDeposit(alice);

assertEqUint(maxDeposit, type(uint256).max);
}

/// Test vault sets correct max Mint
function testMaxShares(uint256 fuzzedKeyAlice, string memory assetName, address vaultOracle) external {
function testMaxShares(uint256 fuzzedKeyAlice, string memory assetName) external {
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);

vm.startPrank(alice);

ERC20PriceOracleReceiptVault vault = createVault(vaultOracle, assetName, assetName);
ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, assetName, assetName);

uint256 maxMint = vault.maxMint(alice);

assertEqUint(maxMint, type(uint256).max);
}

/// Test vault receiptVaultInformation
function testReceiptVaultInformation(
uint256 fuzzedKeyAlice,
string memory assetName,
bytes memory information,
address vaultOracle
) external {
function testReceiptVaultInformation(uint256 fuzzedKeyAlice, string memory assetName, bytes memory information)
external
{
// Ensure the fuzzed key is within the valid range for secp256
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);

vm.startPrank(alice);

ERC20PriceOracleReceiptVault vault = createVault(vaultOracle, assetName, assetName);
ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, assetName, assetName);

vm.expectEmit(false, false, false, true);
emit IReceiptVaultV1.ReceiptVaultInformation(alice, information);
Expand Down

0 comments on commit 14a40cb

Please sign in to comment.