Solidity

Solidity safeTransferFrom 編譯錯誤

  • December 10, 2021

我的契約中出現以下 Solidity 錯誤,我不確定問題出在哪裡:Expected identifier but got '('

這是我的契約剝離到問題。

// SPDX-License-Identifier: MIT

// pragma solidity defines compiler version
pragma solidity ^0.8.10;

import '@openzeppelin/contracts/utils/Counters.sol';
import '@openzeppelin/contracts/token/ERC721/ERC721.sol';
import '@openzeppelin/contracts/security/ReentrancyGuard.sol';

contract NFTMarket is ReentrancyGuard {
   using Counters for Counters.Counter;
   Counters.Counter private _itemIds;
   Counters.Counter private _itemsSold;

   address payable owner;
   uint256 listingPrice = 0.050 ether;

   constructor() {
       owner = payable(msg.sender);
   }

   function createMarketItem(
       address nftContract, uint256 tokenId, uint256 price
   ) public payable nonReentrant {
        _itemIds.increment();
        uint256 itemId = _itemIds.current();
   }

   IERC721(nftContract).safeTransferFrom(msg.sender, address.this, tokenId);
          ^
   Expected identifier but got '('
}

有誰知道如何正確解決這個問題?

更新:我沒有意識到我在createMarketItem函式之外擁有它。這解決了這個錯誤。但是,它現在說member this not foundfor address.this。這不應該適用於契約地址嗎?

// SPDX-License-Identifier: MIT

// pragma solidity defines compiler version
pragma solidity ^0.8.10;

import '@openzeppelin/contracts/utils/Counters.sol';
import '@openzeppelin/contracts/token/ERC721/ERC721.sol';
import '@openzeppelin/contracts/security/ReentrancyGuard.sol';

contract NFTMarket is ReentrancyGuard {
   using Counters for Counters.Counter;
   Counters.Counter private _itemIds;
   Counters.Counter private _itemsSold;

   address payable owner;
   uint256 listingPrice = 0.050 ether;

   constructor() {
       owner = payable(msg.sender);
   }

   function createMarketItem(
       address nftContract, uint256 tokenId, uint256 price
   ) public payable nonReentrant {
        _itemIds.increment();
        uint256 itemId = _itemIds.current();

        IERC721(nftContract).safeTransferFrom(msg.sender, address.this, tokenId);
   }
}

您需要使用address(this) 來address(this)代替 address.this,address(this)` 將返回合約的地址。

引用自:https://ethereum.stackexchange.com/questions/115323