|
| 1 | +--- |
| 2 | +section: developers |
| 3 | +date: Last Modified |
| 4 | +title: "Bridge ERC1155 through the Custom Gateway" |
| 5 | +lang: "en" |
| 6 | +permalink: "TODO" |
| 7 | +excerpt: "TODO" |
| 8 | +--- |
| 9 | + |
| 10 | +Whenever you want to bridge an ERC1155 NFT, you may interact with the Gateway and NFT contracts on Sepolia and Scroll testnet. In this guide, we'll cover different approaches to doing so. |
| 11 | + |
| 12 | +## Step 1: Launch an 1155 Contract on Sepolia |
| 13 | + |
| 14 | +If you already have an existing token on Sepolia, feel free to skip this step. If you don't have an ERC1155 on L1, you can grab the following minimal example and launch it on L1. |
| 15 | + |
| 16 | +```solidity |
| 17 | +// SPDX-License-Identifier: UNLICENSED |
| 18 | +pragma solidity 0.8.19; |
| 19 | +
|
| 20 | +import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; |
| 21 | +
|
| 22 | +contract MockNFT is ERC1155 { |
| 23 | + constructor() ERC1155("") {} |
| 24 | +
|
| 25 | + function mint(address to, uint256 id, uint256 amount) public { |
| 26 | + _mint(to, id, amount, ""); |
| 27 | + } |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +## Step 2: Launch the Gateway on Sepolia |
| 32 | + |
| 33 | +This step is needed only if you want to launch your own Gateway. Launching your own gateway is fully permissionless and also allows you to have custom logic called every time a token is deposited. You can skip this step if you use the Scroll ERC1155 bridge launched at `TODO: TODO: 0xd1bE599aaCBC21448fD6373bbc7c1b4c7806f135`. More information is available [here](https://github.com/scroll-tech/token-list). This contract will allow you to send ERC1155 tokens from Sepolia to Scroll testnet. |
| 34 | + |
| 35 | +<pre class="language-solidity"><code class="lang-solidity"><strong>// SPDX-License-Identifier: MIT |
| 36 | +</strong>pragma solidity 0.8.19; |
| 37 | + |
| 38 | +import "@scroll-tech/ [email protected]/L1/gateways/L1ERC1155Gateway.sol"; |
| 39 | + |
| 40 | +contract MyL1ERC1155Gateway is L1ERC1155Gateway { |
| 41 | + function _depositERC1155( |
| 42 | + address _token, |
| 43 | + address _to, |
| 44 | + uint256 _tokenId, |
| 45 | + uint256 _amount, |
| 46 | + uint256 _gasLimit |
| 47 | + ) internal override nonReentrant { |
| 48 | + super._depositERC1155(_token, _to, _tokenId, _amount, _gasLimit); |
| 49 | + /*custom logic goes here*/ |
| 50 | + } |
| 51 | + |
| 52 | + function _batchDepositERC1155( |
| 53 | + address _token, |
| 54 | + address _to, |
| 55 | + uint256[] calldata _tokenIds, |
| 56 | + uint256[] calldata _amounts, |
| 57 | +<strong> uint256 _gasLimit |
| 58 | +</strong> ) internal override nonReentrant { |
| 59 | + super._batchDepositERC1155(_token, _to, _tokenIds, _amounts, _gasLimit); |
| 60 | + /*custom logic goes here*/ |
| 61 | + } |
| 62 | +} |
| 63 | +</code></pre> |
| 64 | + |
| 65 | +## Step 3: Launch the Gateway on Scroll |
| 66 | + |
| 67 | +This step is needed only if you want to launch your own Gateway. Launching your own gateway is fully permissionless and also allows you to have custom logic called every time a token is deposited. You can skip this step if you use the Scroll ERC1155 bridge launched at `TODO: 0xfe5Fc32777646bD123564C41f711FF708Dd48360`. More information is available [here](https://github.com/scroll-tech/token-list). This contract will allow you to send ERC1155 tokens from Sepolia to Scroll testnet. |
| 68 | + |
| 69 | +```solidity |
| 70 | +// SPDX-License-Identifier: MIT |
| 71 | +pragma solidity 0.8.19; |
| 72 | +
|
| 73 | +import "@scroll-tech/[email protected]/L2/gateways/L2ERC1155Gateway.sol"; |
| 74 | +
|
| 75 | +contract MyL2ERC1155Gateway is L2ERC1155Gateway { |
| 76 | + function _withdrawERC1155( |
| 77 | + address _token, |
| 78 | + address _to, |
| 79 | + uint256 _tokenId, |
| 80 | + uint256 _amount, |
| 81 | + uint256 _gasLimit |
| 82 | + ) internal override nonReentrant { |
| 83 | + super._withdrawERC1155(_token, _to, _tokenId, _amount, _gasLimit); |
| 84 | + /*custom logic goes here*/ |
| 85 | + } |
| 86 | +
|
| 87 | + function _batchWithdrawERC1155( |
| 88 | + address _token, |
| 89 | + address _to, |
| 90 | + uint256[] calldata _tokenIds, |
| 91 | + uint256[] calldata _amounts, |
| 92 | + uint256 _gasLimit |
| 93 | + ) internal override nonReentrant { |
| 94 | + super._batchWithdrawERC1155(_token, _to, _tokenIds, _amounts, _gasLimit); |
| 95 | + /*custom logic goes here*/ |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Step 3: Launch the custom token on Scroll testnet |
| 101 | + |
| 102 | +This contract has to follow the IScrollERC1155 standard interface. It has to allow the gateway to mint tokens on deposit and burn on withdrawal. The following example shows a sample implementation by passing as constructor parameters the L2 gateway (either the one you just launched or Scroll's at `TODO: 0xd1bE599aaCBC21448fD6373bbc7c1b4c7806f135`) and your token address on Sepolia. |
| 103 | + |
| 104 | +```solidity |
| 105 | +// SPDX-License-Identifier: MIT |
| 106 | +pragma solidity 0.8.19; |
| 107 | +
|
| 108 | +import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; |
| 109 | +import "@scroll-tech/[email protected]/libraries/token/IScrollERC1155.sol"; |
| 110 | +
|
| 111 | +contract MockNFT is ERC1155, IScrollERC1155 { |
| 112 | + address GATEWAY; |
| 113 | + address COUNTERPART; |
| 114 | +
|
| 115 | + constructor(address _gateway, address _counterpart) ERC1155("") { |
| 116 | + GATEWAY = _gateway; |
| 117 | + COUNTERPART = _counterpart; |
| 118 | + } |
| 119 | +
|
| 120 | + /// @notice Return the address of Gateway the token belongs to. |
| 121 | + function gateway() public view returns (address) { |
| 122 | + return GATEWAY; |
| 123 | + } |
| 124 | +
|
| 125 | + /// @notice Return the address of counterpart token. |
| 126 | + function counterpart() public view returns (address) { |
| 127 | + return COUNTERPART; |
| 128 | + } |
| 129 | +
|
| 130 | + /// @notice Mint some token to recipient's account. |
| 131 | + /// @dev Gateway Utilities, only gateway contract can call |
| 132 | + /// @param _to The address of recipient. |
| 133 | + /// @param _tokenId The token id to mint. |
| 134 | + function mint(address _to, uint256 _tokenId, uint256 _amount, bytes memory _data) external { |
| 135 | + require(msg.sender == gateway(), "Only gateway can mint"); |
| 136 | + _mint(_to, _tokenId, _amount, _data); |
| 137 | + } |
| 138 | +
|
| 139 | + /// @notice Burn some token from account. |
| 140 | + /// @dev Gateway Utilities, only gateway contract can call |
| 141 | + /// @param _tokenId The token id to burn. |
| 142 | + function burn(address _from, uint256 _tokenId, uint256 _amount) external { |
| 143 | + require(msg.sender == gateway(), "Only gateway can mint"); |
| 144 | + _burn(_from, _tokenId, _amount); |
| 145 | + } |
| 146 | +
|
| 147 | + function batchMint( |
| 148 | + address _to, |
| 149 | + uint256[] calldata _tokenIds, |
| 150 | + uint256[] calldata _amounts, |
| 151 | + bytes calldata _data |
| 152 | + ) public { |
| 153 | + require(msg.sender == gateway(), "Only gateway can mint"); |
| 154 | + _mintBatch(_to, _tokenIds, _amounts, _data); |
| 155 | + } |
| 156 | +
|
| 157 | + /// @notice Batch burn some token from account. |
| 158 | + /// @dev Gateway Utilities, only gateway contract can call |
| 159 | + /// @param _from The address of account to burn token. |
| 160 | + /// @param _tokenIds The list of token ids to burn. |
| 161 | + /// @param _amounts The list of corresponding amount of token to burn. |
| 162 | + function batchBurn(address _from, uint256[] calldata _tokenIds, uint256[] calldata _amounts) public { |
| 163 | + require(msg.sender == gateway(), "Only gateway can Burn"); |
| 164 | + _burnBatch(_from, _tokenIds, _amounts); |
| 165 | + } |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +## Step 4: Initialize the Gateways |
| 170 | + |
| 171 | +This step is only needed if you're launching your own custom Gateways. If you're using Scroll's Gateway, the Scroll team will take care of this. |
| 172 | + |
| 173 | +On Sepolia, first call the `initialize` function on the Sepolia L1 Gateway by passing the following params: |
| 174 | + |
| 175 | +- `counterpart` : L2 gateway you just launched on Scroll testnet. |
| 176 | +- messenger: The messenger contract on Sepolia at `TODO: 0x5260e38080BFe97e6C4925d9209eCc5f964373b6` |
| 177 | + |
| 178 | +Next, you will need to call the `updateTokenMapping` to `bind the L1 and L2 tokens.` |
| 179 | + |
| 180 | +- `l1 token`: Token launched on Sepolia |
| 181 | +- `l2 token`: Token launched on Scroll testnet |
| 182 | + |
| 183 | +Now let's move to Scroll testnet and call the initialize function on your Scroll testnet L2 Gateway: |
| 184 | + |
| 185 | +- `counterpart`: L1 gateway you just launched on Sepolia. |
| 186 | +- `messenger`: The messenger contract on Scroll testnet at `TODO: 0xb75d7e84517e1504C151B270255B087Fd746D34C` |
| 187 | + |
| 188 | +Finally, let's bind the tokens on Scroll testnet by calling `updateTokenMapping` on the same contract: |
| 189 | + |
| 190 | +- `l2 token` : Token launched on Scroll testnet |
| 191 | +- `l1 Token`: Token launched on Sepolia |
| 192 | + |
| 193 | +## Step 5: Deposit from Sepolia to Scroll testnet |
| 194 | + |
| 195 | +Deposits can be made by first approving the Gateway on L1 and then calling the `depositERC1155` function by passing the following parameters. Notice that this is a payable function, so if you send 0.0001 ETH, it should be more than enough to confirm your transaction on L2: |
| 196 | + |
| 197 | +1. `token`: token address on Sepolia |
| 198 | +2. `token id`: token id that you want to deposit |
| 199 | +3. `amount`: amount of tokens you want to deposit |
| 200 | +4. `gas limit`: 4000 should be enough |
0 commit comments