-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a handshake protocol that ensures no funds are lost when depositing in the canonical bridge. --------- Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com>
- Loading branch information
Showing
19 changed files
with
541 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@fuel-bridge/solidity-contracts': minor | ||
'@fuel-bridge/fungible-token': minor | ||
'@fuel-bridge/test-utils': minor | ||
--- | ||
|
||
Introduces a handshake protocol to avoid loss of funds while bridging assets from L1 to L2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
|
||
/// @dev The only purpose of this contract is to be copied during the Dockerfile build | ||
/// so that hardhat downloads the compiler and it gets cached | ||
contract Placeholder {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/solidity-contracts/contracts/messaging/FuelMessagesEnabled.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/solidity-contracts/contracts/messaging/FuelMessagesEnabledUpgradeable.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/solidity-contracts/contracts/messaging/gateway/FuelBridgeBase.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/solidity-contracts/contracts/messaging/gateway/v2/FuelBridgeBaseV2.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity 0.8.9; | ||
|
||
abstract contract FuelBridgeBaseV2 { | ||
error FuelContractIsNotBridge(); | ||
|
||
event ReceiverRegistered( | ||
bytes32 indexed fuelContractId, | ||
address indexed tokenAddress | ||
); | ||
|
||
mapping(bytes32 => mapping(address => bool)) public isBridge; | ||
|
||
/// @notice Accepts a message from a fuel entity to acknowledge it can receive tokens | ||
/// @param tokenAddress The token address that the fuel entity can receive | ||
/// @dev Made payable to reduce gas costs | ||
/// @dev funcSig: aec97dc6 => registerAsReceiver(address) | ||
function registerAsReceiver(address tokenAddress) external payable virtual; | ||
|
||
/** | ||
* @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; | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/solidity-contracts/contracts/messaging/gateway/v2/FuelERC20GatewayV2.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity 0.8.9; | ||
|
||
import '../FuelERC20Gateway.sol'; | ||
import './FuelBridgeBaseV2.sol'; | ||
|
||
contract FuelERC20GatewayV2 is FuelERC20Gateway, FuelBridgeBaseV2 { | ||
using SafeERC20Upgradeable for IERC20Upgradeable; | ||
|
||
function registerAsReceiver(address tokenAddress) external payable virtual override onlyFromPortal() { | ||
bytes32 sender = messageSender(); | ||
|
||
isBridge[sender][tokenAddress] = true; | ||
|
||
emit ReceiverRegistered(sender, tokenAddress); | ||
} | ||
|
||
/// @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 { | ||
require(amount > 0, "Cannot deposit zero"); | ||
if (!isBridge[fuelContractId][tokenAddress]) revert FuelContractIsNotBridge(); | ||
|
||
//transfer tokens to this contract and update deposit balance | ||
IERC20Upgradeable(tokenAddress).safeTransferFrom(msg.sender, address(this), amount); | ||
_deposits[tokenAddress][fuelContractId] = _deposits[tokenAddress][fuelContractId] + amount; | ||
|
||
//send message to gateway on Fuel to finalize the deposit | ||
sendMessage(CommonPredicates.CONTRACT_MESSAGE_PREDICATE, messageData); | ||
|
||
//emit event for successful token deposit | ||
emit Deposit(bytes32(uint256(uint160(msg.sender))), 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; | ||
} |
48 changes: 48 additions & 0 deletions
48
packages/solidity-contracts/contracts/messaging/gateway/v2/FuelERC721GatewayV2.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity 0.8.9; | ||
|
||
import '../FuelERC721Gateway.sol'; | ||
import './FuelBridgeBaseV2.sol'; | ||
|
||
contract FuelERC721GatewayV2 is FuelERC721Gateway, FuelBridgeBaseV2 { | ||
function registerAsReceiver(address tokenAddress) external payable virtual override onlyFromPortal() { | ||
bytes32 sender = messageSender(); | ||
|
||
isBridge[sender][tokenAddress] = true; | ||
|
||
emit ReceiverRegistered(sender, tokenAddress); | ||
} | ||
|
||
/// @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 tokenId tokenId to deposit | ||
/// @param messageData The data of the message to send for deposit | ||
function _deposit( | ||
address tokenAddress, | ||
bytes32 fuelContractId, | ||
uint256 tokenId, | ||
bytes memory messageData | ||
) internal virtual override { | ||
// TODO: this check might be unnecessary. If the token is conformant to ERC721 | ||
// it should not be possible to deposit the same token again | ||
require(_deposits[tokenAddress][tokenId] == 0, "tokenId is already owned by another fuel bridge"); | ||
if (!isBridge[fuelContractId][tokenAddress]) revert FuelContractIsNotBridge(); | ||
|
||
_deposits[tokenAddress][tokenId] = fuelContractId; | ||
|
||
//send message to gateway on Fuel to finalize the deposit | ||
sendMessage(CommonPredicates.CONTRACT_MESSAGE_PREDICATE, messageData); | ||
|
||
IERC721Upgradeable(tokenAddress).transferFrom(msg.sender, address(this), tokenId); | ||
//emit event for successful token deposit | ||
emit Deposit(bytes32(uint256(uint160(msg.sender))), tokenAddress, fuelContractId, tokenId); | ||
} | ||
|
||
/** | ||
* @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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.