Erc-721
ERC721 批准智能合約並從中發送交易
我正在開發 ERC721。我希望 NFT 獲得批准並進行交易。當我呼叫 getApproved() 時,地址是智能合約地址,但是當我嘗試通過呼叫 safeTransferFrom() 方法進行交易時,它不起作用。我認為這可能是因為合約地址不是 EOA,你不能為智能合約支付 gas 費。但是交易所上的交易也由智能合約管理,它是如何工作的?我需要創建第二個智能合約嗎?
問候盧卡
如果我理解您的問題,您希望將 ERC721 交易從合約發送到另一個合約地址,並且您在發件人合約上使用 approve() 和 safeTransferFrom() 方法。所以你需要在接收者合約中實現 onERC721Received() 方法。看這個例子
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "@openzeppelin/contracts/utils/Counters.sol"; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol"; contract Sender is ERC721 { using Counters for Counters.Counter; Counters.Counter private _tokenIdCounter; address public receiver; constructor( ) ERC721("shx", "shx") { } function createNFT(address _receiver) public returns (uint) { require(_receiver != address(0), "Shx: zero address"); receiver = _receiver; _tokenIdCounter.increment(); uint256 tokenId = _tokenIdCounter.current(); _mint(msg.sender, tokenId); approve(receiver, tokenId); // or you could use: setApprovalForAll(b, true) // setApprovalForAll(b, true); return tokenId; } function createNFT2(address _receiver) public returns (uint) { receiver = _receiver; _tokenIdCounter.increment(); uint256 tokenId = _tokenIdCounter.current(); _mint(msg.sender, tokenId); setApprovalForAll(receiver, true); return tokenId; } function sendTokenToSC(uint256 tokenId) public returns(bool){ safeTransferFrom(msg.sender, receiver, tokenId); return true; } } contract Receiver is IERC721Receiver{ address public contractSenderAddress; function first( address _nft) public { contractSenderAddress = _nft; } function ownerOf(uint256 tokenId ) public view returns(address) { return IERC721(contractSenderAddress).ownerOf( tokenId); } function onERC721Received(address , address , uint256 , bytes memory) external pure override returns (bytes4){ return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)")); } }