I'm writing a solidity test script. foundry
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
contract TestERC20Helper is ERC20Burnable {
constructor(
string memory name,
string memory symbol,
uint256 initialSupply,
address owner
) ERC20(name, symbol) {
_mint(owner, initialSupply);
}
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "forge-std/Script.sol";
import "forge-std/console.sol";
import "forge-std/Test.sol";
import "@test/TestERC20Helper.t.sol";
//import "@test/TokenTest_2.sol";
contract TokenTest is Script, Test {
address public constant EOAowner = address(0xa0Ee7A142d267C1f36714E4a8F75612F20a79720);
address public constant EARNINGS_RECEIVER = address(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266);
using SafeERC20 for IERC20;
TestERC20Helper public erc20TestToken;
function test_IERC20_safeTransferFrom() public {
vm.startPrank(EOAowner);
console.log("test_1 msg.sender:", msg.sender);
// TokenTest_2 test2 = new TokenTest_2();
// test2.test_change_msg_sender();
console.log("test_1 msg.sender:", msg.sender);
string memory token_name = "TestToken";
string memory token_symbol = "TTK";
erc20TestToken = new TestERC20Helper("TestToken", "TTK", 12345678910 * 1e18, EOAowner);
console.log("test_1 erc20TestToken:", address(erc20TestToken));
IERC20 tokenAddressErc20 = IERC20(address(erc20TestToken));
vm.deal(address(EOAowner), 1111 ether);
uint256 before_EOAowner_balance = tokenAddressErc20.balanceOf(EOAowner);
console.log("test_1 before_EOAowner_balance :", before_EOAowner_balance);
uint256 before_EARNINGS_RECEIVER_balance = tokenAddressErc20.balanceOf(EARNINGS_RECEIVER);
console.log("test_1 before_EARNINGS_RECEIVER_balance :", before_EARNINGS_RECEIVER_balance);
bool before_approveFlag = tokenAddressErc20.approve(EARNINGS_RECEIVER, 1 * 1e19);
console.log("test_1 before_approveFlag:", before_approveFlag);
uint256 before_allowance = tokenAddressErc20.allowance(EOAowner, EARNINGS_RECEIVER);
console.log("test_1 before_allowance:", before_allowance);
tokenAddressErc20.safeTransferFrom(EOAowner, EARNINGS_RECEIVER, 1 * 1e18);
uint256 after_allowance = tokenAddressErc20.allowance(EOAowner, EARNINGS_RECEIVER);
console.log("test_1 after_allowance:", after_allowance);
uint256 after_EOAowner_balance = tokenAddressErc20.balanceOf(EOAowner);
console.log("test_1 after_EOAowner_balance :", after_EOAowner_balance);
uint256 after_EARNINGS_RECEIVER_balance = tokenAddressErc20.balanceOf(EARNINGS_RECEIVER);
console.log("test_1 after_EARNINGS_RECEIVER_balance :", after_EARNINGS_RECEIVER_balance);
vm.stopPrank();
}
}
error
Ran 1 test for test/TokenTest.sol:TokenTest
[FAIL. Reason: ERC20InsufficientAllowance(0xa0Ee7A142d267C1f36714E4a8F75612F20a79720, 0, 1000000000000000000 [1e18])] test_IERC20_safeTransferFrom() (gas: 564448)
Logs:
test_1 msg.sender: 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
test_1 msg.sender: 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
test_1 erc20TestToken: 0x700b6A60ce7EaaEA56F065753d8dcB9653dbAD35
test_1 before_EOAowner_balance : 12345678910000000000000000000
test_1 before_EARNINGS_RECEIVER_balance : 0
test_1 before_approveFlag: true
test_1 before_allowance: 10000000000000000000
Traces:
[564448] TokenTest::test_IERC20_safeTransferFrom()
├─ [0] VM::startPrank(0xa0Ee7A142d267C1f36714E4a8F75612F20a79720)
│ └─ ← [Return]
├─ [0] console::log("test_1 msg.sender:", DefaultSender: [0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38]) [staticcall]
│ └─ ← [Stop]
├─ [0] console::log("test_1 msg.sender:", DefaultSender: [0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38]) [staticcall]
│ └─ ← [Stop]
├─ [481979] → new TestERC20Helper@0x700b6A60ce7EaaEA56F065753d8dcB9653dbAD35
│ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720, value: 12345678910000000000000000000 [1.234e28])
│ └─ ← [Return] 1947 bytes of code
├─ [0] console::log("test_1 erc20TestToken:", TestERC20Helper: [0x700b6A60ce7EaaEA56F065753d8dcB9653dbAD35]) [staticcall]
│ └─ ← [Stop]
├─ [0] VM::deal(0xa0Ee7A142d267C1f36714E4a8F75612F20a79720, 1111000000000000000000 [1.111e21])
│ └─ ← [Return]
├─ [552] TestERC20Helper::balanceOf(0xa0Ee7A142d267C1f36714E4a8F75612F20a79720) [staticcall]
│ └─ ← [Return] 12345678910000000000000000000 [1.234e28]
├─ [0] console::log("test_1 before_EOAowner_balance :", 12345678910000000000000000000 [1.234e28]) [staticcall]
│ └─ ← [Stop]
├─ [2552] TestERC20Helper::balanceOf(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266) [staticcall]
│ └─ ← [Return] 0
├─ [0] console::log("test_1 before_EARNINGS_RECEIVER_balance :", 0) [staticcall]
│ └─ ← [Stop]
├─ [24339] TestERC20Helper::approve(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266, 10000000000000000000 [1e19])
│ ├─ emit Approval(owner: 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720, spender: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266, value: 10000000000000000000 [1e19])
│ └─ ← [Return] true
├─ [0] console::log("test_1 before_approveFlag:", true) [staticcall]
│ └─ ← [Stop]
├─ [783] TestERC20Helper::allowance(0xa0Ee7A142d267C1f36714E4a8F75612F20a79720, 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266) [staticcall]
│ └─ ← [Return] 10000000000000000000 [1e19]
├─ [0] console::log("test_1 before_allowance:", 10000000000000000000 [1e19]) [staticcall]
│ └─ ← [Stop]
├─ [2839] TestERC20Helper::transferFrom(0xa0Ee7A142d267C1f36714E4a8F75612F20a79720, 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266, 1000000000000000000 [1e18])
│ └─ ← [Revert] ERC20InsufficientAllowance(0xa0Ee7A142d267C1f36714E4a8F75612F20a79720, 0, 1000000000000000000 [1e18])
└─ ← [Revert] ERC20InsufficientAllowance(0xa0Ee7A142d267C1f36714E4a8F75612F20a79720, 0, 1000000000000000000 [1e18])
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 820.40µs (426.90µs CPU time)
Ran 1 test suite in 709.93ms (820.40µs CPU time): 0 tests passed, 1 failed, 0 skipped (1 total tests)
Failing tests:
Encountered 1 failing test in test/TokenTest.sol:TokenTest
[FAIL. Reason: ERC20InsufficientAllowance(0xa0Ee7A142d267C1f36714E4a8F75612F20a79720, 0, 1000000000000000000 [1e18])] test_IERC20_safeTransferFrom() (gas: 564448)
Encountered a total of 1 failing tests, 0 tests succeeded