|
| 1 | +--- |
| 2 | +title: Deploy an NFT to Asset Hub |
| 3 | +description: Deploy an NFT on Asset Hub using PolkaVM and OpenZeppelin. Learn how to compile, deploy, and interact with your contract using Polkadot Remix IDE. |
| 4 | +--- |
| 5 | + |
| 6 | +# Deploy an NFT to Asset Hub |
| 7 | + |
| 8 | +## Introduction |
| 9 | + |
| 10 | +Non-Fungible Tokens (NFTs) represent unique digital assets commonly used for digital art, collectibles, gaming, and identity verification. Asset Hub supports EVM-compatible smart contracts through PolkaVM, enabling straightforward NFT deployment. |
| 11 | + |
| 12 | +This tutorial guides you through deploying an [ERC-721](https://eips.ethereum.org/EIPS/eip-721){target=\_blank} NFT contract on the Westend TestNet using the [Polkadot Remix IDE](https://remix.polkadot.io){target=\_blank}, a web-based development environment. It uses [OpenZeppelin's NFT contracts]({{ dependencies.open_zeppelin_contracts.repository_url}}/tree/{{ dependencies.open_zeppelin_contracts.version}}){target=\_blank} implementation to ensure security and standard compliance. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +Before starting, make sure you have: |
| 17 | + |
| 18 | +- MetaMask installed and connected to Westend Asset Hub |
| 19 | +- A funded account with some WND tokens (you can get them from the [Westend Faucet](https://faucet.polkadot.io/westend?parachain=1000){target=\_blank}) |
| 20 | +- Basic understanding of Solidity and NFTs |
| 21 | + |
| 22 | +## Create the NFT Contract |
| 23 | + |
| 24 | +To create the NFT contract, you can follow the steps below: |
| 25 | + |
| 26 | +1. Navigate to the [Polkadot Remix IDE](https://remix.polkadot.io/){target=\_blank} |
| 27 | +2. Click in the **Create new file** button under the **contracts** folder, and name your contract as `MyNFT.sol` |
| 28 | + |
| 29 | +  |
| 30 | + |
| 31 | +3. Now, paste the following NFT contract code into the editor |
| 32 | + |
| 33 | + ```solidity title="MyNFT.sol" |
| 34 | + --8<-- 'code/tutorials/smart-contracts/deploy-nft/MyNFT.sol' |
| 35 | + ``` |
| 36 | +
|
| 37 | + The key components of the code above are: |
| 38 | +
|
| 39 | + - Contract imports |
| 40 | +
|
| 41 | + - [**`ERC721.sol`**]({{ dependencies.open_zeppelin_contracts.repository_url }}/blob/{{ dependencies.open_zeppelin_contracts.version }}/contracts/token/ERC721/ERC721.sol){target=\_blank} - the base contract for non-fungible tokens, implementing core NFT functionality like transfers and approvals |
| 42 | + - [**`Ownable.sol`**]({{ dependencies.open_zeppelin_contracts.repository_url }}/blob/{{ dependencies.open_zeppelin_contracts.version }}/contracts/access/Ownable.sol){target=\_blank} - provides basic authorization control, ensuring only the contract owner can mint new tokens |
| 43 | + |
| 44 | + - Constructor parameters |
| 45 | +
|
| 46 | + - **`initialOwner`** - sets the address that will have administrative rights over the contract |
| 47 | + - **`"MyToken"`** - the full name of your NFT collection |
| 48 | + - **`"MTK"`** - the symbol representing your token in wallets and marketplaces |
| 49 | +
|
| 50 | + - Key functions |
| 51 | +
|
| 52 | + - [**`_safeMint(to, tokenId)`**]({{ dependencies.open_zeppelin_contracts.repository_url }}/blob/{{ dependencies.open_zeppelin_contracts.version }}/contracts/token/ERC721/ERC721.sol#L304){target=\_blank} - an internal function from `ERC721` that safely mints new tokens. It includes checks to ensure the recipient can handle ERC721 tokens (important when minting smart contracts) |
| 53 | + - Inherited [Standard ERC721](https://ethereum.org/en/developers/docs/standards/tokens/erc-721/){target=\_blank} functions: |
| 54 | + - **`transferFrom(address from, address to, uint256 tokenId)`** - transfers a specific NFT from one address to another |
| 55 | + - **`safeTransferFrom(address from, address to, uint256 tokenId)`** - safely transfers an NFT, including additional checks to prevent loss |
| 56 | + - **`approve(address to, uint256 tokenId)`** - grants permission for another address to transfer a specific NFT |
| 57 | + - **`setApprovalForAll(address operator, bool approved)`** - allows an address to manage all of the owner's NFTs |
| 58 | + - **`balanceOf(address owner)`** - returns the number of NFTs owned by a specific address |
| 59 | + - **`ownerOf(uint256 tokenId)`** - returns the current owner of a specific NFT |
| 60 | +
|
| 61 | + !!! tip |
| 62 | + Use the [OpenZeppelin Contracts Wizard](https://wizard.openzeppelin.com/){target=\_blank} to generate customized smart contracts quickly. Simply configure your contract, copy the generated code, and paste it into Polkadot Remix IDE for deployment. |
| 63 | +
|
| 64 | +## Compile the Contract |
| 65 | +
|
| 66 | +Compilation is an stage that converts your Solidity source code into bytecode suitable for deployment on the blockchain. Throughout this process, the compiler examines your contract for syntax errors, verifies type safety, and produces the machine-readable instructions required for executing on the blockchain. |
| 67 | +
|
| 68 | +1. Select the **Solidity Compiler** plugin from the left panel |
| 69 | +
|
| 70 | +  |
| 71 | +
|
| 72 | +2. Click in the **Compile MyNFT.sol** button |
| 73 | +
|
| 74 | +  |
| 75 | +
|
| 76 | +3. If the compilation succeeded, you can see a green checkmark indicating success in the **Solidity Compiler** icon |
| 77 | +
|
| 78 | +  |
| 79 | +
|
| 80 | +## Deploy the Contract |
| 81 | +
|
| 82 | +Deployment is the process of uploading your compiled smart contract to the blockchain, allowing for interaction. During deployment, you will instantiate your contract on the blockchain, which involves: |
| 83 | +
|
| 84 | +1. Select the **Deploy & Run Transactions** plugin from the left panel |
| 85 | +
|
| 86 | +  |
| 87 | +
|
| 88 | +2. Configure the deployment settings |
| 89 | + 1. From the **ENVIRONMENT** dropdown, select **Westend Testnet - MetaMask** |
| 90 | + 2. From the **ACCOUNT** dropdown, select the account you want to use for the deploy |
| 91 | +
|
| 92 | +  |
| 93 | +
|
| 94 | +3. Configure the contract parameters |
| 95 | + 1. Enter the address that will own the deployed NFT. |
| 96 | + 2. Click the **Deploy** button to initiate the deployment |
| 97 | +
|
| 98 | +  |
| 99 | +
|
| 100 | +4. MetaMask will pop up - review the transaction details. Click **Confirm** to deploy your contract |
| 101 | +
|
| 102 | + {: .browser-extension} |
| 103 | +
|
| 104 | + If the deployment process succeeded, you will see the following output in the terminal: |
| 105 | +
|
| 106 | +  |
| 107 | +
|
| 108 | +## Interact with Your NFT Contract |
| 109 | +
|
| 110 | +Once deployed, you can interact with your contract through Remix: |
| 111 | +
|
| 112 | +1. Find your contract under **Deployed/Unpinned Contracts**, and click it to expand the available methods for the contract |
| 113 | +
|
| 114 | +  |
| 115 | +
|
| 116 | +2. To mint an NFT |
| 117 | +
|
| 118 | + 1. Click on the contract to expand its associated methods |
| 119 | + 2. Expand the **safeMint** function |
| 120 | + 3. Enter the recipient address |
| 121 | + 4. Click **Transact** |
| 122 | +
|
| 123 | +  |
| 124 | +
|
| 125 | +3. Click **Confirm** to confirm the transaction in MetaMask |
| 126 | +
|
| 127 | + {: .browser-extension} |
| 128 | +
|
| 129 | + If the transaction is successful, the terminal will display the following output, which details the information about the transaction, including the transaction hash, the block number, the associated logs, and so on. |
| 130 | +
|
| 131 | +  |
| 132 | +
|
| 133 | +Feel free to explore and interact with the contract's other functions using the same approach - selecting the method, providing any required parameters, and confirming the transaction through MetaMask when needed. |
0 commit comments