Solidity
我正在嘗試創建一個市場智能合約來使用 ERC20 代幣買賣 NFT,但是當我嘗試實現購買功能時出現錯誤
I am trying to create a Marketplace Smart contract to buy and sell NFT with ERC20 token.
我的令牌.sol
pragma solidity ^0.8.4; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; contract YourToken is ERC20 { constructor() ERC20("Solu", "SOLU") { _mint(msg.sender, 1000 * 10 ** 18); } }
我的 NFT.sol
pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract NFTToken is ERC721 { string public nameNFT; string public nameSymbol; string public nftTokenURI; uint256 public nftId; //Keep the record of nfts mapping(uint256 => string) public tokenURIExists; //Keep the record for nfts value => give id returns cost mapping(uint256 => uint256) public tokenIdToValue; // Base URI string _baseURIextended; constructor () ERC721("Samresh", "SAM") { nameNFT = "Sam"; nameSymbol = "SAM"; } function setBaseURI(string memory baseURI_) external { _baseURIextended = baseURI_; } function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual { require( _exists(tokenId),"ERC721Metadata: URI set of nonexistent token"); tokenURIExists[tokenId] = _tokenURI; } function _baseURI() internal view virtual override returns (string memory) { return _baseURIextended; } function tokenURI(uint256 tokenId) public view virtual override returns (string memory) { require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token"); string memory _tokenURI = tokenURIExists[tokenId]; string memory base = _baseURI(); // If there is no base URI, return the token URI. if (bytes(base).length == 0) { return _tokenURI; } // If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked). if (bytes(_tokenURI).length > 0) { return string(abi.encodePacked(base, _tokenURI)); } // // If there is a baseURI but no tokenURI, concatenate the tokenID to the baseURI. // return string(abi.encodePacked(base, tokenId.toString())); return string(abi.encodePacked(base, tokenId)); } function Mint (string memory _tokenURI, uint256 _nftPrice) public returns (uint256) { require(msg.sender != address(0)); nftTokenURI = _tokenURI; // used as token id nftId ++; // check if a token exists with the above token id => incremented counter require(!_exists(nftId)); tokenIdToValue[nftId] = _nftPrice; _mint(msg.sender,nftId); _setTokenURI(nftId, nftTokenURI); return nftId; } function tokenPrice (uint256 _tokenID) public view returns (uint256 nftPrice) { require(!_exists(nftId)); nftPrice = tokenIdToValue[_tokenID]; } }
我的市場.sol
pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import "./3_Ballot.sol"; import "./1_Storage.sol"; contract NFTMarketPlace { YourToken token; NFTToken NFT; //keep the record for tokenID is listed on sale or not mapping(uint256 => bool) public tokenIdForSale; //keep the address of the nft buyer mapping(uint256 => address) public nftBuyers; constructor (address tokenAddress, address NFTAddress) { token = YourToken(tokenAddress); NFT = NFTToken(NFTAddress); } function nftSale(uint256 _tokenId,bool forSale) external { require(msg.sender == NFT.ownerOf(_tokenId),"Only owners can change this status"); tokenIdForSale[_tokenId] = forSale; } function nftBuy(uint256 _tokenId) payable public { require(tokenIdForSale[_tokenId],"Token must be on sale first"); uint nftPrice = NFT.tokenPrice(_tokenId); require(msg.value >= nftPrice, "Insufficient balance."); token.transfer(payable(NFT.ownerOf(_tokenId)),nftPrice); nftBuyers[_tokenId] = msg.sender; } }
一切都按預期工作,但是當我嘗試購買代幣時出現錯誤 注意:如果您發送價值並且您發送的價值應該小於您目前的餘額,則應該支付呼叫的函式。
我在 remix IDE 上部署程式碼並將 1000 Wei 作為 msg.value 發送
我是一個新手,任何幫助表示讚賞。提前致謝
您遇到的錯誤與程式碼無關。您的購買功能是一個
payable
功能。所以你必須在msg.value中發送一些ETH在 remix 中,您會在視窗左側看到一個名為***value的參數。***輸入您希望轉移的乙太幣數量。該功能應該按預期工作!中提琴!