交易完成,但 NFT 未转账
Transaction complete, but NFT not transferred
我创建了一个智能合约,铸造了一个 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
已在函数的第二个参数中传递。
我创建了一个智能合约,铸造了一个 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
已在函数的第二个参数中传递。