-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: nft collection example for python (#739)
* python examples * Fix examples * added counter --------- Co-authored-by: Thoralf Müller <[email protected]>
- Loading branch information
Showing
14 changed files
with
139 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
bindings/python/examples/how_tos/nft_collection/00_mint_issuer_nft.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from iota_sdk import Wallet, Utils, utf8_to_hex | ||
from dotenv import load_dotenv | ||
import os | ||
|
||
load_dotenv() | ||
|
||
# In this example we will mint the issuer NFT for the NFT collection. | ||
|
||
wallet = Wallet(os.environ['WALLET_DB_PATH']) | ||
|
||
if 'STRONGHOLD_PASSWORD' not in os.environ: | ||
raise Exception(".env STRONGHOLD_PASSWORD is undefined, see .env.example") | ||
|
||
wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) | ||
|
||
account = wallet.get_account('Alice') | ||
|
||
# Sync account with the node | ||
account.sync() | ||
|
||
# Issue the minting transaction and wait for its inclusion | ||
print('Sending NFT minting transaction...') | ||
params = { | ||
"immutableMetadata": utf8_to_hex("This NFT will be the issuer from the awesome NFT collection"), | ||
} | ||
|
||
prepared = account.prepare_mint_nfts([params]) | ||
transaction = prepared.send() | ||
|
||
# Wait for transaction to get included | ||
block_id = account.retry_transaction_until_included(transaction.transactionId) | ||
|
||
print( | ||
f'Block sent: {os.environ["EXPLORER_URL"]}/block/{block_id}') | ||
|
||
essence = transaction.payload["essence"] | ||
|
||
for outputIndex, output in enumerate(essence["outputs"]): | ||
# New minted NFT id is empty in the output | ||
if output["type"] == 6 and output["nftId"] == '0x0000000000000000000000000000000000000000000000000000000000000000': | ||
outputId = Utils.compute_output_id(transaction.transactionId, outputIndex) | ||
nftId = Utils.compute_nft_id(outputId) | ||
print(f'New minted NFT id: {nftId}') |
69 changes: 69 additions & 0 deletions
69
bindings/python/examples/how_tos/nft_collection/01_mint_collection_nft.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from iota_sdk import Wallet, Utils, utf8_to_hex | ||
from dotenv import load_dotenv | ||
import os | ||
import sys | ||
import json | ||
|
||
load_dotenv() | ||
|
||
# The NFT collection size | ||
NFT_COLLECTION_SIZE = 150 | ||
# Mint NFTs in chunks since the transaction size is limited | ||
NUM_NFTS_MINTED_PER_TRANSACTION = 50 | ||
|
||
# In this example we will mint some collection NFTs with issuer feature. | ||
|
||
if len(sys.argv) < 2: | ||
raise Exception("missing example argument: ISSUER_NFT_ID") | ||
|
||
issuer_nft_id = sys.argv[1] | ||
|
||
wallet = Wallet(os.environ['WALLET_DB_PATH']) | ||
|
||
if 'STRONGHOLD_PASSWORD' not in os.environ: | ||
raise Exception(".env STRONGHOLD_PASSWORD is undefined, see .env.example") | ||
|
||
wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) | ||
|
||
account = wallet.get_account('Alice') | ||
|
||
# Sync account with the node | ||
account.sync() | ||
|
||
bech32_hrp = wallet.get_client().get_bech32_hrp() | ||
issuer = Utils.nft_id_to_bech32(issuer_nft_id, bech32_hrp) | ||
|
||
def get_immutable_metadata(index: int, issuer_nft_id: str) -> str: | ||
data = { | ||
"standard": "IRC27", | ||
"version": "v1.0", | ||
"type": "video/mp4", | ||
"uri": "ipfs://wrongcVm9fx47YXNTkhpMEYSxCD3Bqh7PJYr7eo5Ywrong", | ||
"name": "Shimmer OG NFT #" + str(index), | ||
"description": "The Shimmer OG NFT was handed out 1337 times by the IOTA Foundation to celebrate the official launch of the Shimmer Network.", | ||
"issuerName": "IOTA Foundation", | ||
"collectionId": issuer_nft_id, | ||
"collectionName": "Shimmer OG" | ||
} | ||
return json.dumps(data, separators=(',', ':')) | ||
|
||
# Create the metadata with another index for each | ||
nft_mint_params = list(map(lambda index: { | ||
"immutableMetadata": utf8_to_hex(get_immutable_metadata(index, issuer_nft_id)), | ||
"issuer": issuer | ||
}, range(NFT_COLLECTION_SIZE))) | ||
|
||
while nft_mint_params: | ||
chunk, nft_mint_params = nft_mint_params[:NUM_NFTS_MINTED_PER_TRANSACTION], nft_mint_params[NUM_NFTS_MINTED_PER_TRANSACTION:] | ||
print(f'Minting {len(chunk)} NFTs... ({NFT_COLLECTION_SIZE-len(nft_mint_params)}/{NFT_COLLECTION_SIZE})') | ||
prepared = account.prepare_mint_nfts(chunk) | ||
transaction = prepared.send() | ||
|
||
# Wait for transaction to get included | ||
block_id = account.retry_transaction_until_included(transaction.transactionId) | ||
|
||
print( | ||
f'Block sent: {os.environ["EXPLORER_URL"]}/block/{block_id}') | ||
|
||
# Sync so the new outputs are available again for new transactions | ||
account.sync() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters