-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathFuelERC20GatewayV3.sol
104 lines (86 loc) · 3.92 KB
/
FuelERC20GatewayV3.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
// SPDX-License-Identifier: Apache 2.0
pragma solidity ^0.8.0;
import "./FuelERC20GatewayV2.sol";
/// @custom:deprecation THIS CONTRACT IS DEPRECATED. CHECK FuelERC20GatewayV4
contract FuelERC20GatewayV3 is FuelERC20GatewayV2 {
using SafeERC20Upgradeable for IERC20Upgradeable;
error GlobalDepositLimit();
error CannotDepositZero();
error CannotWithdrawZero();
error TokenIdNotAllowed();
mapping(address => uint256) public depositLimitGlobal;
mapping(address => uint256) public depositTotals;
function setGlobalDepositLimit(address token, uint256 limit) external payable virtual onlyRole(DEFAULT_ADMIN_ROLE) {
depositLimitGlobal[token] = limit;
}
/// @notice Deposits the given tokens to an account or contract on Fuel
/// @param tokenAddress Address of the token being transferred to Fuel
/// @param fuelContractId ID of the contract on Fuel that manages the deposited tokens
/// @param amount Amount of tokens to deposit
/// @param messageData The data of the message to send for deposit
function _deposit(
address tokenAddress,
bytes32 fuelContractId,
uint256 amount,
bytes memory messageData
) internal virtual override {
////////////
// Checks //
////////////
if (amount == 0) revert CannotDepositZero();
uint256 updatedDepositTotals = depositTotals[tokenAddress] + amount;
if (updatedDepositTotals > depositLimitGlobal[tokenAddress]) revert GlobalDepositLimit();
if (!isBridge[fuelContractId][tokenAddress]) revert FuelContractIsNotBridge();
/////////////
// Effects //
/////////////
_deposits[tokenAddress][fuelContractId] += amount;
depositTotals[tokenAddress] = updatedDepositTotals;
/////////////
// Actions //
/////////////
//send message to gateway on Fuel to finalize the deposit
sendMessage(CommonPredicates.CONTRACT_MESSAGE_PREDICATE, messageData);
//transfer tokens to this contract and update deposit balance
IERC20Upgradeable(tokenAddress).safeTransferFrom(msg.sender, address(this), amount);
//emit event for successful token deposit
emit Deposit(bytes32(uint256(uint160(msg.sender))), tokenAddress, fuelContractId, amount);
}
/// @notice Finalizes the withdrawal process from the Fuel side gateway contract
/// @param to Account to send withdrawn tokens to
/// @param tokenAddress Address of the token being withdrawn from Fuel
/// @param amount Amount of tokens to withdraw
/// @param tokenId Discriminator for ERC721 / ERC1155 tokens. For ERC20, it must be 0
/// @dev Made payable to reduce gas costs
function finalizeWithdrawal(
address to,
address tokenAddress,
uint256 amount,
uint256 tokenId
) external payable override whenNotPaused onlyFromPortal {
////////////
// Checks //
////////////
if (amount == 0) revert CannotWithdrawZero();
if (tokenId > 0) revert TokenIdNotAllowed();
/////////////
// Effects //
/////////////
bytes32 fuelContractId = messageSender();
//reduce deposit balance and transfer tokens (math will underflow if amount is larger than allowed)
_deposits[tokenAddress][fuelContractId] -= amount;
depositTotals[tokenAddress] -= amount;
/////////////
// Actions //
/////////////
IERC20Upgradeable(tokenAddress).safeTransfer(to, amount);
//emit event for successful token withdraw
emit Withdrawal(bytes32(uint256(uint160(to))), tokenAddress, fuelContractId, amount);
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[49] private __gap;
}