ERC-721 Caighdeán Comhartha Neamh-idirmhalartach
Eagarthóireacht dheireanach: @corwintines, 19 Samhain 2023
Réamhrá
Cad is Comhartha Neamh-idirmhalartach ann?
Baintear úsáid as Comhartha Neamh-idirmhalartach (NFT) chun rud nó duine éigin a aithint ar bhealach uathúil. Tá an cineál seo Comhartha foirfe le húsáid ar ardáin a thairgeann míreanna inbhailithe, eochracha rochtana, ticéid chrannchuir, suíocháin uimhrithe do cheolchoirmeacha agus cluichí spóirt, srl. Tá féidearthachtaí iontacha ag an gcineál speisialta Comhartha seo agus mar sin tá Caighdeán ceart, ERC-721, a tháinig chun é sin a réiteach, tuillte aige!
Cad é ERC-721?
Tugann an ERC-721 isteach caighdeán do NFT, i bhfocail eile, tá an cineál Comhartha seo uathúil agus is féidir luach difriúil a bheith aige ó Chomhartha eile ón gConradh Cliste céanna, b'fhéidir mar gheall ar a aois, a theirce nó fiú rud éigin eile cosúil lena amharc. Fan, amharc?
Sin é! Tá athróg uint256
ag gach NFT ar a dtugtar tokenId
, mar sin d'aon Chonradh ERC-721, ní mór an péire contract address, uint256 tokenId
a bheith uathúil ar fud an domhain. Mar sin féin, is féidir "tiontaire" a bheith ag dapp a úsáideann an tokenId
mar ionchur agus a aschuireann íomhá de rud éigin faiseanta, cosúil le zombaithe, airm, scileanna nó puisíní gleoite!
Réamhriachtanais
Comhlacht
An ERC-721 (Iarratas Ethereum ar Thuairimí 721), molta ag William Entriken, Dieter Shirley, Jacob Evans, Tá Nastassia Sachs i mí Eanáir 2018, ina Chaighdeán Comhartha Neamh-idirmhalartacha a chuireann API le haghaidh comharthaí laistigh de Chonarthaí Cliste i bhfeidhm.
Soláthraíonn sé feidhmiúlachtaí cosúil le comharthaí a aistriú ó chuntas amháin go cuntas eile, chun an iarmhéid reatha chomharthaí a fháil ar chuntas, chun úinéir chomhartha sainiúil a aimsiú agus freisin soláthar iomlán an chomhartha atá ar fáil ar an líonra. Chomh maith leo seo tá roinnt feidhmiúlachtaí eile aige ar nós an méid de chomhartha is féidir le cuntas tríú pháirtí a bhogadh ó chuntas a fhaomhadh.
Má chuireann Conradh Cliste na modhanna agus na teagmhais seo a leanas i bhfeidhm is féidir Conradh Comhartha Neamh-idirmhalartach ERC-721 a thabhairt air agus, ar é a imscaradh, beidh sé freagrach as súil a choinneáil ar na comharthaí cruthaithe ar Ethereum.
Ó EIP-721:
Modhanna
1 function balanceOf(address _owner) external view returns (uint256);2 function ownerOf(uint256 _tokenId) external view returns (address);3 function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable;4 function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable;5 function transferFrom(address _from, address _to, uint256 _tokenId) external payable;6 function approve(address _approved, uint256 _tokenId) external payable;7 function setApprovalForAll(address _operator, bool _approved) external;8 function getApproved(uint256 _tokenId) external view returns (address);9 function isApprovedForAll(address _owner, address _operator) external view returns (bool);Taispeáin gach rudCóip
Imeachtaí
1 event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);2 event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);3 event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);Cóip
Samplaí
Feicimis conas atá Caighdeán chomh tábhachtach chun go mbeadh sé simplí dúinn iniúchadh a dhéanamh ar aon Chonradh Chomhartha ERC-721 ar Ethereum. Níl uainn ach Comhéadan Dénártha Feidhmchláir Conartha (ABI) chun comhéadan a chruthú d’aon Chomhartha ERC-721. Mar atá le feiceáil thíos bainfimid úsáid as ABI simplithe, chun sampla frithchuimilte íseal a dhéanamh de.
Sampla Web3.py
Ar dtús, déan cinnte go bhfuil Web3.py leabharlann Python suiteáilte agat:
1pip install web3
1from web3 import Web32from web3._utils.events import get_event_data345w3 = Web3(Web3.HTTPProvider("https://cloudflare-eth.com"))67ck_token_addr = "0x06012c8cf97BEaD5deAe237070F9587f8E7A266d" # CryptoKitties Contract89acc_address = "0xb1690C08E213a35Ed9bAb7B318DE14420FB57d8C" # CryptoKitties Sales Auction1011# This is a simplified Contract Application Binary Interface (ABI) of an ERC-721 NFT Contract.12# It will expose only the methods: balanceOf(address), name(), ownerOf(tokenId), symbol(), totalSupply()13simplified_abi = [14 {15 'inputs': [{'internalType': 'address', 'name': 'owner', 'type': 'address'}],16 'name': 'balanceOf',17 'outputs': [{'internalType': 'uint256', 'name': '', 'type': 'uint256'}],18 'payable': False, 'stateMutability': 'view', 'type': 'function', 'constant': True19 },20 {21 'inputs': [],22 'name': 'name',23 'outputs': [{'internalType': 'string', 'name': '', 'type': 'string'}],24 'stateMutability': 'view', 'type': 'function', 'constant': True25 },26 {27 'inputs': [{'internalType': 'uint256', 'name': 'tokenId', 'type': 'uint256'}],28 'name': 'ownerOf',29 'outputs': [{'internalType': 'address', 'name': '', 'type': 'address'}],30 'payable': False, 'stateMutability': 'view', 'type': 'function', 'constant': True31 },32 {33 'inputs': [],34 'name': 'symbol',35 'outputs': [{'internalType': 'string', 'name': '', 'type': 'string'}],36 'stateMutability': 'view', 'type': 'function', 'constant': True37 },38 {39 'inputs': [],40 'name': 'totalSupply',41 'outputs': [{'internalType': 'uint256', 'name': '', 'type': 'uint256'}],42 'stateMutability': 'view', 'type': 'function', 'constant': True43 },44]4546ck_extra_abi = [47 {48 'inputs': [],49 'name': 'pregnantKitties',50 'outputs': [{'name': '', 'type': 'uint256'}],51 'payable': False, 'stateMutability': 'view', 'type': 'function', 'constant': True52 },53 {54 'inputs': [{'name': '_kittyId', 'type': 'uint256'}],55 'name': 'isPregnant',56 'outputs': [{'name': '', 'type': 'bool'}],57 'payable': False, 'stateMutability': 'view', 'type': 'function', 'constant': True58 }59]6061ck_contract = w3.eth.contract(address=w3.to_checksum_address(ck_token_addr), abi=simplified_abi+ck_extra_abi)62name = ck_contract.functions.name().call()63symbol = ck_contract.functions.symbol().call()64kitties_auctions = ck_contract.functions.balanceOf(acc_address).call()65print(f"{name} [{symbol}] NFTs in Auctions: {kitties_auctions}")6667pregnant_kitties = ck_contract.functions.pregnantKitties().call()68print(f"{name} [{symbol}] NFTs Pregnants: {pregnant_kitties}")6970# Using the Transfer Event ABI to get info about transferred Kitties.71tx_event_abi = {72 'anonymous': False,73 'inputs': [74 {'indexed': False, 'name': 'from', 'type': 'address'},75 {'indexed': False, 'name': 'to', 'type': 'address'},76 {'indexed': False, 'name': 'tokenId', 'type': 'uint256'}],77 'name': 'Transfer',78 'type': 'event'79}8081# We need the event's signature to filter the logs82event_signature = w3.keccak(text="Transfer(address,address,uint256)").hex()8384logs = w3.eth.get_logs({85 "fromBlock": w3.eth.block_number - 120,86 "address": w3.to_checksum_address(ck_token_addr),87 "topics": [event_signature]88})8990# Notes:91# - Increase the number of blocks up from 120 if no Transfer event is returned.92# - If you didn't find any Transfer event you can also try to get a tokenId at:93# https://etherscan.io/address/0x06012c8cf97BEaD5deAe237070F9587f8E7A266d#events94# Click to expand the event's logs and copy its "tokenId" argument95recent_tx = [get_event_data(w3.codec, tx_event_abi, log)["args"] for log in logs]9697if recent_tx:98 kitty_id = recent_tx[0]['tokenId'] # Paste the "tokenId" here from the link above99 is_pregnant = ck_contract.functions.isPregnant(kitty_id).call()100 print(f"{name} [{symbol}] NFTs {kitty_id} is pregnant: {is_pregnant}")Taispeáin gach rudCóip
Tá roinnt Imeachtaí suimiúla ag CryptoKitties Contract seachas na cinn Caighdeánacha.
Déanaimis seiceáil ar dhá cheann acu, Toirchis
agus Breith
.
1# Using the Pregnant and Birth Events ABI to get info about new Kitties.2ck_extra_events_abi = [3 {4 'anonymous': False,5 'inputs': [6 {'indexed': False, 'name': 'owner', 'type': 'address'},7 {'indexed': False, 'name': 'matronId', 'type': 'uint256'},8 {'indexed': False, 'name': 'sireId', 'type': 'uint256'},9 {'indexed': False, 'name': 'cooldownEndBlock', 'type': 'uint256'}],10 'name': 'Pregnant',11 'type': 'event'12 },13 {14 'anonymous': False,15 'inputs': [16 {'indexed': False, 'name': 'owner', 'type': 'address'},17 {'indexed': False, 'name': 'kittyId', 'type': 'uint256'},18 {'indexed': False, 'name': 'matronId', 'type': 'uint256'},19 {'indexed': False, 'name': 'sireId', 'type': 'uint256'},20 {'indexed': False, 'name': 'genes', 'type': 'uint256'}],21 'name': 'Birth',22 'type': 'event'23 }]2425# We need the event's signature to filter the logs26ck_event_signatures = [27 w3.keccak(text="Pregnant(address,uint256,uint256,uint256)").hex(),28 w3.keccak(text="Birth(address,uint256,uint256,uint256,uint256)").hex(),29]3031# Here is a Pregnant Event:32# - https://etherscan.io/tx/0xc97eb514a41004acc447ac9d0d6a27ea6da305ac8b877dff37e49db42e1f8cef#eventlog33pregnant_logs = w3.eth.get_logs({34 "fromBlock": w3.eth.block_number - 120,35 "address": w3.to_checksum_address(ck_token_addr),36 "topics": [ck_event_signatures[0]]37})3839recent_pregnants = [get_event_data(w3.codec, ck_extra_events_abi[0], log)["args"] for log in pregnant_logs]4041# Here is a Birth Event:42# - https://etherscan.io/tx/0x3978028e08a25bb4c44f7877eb3573b9644309c044bf087e335397f16356340a43birth_logs = w3.eth.get_logs({44 "fromBlock": w3.eth.block_number - 120,45 "address": w3.to_checksum_address(ck_token_addr),46 "topics": [ck_event_signatures[1]]47})4849recent_births = [get_event_data(w3.codec, ck_extra_events_abi[1], log)["args"] for log in birth_logs]Taispeáin gach rudCóip
NFTanna Coitianta
- Liostaíonn Etherscan NFT Tracker an NFT is fearr ar Ethereum de réir méid aistrithe.
- Is cluiche é CryptoKitties atá dírithe ar neacha inphóraithe, inbhailithe aoibhne a dtugtar CryptoKitties orthu.
- Is cluiche peile fantaisíochta domhanda é Sorare inar féidir leat eagráin teoranta nithe inbhailithe a bhailiú, do chuid foirne a bhainistiú agus dul san iomaíocht chun duaiseanna a thuilleamh.
- Soláthraíonn Seirbhís Ainmneacha Ethereum (ENS) bealach díláraithe slán & chun aghaidh a thabhairt ar acmhainní ar an mblocshlabhra agus as, ag baint úsáide as ainmneacha simplí atá inléite ag an duine.
- Seachadann POAP NFTanna saor in aisce do dhaoine a fhreastalaíonn ar imeachtaí nó a dhéanann gníomhartha sonracha. Is féidir POAPanna a chruthú agus a dháileadh.
- Is cuideachta atá lonnaithe i San Francisco é Unstoppable Domains a thógann fearainn ar blocshlabhra. Cuireann fearainn Bhlocshlabhra ainmneacha inléite daonna in ionad seoltaí criptea-airgeadra agus is féidir iad a úsáid chun a chumasú láithreáin ghréasáin atá díonach ar chinsireacht.
- Is TCG é Gods Unchained Cards ar bhlocshlabhra Ethereum a úsáideann NFTanna chun fíor-úinéireacht a thabhairt chuig sócmhainní in-chluiche.
- Is bailiúchán é Bored Ape Yacht Club bailiúchán de 10,000 NFT uathúla, a fheidhmíonn, chomh maith le bheith ina phíosa ealaíne annamh, mar chomhartha ballraíochta don chlub, ag soláthar buntáistí agus tairbhí do chomhaltaí a mhéadaíonn le himeacht ama mar thoradh ar iarrachtaí pobail.