-
Notifications
You must be signed in to change notification settings - Fork 403
/
HypNativeScaled.t.sol
217 lines (180 loc) · 7.04 KB
/
HypNativeScaled.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// SPDX-License-Identifier: Apache-2.0
pragma solidity >=0.8.0;
import "forge-std/Test.sol";
import {TransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import {TestPostDispatchHook} from "../../contracts/test/TestPostDispatchHook.sol";
import {HypNativeScaled} from "../../contracts/token/extensions/HypNativeScaled.sol";
import {HypERC20} from "../../contracts/token/HypERC20.sol";
import {HypNative} from "../../contracts/token/HypNative.sol";
import {TypeCasts} from "../../contracts/libs/TypeCasts.sol";
import {MockHyperlaneEnvironment} from "../../contracts/mock/MockHyperlaneEnvironment.sol";
contract HypNativeScaledTest is Test {
uint32 nativeDomain = 1;
uint32 synthDomain = 2;
address internal constant ALICE = address(0x1);
uint8 decimals = 9;
uint256 mintAmount = 123456789;
uint256 nativeDecimals = 18;
uint256 scale = 10 ** (nativeDecimals - decimals);
event Donation(address indexed sender, uint256 amount);
event SentTransferRemote(
uint32 indexed destination,
bytes32 indexed recipient,
uint256 amount
);
event ReceivedTransferRemote(
uint32 indexed origin,
bytes32 indexed recipient,
uint256 amount
);
HypNativeScaled native;
HypERC20 synth;
MockHyperlaneEnvironment environment;
function setUp() public {
environment = new MockHyperlaneEnvironment(synthDomain, nativeDomain);
HypERC20 implementationSynth = new HypERC20(
decimals,
address(environment.mailboxes(synthDomain))
);
TransparentUpgradeableProxy proxySynth = new TransparentUpgradeableProxy(
address(implementationSynth),
address(9),
abi.encodeWithSelector(
HypERC20.initialize.selector,
mintAmount * (10 ** decimals),
"Zebec BSC Token",
"ZBC",
address(0),
address(0),
address(this)
)
);
synth = HypERC20(address(proxySynth));
HypNativeScaled implementationNative = new HypNativeScaled(
scale,
address(environment.mailboxes(nativeDomain))
);
TransparentUpgradeableProxy proxyNative = new TransparentUpgradeableProxy(
address(implementationNative),
address(9),
abi.encodeWithSelector(
HypNative.initialize.selector,
address(0),
address(0),
address(this)
)
);
native = HypNativeScaled(payable(address(proxyNative)));
native.enrollRemoteRouter(
synthDomain,
TypeCasts.addressToBytes32(address(synth))
);
synth.enrollRemoteRouter(
nativeDomain,
TypeCasts.addressToBytes32(address(native))
);
}
function test_constructor() public {
assertEq(native.scale(), scale);
}
uint256 receivedValue;
receive() external payable {
receivedValue = msg.value;
}
function test_receive(uint256 amount) public {
vm.assume(amount < address(this).balance);
vm.expectEmit(true, true, true, true);
emit Donation(address(this), amount);
(bool success, bytes memory returnData) = address(native).call{
value: amount
}("");
assert(success);
assertEq(returnData.length, 0);
}
function test_handle(uint256 amount) public {
vm.assume(amount <= mintAmount);
uint256 synthAmount = amount * (10 ** decimals);
uint256 nativeAmount = amount * (10 ** nativeDecimals);
vm.deal(address(native), nativeAmount);
bytes32 recipient = TypeCasts.addressToBytes32(address(this));
synth.transferRemote(nativeDomain, recipient, synthAmount);
vm.expectEmit(true, true, true, true);
emit ReceivedTransferRemote(synthDomain, recipient, synthAmount);
environment.processNextPendingMessage();
assertEq(receivedValue, nativeAmount);
}
function test_handle_reverts_whenAmountExceedsSupply(
uint256 amount
) public {
vm.assume(amount <= mintAmount);
bytes32 recipient = TypeCasts.addressToBytes32(address(this));
synth.transferRemote(nativeDomain, recipient, amount);
uint256 nativeValue = amount * scale;
vm.deal(address(native), nativeValue / 2);
if (amount > 0) {
vm.expectRevert(bytes("Address: insufficient balance"));
}
environment.processNextPendingMessage();
}
function test_tranferRemote(uint256 amount) public {
vm.assume(amount <= mintAmount);
uint256 nativeValue = amount * (10 ** nativeDecimals);
uint256 synthAmount = amount * (10 ** decimals);
address recipient = address(0xdeadbeef);
bytes32 bRecipient = TypeCasts.addressToBytes32(recipient);
vm.deal(address(this), nativeValue);
vm.expectEmit(true, true, true, true);
emit SentTransferRemote(synthDomain, bRecipient, synthAmount);
native.transferRemote{value: nativeValue}(
synthDomain,
bRecipient,
nativeValue
);
environment.processNextPendingMessageFromDestination();
assertEq(synth.balanceOf(recipient), synthAmount);
}
function testTransfer_withHookSpecified(
uint256 amount,
bytes calldata metadata
) public {
vm.assume(amount <= mintAmount);
uint256 nativeValue = amount * (10 ** nativeDecimals);
uint256 synthAmount = amount * (10 ** decimals);
address recipient = address(0xdeadbeef);
bytes32 bRecipient = TypeCasts.addressToBytes32(recipient);
TestPostDispatchHook hook = new TestPostDispatchHook();
vm.deal(address(this), nativeValue);
vm.expectEmit(true, true, true, true);
emit SentTransferRemote(synthDomain, bRecipient, synthAmount);
native.transferRemote{value: nativeValue}(
synthDomain,
bRecipient,
nativeValue,
metadata,
address(hook)
);
environment.processNextPendingMessageFromDestination();
assertEq(synth.balanceOf(recipient), synthAmount);
}
function test_transferRemote_reverts_whenAmountExceedsValue(
uint256 nativeValue
) public {
vm.assume(nativeValue < address(this).balance);
address recipient = address(0xdeadbeef);
bytes32 bRecipient = TypeCasts.addressToBytes32(recipient);
vm.expectRevert("Native: amount exceeds msg.value");
native.transferRemote{value: nativeValue}(
synthDomain,
bRecipient,
nativeValue + 1
);
vm.expectRevert("Native: amount exceeds msg.value");
native.transferRemote{value: nativeValue}(
synthDomain,
bRecipient,
nativeValue + 1,
bytes(""),
address(0)
);
}
}