Skip to content

Commit

Permalink
Add testERC20TransferFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
ninokeldishvili committed Nov 6, 2024
1 parent 2c84898 commit 5c4e3ac
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/src/concrete/vault/ERC20Standard.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ contract ERC20StandardTest is ERC20PriceOracleReceiptVaultTest {
{
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);
vm.assume(alice != bob);
amount = bound(amount, 1, type(uint128).max);

oraclePrice = bound(oraclePrice, 0.01e18, 100e18);
Expand Down Expand Up @@ -115,6 +116,8 @@ contract ERC20StandardTest is ERC20PriceOracleReceiptVaultTest {
function testERC20AllowanceAndApprove(uint256 fuzzedKeyAlice, uint256 fuzzedKeyBob, uint256 amount) external {
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);
vm.assume(alice != bob);

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

ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, "Test Token", "TST");
Expand All @@ -126,4 +129,50 @@ contract ERC20StandardTest is ERC20PriceOracleReceiptVaultTest {
// Check allowance
assertEq(vault.allowance(alice, bob), amount);
}

// Test ERC20 transferFrom()
function testERC20TransferFrom(
uint256 fuzzedKeyAlice,
uint256 fuzzedKeyBob,
uint256 amount,
uint256 transferFromAmount,
uint256 oraclePrice
) external {
address alice = vm.addr((fuzzedKeyAlice % (SECP256K1_ORDER - 1)) + 1);
address bob = vm.addr((fuzzedKeyBob % (SECP256K1_ORDER - 1)) + 1);
vm.assume(alice != bob);
amount = bound(amount, 1, type(uint128).max);
transferFromAmount = bound(transferFromAmount, 1, type(uint128).max);

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

ERC20PriceOracleReceiptVault vault = createVault(iVaultOracle, "Test Token", "TST");

vm.startPrank(alice);
vm.mockCall(address(iAsset), abi.encodeWithSelector(IERC20.balanceOf.selector, alice), abi.encode(amount));
vm.mockCall(
address(iAsset),
abi.encodeWithSelector(IERC20.transferFrom.selector, alice, vault, amount),
abi.encode(true)
);

uint256 expectedShares = amount.fixedPointMul(oraclePrice, Math.Rounding.Down);
vm.assume(transferFromAmount < expectedShares);

vault.deposit(amount, alice, oraclePrice, bytes(""));

uint256 aliceBalanceBeforeTransfer = vault.balanceOf(alice);

vault.approve(bob, expectedShares);

vm.stopPrank();
vm.startPrank(bob);

// Bob transfers from Alice's account to his own
vault.transferFrom(alice, bob, transferFromAmount);

assertEqUint(vault.balanceOf(alice), aliceBalanceBeforeTransfer - transferFromAmount);
assertEqUint(vault.balanceOf(bob), transferFromAmount);
}
}

0 comments on commit 5c4e3ac

Please sign in to comment.