我试图为用户之间的NFT创建一个明智的契约。每当用户尝试创建抽奖时,他们都需要指定tokenContract (这是NFT的契约地址)和tokenId (将是相应的id )。我使用一个简单的造币合同来模拟向合同发送令牌。但是,当发送抽奖合同的造币券,并有onERC721Received设置,State不会被更改为1。
根据以太扫描的说法,抽奖合同确实成功地接收了ERC721令牌。
抽奖合同的有关部分:
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
...
contract Raffle is VRFConsumerBase, IERC721Receiver {
enum State {
Pending,
Active,
Calculating,
Finished
}
State public state;
...
...
function onERC721Received(address, address, uint256, bytes calldata) external override returns (bytes4) {
state = State.Active;
return IERC721Receiver.onERC721Received.selector;
}铸币合同:
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract TestToken is ERC721URIStorage, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() ERC721("TestToken", "TST") {}
function mintNFT(address recipient)
public onlyOwner
returns (uint256)
{
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
return newItemId;
}
}发布于 2022-12-19 02:43:19
为什么不内部化传输过程并使用标准的ERC721库呢?
function transferNFT(address _nft, uint256 _tokenId) public {
// Check if sender approved contract to use _tokenId
require(ERC721(_nft).getApproved(_tokenId) == address(this) ||
isApprovedForAll(tx.origin, address(this)) == true,
"Not approved to transfer NFT");
// Check if sender is the owner of _tokenId
require(ERC721(_nft).ownerOf(_tokenId) == tx.origin, "Not owner of token");
// Process transaction
ERC721(_nft).transferFrom(tx.origin, address(this), _tokenId);
// Check that transaction was successful
require(ERC721(_nft).ownerOf(_tokenId) == address(this), "Transfer failed");
}您可以使用标准的ERC721 ownerOf函数来验证调用传递函数后是否完成了传输。
https://ethereum.stackexchange.com/questions/141453
复制相似问题