旨在促進軟件應用程序中NFT(ERC721,ERC1155)令牌的集成和採用的Python軟件包。
在1.2.2a1中,集成了輸出類,但所有功能都返回RAW
NFTPY可以通過RPC與以太坊虛擬機(EVM)進行互動,以檢索合同詳細信息和令牌持有人。它提供了客戶和區塊鏈之間的直接通信路徑。當前,不支持交易方法,但將在以後的更新中實現。可用以下方法:
NFTPY包括與以太坊錢包互動的全面功能,包括查詢餘額,獲取汽油價格和轉移NFT。錢包界面支持只讀和交易操作。
錢包特徵:
NFTPY包括一個內置界面,用於通過API鍵與Opensea進行交互。這允許對OpenSea進行包裝查詢,從而訪問定價信息和其他特定於OpenSea的數據。可以將OpenSea接口配置為專注於單個集合或查詢多個集合。可用方法包括:
Opensea類:
OpenSeAcollection類:
OpenSeawallet類:
NFTPY包括通過API鍵與稀有交互的綜合接口。這允許包裝查詢可稀有,以訪問NFT信息,市場數據等。可用方法包括:
稀有類:
NFTPY允許創建具有特定鏈條ID,RPC URL,Explorer URL和名稱的自定義鏈。此功能通過啟用庫中未預定義的區塊鍊網絡的添加來增強靈活性。
創建自定義鏈:
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
)使用nftpy.evm.nft,我們將在以太坊主網上查詢Pixelmon NFT系列!我們將首先從創建班級開始。我們將通過三個論點來定義我們的班級:
import nftpy . EVM as EVM
Pixelmon = EVM . NFT ( "0x32973908FaeE0Bf825A343000fE412ebE56F802A" , abi = EVM . ABI . ERC721 , network = EVM . Chains . ETH )
# Contract Address ABI Network To Query (Ethereum)現在我們已經創建了NFT對象,我們可以查詢它。我們將從獲得令牌的元數據開始。我只是將隨機令牌作為論點。
print ( Pixelmon . get_token_metadata ( 5580 ))運行此操作後,我們應該看到一個類似於此的輸出
{
"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 "
}
我們可以為此做更多的事情。例如:
print ( Pixelmon . get_token_uri ( 5580 ))
print ( Pixelmon . get_owner ( 5580 ))
print ( Pixelmon . get_balance ( "0x5AF7875766D1a50d144DF63E581c0764f6573487" ))
print ( Pixelmon . get_approved ( 5580 ))對於ERC1155代幣,您可以查詢多個令牌ID的餘額並檢查批准:
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 } ' )創建NFTWallet的實例需要一個私鑰以進行完整訪問,或者只是一個僅讀取訪問的地址。您還可以指定多個鏈以同時連接到不同網絡。
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'} 獲得交易哈希後,我們可以將程序延遲延遲,直到區塊鏈上的交易過程為止。
# 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 當使用僅讀取地址(即僅提供地址而不是私鑰)時,您仍然可以與區塊鏈進行查詢信息進行交互,但是您將無法執行交易。這對於監視錢包和檢索數據很有用,而無需敏感憑證。
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}我們將首先通過以下參數創建我們的班級:
請注意:定義鏈條時,應使用nftpy.OpenSea.OpenSeaChain進行,因為API需要特殊的鏈條定義格式。
from nftpy import OpenSea , OpenSeaChain
opensea = OpenSea ( api_key = 'your-opensea-api-key' , chain = OpenSeaChain . POLYGON )要查詢NFT集合的統計數據,請使用以下方法:
opensea . get_collection_stats ( 'your-collection-slug' )運行後,我們應該看到一個類似於此的輸出:
{
"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
}
}要獲取集合的詳細信息,請使用以下方法:
opensea . get_collection ( 'your-collection-slug' )要獲取特定NFT的詳細信息,請使用以下方法:
opensea . get_nft ( '0xYourContractAddress' , '1' )要列出與特定NFT相關的事件,請使用以下方法:
opensea . list_events_by_nft ( '0xYourContractAddress' , '1' )要列出特定帳戶擁有的NFT,請使用以下方法:
opensea . list_nfts_by_account ( '0xYourWalletAddress' )要管理集合,請創建一個OpenSeaCollection類的實例:
from nftpy import OpenSeaCollection
collection = OpenSeaCollection ( collection_name = 'your-collection-name' , api_key = 'your-api-key' )要獲取特定集合的詳細信息,請使用以下方法:
details = collection . get_collection_details ()要在集合中列出所有NFT,請使用以下方法:
nfts = collection . get_nfts ()要管理錢包,請創建一個openseawallet類的實例:
from nftpy import OpenSeaWallet
wallet = OpenSeaWallet ( address = 'your-wallet-address' , api_key = 'your-api-key' )要檢查錢包的餘額,請使用以下方法:
balance = wallet . get_balance ()要檢索錢包擁有的所有NFT,請使用以下方法:
nfts = wallet . get_nfts ()我們將首先通過以下參數創建我們的班級:
請注意:定義鏈條時,應使用nftpy.rarible.rariblechain進行,因為API需要針對鏈條定義的特殊格式。
from nftpy import Rarible , RaribleChain
rarible = Rarible ( api_key = 'your-rarible-api-key' , chain = RaribleChain . ETHEREUM )要通過其ID獲取特定項目的詳細信息,請使用以下方法:
rarible . get_item_by_id ( 'item_id' )要通過其ID獲取多個項目的詳細信息,請使用以下方法:
rarible . get_items_by_ids ([ 'item_id1' , 'item_id2' ])要通過其ID檢索特定項目的特許權使用費信息,請使用以下方法:
rarible . get_item_royalties_by_id ( 'item_id' )要獲取由特定地址擁有的項目,請使用以下方法:
rarible . get_items_by_owner ( 'owner_address' )要驗證給定數據集的簽名,請使用以下方法:
rarible . validate_signature ( data = { 'your' : 'data' })要獲取生成簽名所需的輸入數據,請使用以下方法:
rarible . get_signature_input ( data = { 'your' : 'data' })要編碼可稀有協議的數據,請使用以下方法:
rarible . encode_data ( data = { 'your' : 'data' })要獲得特定貨幣的美元匯率,請使用以下方法:
rarible . get_usd_rate ( 'currency' )要獲取所有支持的貨幣,請使用以下方法:
rarible . get_all_currencies ()要以指定貨幣檢索特定用戶的餘額,請使用以下方法:
rarible . get_user_balance ( 'user_address' , 'currency' )