Skip to content

chore(test): migrate GPv2SafeERC20 tests #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions src/contracts/test/GPv2SafeERC20TestInterface.sol

This file was deleted.

255 changes: 0 additions & 255 deletions test/GPv2SafeERC20.test.ts

This file was deleted.

28 changes: 28 additions & 0 deletions test/GPv2SafeERC20/Helper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
pragma solidity ^0.8.0;

import {Test} from "forge-std/Test.sol";

import {IERC20} from "src/contracts/interfaces/IERC20.sol";
import {GPv2SafeERC20} from "src/contracts/libraries/GPv2SafeERC20.sol";

contract Harness {
using GPv2SafeERC20 for IERC20;

function transfer(IERC20 token, address to, uint256 value) public {
token.safeTransfer(to, value);
}

function transferFrom(IERC20 token, address from, address to, uint256 value) public {
token.safeTransferFrom(from, to, value);
}
}

contract Helper is Test {
Harness executor;
address recipient = makeAddr("TestHelper: recipient");

function setUp() public {
executor = new Harness();
}
}
87 changes: 87 additions & 0 deletions test/GPv2SafeERC20/Transfer.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
pragma solidity ^0.8.0;

import {IERC20} from "src/contracts/interfaces/IERC20.sol";

import {Helper} from "./Helper.sol";

contract Transfer is Helper {
function test_succeeds_when_internal_call_succeeds() public {
uint256 amount = 13.37 ether;

IERC20 standardToken = IERC20(makeAddr("standard token"));
vm.mockCall(address(standardToken), abi.encodeCall(IERC20.transfer, (recipient, amount)), abi.encode(true));

executor.transfer(standardToken, recipient, amount);
}

function test_reverts_on_failed_internal_call() public {
uint256 amount = 42 ether;

IERC20 revertingToken = IERC20(makeAddr("reverting token"));

vm.mockCallRevert(
address(revertingToken), abi.encodeCall(IERC20.transfer, (recipient, amount)), bytes("test error")
);

vm.expectRevert("test error");
executor.transfer(revertingToken, recipient, amount);
}

function test_reverts_when_calling_a_non_contract() public {
uint256 amount = 4.2 ether;

IERC20 standardToken = IERC20(makeAddr("address without code"));

vm.expectRevert("GPv2: not a contract");
executor.transfer(standardToken, recipient, amount);
}

function test_does_not_revert_when_the_internal_call_has_no_return_data() public {
uint256 amount = 13.37 ether;

IERC20 tokenNoReturnValue = IERC20(makeAddr("does not return on transfer"));

vm.mockCall(address(tokenNoReturnValue), abi.encodeCall(IERC20.transfer, (recipient, amount)), hex"");

executor.transfer(tokenNoReturnValue, recipient, amount);
}

function test_reverts_when_the_internal_call_returns_false() public {
uint256 amount = 13.37 ether;

IERC20 tokenReturnsFalse = IERC20(makeAddr("returns false on transfer"));

vm.mockCall(address(tokenReturnsFalse), abi.encodeCall(IERC20.transfer, (recipient, amount)), abi.encode(false));

vm.expectRevert("GPv2: failed transfer");
executor.transfer(tokenReturnsFalse, recipient, amount);
}

function test_reverts_when_too_much_data_is_returned() public {
uint256 amount = 1 ether;

IERC20 tokenReturnsTooMuchData = IERC20(makeAddr("returns too much data transfer"));

vm.mockCall(
address(tokenReturnsTooMuchData),
abi.encodeCall(IERC20.transfer, (recipient, amount)),
abi.encodePacked(new bytes(256))
);

vm.expectRevert("GPv2: malformed transfer result");
executor.transfer(tokenReturnsTooMuchData, recipient, amount);
}

function test_coerces_invalid_abi_encoded_bool() public {
uint256 amount = 1 ether;

IERC20 tokenReturnsLargeUint = IERC20(makeAddr("returns uint256 larger than 1"));

vm.mockCall(
address(tokenReturnsLargeUint), abi.encodeCall(IERC20.transfer, (recipient, amount)), abi.encode(42)
);

executor.transfer(tokenReturnsLargeUint, recipient, amount);
}
}
Loading