thirdweb

Create a NFT Marketplace with Python

Create a NFT Marketplace with Python

marketplacepythonsdk

Published on:

November 19, 2021

Learn how to create your own marketplace to sell your own NFTs with Python

Pratham Prasoon & Raza Zaidi

Intro

In this guide we'll create our own marketplace with the Python SDK! In a marketplace you can list your tokens, like NFTs, for sale. Opensea is an example of a marketplace.

Did you know you can do both? After we create our marketplace, we will list the NFT we minted with our SDK in our previous guide. Check that guide out over here.

By the way, we also need to connect a wallet. So if you don't have a wallet, make sure to sign up with MetaMask or CoinWallet. We are gonna use Metamask, if you don't have one, you can follow this guide.

Alright without further ado, let's create our marketplace!

Dashboard setup

Create a Project and Module

Head over to the dashboard and connect your wallet, select a network and create a project. We will be working on the Rinkeby Test Network on this guide.

Let's call our project Minting NFT's with Python and click on 'Create'!

You will be asked to confirm a transaction. Click 'confirm' to create the project.

Let's head over to our project and add a 'Module'.

We want to create a marketplace, so we need the 'Market' module for this.

Go ahead and fill in a name for this module.

You will be asked to confirm two transactions again to create the module.

Now it's time to list our first NFT, but we don't need the dashboard for that! I mean we could, but why use GUI if we can use Python!?

The code

1. Create a Python file and fire up a virtual environment

Go ahead and create a new Python file. Let's call it python_nft and open it in your favorite code editor. We'll use VS code.

Next up use the following code in your terminal to create a virtual environment and install the ThirdWeb SDK!

virtual-env
#create a virtual environment
python3 -m venv ./myenv

#activate virtual environment
source ./myenv/bin/activate

#install the thirdweb sdk
pip install thirdweb-sdk
virtual-env
#create a virtual environment
conda create -n "thirdweb" python=3.9

#activate virtual environment
conda activate thirdweb

#install the thirdweb sdk
pip install thirdweb-sdk

2. Import the SDK

Let's take the code step by step. At the end we'll include a full code snippet.

Import the following classes from the sdk.

We will use the dotenv library for our private key in a bit.

python_nft.py
# Importing stuff
from thirdweb import ThirdwebSdk, SdkOptions, MintArg
from dotenv import loadenv
import os

3. Choose your network

Over here we need to enter the network that we will use. If you don't know what the url of your network looks like, take a look over here.

To start using the SDK, we will create object inheriting all the methods inside the SDK.

python_nft.py
#choose your network
network = "https://rinkeby-light.eth.linkpool.io/"

#create the object
sdk = ThirdwebSdk(SdkOptions(), network)

4.Setting the Private Key

So your wallet has a private key. We will need that to make sure any transaction is done from this wallet. This is where the sdk uses your wallet.

Create a .env file with no extensions. Open the .env file in your code editor and paste your private key inside a variable called PRIVATE_KEY.

python_nft.py
#example of the '.env' file in the same folder

PRIVATE_KEY = '1234OUI132UIO5YAZKSJFDGIO'
python_nft.py
#use the '.env' file inside Python
load_dotenv("./.env")
PRIVATE_KEY = os.getenv('PRIVATE_KEY')

#over here we connect our wallet via the private key stored in the '.env' file
sdk.set_private_key("PRIVATE_KEY")

5. Define which module to use

Now we need to define which module we want to use. This is the module we just created inside our project Minting NFTs with Python. We called the module 'Cool NFT marketplace'. This module has an address. We need to pass the address here. You can find the address here, under 'Cool NFT marketplace':

In our python_nft.py file we include the following code. First we define a variable market_smart_contract_address and store our module address in here.

Then we create an object that uses the market module inside the SDK with our address.

python_nft.py
#pick your module and enter the smart contract address
market_smart_contract_address = "0x56c1F1f63dC507F24495E5fD56003D56FD12A7F9"
market_module = sdk.get_market_module(market_smart_contract_address)

#pick your module and enter the smart contract address
nft_smart_contract_address = "0xdd25FAEE772FbB1bcB7ba0b2cEE6387A8F82f032"
nft_module = sdk.get_nft_module(nft_smart_contract_address)

#currency

6. Time to list an NFT on the marketplace!

We are using the NFT that we created in our previous guide. Each NFT has token_id. We will pass that the following parameters to the method list, via the class ListArg:

  • token_id - id of the NFT. You can find this ID in the NFT Collection module or by using the XXX method in the NFT Collection module
  • currency_contract - address of the Token module
  • price_per_token - quantity of tokens
  • quantity - amount of nft's you want to sell
  • tokens_per_buyer - how many tokens a buyer can buy at once (the total limit)
  • seconds_until_start - time in seconds until the method will execute and list the token
  • seconds_until_end - period in seconds that the NFT will remain listed
python_nft.py
#list an asset in the marketplace
#what arguments should be passed for the listarg
market_module.list(ListArg(asset_contract=nft_smart_contract_address,
token_id=1,
currency_contract="0x75b37A3EDB89bedF68D7866e4c539f914F7A985c",
price_per_token=5000000000000000000,
quantity=1,
tokens_per_buyer=1,
seconds_until_start=30,
seconds_until_end=3600))

7. Results

Use this method to get all the listings or head over to the dashboard.

python_nft.py
#check out out all your listings!!
print(market_module.get_all_listings())

That's it!

Now you have your own marketplace built with the Python SDK, without writing a single line of Solidity, isn't that nice?


Ready to build your first web3 app? Get early access & add web3 features to your project today.

Pratham Prasoon & Raza Zaidi

Contents

Intro

Dashboard setup

Create a Project and Module

The code

1. Create a Python file and fire up a virtual environment

2. Import the SDK

3. Choose your network

4.Setting the Private Key

5. Define which module to use

6. Time to list an NFT on the marketplace!

7. Results

That's it!