Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
A Python package designed to facilitate the integration and adoption of NFT (ERC721, ERC1155) tokens in software applications.
nftpy enables interaction with the Ethereum Virtual Machine (EVM) through RPC to retrieve contract details and token holders. It provides a direct communication pathway between the client and the blockchain. Currently, transactional methods are not supported but will be implemented in future updates. The following methods are available:
nftpy includes comprehensive features for interacting with Ethereum wallets, including querying balances, fetching gas prices, and transferring NFTs. The wallet interface supports both read-only and transactional operations.
Wallet Features:
nftpy includes a built-in interface for interacting with OpenSea via an API key. This allows for in-package queries to OpenSea, enabling access to pricing information and other OpenSea-specific data. The OpenSea interface can be configured to focus on a single collection or query multiple collections. The available methods include:
OpenSea Class:
OpenSeaCollection Class:
OpenSeaWallet Class:
nftpy includes a comprehensive interface for interacting with Rarible via an API key. This allows for in-package queries to Rarible, enabling access to NFT information, market data, and more. The available methods include:
Rarible Class:
nftpy allows the creation of custom chains with specific chain IDs, RPC URLs, explorer URLs, and names. This feature enhances flexibility by enabling the addition of blockchain networks that are not predefined in the library.
Creating a Custom Chain:
from nftpy.EVM import Chain
custom_chain = Chain(
name = "Ethereum",
symbol = "ETH",
chain_id = 1,
rpc_url = "https://eth.llamarpc.com",
explorer_url = "https://etherscan.io",
testnet = False
)
Using nftpy.EVM.NFT we are going to be querying the Pixelmon NFT collection on Ethereum mainnet! We will first start off by creating our class. We are going to define our class with three arguments:
import nftpy.EVM as EVM
Pixelmon = EVM.NFT("0x32973908FaeE0Bf825A343000fE412ebE56F802A", abi=EVM.ABI.ERC721, network=EVM.Chains.ETH)
# Contract Address ABI Network To Query (Ethereum)
Now that we have created our NFT object, we can query it. We will start with getting the metadata of a token. I am just putting a random token as the argument.
print(Pixelmon.get_token_metadata(5580))
After running this we should see an output resembling this
{
"name": "Pixelmon #5580",
"image_url": "https://pixelmon-training-rewards.s3-accelerate.amazonaws.com/0/Moler.jpg",
"external_url": "https://pixelmon.club/",
"reward_bitmask": 6,
"attributes": [
{"trait_type": "Species", "value": "Moler"},
{"trait_type": "Origin", "value": "Earth"},
{"trait_type": "Rarity", "value": "Uncommon"},
{"trait_type": "Evolution", "value": "Evolution 1"},
{"trait_type": "Hatched On", "display_type": "date", "value": 1672272943}
],
"animation_url": "https://pixelmon-training-rewards.s3-accelerate.amazonaws.com/6/Moler.mp4"
}
We can do a lot more with this. For example:
print(Pixelmon.get_token_uri(5580))
print(Pixelmon.get_owner(5580))
print(Pixelmon.get_balance("0x5AF7875766D1a50d144DF63E581c0764f6573487"))
print(Pixelmon.get_approved(5580))
For ERC1155 tokens, you can query balances for multiple token IDs and check approvals:
erc1155_nft = EVM.NFT(contract_address='0xYourERC1155ContractAddress', network=EVM.Chains.ETH, abi=EVM.ABI.ERC1155)
wallet_address = '0xYourWalletAddress'
token_id = 1
token_ids = [1, 2, 3, 4, 5]
# Get the balance of a specific token owned by the wallet
token_balance = erc1155_nft.get_token_balance(wallet_address, token_id)
print(f'Token ID {token_id} Balance: {token_balance}')
# Get the balance of multiple tokens owned by the wallet
tokens_balance = erc1155_nft.get_tokens(wallet_address, token_ids)
print(f'Tokens Balance: {tokens_balance}')
# Check if an address is approved for all tokens (ERC1155)
is_approved_erc1155 = erc1155_nft.is_approved_for_all_erc1155(wallet_address, '0xOperatorAddress')
print(f'Is Approved For All (ERC1155): {is_approved_erc1155}')
Creating an instance of NFTWallet
requires either a private key for full access or just an address for read-only access. You can also specify multiple chains to connect to different networks simultaneously.
from nftpy import *
# Initialize the wallet with a private key and specify chains
wallet = NFTWallet(private_key="0x9015a0eb4c1ceab5f5544ac6e0a75eabb37d7dec26f1dfcb09adb43632330736", chains=[Chains.ETH_SEPOLIA])
# Get the balance of the wallet in Ether
print(wallet.get_balance())
# Output: {"Balances": {'Sepolia Testnet': Decimal('0.8341469847291797')}}
# Get the balance of the wallet in Wei
print(wallet.get_balance_wei())
# Output: {"Balances": {'Sepolia Testnet': 834146984729179700}}
# Get the current gas price in Wei
print(wallet.get_gas_price_wei())
# Output: {'Sepolia Testnet': 20000000000}
# Get the current gas price in Gwei
print(wallet.get_gas_price_gwei())
# Output: {'Sepolia Testnet': Decimal('20')}
# Transfer an NFT to another wallet
to_wallet = "0xa693190103733280E23055BE70C838d9b6708b9a"
contract = "0x725Ea5eEA79F1515e34A921b83D4307b325cC8b9"
gas_price = wallet.get_gas_price_gwei()["Sepolia Testnet"]
gas_limit = 65000 # Disclaimer! Gas Limit set for Sepolia, WILL fail on other networks
# Transfer the NFT and get the transaction hash and explorer URL
print(wallet.transfer_nft(to=to_wallet, contract_address=contract, amount=1, gas_limit=gas_limit,
gas_price_gwei=gas_price, abi=ABI.OPENSEA_ERC1155, token_id=1))
# Output: {'transaction_hash': '0x18a076a4a30c1cc014b1620aa907db06a04e8a709bda47e9beed2233a23f532f', 'explorer_url': 'https://sepolia.etherscan.io/tx/0x18a076a4a30c1cc014b1620aa907db06a04e8a709bda47e9beed2233a23f532f'}
After we get the transaction hash, we can have the program delay until the transaction processes on the blockchain.
# Wait until the transaction is processed
transaction_hash = "0xcd74c93bbf42cae24f329c45da995bde7e1c89ea848855d04db516c6460eda02"
print(wallet.wait_until_transaction_processes(transaction_hash, chain=Chains.ETH_SEPOLIA))
# Output: True | When the transaction fully processes on the blockchain
When using a read-only address (i.e., only providing an address and not a private key), you can still interact with the blockchain to query information, but you will not be able to perform transactions. This is useful for monitoring wallets and retrieving data without the need for sensitive credentials.
from nftpy import *
# Initialize the wallet with an address and specify chains
readonly_wallet = NFTWallet(address="0xYourReadOnlyWalletAddress", chains=[Chains.ETH_SEPOLIA])
# Get the balance of the wallet in Ether
print(readonly_wallet.get_balance())
# Output: {"Balances": {'Sepolia Testnet': Decimal('0.123456789012345678')}}
# Get the balance of the wallet in Wei
print(readonly_wallet.get_balance_wei())
# Output: {"Balances": {'Sepolia Testnet': 123456789012345678}}
# Get the current gas price in Wei
print(readonly_wallet.get_gas_price_wei())
# Output: {'Sepolia Testnet': 20000000000}
We will first start by creating our class with the following arguments:
Please Note: When defining the chain, it should be done with nftpy.OpenSea.OpenSeaChain
as the API requires a special format for chain definition.
from nftpy import OpenSea, OpenSeaChain
opensea = OpenSea(api_key='your-opensea-api-key', chain=OpenSeaChain.POLYGON)
To query the stats of an NFT collection, use the following method:
opensea.get_collection_stats('your-collection-slug')
After running that, we should see an output resembling this:
{
"stats": {
"one_day_volume": 12.34,
"one_day_change": 0.56,
"one_day_sales": 78,
"one_day_average_price": 0.16,
"total_volume": 1234.56,
"total_sales": 7890,
"total_supply": 10000,
"count": 10000,
"num_owners": 2345,
"average_price": 0.123,
"num_reports": 0,
"market_cap": 4567.89,
"floor_price": 0.123
}
}
To fetch details of a collection, use the following method:
opensea.get_collection('your-collection-slug')
To get details of a specific NFT, use the following method:
opensea.get_nft('0xYourContractAddress', '1')
To list events related to a specific NFT, use the following method:
opensea.list_events_by_nft('0xYourContractAddress', '1')
To list NFTs owned by a specific account, use the following method:
opensea.list_nfts_by_account('0xYourWalletAddress')
To manage a collection, create an instance of the OpenSeaCollection class:
from nftpy import OpenSeaCollection
collection = OpenSeaCollection(collection_name='your-collection-name', api_key='your-api-key')
To get details of a specific collection, use the following method:
details = collection.get_collection_details()
To list all NFTs within a collection, use the following method:
nfts = collection.get_nfts()
To manage a wallet, create an instance of the OpenSeaWallet class:
from nftpy import OpenSeaWallet
wallet = OpenSeaWallet(address='your-wallet-address', api_key='your-api-key')
To check the balance of the wallet, use the following method:
balance = wallet.get_balance()
To retrieve all NFTs owned by the wallet, use the following method:
nfts = wallet.get_nfts()
We will first start by creating our class with the following arguments:
Please Note: When defining the chain, it should be done with nftpy.Rarible.RaribleChain as the API requires a special format for chain definition.
from nftpy import Rarible, RaribleChain
rarible = Rarible(api_key='your-rarible-api-key', chain=RaribleChain.ETHEREUM)
To fetch details of a specific item by its ID, use the following method:
rarible.get_item_by_id('item_id')
To fetch details of multiple items by their IDs, use the following method:
rarible.get_items_by_ids(['item_id1', 'item_id2'])
To retrieve royalty information for a specific item by its ID, use the following method:
rarible.get_item_royalties_by_id('item_id')
To fetch items owned by a specific address, use the following method:
rarible.get_items_by_owner('owner_address')
To validate a signature for a given data set, use the following method:
rarible.validate_signature(data={'your': 'data'})
To get input data required for generating a signature, use the following method:
rarible.get_signature_input(data={'your': 'data'})
To encode data for the Rarible protocol, use the following method:
rarible.encode_data(data={'your': 'data'})
To get the USD exchange rate for a specific currency, use the following method:
rarible.get_usd_rate('currency')
To fetch all supported currencies, use the following method:
rarible.get_all_currencies()
To retrieve the balance of a specific user in a specified currency, use the following method:
rarible.get_user_balance('user_address', 'currency')
FAQs
A NFT specific Python library for easy NFT integration in Python
We found that nftpy demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.