-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUniversalBridgeV1.sol
280 lines (234 loc) · 10.3 KB
/
UniversalBridgeV1.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.22;
/// @author thirdweb
import { SafeTransferLib } from "lib/solady/src/utils/SafeTransferLib.sol";
import { ReentrancyGuard } from "lib/solady/src/utils/ReentrancyGuard.sol";
import { Ownable } from "lib/solady/src/auth/Ownable.sol";
import { UUPSUpgradeable } from "lib/solady/src/utils/UUPSUpgradeable.sol";
import { Initializable } from "lib/solady/src/utils/Initializable.sol";
library UniversalBridgeStorage {
/// @custom:storage-location erc7201:universal.bridge
bytes32 public constant UNIVERSAL_BRIDGE_STORAGE_POSITION =
keccak256(abi.encode(uint256(keccak256("tw.universal.bridge")) - 1)) & ~bytes32(uint256(0xff));
struct Data {
/// @dev Mapping from pay request UID => whether the pay request is processed.
mapping(bytes32 => bool) processed;
/// @dev Mapping from forward address or token address => whether restricted.
mapping(address => bool) isRestricted;
/// @dev protocol fee bps, capped at 300 bps (3%)
uint256 protocolFeeBps;
/// @dev protocol fee recipient address
address protocolFeeRecipient;
/// @dev whether the bridge is paused
bool isPaused;
}
function data() internal pure returns (Data storage data_) {
bytes32 position = UNIVERSAL_BRIDGE_STORAGE_POSITION;
assembly {
data_.slot := position
}
}
}
contract UniversalBridgeV1 is Initializable, UUPSUpgradeable, Ownable, ReentrancyGuard {
/*///////////////////////////////////////////////////////////////
State, constants, structs
//////////////////////////////////////////////////////////////*/
address private constant NATIVE_TOKEN_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
uint256 private constant MAX_PROTOCOL_FEE_BPS = 300; // 3%
/*///////////////////////////////////////////////////////////////
Events
//////////////////////////////////////////////////////////////*/
event TransactionInitiated(
address indexed sender,
bytes32 indexed transactionId,
address tokenAddress,
uint256 tokenAmount,
address developerFeeRecipient,
uint256 developerFeeBps,
bytes extraData
);
/*///////////////////////////////////////////////////////////////
Errors
//////////////////////////////////////////////////////////////*/
error UniversalBridgeMismatchedValue(uint256 expected, uint256 actual);
error UniversalBridgeInvalidAmount(uint256 amount);
error UniversalBridgeFailedToForward();
error UniversalBridgeMsgValueNotZero();
error UniversalBridgeInvalidFeeBps();
error UniversalBridgeZeroAddress();
error UniversalBridgePaused();
error UniversalBridgeRestrictedAddress();
constructor() {
_disableInitializers();
}
function initialize(
address _owner,
address payable _protocolFeeRecipient,
uint256 _protocolFeeBps
) external initializer {
_initializeOwner(_owner);
_setProtocolFeeInfo(_protocolFeeRecipient, _protocolFeeBps);
}
/*///////////////////////////////////////////////////////////////
External / public functions
//////////////////////////////////////////////////////////////*/
/// @notice check if transaction id has been used / processed
function isProcessed(bytes32 transactionId) external view returns (bool) {
return _universalBridgeStorage().processed[transactionId];
}
/// @notice some bridges may refund need a way to get funds back to user
function withdrawTo(
address tokenAddress,
uint256 tokenAmount,
address payable receiver
) public nonReentrant onlyOwner {
if (_isNativeToken(tokenAddress)) {
SafeTransferLib.safeTransferETH(receiver, tokenAmount);
} else {
SafeTransferLib.safeTransfer(tokenAddress, receiver, tokenAmount);
}
}
function setProtocolFeeInfo(address payable feeRecipient, uint256 feeBps) external onlyOwner {
_setProtocolFeeInfo(feeRecipient, feeBps);
}
function pause(bool _pause) external onlyOwner {
_universalBridgeStorage().isPaused = _pause;
}
function restrictAddress(address _target, bool _restrict) external onlyOwner {
_universalBridgeStorage().isRestricted[_target] = _restrict;
}
function getProtocolFeeInfo() external view returns (address feeRecipient, uint256 feeBps) {
feeRecipient = _universalBridgeStorage().protocolFeeRecipient;
feeBps = _universalBridgeStorage().protocolFeeBps;
}
function isPaused() external view returns (bool) {
return _universalBridgeStorage().isPaused;
}
function isRestricted(address _target) external view returns (bool) {
return _universalBridgeStorage().isRestricted[_target];
}
/**
@notice
The purpose of initiateTransaction is to be the entrypoint for all thirdweb pay swap / bridge
transactions. This function will allow us to standardize the logging and fee splitting across all providers.
*/
function initiateTransaction(
bytes32 transactionId,
address tokenAddress,
uint256 tokenAmount,
address payable forwardAddress,
address payable spenderAddress,
address payable developerFeeRecipient,
uint256 developerFeeBps,
bytes calldata callData,
bytes calldata extraData
) external payable nonReentrant onlyProxy {
if (_universalBridgeStorage().isPaused) {
revert UniversalBridgePaused();
}
if (
_universalBridgeStorage().isRestricted[forwardAddress] ||
_universalBridgeStorage().isRestricted[tokenAddress]
) {
revert UniversalBridgeRestrictedAddress();
}
// verify amount
if (tokenAmount == 0) {
revert UniversalBridgeInvalidAmount(tokenAmount);
}
// mark the pay request as processed
_universalBridgeStorage().processed[transactionId] = true;
uint256 sendValue = msg.value; // includes bridge fee etc. (if any)
// distribute fees
uint256 totalFeeAmount = _distributeFees(tokenAddress, tokenAmount, developerFeeRecipient, developerFeeBps);
if (_isNativeToken(tokenAddress)) {
sendValue = msg.value - totalFeeAmount;
if (sendValue < tokenAmount) {
revert UniversalBridgeMismatchedValue(tokenAmount, sendValue);
}
_call(forwardAddress, sendValue, callData); // calldata empty for direct transfer
} else if (callData.length == 0) {
if (msg.value != 0) {
revert UniversalBridgeMsgValueNotZero();
}
SafeTransferLib.safeTransferFrom(tokenAddress, msg.sender, forwardAddress, tokenAmount);
} else {
// pull user funds
SafeTransferLib.safeTransferFrom(tokenAddress, msg.sender, address(this), tokenAmount);
// approve to spender address and call forward address -- both will be same in most cases
SafeTransferLib.safeApprove(tokenAddress, spenderAddress, tokenAmount);
_call(forwardAddress, sendValue, callData);
}
emit TransactionInitiated(
msg.sender,
transactionId,
tokenAddress,
tokenAmount,
developerFeeRecipient,
developerFeeBps,
extraData
);
}
function _call(address forwardAddress, uint256 sendValue, bytes memory callData) internal {
(bool success, bytes memory response) = forwardAddress.call{ value: sendValue }(callData);
if (!success) {
// If there is return data, the delegate call reverted with a reason or a custom error, which we bubble up.
if (response.length > 0) {
assembly {
let returndata_size := mload(response)
revert(add(32, response), returndata_size)
}
} else {
revert UniversalBridgeFailedToForward();
}
}
}
/*///////////////////////////////////////////////////////////////
Internal functions
//////////////////////////////////////////////////////////////*/
function _distributeFees(
address tokenAddress,
uint256 tokenAmount,
address developerFeeRecipient,
uint256 developerFeeBps
) private returns (uint256) {
address protocolFeeRecipient = _universalBridgeStorage().protocolFeeRecipient;
uint256 protocolFeeBps = _universalBridgeStorage().protocolFeeBps;
uint256 protocolFee = (tokenAmount * protocolFeeBps) / 10_000;
uint256 developerFee = (tokenAmount * developerFeeBps) / 10_000;
uint256 totalFeeAmount = protocolFee + developerFee;
if (_isNativeToken(tokenAddress)) {
if (protocolFee != 0) {
SafeTransferLib.safeTransferETH(protocolFeeRecipient, protocolFee);
}
if (developerFee != 0) {
SafeTransferLib.safeTransferETH(developerFeeRecipient, developerFee);
}
} else {
if (protocolFee != 0) {
SafeTransferLib.safeTransferFrom(tokenAddress, msg.sender, protocolFeeRecipient, protocolFee);
}
if (developerFee != 0) {
SafeTransferLib.safeTransferFrom(tokenAddress, msg.sender, developerFeeRecipient, developerFee);
}
}
return totalFeeAmount;
}
function _setProtocolFeeInfo(address payable feeRecipient, uint256 feeBps) internal {
if (feeRecipient == address(0)) {
revert UniversalBridgeZeroAddress();
}
if (feeBps > MAX_PROTOCOL_FEE_BPS) {
revert UniversalBridgeInvalidFeeBps();
}
_universalBridgeStorage().protocolFeeRecipient = feeRecipient;
_universalBridgeStorage().protocolFeeBps = feeBps;
}
function _isNativeToken(address tokenAddress) private pure returns (bool) {
return tokenAddress == NATIVE_TOKEN_ADDRESS;
}
function _authorizeUpgrade(address) internal override onlyOwner {}
function _universalBridgeStorage() internal view returns (UniversalBridgeStorage.Data storage) {
return UniversalBridgeStorage.data();
}
}