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

Add Create an NFT Collection using LSP8 tutorial #715

Merged
merged 15 commits into from
Nov 24, 2023
Merged
Changes from 6 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
168 changes: 168 additions & 0 deletions docs/learn/smart-contract-developers/create-nft-collection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
---
sidebar_label: '🗃 Create an NFT Collection'
sidebar_position: 4
---

# Create an NFT Collection Using LSP8

This tutorial will explore how to create a collection of unique digital assets.
biancabuzea200 marked this conversation as resolved.
Show resolved Hide resolved

:::info
⌨️ The full code of this example can be found in the 👾 [lukso-hardhat-template](https://github.com/CJ42/LUKSO-Hardhat-template).
:::
biancabuzea200 marked this conversation as resolved.
Show resolved Hide resolved

:::note

This guide builds on top of a Hartdhat project using TypeScript as described in the [Getting Started section](../smart-contract-developers/getting-started.md).
biancabuzea200 marked this conversation as resolved.
Show resolved Hide resolved

:::

## Setup

Make sure you have the following dependencies installed:

- [`ethers.js`](https://github.com/ethers-io/ethers.js/) (alternatively you can use [`web3.js`](https://github.com/web3/web3.js))
biancabuzea200 marked this conversation as resolved.
Show resolved Hide resolved
- [`@lukso/lsp-smart-contracts`](https://github.com/lukso-network/lsp-smart-contracts/)

biancabuzea200 marked this conversation as resolved.
Show resolved Hide resolved
```bash
npm install ethers @lukso/lsp-smart-contracts
```

## Create the Smart Contracts

When creating smart contracts representing digital assets on LUKSO, you will need to specify the token type and data keys for the 📄 [LSP4 Digital Asset Metadata](../../standards/tokens/LSP4-Digital-Asset-Metadata) that will be stored in the 🗂️ [ERC725Y](../../standards/lsp-background/erc725.md#erc725y-generic-data-keyvalue-store) storage of the Universal Profile. There are three different token types:
biancabuzea200 marked this conversation as resolved.
Show resolved Hide resolved

- `0` = Token
- `1` = NFT
- `2` = Collection

```solidity title="contracts/TokenTypes.sol"
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

bytes32 constant _LSP4_TOKEN_TYPE_DATA_KEY = 0xe0261fa95db2eb3b5439bd033cda66d56b96f92f243a8228fd87550ed7bdfdb3;
biancabuzea200 marked this conversation as resolved.
Show resolved Hide resolved

enum TokenType {
TOKEN,
NFT,
COLLECTION
}
```

After defining the type of the asset and its 🗂️ [ERC725 data key](../../standards/lsp-background/erc725.md#erc725y-generic-data-keyvalue-store) you can create a custom 🌄 [LSP8 Identfiable Digital Asset Collection](../../standards/tokens/LSP8-Identifiable-Digital-Asset.md) that extends [LSP8Mintable](../../contracts/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8Mintable.md) so that new assets can be created within the smart contract.

```solidity title="contracts/MyNFTCollection.sol"
biancabuzea200 marked this conversation as resolved.
Show resolved Hide resolved

biancabuzea200 marked this conversation as resolved.
Show resolved Hide resolved
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

// modules
import {
LSP8Mintable
} from "@lukso/lsp-smart-contracts/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8Mintable.sol";

// constants
import {
_LSP8_TOKENID_TYPE_NUMBER
} from "@lukso/lsp-smart-contracts/contracts/LSP8IdentifiableDigitalAsset/LSP8Constants.sol";
import {_LSP4_TOKEN_TYPE_DATA_KEY, TokenType} from "../TokenTypes.sol";

contract BasicNFTCollection is LSP8Mintable {
constructor(
string memory nftCollectionName,
string memory nftCollectionSymbol,
address contractOwner
)
LSP8Mintable(
nftCollectionName, // NFT collection name
nftCollectionSymbol, // NFT collection symbol
contractOwner, // owner of the NFT contract (the address that controls it, sets metadata, can transfer the ownership of the contract)
_LSP8_TOKENID_TYPE_NUMBER // type of NFT/ tokenIds
)
{
// set token type
_setData(_LSP4_TOKEN_TYPE_DATA_KEY, abi.encode(TokenType.COLLECTION));
}
}
```

## Deploy your NFT Collection Contract on the LUKSO Testnet

:::info

By default, the deployment will be to your local network. If you want to deploy to the LUKSO Testnet, you will need to add the LUKSO Testnet network in your `hardhat.config.ts`.

:::

```js
function getTestnetChainConfig(): NetworkUserConfig {
const config: NetworkUserConfig = {
url: 'https://rpc.testnet.lukso.network',
chainId: 4201,
};

if (process.env.PRIVATE_KEY !== undefined) {
config['accounts'] = [process.env.PRIVATE_KEY];
}

return config;
}
```

Also add a definition for the testnet in the `HardhatUserConfig`:

```js
const config: HardhatUserConfig = {
solidity: {
version: '0.8.19',
settings: {
optimizer: {
enabled: true,
runs: 200,
},
biancabuzea200 marked this conversation as resolved.
Show resolved Hide resolved
},
},
typechain: {
outDir: 'typechain-types',
target: 'ethers-v6',
},
networks: {
luksoTestnet: getTestnetChainConfig(),
},
};
```

Next you define the deployment script:

biancabuzea200 marked this conversation as resolved.
Show resolved Hide resolved
```js
import { ethers } from "hardhat";

import {BasicNFTCollection, BasicNFTCollection__factory} from "../typechain-types";

async function deployLSP8Collection() {
const accounts = await ethers.getSigners();
const deployer = accounts[0];

const nftCollection: BasicNFTCollection = await new BasicNFTCollection__factory(deployer).deploy(
"NFT Collection Name", // collection name
"NFT", // collection symbol
deployer.address
);

}

deployLSP8Collection().catch((error) => {
console.error(error);
process.exitCode = 1;
});


```

## Check Your NFT Collection

You can now check your NFT collection using the [execution block explorer](https://explorer.execution.testnet.lukso.network/).
biancabuzea200 marked this conversation as resolved.
Show resolved Hide resolved

## References

- [BuildUP #2 | Create an NFT Collection using LSP7 or LSP8 (YouTube)](https://www.youtube.com/watch?v=DMpeMswK12w)