我创建了一个智能合约,铸造了一个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); }
}
const tx = {
'from': sender,
'to': receiver,
你将交易发送给令牌接收者,这是一个不持有任何智能合约的常规地址。
所以当你传递给他们data
字段时,没有合约来处理它,交易只是忽略data
字段。
解决方案:将事务的to
字段设置为NFT托收合同。然后它将能够处理tranferFrom()
函数及其在data
字段中传递的参数。注意,receiver
已经在函数的第二个参数中传递了。