我创建了一个智能合约,铸造了一个 nft,现在尝试转移它。我的问题是交易完成得很好——我可以在 etherscan 等中看到它,但 nft 没有被转移。可能的根本原因是什么?
async function transferNFT() {
const nonce = await web3.eth.getTransactionCount(PUBLIC_KEY, 'latest'); //get latest nonce
//the transaction
const tx = {
'from': sender,
'to': receiver,
'nonce': nonce,
'gas': 500000,
'data': myContract.methods.transferFrom(sender,receiver, 1).encodeABI()
}
const signPromise = web3.eth.accounts.signTransaction(tx, PRIVATE_KEY)
signPromise
.then((signedTx) => {
web3.eth.sendSignedTransaction(
signedTx.rawTransaction,
function (err, hash) {
if (!err) {
console.log(
"The hash of your transaction is: ",
hash,
"\nCheck Alchemy's Mempool to view the status of your transaction!"
)
} else {
console.log(
"Something went wrong when submitting your transaction:",
err
)
}
}
)
})
.catch((err) => {
console.log(" Promise failed:", err)
})
}
transferNFT()
还有合同。我实际上并没有调用传输函数,因为我期望使用 openzeppelin transferFrom 函数。但是如果我使用合同中的转移函数 - 结果是一样的:
交易被执行
NFT 不会被转移。
contract MyNFTv2 is ERC721URIStorage, Ownable { using Counters for Counters.Counter; Counters.Counter private _tokenIds; event NftBought(address _seller, address _buyer, uint256 _price); event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId); mapping (uint256 => uint256) public tokenIdToPrice; mapping(uint256 => address) internal idToOwner; constructor() public ERC721("MyNFTv2", "NFT") {} 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; } function allowBuy(uint256 _tokenId, uint256 _price) external { require(msg.sender == ownerOf(_tokenId), 'Not owner of this token'); require(_price > 0, 'Price zero'); tokenIdToPrice[_tokenId] = _price; } function disallowBuy(uint256 _tokenId) external { require(msg.sender == ownerOf(_tokenId), 'Not owner of this token'); tokenIdToPrice[_tokenId] = 0; } function buy(uint256 _tokenId) external payable { uint256 price = tokenIdToPrice[_tokenId]; require(price > 0, 'This token is not for sale'); require(msg.value == price, 'Incorrect value'); address seller = ownerOf(_tokenId); _transfer(seller, msg.sender, _tokenId); tokenIdToPrice[_tokenId] = 0; // not for sale anymore payable(seller).transfer(msg.value); // send the ETH to the seller emit NftBought(seller, msg.sender, msg.value); } function transfer(address _to, uint256 _tokenId) public { require(msg.sender == idToOwner[_tokenId]); idToOwner[_tokenId] = _to; emit Transfer(msg.sender, _to, _tokenId); }
}