Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XERC20Bridge POC #1

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open

Add XERC20Bridge POC #1

wants to merge 1 commit into from

Conversation

lmcorbalan
Copy link
Collaborator

@lmcorbalan lmcorbalan commented Jul 17, 2024

Problem: To be able to use a XERC20 token (e.g. Magic token) from the parent chain as the gas token on an Orbit chain (e.g. Treasure chain).

Solution: A possible solution for using an XERC20 as the gas token could be upgrading the ERC20Bridge for burning and minting the XERC20 token (it could also involve a Lockbox if necessary).

  1. Logic for minting here https://github.com/OffchainLabs/nitro-contracts/blob/main/src/bridge/ERC20Bridge.sol#L67-L82
  2. Logic for burning here https://github.com/OffchainLabs/nitro-contracts/blob/main/src/bridge/ERC20Bridge.sol#L48-L51

image

Comment on lines +41 to +126
contract XERC20Bridge is AbsBridge, IERC20Bridge {
using SafeERC20 for IERC20;

/// @inheritdoc IERC20Bridge
address public nativeToken;

/// @inheritdoc IERC20Bridge
uint8 public nativeTokenDecimals;

/// @inheritdoc IERC20Bridge
function initialize(IOwnable rollup_, address nativeToken_) external initializer onlyDelegated {
if (nativeToken_ == address(0)) revert InvalidTokenSet(nativeToken_);
nativeToken = nativeToken_;
_activeOutbox = EMPTY_ACTIVEOUTBOX;
rollup = rollup_;

// store number of decimals used by native token
try ERC20(nativeToken_).decimals() returns (uint8 decimals) {
if (decimals > MAX_ALLOWED_NATIVE_TOKEN_DECIMALS) {
revert NativeTokenDecimalsTooLarge(decimals);
}
nativeTokenDecimals = decimals;
} catch {
// decimal is not part of the ERC20 spec
// assume it have 0 decimals if it does not have decimals() method
// we do this to align with the token bridge behavior
nativeTokenDecimals = 0;
}
}

/// @inheritdoc IERC20Bridge
function enqueueDelayedMessage(
uint8 kind,
address sender,
bytes32 messageDataHash,
uint256 tokenFeeAmount
) external returns (uint256) {
return _enqueueDelayedMessage(kind, sender, messageDataHash, tokenFeeAmount);
}

function _transferFunds(uint256 amount) internal override {
// fetch native token from Inbox
// IERC20(nativeToken).safeTransferFrom(msg.sender, address(this), amount);
IXERC20(nativeToken).burn(msg.sender, amount);
}

function _executeLowLevelCall(
address to,
uint256 value,
bytes memory data
) internal override returns (bool success, bytes memory returnData) {
address _nativeToken = nativeToken;

// we don't allow outgoing calls to native token contract because it could
// result in loss of native tokens which are escrowed by ERC20Bridge
if (to == _nativeToken) {
revert CallTargetNotAllowed(_nativeToken);
}

// first release native token
// IERC20(_nativeToken).safeTransfer(to, value);
IXERC20(nativeToken).mint(to, value);
success = true;

// if there's data do additional contract call. Make sure that call is not used to
// changing native token totalSupply
if (data.length > 0) {
uint256 totalSupplyBefore = IERC20(_nativeToken).totalSupply();

// solhint-disable-next-line avoid-low-level-calls
(success, returnData) = to.call(data);

uint256 totalSupplyAfter = IERC20(_nativeToken).totalSupply();
if (totalSupplyAfter != totalSupplyBefore) {
revert CallNotAllowed();
}
}
}

function _baseFeeToReport() internal pure override returns (uint256) {
// ArbOs uses formula 'l1BaseFee * (1400 + 6 * calldataLengthInBytes)' to calculate retryable ticket's
// submission fee. When custom ERC20 token is used to pay for fees, submission fee shall be 0. That's
// why baseFee is reported as 0 here.
return 0;
}
}

Check warning

Code scanning / Slither

Contracts that lock Ether Medium

Contract locking ether found:
Contract XERC20Bridge has payable functions:
- AbsBridge.acceptFundsFromOldBridge()
But does not have a function to withdraw the ether
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant