我已经创建了一个ERC-721合同部署在ropston网络上。使用合同,我正在创建NFT,它完全可以工作。
现在,对于传输部分,我需要获得任何NFT的tokenID,并将其传输到其他地址,但每当我从以太扫描或使用web3获取事务详细信息时,我都无法获得tokenID。
我想将tokenID存储在DB中,这样就可以在传输到其他地址时使用它。

我已经在上面的图像中包围了所需的确切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函数。
发布于 2021-06-02 11:14:13
您的mintNFT()函数不会发出任何包含newItemId的事件。
坚固性是使用标准的转移定义。
没有“标准定义”,ERC-721标准只定义了一个接口和很少的其他规则--并且(接口的实际实现)是在每个开发人员身上。但是,我假设“标准定义”指的是OpenZeppelin实现,它是ERC-721标准的一个广泛使用的实现,并且被许多开始扎实编码的人使用。
在链接的实现中,可以看到OZ _mint()函数发出Transfer()事件,其中第三个参数是造币标记ID。
因此,执行mintNFT()函数可以有效地发出包含新创建的令牌ID的Transfer()事件,作为第三个参数的值。
在您从JS代码中执行mintNFT()契约函数之后,它返回一个PromiEvent对象,您可以使用该对象来捕获它的receipt事件。
收据包含发送的日志,您也可以在其中找到Transfer()日志。
const tx = nftContract.methods.mintNFT(...).send({from: ...});
tx.on('receipt', function(receipt){
console.log(receipt.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]));
});发布于 2022-03-23 14:04:11
你可以试试:
const receipt = await web3.eth.getTransactionReceipt(hash)
const tokenId = Web3.utils.hexToNumber(receipt.logs[0].topics[3])我检查了ropsten testnet:https://ropsten.etherscan.io/tx/0x59928012c3e0605b9346215c24654e84be29f2bf47949a2284aecf9991996a28的散列。
产量为11
发布于 2022-09-08 07:40:37
如果receipt.logs[0].topics[3]不能工作,请尝试这样做:
receipt.events.Transfer.returnValues.tokenIdhttps://stackoverflow.com/questions/67803090
复制相似问题