-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathParentChainToken.sol
More file actions
112 lines (101 loc) · 3.72 KB
/
ParentChainToken.sol
File metadata and controls
112 lines (101 loc) · 3.72 KB
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
105
106
107
108
109
110
111
112
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
import "@arbitrum/token-bridge-contracts/contracts/tokenbridge/ethereum/ICustomToken.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
/**
* @title Interface needed to call function registerTokenToL2 of the L1CustomGateway
* (We don't need this interface for this example, but we're keeping it for completion)
*/
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 IL1GatewayRouter {
function setGateway(
address gateway,
uint256 maxGas,
uint256 gasPriceBid,
uint256 maxSubmissionCost,
address creditBackAddress
) external payable returns (uint256);
}
/**
* @title Example implementation of a custom ERC20 token to be deployed on L1
*/
contract ParentChainToken is Ownable, ERC20, ICustomToken {
address public gateway; // The parent chain custom gateway contract
address public router; // The parent chain router contract
bool private shouldRegisterGateway;
/**
* @dev See {ERC20-constructor} and {Ownable-constructor}
* An initial supply amount is passed, which is preminted to the deployer.
* @param _gateway address of the L1 custom gateway
* @param _router address of the L1GatewayRouter
* @param initialSupply initial supply amount to be minted to the deployer
*/
constructor(
address _gateway,
address _router,
uint256 initialSupply
) ERC20("L1CustomToken", "LCT") {
gateway = _gateway;
router = _router;
_mint(msg.sender, initialSupply * 10 ** decimals());
}
/// @dev we only set shouldRegisterGateway to true when in `registerTokenOnL2`
function isArbitrumEnabled() external view override returns (uint8) {
require(shouldRegisterGateway, "NOT_EXPECTED_CALL");
return uint8(0xb1);
}
/**
* @dev See {ICustomToken-registerTokenOnL2}
* In this case, we don't need to call IL1CustomGateway.registerTokenToL2, because our
* custom gateway works for a single token it already knows.
*/
function registerTokenOnL2(
address /* l2CustomTokenAddress */,
uint256 /* maxSubmissionCostForCustomGateway */,
uint256 maxSubmissionCostForRouter,
uint256 /* maxGasForCustomGateway */,
uint256 maxGasForRouter,
uint256 gasPriceBid,
uint256 /* valueForGateway */,
uint256 valueForRouter,
address creditBackAddress
) public payable override onlyOwner {
// we temporarily set `shouldRegisterGateway` to true for the callback in registerTokenToL2 to succeed
bool prev = shouldRegisterGateway;
shouldRegisterGateway = true;
IL1GatewayRouter(router).setGateway{ value: valueForRouter }(
gateway,
maxGasForRouter,
gasPriceBid,
maxSubmissionCostForRouter,
creditBackAddress
);
shouldRegisterGateway = prev;
}
/// @dev See {ERC20-transferFrom}
function transferFrom(
address sender,
address recipient,
uint256 amount
) public override(ICustomToken, ERC20) returns (bool) {
return super.transferFrom(sender, recipient, amount);
}
/// @dev See {ERC20-balanceOf}
function balanceOf(
address account
) public view override(ICustomToken, ERC20) returns (uint256) {
return super.balanceOf(account);
}
}