Skip to content

Commit 2bc38fa

Browse files
committed
refactor(core): consolidate WETH test mock
Replaces the no-op MockWETH stub with a functional WETH9-compatible implementation and removes the inline TestWETH from the wrapper test. The TS hardhat test that deploys MockWETH only reads its address, so upgrading the stub to working behavior is a no-op for existing callers.
1 parent 2c19d29 commit 2bc38fa

2 files changed

Lines changed: 56 additions & 93 deletions

File tree

solidity/contracts/mock/MockWETH.sol

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,75 @@ pragma solidity ^0.8.22;
33

44
import {IWETH} from "../token/interfaces/IWETH.sol";
55

6+
/// @dev Minimal WETH9-compatible mock for tests.
67
contract MockWETH is IWETH {
7-
function allowance(
8-
address _owner,
9-
address _spender
10-
) external view returns (uint256) {}
8+
string public constant name = "Wrapped Ether";
9+
string public constant symbol = "WETH";
10+
uint8 public constant decimals = 18;
1111

12-
function approve(
13-
address _spender,
14-
uint256 _amount
15-
) external returns (bool) {
16-
return true;
17-
}
12+
mapping(address => uint256) public override balanceOf;
13+
mapping(address => mapping(address => uint256)) public override allowance;
1814

19-
function balanceOf(address _account) external view returns (uint256) {
20-
return 0;
15+
event Deposit(address indexed account, uint256 amount);
16+
event Withdrawal(address indexed account, uint256 amount);
17+
18+
function totalSupply() external view override returns (uint256) {
19+
return address(this).balance;
2120
}
2221

23-
function deposit() external payable {}
22+
function deposit() public payable override {
23+
balanceOf[msg.sender] += msg.value;
24+
emit Deposit(msg.sender, msg.value);
25+
}
2426

25-
function totalSupply() external view returns (uint256) {
26-
return address(this).balance;
27+
function withdraw(uint256 amount) external override {
28+
require(balanceOf[msg.sender] >= amount, "WETH: balance");
29+
balanceOf[msg.sender] -= amount;
30+
(bool ok, ) = msg.sender.call{value: amount}("");
31+
require(ok, "WETH: send failed");
32+
emit Withdrawal(msg.sender, amount);
2733
}
2834

29-
function transfer(address to, uint256 amount) external returns (bool) {
35+
function approve(
36+
address spender,
37+
uint256 amount
38+
) external override returns (bool) {
39+
allowance[msg.sender][spender] = amount;
3040
return true;
3141
}
3242

43+
function transfer(
44+
address to,
45+
uint256 amount
46+
) external override returns (bool) {
47+
return _transfer(msg.sender, to, amount);
48+
}
49+
3350
function transferFrom(
3451
address from,
3552
address to,
3653
uint256 amount
37-
) external returns (bool) {
54+
) external override returns (bool) {
55+
uint256 allowed = allowance[from][msg.sender];
56+
if (allowed != type(uint256).max) {
57+
require(allowed >= amount, "WETH: allowance");
58+
allowance[from][msg.sender] = allowed - amount;
59+
}
60+
return _transfer(from, to, amount);
61+
}
62+
63+
function _transfer(
64+
address from,
65+
address to,
66+
uint256 amount
67+
) internal returns (bool) {
68+
require(balanceOf[from] >= amount, "WETH: balance");
69+
balanceOf[from] -= amount;
70+
balanceOf[to] += amount;
3871
return true;
3972
}
4073

41-
function withdraw(uint256 amount) external {}
74+
receive() external payable {
75+
deposit();
76+
}
4277
}

solidity/test/token/HypNativeWethWrapper.t.sol

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "forge-std/Test.sol";
55

66
import {TypeCasts} from "../../contracts/libs/TypeCasts.sol";
77
import {MockMailbox} from "../../contracts/mock/MockMailbox.sol";
8+
import {MockWETH} from "../../contracts/mock/MockWETH.sol";
89
import {TestPostDispatchHook} from "../../contracts/test/TestPostDispatchHook.sol";
910
import {HypNative} from "../../contracts/token/HypNative.sol";
1011
import {IWETH} from "../../contracts/token/interfaces/IWETH.sol";
@@ -13,79 +14,6 @@ import {HypNativeWethWrapperFactory} from "../../contracts/token/extensions/HypN
1314
import {Quote} from "../../contracts/interfaces/ITokenBridge.sol";
1415
import {Quotes} from "../../contracts/token/libs/Quotes.sol";
1516

16-
/// @dev Minimal WETH9-compatible token for unit tests.
17-
contract TestWETH is IWETH {
18-
string public constant name = "Wrapped Ether";
19-
string public constant symbol = "WETH";
20-
uint8 public constant decimals = 18;
21-
22-
mapping(address => uint256) public override balanceOf;
23-
mapping(address => mapping(address => uint256)) public override allowance;
24-
25-
event Deposit(address indexed account, uint256 amount);
26-
event Withdrawal(address indexed account, uint256 amount);
27-
28-
function totalSupply() external view override returns (uint256) {
29-
return address(this).balance;
30-
}
31-
32-
function deposit() public payable override {
33-
balanceOf[msg.sender] += msg.value;
34-
emit Deposit(msg.sender, msg.value);
35-
}
36-
37-
function withdraw(uint256 amount) external override {
38-
require(balanceOf[msg.sender] >= amount, "WETH: balance");
39-
balanceOf[msg.sender] -= amount;
40-
(bool ok, ) = msg.sender.call{value: amount}("");
41-
require(ok, "WETH: send failed");
42-
emit Withdrawal(msg.sender, amount);
43-
}
44-
45-
function approve(
46-
address spender,
47-
uint256 amount
48-
) external override returns (bool) {
49-
allowance[msg.sender][spender] = amount;
50-
return true;
51-
}
52-
53-
function transfer(
54-
address to,
55-
uint256 amount
56-
) external override returns (bool) {
57-
return _transfer(msg.sender, to, amount);
58-
}
59-
60-
function transferFrom(
61-
address from,
62-
address to,
63-
uint256 amount
64-
) external override returns (bool) {
65-
uint256 allowed = allowance[from][msg.sender];
66-
if (allowed != type(uint256).max) {
67-
require(allowed >= amount, "WETH: allowance");
68-
allowance[from][msg.sender] = allowed - amount;
69-
}
70-
return _transfer(from, to, amount);
71-
}
72-
73-
function _transfer(
74-
address from,
75-
address to,
76-
uint256 amount
77-
) internal returns (bool) {
78-
require(balanceOf[from] >= amount, "WETH: balance");
79-
balanceOf[from] -= amount;
80-
balanceOf[to] += amount;
81-
return true;
82-
}
83-
84-
receive() external payable {
85-
deposit();
86-
}
87-
}
88-
8917
contract HypNativeWethWrapperTest is Test {
9018
using TypeCasts for address;
9119
using Quotes for Quote[];
@@ -101,7 +29,7 @@ contract HypNativeWethWrapperTest is Test {
10129
TestPostDispatchHook internal noopHook;
10230
HypNative internal localRouter;
10331
HypNative internal remoteRouter;
104-
TestWETH internal weth;
32+
MockWETH internal weth;
10533
HypNativeWethWrapper internal wrapper;
10634

10735
function setUp() public {
@@ -130,7 +58,7 @@ contract HypNativeWethWrapperTest is Test {
13058
address(localRouter).addressToBytes32()
13159
);
13260

133-
weth = new TestWETH();
61+
weth = new MockWETH();
13462
wrapper = new HypNativeWethWrapper(IWETH(address(weth)), localRouter);
13563

13664
vm.label(ALICE, "ALICE");

0 commit comments

Comments
 (0)