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

Nft bridge example #3

Merged
merged 11 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions boba_examples/nft_bridging/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# local
ADDRESS_MANAGER_ADDRESS=0x5FbDB2315678afecb367f032d93F642f64180aa3
L1_NODE_WEB3_URL=http://localhost:9545
L2_NODE_WEB3_URL=http://localhost:8545
PRIV_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80



# rinkeby
ADDRESS_MANAGER_ADDRESS=0x93A96D6A5beb1F661cf052722A1424CDDA3e9418
L1_NODE_WEB3_URL=https://rinkeby.infura.io/v3/...
L2_NODE_WEB3_URL=https://rinkeby.boba.network
PRIV_KEY=0x...
17 changes: 17 additions & 0 deletions boba_examples/nft_bridging/BridgeToL1Support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Guide to support NFT Bridging

If you are a marketplace looking to support NFT bridging - great Idea! its simple, just keep reading-

## Requirements and Support

Note: Not all native ERC721s on Boba will be bridgeable to L1 by default

To enable the withdraw option, simply -
- determine if the ERC721 contract is registered on the `L2NFTBridge` and has a counterpart L1StandardERC721 registered
- determine if the ERC721 contract registered on the `L2NFTBridge` is as a base Network "L2"

To withdraw to L1-
- Allow the owner to approve the L2NFTBridge for the token
- call withdraw() on L2NFTBridge with the params - ERC721Address and tokenId

For exact representation of the following please refer to - `src/bridgeToL1.js`
151 changes: 151 additions & 0 deletions boba_examples/nft_bridging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# NFT-Bridge Example

This example will walk you through the process of bridging any L2 native ERC721 to L1 (Ethereum) (and also bridging them back to L2)

If you are a NFT marketplace, willing to support the bridge feature - please refer to the simpler section - [Supporting the 'Bridge to L1'](BridgeToL1Support.md)

Please note - this example is towards bridging in/out a Layer-2 native ERC721 (meaning a NFT originally deployed to L2 Boba). However, the bridge can support the same features with Layer-1 NFTs as well, for which please refer to the [more elaborate documentation - here](https://github.com/omgnetwork/optimism-v2/blob/develop/packages/boba/contracts/contracts/bridges/README.md)

## Quickstart - Rinkeby
If you don't have much time and are looking to quickly run through the example, just keep looking at this section. However, If you want a more enhanced understanding, which is encouraged, skip forward to the [next section](#getting-started-with-the-example)

Alright, if you are still here, we don't have much time, so we will try running through the example on Boba-Rinkeby

- Step 1: set up your env according to the .env.example
you would need a rinkeby infura endpoint, and an account with some Rinkeby Eth (> 0.01 ETH)

- Step 2: You are all set!
Since, we are on Boba Rinkeby we will use the Boba NFT Bridges on Rinkeby, but in order to use your own ERC721s with the bridge, the Boba Team would need to register your contracts on the bridges
So, for the sake of this tutorial, we have set you up with a pre-deployed contract that is registered on the bridge!

To quickly run the example, do a:

```bash
$ yarn
$ yarn compile
$ yarn start:rinkeby
```
When the script has finished running, you would have minted -> and initiated a NFT withdraw to L1!
Your bridged NFT will arrive on L1 after the fraud-proof-window

## Getting-Started with the Example

### Setup
For the tutorial, we would need a local Boba stack running
Get the services running with-

```bash
$ git clone https://github.com/omgnetwork/optimism-v2.git
$ yarn
$ yarn build
```
and then..

```bash
$ cd ops
$ docker-compose build && docker-compose up -V
```

**For local, use the same .env as .env.example**
this includes the priv_key of the account which can register the ERC721 on the NFTBridges

## Running through the Example
Once the local Boba stack is running, switch to:

```bash
$ cd ../boba_examples/nft_bridging/
```

To quickly run the example, run:

```bash
$ yarn
$ yarn compile
$ yarn start
```

Instead, to keep following through the tutorial keep reading..

### Deploying your NFT contract
The first step is to deploy your ERC721 contract on Boba! There is no limitation as to what you can deploy as long as it is ERC721 compliant.
For this example, we have preset a sample ERC721 contract for you to use here `/contracts/SampleERC721.sol` - feel free to select your own ERC721 that you fancy

Once, you have settled on the contract to use,
compile the contract using -
```bash
yarn compile
```

and, then run the example script using -
```bash
yarn start
```

The script starts with deploying your ERC721 on Boba, and then mints some tokens to your address
```
Deployed the NFT contract at: 0x....
And, Minted some NFTs to address
```

So, nothing out of the ordinary up until this point, but now we will actually get into making the NFTs of this contract bridgeable to L1

## Making the NFTs bridgeable to L1
Now, with your ERC721 deployed on Boba, we want to make all the NFTs of this contract bridge-able to L1(Ethereum). To enforce this - we only need to deploy a representation contract on L1 which can accept messages from the NFT Bridges and generate a L1 representation for your L2 NFT.

Simply put, any arbitrary ERC721 contract deployed on Boba needs a single and potentially unmodified deployment of L1StandardERC721 on Ethereum to make it bridgeable!

#### Deploying a standard L1ERC721 representation
Following the script - you should see your L1 represntation contract being deployed
```
Deployed the L1 NFT Representaton contract at: 0x....
```

Additionally, to enable the L1 representation NFTs to represent the metadat correctly, set the baseURI on the L1StandardERC721 to the baseURI of your L2 ERC721 contract (optional)

#### Registering pair on NFT Bridges
Finally, to enable your contracts to be used on the NFT Bridge contracts - register the L1ERC721/L2ERC721 pair on both the L1/L2NFTBridge contracts. For the sake of this tutorial - you should be able to complete this step on the local stack (with the priv key on env.example)
However, *doing this on prod environments - would need help from the operators of the NFT Briges*
Please Reach out to the team in order to register your ERC721 pair on the official Boba NFT Bridges on Mainnet/Rinkeby

```
And registered the L2ERC721 and L1ERC721 to the NFTBridges!
```

And thats it! Your NFTs are now bridgeable to L1 and back

## Bridge an NFT to L1
If you are a marketplace looking to support NFT bridging - great Idea! its simple, [just follow here](BridgeToL1Support.md) - for a more elaborate instruction

To try this we will attempt to bridge the token#1 to L1

First, we will approve the L2NFTBridge for the token we want to bridge to L1
Second, we will call withdraw() on the L2NFTBridge to initiate a withdrawal

After the xDomain message is finalized you will have your NFT bridged to L1

```
Attempting to Bridge NFT#1 to L1
Approved Bridge
Sending tx to withdraw NFT to L1..
Waiting for withdrawal to be relayed to L1...
#################################
Your address: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
L1NFT owner: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
NFT bridged to L1 successfully!
#################################
```
Boom, done! Your NFT is now on L1 and you are free to keep, transfer, list, sell, auction your bridged NFT or even bridge it back to L2

## Bridge NFT back to L2
Now, in order to bring your NFT back to Boba, the (current) L1 owner of the NFT will be able to bridge it back to their respective address on L2 by calling depositNFT() on L1NFTBridge

```
Attempting to bridge NFT#1 back to L2
Sending tx to deposit NFT back to L2..
Waiting for deposit to be relayed to L2...
#################################
Your address: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
L2NFT owner: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
NFT bridged back to L2 successfully!
#################################
```
22 changes: 22 additions & 0 deletions boba_examples/nft_bridging/contracts/SampleERC721.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;

import '@openzeppelin/contracts/token/ERC721/ERC721.sol';
import "@openzeppelin/contracts/access/Ownable.sol";

/**
* @title ERC721
* @dev A super simple ERC721 implementation!
*/
contract SampleERC721 is Ownable, ERC721 {
constructor()
ERC721(
"Bubble",
"BUBL"
) {
}

function mint(address _to, uint256 _tokenId) public onlyOwner {
_safeMint(_to, _tokenId);
}
}
23 changes: 23 additions & 0 deletions boba_examples/nft_bridging/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: {
compilers: [
{
version: '0.8.9',
settings: {
optimizer: { enabled: true, runs: 10_000 },
metadata: {
bytecodeHash: 'none',
},
outputSelection: {
'*': {
'*': ['storageLayout'],
},
},
},
},
],
},
}
18 changes: 18 additions & 0 deletions boba_examples/nft_bridging/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@boba/nft-bridging",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"compile": "hardhat compile",
"start": "node src/index.js",
"start:rinkeby": "node src/quickStart-rinkeby.js"
},
"license": "MIT",
"dependencies": {
"@eth-optimism/contracts": "^0.5.15",
"chalk": "^2.4.1",
"dotenv": "^16.0.0",
"ethers": "^5.5.4",
"hardhat": "^2.9.0"
}
}
Loading