thirdweb

Create a Pack with Python

Create a Pack with Python

Published on:

December 27, 2021

Create your own Pack of NFTs using the thirdweb Python SDK

Raza Zaidi

Intro

This guide explains in 7 easy steps how to create a pack. We need to create a project in the dashboard and create a Pack module inside our project. 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.

Dashboard setup

The first thing we need to do is head over to the dashboard and create a project and a Bundle Collection module. if you don't know how to do this, check out this guide.

After you're done this is what you Pack module should look like in the dashboard. I called my pack module random.

A pack module

A pack module

Create a Pack - 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.py and open it in your favorite code editor. We'll use Visual Studio.

Creating a python file inside the terminal

Creating a python file inside the terminal

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

Conda

#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.

# Importing stuff from thirdweb import ThirdwebSdk, SdkOptions, CreatePackArg, AssetAmountPair from dotenv import loadenv import os

In order to make use of our SDK, you need to establish a connection to a blockchain. Click here to learn how to connect to the blockchain and instantiate our SDK.

For this guide, you will need to instantiate the SDK following these steps, using your own signer with thirdweb.

3. Define which module to use

Now we need to define which module we want to use. This is the module we created inside our project. This module has an address. We need to pass the address here. You can find the address inside the dashboard:

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

Then we create an object that uses the Pack module inside the sdk with our address.

#pick your module and enter the smart contract address pack_contract = "0xEA9e245C4663AC7A82Df0329A1a480C4729adC52" pack_module = sdk.get_pack_module(pack_contract)

4. Time to create the pack!

The arguments passed over here are the same as inside the dashboard. If you want to familiarize yourself with the process, check out the dashboard!

Note, the Pack module only works with NFTs from the 'Drop' and 'Bundle' module, because these modules are based on ERC-1155. If this is new to you, in short a Pack holds multiple NFTs.

#Create the Pack pack_module.create(CreatePackArg( #asset_contract_address: The address of the asset being put in the pack #Note, the Pack module only works with NFTs from the 'Drop' and 'Bundle' module asset_contract_address= bundle_smart_contract_address, #metadata: The metadata of the pack (name, description, image, etc) metadata=('packme'), #assets: The list of assets (pair of [id, amount]) to list in in the pack assets= [AssetAmountPair(amount=1, token_id=1), AssetAmountPair(amount=1, token_id=0)], #rewards_per_open: The number of tokens that will be awarded when opening the pack rewards_per_open=1, #seconds_until_open_start: The number of seconds the pack is allowed to be opened seconds_until_open_start=0, ))

5. Results

Get the balance to check it out or head over to the dashboard.

#check your balance to check if you minted an nft! print(pack_module.get_all())

Full Code Snippet

from thirdweb import ThirdwebSdk, SdkOptions, CreatePackArg, AssetAmountPair from dotenv import load_dotenv import os #network = "https://polygon-rpc.com" network = "https://rpc-mumbai.maticvigil.com" sdk = ThirdwebSdk(SdkOptions(), network) load_dotenv() PRIVATE_KEY = os.getenv('PRIVATE_KEY') sdk.set_private_key(PRIVATE_KEY) bundle_smart_contract_address = "0xC92702c53620DcA7B0d2898e361333957f547525" pack_contract = "0xEA9e245C4663AC7A82Df0329A1a480C4729adC52" pack_module = sdk.get_pack_module(pack_contract) pack_module.create(CreatePackArg( asset_contract_address= bundle_smart_contract_address, metadata=('packme'), assets= [AssetAmountPair(amount=1, token_id=1), AssetAmountPair(amount=1, token_id=0)], rewards_per_open=1, seconds_until_open_start=0, ))

Open a Pack

In order to open a Pack we can re-use most of the code. instead of the create() method we will call the open_pack method 👇

You can find the ‘pack_id' inside your dashboard of fetch the data by using the get_all method we used in step 7.

#pass the pack_id inside the method. pack_module.open_pack(1)

Open a random Pack with Deposit Link

To open a random pack we need to perform some additional steps. In order to create a random generated number, we need the help of ChainLink.

The following method, gets a random number from Chainlink and passes it on to our sdk.

The argument that we're passing to our method is the amount of Chainlink tokens we need to pay. To make use of Chainlink we need special tokens. For the Rinkeby network you can get them here.

#Creating a deposit link pack_module.deposit_link(1)

That's it!

Congratulations! You have now created your own pack using only Python code. The next step is to open it! If you have any questions, drop by our discord!


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

Raza Zaidi

Contents

Intro

Dashboard setup

Create a Pack - The code

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

Virtual-env

Conda

2. Import the SDK

3. Define which module to use

4. Time to create the pack!

5. Results

Full Code Snippet

Open a Pack

Open a random Pack with Deposit Link

That's it!