Skip to main content

ERC721 NFT Transfer

This section guides you through setting up an ERC721 bridge and sending NFTs between blockchain networks.

Step 1: Register resource ID

You will first need to register the resource ID for the ERC721 token in the Bridge contracts on both chains.

# For Polygon PoS chain
$ cb-sol-cli bridge register-resource \
--url https://rpc-mumbai.matic.today \
--privateKey [ADMIN_ACCOUNT_PRIVATE_KEY] \
--gasPrice [GAS_PRICE] \
# Set ResourceID for ERC721 Token
--resourceId "0x000000000000000000000000000000e389d61c11e5fe32ec1735b3cd38c69501" \
--bridge "[BRIDGE_CONTRACT_ADDRESS]" \
--handler "[ERC721_HANDLER_CONTRACT_ADDRESS]" \
--targetContract "[ERC721_CONTRACT_ADDRESS]"

# For Polygon Edge chain
$ cb-sol-cli bridge register-resource \
--url http://localhost:10002 \
--privateKey [ADMIN_ACCOUNT_PRIVATE_KEY] \
# Set ResourceID for ERC721 Token
--resourceId "0x000000000000000000000000000000e389d61c11e5fe32ec1735b3cd38c69501" \
--bridge "[BRIDGE_CONTRACT_ADDRESS]" \
--handler "[ERC721_HANDLER_CONTRACT_ADDRESS]" \
--targetContract "[ERC721_CONTRACT_ADDRESS]"

(Optional): Make contracts mintable/burnable

To make the Tokens mintable/burnable, you will need to call the following commands:

# Let ERC721 contract burn on source chain or mint on destination chain
$ cb-sol-cli bridge set-burn \
--url http://localhost:10002 \
--privateKey [ADMIN_ACCOUNT_PRIVATE_KEY] \
--bridge "[BRIDGE_CONTRACT_ADDRESS]" \
--handler "[ERC721_HANDLER_CONTRACT_ADDRESS]" \
--tokenContract "[ERC721_CONTRACT_ADDRESS]"

# Grant minter role to ERC721 Handler contract (Only if you want to mint)
$ cb-sol-cli erc721 add-minter \
--url http://localhost:10002 \
--privateKey [ADMIN_ACCOUNT_PRIVATE_KEY] \
--erc721Address "[ERC721_CONTRACT_ADDRESS]" \
--minter "[ERC721_HANDLER_CONTRACT_ADDRESS]"

Step 2: Transfer NFT

Firstly, you will mint an NFT if you need it.

# Mint NFT 0x50
$ cb-sol-cli erc721 mint \
--url https://rpc-mumbai.matic.today \
--privateKey [MINTER_ROLE_ACCOUNT] \
--gasPrice [GAS_PRICE] \
--erc721Address "[ERC721_CONTRACT_ADDRESS]" \
--id 0x50

To check the NFT owner, you can use cb-sol-cli erc721 owner

# Check the current owner of NFT
$ cb-sol-cli erc721 owner \
--url https://rpc-mumbai.matic.today \
--erc721Address "[ERC721_CONTRACT_ADDRESS]" \
--id 0x50

Then, you will approve a transfer of the NFT by ERC721 Handler

# Approve transfer of the NFT 0x50 by ERC721 Handler
$ cb-sol-cli erc721 approve \
--url https://rpc-mumbai.matic.today \
--privateKey [PRIVATE_KEY] \
--gasPrice [GAS_PRICE] \
--erc721Address "[ERC721_CONTRACT_ADDRESS]" \
--recipient "[ERC721_HANDLER_CONTRACT_ADDRESS]" \
--id 0x50

Finally, you will start the transfer

# Start transfer from Polygon PoS to Polygon Edge chain
$ cb-sol-cli erc721 deposit \
--url https://rpc-mumbai.matic.today \
--privateKey [PRIVATE_KEY] \
--gasPrice [GAS_PRICE] \
--bridge "[BRIDGE_CONTRACT_ADDRESS]" \
--resourceId "0x000000000000000000000000000000e389d61c11e5fe32ec1735b3cd38c69501" \
--id 0x50 \
# ChainID of Polygon Edge chain
--dest 100 \
--recipient "[RECIPIENT_ADDRESS_IN_POLYGON_EDGE_CHAIN]"

The relayer will get the event and vote for the proposal. It executes a transaction to send NFTs to the recipient account in the Polygon Edge chain after the required number of votes are submitted.

INFO[11-19|09:07:50] Handling nonfungible deposit event       chain=mumbai
INFO[11-19|09:07:50] Attempting to resolve message chain=polygon-edge type=NonFungibleTransfer src=99 dst=100 nonce=2 rId=000000000000000000000000000000e389d61c11e5fe32ec1735b3cd38c69501
INFO[11-19|09:07:50] Creating erc721 proposal chain=polygon-edge src=99 nonce=2
INFO[11-19|09:07:50] Watching for finalization event chain=polygon-edge src=99 nonce=2
INFO[11-19|09:07:50] Submitted proposal vote chain=polygon-edge tx=0x58a22d84a08269ad2e8d52d8dc038621f1a21109d11c7b6e0d32d5bf21ea8505 src=99 depositNonce=2 gasPrice=1
INFO[11-19|09:08:15] Submitted proposal execution chain=polygon-edge tx=0x57419844881a07531e31667c609421662d94d21d0709e64fb728138309267e68 src=99 dst=100 nonce=2 gasPrice=3

You can check the owner of the NFT on the Polygon Edge network after the execution is completed.

# Check the owner of NFT 0x50 in Polygon Edge chain
$ cb-sol-cli erc721 owner \
--url http://localhost:10002 \
--erc721Address "[ERC721_CONTRACT_ADDRESS]" \
--id 0x50