旨在促进软件应用程序中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' )