Skip to content

Commit

Permalink
Integrate token with arbitrum bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-kaufman committed Jul 11, 2024
1 parent 3a3363d commit f48a8fa
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 46 deletions.
38 changes: 0 additions & 38 deletions contracts/HatTokenArbitrumBridgeL1.sol

This file was deleted.

7 changes: 0 additions & 7 deletions contracts/HatTokenArbitrumBridgeL2.sol

This file was deleted.

2 changes: 1 addition & 1 deletion contracts/HATToken.sol → contracts/token/HATToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity 0.8.16;
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Votes.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./interfaces/IHATToken.sol";
import "../interfaces/IHATToken.sol";

contract HATToken is IHATToken, ERC20Votes, ERC20Capped, Ownable {

Expand Down
93 changes: 93 additions & 0 deletions contracts/token/HatTokenArbitrumBridgeL1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

import "./HATToken.sol";
import "@arbitrum/token-bridge-contracts/contracts/tokenbridge/ethereum/ICustomToken.sol";

/**
* @title Interface needed to call function registerTokenToL2 of the L1CustomGateway
*/
interface IL1CustomGateway {
function registerTokenToL2(
address _l2Address,
uint256 _maxGas,
uint256 _gasPriceBid,
uint256 _maxSubmissionCost,
address _creditBackAddress
) external payable returns (uint256);
}

/**
* @title Interface needed to call function setGateway of the L2GatewayRouter
*/
interface IL2GatewayRouter {
function setGateway(
address _gateway,
uint256 _maxGas,
uint256 _gasPriceBid,
uint256 _maxSubmissionCost,
address _creditBackAddress
) external payable returns (uint256);
}

contract HATTokenArbitrumBridgeL1 is ICustomToken, HATToken {
address private customGatewayAddress;
address private routerAddress;
bool private shouldRegisterGateway;

constructor (address _customGatewayAddress, address _routerAddress, address _governance) HATToken(_governance) {
customGatewayAddress = _customGatewayAddress;
routerAddress = _routerAddress;
}

function isArbitrumEnabled() external view override returns (uint8) {
require(shouldRegisterGateway, "NOT_EXPECTED_CALL");
return uint8(0xb1);
}

function registerTokenOnL2(
address l2CustomTokenAddress,
uint256 maxSubmissionCostForCustomGateway,
uint256 maxSubmissionCostForRouter,
uint256 maxGasForCustomGateway,
uint256 maxGasForRouter,
uint256 gasPriceBid,
uint256 valueForGateway,
uint256 valueForRouter,
address creditBackAddress
) public override payable onlyOwner {
// we temporarily set `shouldRegisterGateway` to true for the callback in registerTokenToL2 to succeed
bool prev = shouldRegisterGateway;
shouldRegisterGateway = true;

IL1CustomGateway(customGatewayAddress).registerTokenToL2{ value: valueForGateway }(
l2CustomTokenAddress,
maxGasForCustomGateway,
gasPriceBid,
maxSubmissionCostForCustomGateway,
creditBackAddress
);

IL2GatewayRouter(routerAddress).setGateway{ value: valueForRouter }(
customGatewayAddress,
maxGasForRouter,
gasPriceBid,
maxSubmissionCostForRouter,
creditBackAddress
);

shouldRegisterGateway = prev;
}

function transferFrom(
address sender,
address recipient,
uint256 amount
) public override(ICustomToken, ERC20) returns (bool) {
return super.transferFrom(sender, recipient, amount);
}

function balanceOf(address account) public view override(ICustomToken, ERC20) returns (uint256) {
return super.balanceOf(account);
}
}
36 changes: 36 additions & 0 deletions contracts/token/HatTokenArbitrumBridgeL2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

import "./HATToken.sol";
import "@arbitrum/token-bridge-contracts/contracts/tokenbridge/arbitrum/IArbToken.sol";

contract HATTokenArbitrumBridgeL2 is HATToken, IArbToken {
address public l2Gateway;
address public override l1Address;

modifier onlyL2Gateway() {
require(msg.sender == l2Gateway, "NOT_GATEWAY");
_;
}

constructor(address _l2Gateway, address _l1TokenAddress) HATToken(address(0)) {
l2Gateway = _l2Gateway;
l1Address = _l1TokenAddress;
transferable = true;
emit TransferableSet();
}

/**
* @notice should increase token supply by amount, and should only be callable by the L2Gateway.
*/
function bridgeMint(address account, uint256 amount) external override onlyL2Gateway {
_mint(account, amount);
}

/**
* @notice should decrease token supply by amount, and should only be callable by the L2Gateway.
*/
function bridgeBurn(address account, uint256 amount) external override onlyL2Gateway {
_burn(account, amount);
}
}

0 comments on commit f48a8fa

Please sign in to comment.