You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
195 lines
7.3 KiB
195 lines
7.3 KiB
4 years ago
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||
|
pragma solidity >=0.6.11;
|
||
|
|
||
3 years ago
|
// ============ Internal Imports ============
|
||
4 years ago
|
import {TokenRegistry} from "./TokenRegistry.sol";
|
||
3 years ago
|
import {Router} from "../Router.sol";
|
||
|
import {IBridgeToken} from "../../interfaces/bridge/IBridgeToken.sol";
|
||
|
import {BridgeMessage} from "./BridgeMessage.sol";
|
||
|
// ============ External Imports ============
|
||
4 years ago
|
import {Home} from "@celo-org/optics-sol/contracts/Home.sol";
|
||
4 years ago
|
import {
|
||
3 years ago
|
TypeCasts
|
||
4 years ago
|
} from "@celo-org/optics-sol/contracts/XAppConnectionManager.sol";
|
||
4 years ago
|
import {TypedMemView} from "@summa-tx/memview-sol/contracts/TypedMemView.sol";
|
||
3 years ago
|
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
||
4 years ago
|
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @title BridgeRouter
|
||
|
*/
|
||
4 years ago
|
contract BridgeRouter is Router, TokenRegistry {
|
||
4 years ago
|
using TypedMemView for bytes;
|
||
|
using TypedMemView for bytes29;
|
||
|
using BridgeMessage for bytes29;
|
||
|
using SafeERC20 for IERC20;
|
||
|
|
||
3 years ago
|
// ======== Constructor =========
|
||
|
|
||
4 years ago
|
constructor(address _xAppConnectionManager)
|
||
|
TokenRegistry(_xAppConnectionManager)
|
||
4 years ago
|
{} // solhint-disable-line no-empty-blocks
|
||
4 years ago
|
|
||
3 years ago
|
// ======== External: Handle =========
|
||
|
|
||
4 years ago
|
/**
|
||
|
* @notice Handles an incoming message
|
||
|
* @param _origin The origin domain
|
||
|
* @param _sender The sender address
|
||
|
* @param _message The message
|
||
|
*/
|
||
4 years ago
|
function handle(
|
||
|
uint32 _origin,
|
||
|
bytes32 _sender,
|
||
|
bytes memory _message
|
||
3 years ago
|
) external override onlyReplica onlyRemoteRouter(_origin, _sender) {
|
||
3 years ago
|
// parse tokenId and action from message
|
||
4 years ago
|
bytes29 _msg = _message.ref(0).mustBeMessage();
|
||
|
bytes29 _tokenId = _msg.tokenId();
|
||
|
bytes29 _action = _msg.action();
|
||
3 years ago
|
// handle message based on the intended action
|
||
4 years ago
|
if (_action.isTransfer()) {
|
||
3 years ago
|
_handleTransfer(_tokenId, _action);
|
||
|
} else if (_action.isDetails()) {
|
||
|
_handleDetails(_tokenId, _action);
|
||
|
} else {
|
||
3 years ago
|
require(false, "!valid action");
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
// ======== External: Send Token =========
|
||
|
|
||
4 years ago
|
/**
|
||
3 years ago
|
* @notice Send tokens to a recipient on a remote chain
|
||
4 years ago
|
* @param _token The token address
|
||
3 years ago
|
* @param _amnt The amount
|
||
4 years ago
|
* @param _destination The destination domain
|
||
|
* @param _recipient The recipient address
|
||
|
*/
|
||
4 years ago
|
function send(
|
||
|
address _token,
|
||
3 years ago
|
uint256 _amnt,
|
||
4 years ago
|
uint32 _destination,
|
||
3 years ago
|
bytes32 _recipient
|
||
4 years ago
|
) external {
|
||
3 years ago
|
// get remote BridgeRouter address; revert if not found
|
||
4 years ago
|
bytes32 _remote = _mustHaveRemote(_destination);
|
||
3 years ago
|
// remove tokens from circulation on this chain
|
||
4 years ago
|
IERC20 _bridgeToken = IERC20(_token);
|
||
3 years ago
|
if (_isLocalOrigin(_bridgeToken)) {
|
||
|
// if the token originates on this chain, hold the tokens in escrow in the Router
|
||
4 years ago
|
_bridgeToken.safeTransferFrom(msg.sender, address(this), _amnt);
|
||
4 years ago
|
} else {
|
||
3 years ago
|
// if the token originates on a remote chain, burn the representation tokens on this chain
|
||
4 years ago
|
_downcast(_bridgeToken).burn(msg.sender, _amnt);
|
||
4 years ago
|
}
|
||
3 years ago
|
// format Transfer Tokens action
|
||
4 years ago
|
bytes29 _action = BridgeMessage.formatTransfer(_recipient, _amnt);
|
||
3 years ago
|
// send message to remote chain via Optics
|
||
4 years ago
|
Home(xAppConnectionManager.home()).enqueue(
|
||
4 years ago
|
_destination,
|
||
|
_remote,
|
||
3 years ago
|
BridgeMessage.formatMessage(_formatTokenId(_token), _action)
|
||
4 years ago
|
);
|
||
|
}
|
||
|
|
||
3 years ago
|
// ======== External: Update Token Details =========
|
||
|
|
||
4 years ago
|
/**
|
||
3 years ago
|
* @notice Update the token metadata on another chain
|
||
4 years ago
|
* @param _token The token address
|
||
|
* @param _destination The destination domain
|
||
|
*/
|
||
3 years ago
|
// TODO: people can call this for nonsense non-ERC-20 tokens
|
||
|
// name, symbol, decimals could be nonsense
|
||
|
// remote chains will deploy a token contract based on this message
|
||
4 years ago
|
function updateDetails(address _token, uint32 _destination) external {
|
||
3 years ago
|
require(_isLocalOrigin(_token), "!local origin");
|
||
|
// get remote BridgeRouter address; revert if not found
|
||
4 years ago
|
bytes32 _remote = _mustHaveRemote(_destination);
|
||
3 years ago
|
// format Update Details message
|
||
4 years ago
|
IBridgeToken _bridgeToken = IBridgeToken(_token);
|
||
4 years ago
|
bytes29 _action =
|
||
|
BridgeMessage.formatDetails(
|
||
4 years ago
|
TypeCasts.coerceBytes32(_bridgeToken.name()),
|
||
|
TypeCasts.coerceBytes32(_bridgeToken.symbol()),
|
||
|
_bridgeToken.decimals()
|
||
4 years ago
|
);
|
||
3 years ago
|
// send message to remote chain via Optics
|
||
4 years ago
|
Home(xAppConnectionManager.home()).enqueue(
|
||
4 years ago
|
_destination,
|
||
|
_remote,
|
||
3 years ago
|
BridgeMessage.formatMessage(_formatTokenId(_token), _action)
|
||
4 years ago
|
);
|
||
|
}
|
||
4 years ago
|
|
||
3 years ago
|
// ============ Internal: Send / UpdateDetails ============
|
||
|
|
||
|
/**
|
||
|
* @notice Given a tokenAddress, format the tokenId
|
||
|
* identifier for the message.
|
||
|
* @param _token The token address
|
||
|
* @param _tokenId The bytes-encoded tokenId
|
||
|
*/
|
||
|
function _formatTokenId(address _token) internal view returns (bytes29 _tokenId) {
|
||
|
TokenId memory _tokId = _tokenIdFor(_token);
|
||
|
_tokenId = BridgeMessage.formatTokenId(_tokId.domain, _tokId.id);
|
||
|
}
|
||
|
|
||
|
// ============ Internal: Handle ============
|
||
|
|
||
4 years ago
|
/**
|
||
|
* @notice Handles an incoming Transfer message.
|
||
|
*
|
||
3 years ago
|
* If the token is of local origin, the amount is sent from escrow.
|
||
|
* Otherwise, a representation token is minted.
|
||
4 years ago
|
*
|
||
|
* @param _tokenId The token ID
|
||
|
* @param _action The action
|
||
|
*/
|
||
4 years ago
|
function _handleTransfer(bytes29 _tokenId, bytes29 _action)
|
||
4 years ago
|
internal
|
||
|
typeAssert(_tokenId, BridgeMessage.Types.TokenId)
|
||
|
typeAssert(_action, BridgeMessage.Types.Transfer)
|
||
|
{
|
||
3 years ago
|
// get the token contract for the given tokenId on this chain;
|
||
|
// (if the token is of remote origin and there is
|
||
|
// no existing representation token contract, the TokenRegistry will deploy a new one)
|
||
4 years ago
|
IERC20 _token = _ensureToken(_tokenId);
|
||
3 years ago
|
// send the tokens into circulation on this chain
|
||
|
if (_isLocalOrigin(_token)) {
|
||
|
// if the token is of local origin, the tokens have been held in escrow in this contract
|
||
|
// while they have been circulating on remote chains;
|
||
|
// transfer the tokens to the recipient
|
||
4 years ago
|
_token.safeTransfer(_action.evmRecipient(), _action.amnt());
|
||
|
} else {
|
||
3 years ago
|
// if the token is of remote origin, mint the tokens to the recipient on this chain
|
||
4 years ago
|
_downcast(_token).mint(_action.evmRecipient(), _action.amnt());
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
/**
|
||
|
* @notice Handles an incoming Details message.
|
||
|
* @param _tokenId The token ID
|
||
|
* @param _action The action
|
||
|
*/
|
||
4 years ago
|
function _handleDetails(bytes29 _tokenId, bytes29 _action)
|
||
4 years ago
|
internal
|
||
|
typeAssert(_tokenId, BridgeMessage.Types.TokenId)
|
||
|
typeAssert(_action, BridgeMessage.Types.Details)
|
||
|
{
|
||
3 years ago
|
// get the token contract deployed on this chain
|
||
4 years ago
|
IERC20 _token = _ensureToken(_tokenId);
|
||
3 years ago
|
// require that the token is of remote origin
|
||
|
// (otherwise, the BridgeRouter did not deploy the token contract,
|
||
|
// and therefore cannot update its metadata)
|
||
|
require(!_isLocalOrigin(_token), "!remote origin");
|
||
|
// update the token metadata
|
||
4 years ago
|
_downcast(_token).setDetails(
|
||
4 years ago
|
_action.name(),
|
||
|
_action.symbol(),
|
||
|
_action.decimals()
|
||
|
);
|
||
|
}
|
||
4 years ago
|
}
|