如何获取ERC-721 tokenID?
How to get ERC-721 tokenID?
我创建了一个部署在 ropston 网络上的 ERC-721 合约。
我正在使用合约创建 NFT,它完全可以正常工作。
现在对于转账部分,我需要获取任何 NFT 的 tokenID 并转账到其他地址,但每当我从 etherscan 或使用 web3 获取交易详细信息时,我都无法获取 tokenID。
我想将tokenID存储在数据库中,以便在转移到其他地址时可以使用它。
我已经圈出上图中所需的确切 tokenID。
我正在使用以下代码:
window.ethereum
.request({
method: 'eth_sendTransaction',
params: [
{
from: fromAddress,
to: contractAddress,
gas: '50000',
data: nftContract.methods.transferFrom(fromAddress, toAddress, tokenNumber).encodeABI()
},
],
})
我只是想获取NFT创建时的tokenID,存入DB,供参考和执行业务逻辑。
function mintNFT(address recipient, string memory tokenURI)
public onlyOwner
returns (uint256)
{
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
以上是负责创建 NFT 的 solidity 函数。
您的 mintNFT()
函数不会发出任何包含 newItemId
.
的事件
solidity is using the standard definition of transfer
没有“标准定义”,ERC-721 standard only defines an interface and few other rules - and the actual implementation (of the interface) is on each developer. However I'm assuming that by the "standard definition" you mean the OpenZeppelin implementation,它是 ERC-721 标准的广泛使用的实现,被许多开始使用 Solidity 编码的人使用。
您可以在链接的实现中看到,OZ _mint()
函数发出 Transfer()
事件,其中第三个参数是铸造的令牌 ID。
因此,执行您的 mintNFT()
函数会有效地发出 Transfer()
事件,其中包含新生成的令牌 ID 作为第三个参数的值。
在你从你的 JS 代码中执行了 mintNFT()
合约函数之后,它 returns 一个 PromiEvent
对象,你可以用它来捕捉它的 receipt
事件.
收据包含发出的日志,您还可以在其中找到 Transfer()
日志。
const tx = nftContract.methods.mintNFT(...).send({from: ...});
tx.on('receipt', function(receipt){
console.log(logs[0].topics[3]); // this prints the hex value of the tokenId
// you can use `web3.utils.hexToNumber()` to convert it to decimal
});
如果你想从一个已经存在的交易中获取代币 ID(使用 tx 哈希),你可以使用这个片段:
web3.eth.getTransactionReceipt('0x258a6d35445814d091ae67ec01cf60f87a4a58fa5ac1de25d0746edf8472f189').then(function(data){
let transaction = data;
let logs = data.logs;
console.log(logs);
console.log(web3.utils.hexToNumber(logs[0].topics[3]));
});
您可以在 send() method and the receipt 的 web3 文档中找到更多详细信息。
你可以试试:
const receipt = await web3.eth.getTransactionReceipt(hash)
const tokenId = Web3.utils.hexToNumber(receipt.logs[0].topics[3])
我检查来自 ropsten 测试网的哈希值:
https://ropsten.etherscan.io/tx/0x59928012c3e0605b9346215c24654e84be29f2bf47949a2284aecf9991996a28
输出为 11
我创建了一个部署在 ropston 网络上的 ERC-721 合约。 我正在使用合约创建 NFT,它完全可以正常工作。
现在对于转账部分,我需要获取任何 NFT 的 tokenID 并转账到其他地址,但每当我从 etherscan 或使用 web3 获取交易详细信息时,我都无法获取 tokenID。
我想将tokenID存储在数据库中,以便在转移到其他地址时可以使用它。
我已经圈出上图中所需的确切 tokenID。
我正在使用以下代码:
window.ethereum
.request({
method: 'eth_sendTransaction',
params: [
{
from: fromAddress,
to: contractAddress,
gas: '50000',
data: nftContract.methods.transferFrom(fromAddress, toAddress, tokenNumber).encodeABI()
},
],
})
我只是想获取NFT创建时的tokenID,存入DB,供参考和执行业务逻辑。
function mintNFT(address recipient, string memory tokenURI)
public onlyOwner
returns (uint256)
{
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
以上是负责创建 NFT 的 solidity 函数。
您的 mintNFT()
函数不会发出任何包含 newItemId
.
solidity is using the standard definition of transfer
没有“标准定义”,ERC-721 standard only defines an interface and few other rules - and the actual implementation (of the interface) is on each developer. However I'm assuming that by the "standard definition" you mean the OpenZeppelin implementation,它是 ERC-721 标准的广泛使用的实现,被许多开始使用 Solidity 编码的人使用。
您可以在链接的实现中看到,OZ _mint()
函数发出 Transfer()
事件,其中第三个参数是铸造的令牌 ID。
因此,执行您的 mintNFT()
函数会有效地发出 Transfer()
事件,其中包含新生成的令牌 ID 作为第三个参数的值。
在你从你的 JS 代码中执行了 mintNFT()
合约函数之后,它 returns 一个 PromiEvent
对象,你可以用它来捕捉它的 receipt
事件.
收据包含发出的日志,您还可以在其中找到 Transfer()
日志。
const tx = nftContract.methods.mintNFT(...).send({from: ...});
tx.on('receipt', function(receipt){
console.log(logs[0].topics[3]); // this prints the hex value of the tokenId
// you can use `web3.utils.hexToNumber()` to convert it to decimal
});
如果你想从一个已经存在的交易中获取代币 ID(使用 tx 哈希),你可以使用这个片段:
web3.eth.getTransactionReceipt('0x258a6d35445814d091ae67ec01cf60f87a4a58fa5ac1de25d0746edf8472f189').then(function(data){
let transaction = data;
let logs = data.logs;
console.log(logs);
console.log(web3.utils.hexToNumber(logs[0].topics[3]));
});
您可以在 send() method and the receipt 的 web3 文档中找到更多详细信息。
你可以试试:
const receipt = await web3.eth.getTransactionReceipt(hash)
const tokenId = Web3.utils.hexToNumber(receipt.logs[0].topics[3])
我检查来自 ropsten 测试网的哈希值: https://ropsten.etherscan.io/tx/0x59928012c3e0605b9346215c24654e84be29f2bf47949a2284aecf9991996a28
输出为 11