// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract Marketplace is ReentrancyGuard {
// Variables
address payable public immutable feeAccount; // the account that receives fees
uint public immutable feePercent; // the fee percentage on sales
uint public itemCount;
struct Item {
uint itemId;
IERC721 nft;
uint tokenId;
uint price;
address payable seller;
bool sold;
}
// itemId -> Item
mapping(uint => Item) public items;
event Offered(
uint itemId,
address indexed nft,
uint tokenId,
uint price,
address indexed seller
);
event Bought(
uint itemId,
address indexed nft,
uint tokenId,
uint price,
address indexed seller,
address indexed buyer
);
constructor(uint _feePercent) {
feeAccount = payable(msg.sender);
feePercent = _feePercent;
}
// Make item to offer on the marketplace
function makeItem(IERC721 _nft, uint _tokenId, uint _price) external nonReentrant {
require(_price > 0, "Price must be greater than zero");
// Increment itemCount
itemCount ++;
// Approve Marketplace to transfer the NFT on behalf of the user
_nft.approve(address(this), _tokenId);
// Transfer nft
_nft.transferFrom(msg.sender, address(this), _tokenId);
// Add new item to items mapping
items[itemCount] = Item (
itemCount,
_nft,
_tokenId,
_price,
payable(msg.sender),
false
);
// Emit Offered event
emit Offered(
itemCount,
address(_nft),
_tokenId,
_price,
msg.sender
);
}
function purchaseItem(uint _itemId) external payable nonReentrant {
uint _totalPrice = getTotalPrice(_itemId);
Item storage item = items[_itemId];
require(_itemId > 0 && _itemId <= itemCount, "Item doesn't exist");
require(msg.value >= _totalPrice, "Not enough ether to cover item price and market fee");
require(!item.sold, "Item already sold");
// Pay seller and feeAccount
item.seller.transfer(item.price);
feeAccount.transfer(_totalPrice - item.price);
// Update item to sold
item.sold = true;
// Transfer NFT to buyer
item.nft.transferFrom(address(this), msg.sender, item.tokenId);
// Swap seller and buyer
address payable temp = item.seller;
item.seller = payable(msg.sender);
// Emit Bought event
emit Bought(
_itemId,
address(item.nft),
item.tokenId,
item.price,
temp, // Original seller
msg.sender // Buyer
);
}
function getTotalPrice(uint _itemId) view public returns(uint){
return((items[_itemId].price*(100 + feePercent))/100);
}
}
This is the code of smart contract error is occurring while calling makeItem function. The error is failed to estimate gas value
How to solve this error?
(transactionHash="0xf6a128f64f2117b5454ef82b8694844a13d087f25cfec6d869e415bff78f6848", transaction={"hash":"0xf6a128f64f2117b5454ef82b8694844a13d087f25cfec6d869e415bff78f6848","type":2,"accessList":null,"blockHash":null,"blockNumber":null,"transactionIndex":null,"confirmations":0,"from":"0x23a8704727E7Df40Ed0032553D964d346Dae6c38","gasPrice":{"type":"BigNumber","hex":"0x61c517b2"},"maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x59682f00"},"maxFeePerGas":{"type":"BigNumber","hex":"0x61c517b2"},"gasLimit":{"type":"BigNumber","hex":"0x0493e0"},"to":"0x57adc0d1289bC0D5E2980935E8946eF6AAAd9cFD","value":{"type":"BigNumber","hex":"0x00"},"nonce":170,"data":"0xfa00afc70000000000000000000000007c37c609bef3081770b1a2dba2a31d8b070970b60000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000016345785d8a0000","r":"0xbe6d5f0a133c30b16723e0000a0a11d3095f175f9412c7a28e2a7c8f52c221c1","s":"0x6a54aeeedab5c88a2ac8a2c19a6d89943155e32473ff512c46d1097fce40cf5a","v":1,"creates":null,"chainId":0}, receipt={"to":"0x57adc0d1289bC0D5E2980935E8946eF6AAAd9cFD","from":"0x23a8704727E7Df40Ed0032553D964d346Dae6c38","contractAddress":null,"transactionIndex":17,"gasUsed":{"type":"BigNumber","hex":"0xf12f"},"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","blockHash":"0x1c7a1f0d16c7454cf2866625ae0be2ec26232ae5f29dc836221293a7efd104a4","transactionHash":"0xf6a128f64f2117b5454ef82b8694844a13d087f25cfec6d869e415bff78f6848","logs":[],"blockNumber":5590766,"confirmations":1,"cumulativeGasUsed":{"type":"BigNumber","hex":"0x15b5f8"},"effectiveGasPrice":{"type":"BigNumber","hex":"0x5edf00ad"},"status":0,"type":2,"byzantium":true}, code=CALL_EXCEPTION, version=providers/5.7.2) at Logger.makeError (index.ts:269:1) at Logger.throwError (index.ts:281:1) at Web3Provider.<anonymous> (base-provider.ts:1549:1) at Generator.next (<anonymous>) at fulfilled (base-provider.ts:1:1)