I tried making request to the contract using 'contract.otherFunction()' and they are working fine but just the 'placeBid' function where I have to use add value is giving the error 'Getting Error ' Cannot read properties of undefined (reading 'then')'
All other functionality about connecting the wallet , contract is done already in other components.
Here is the Solidity Code:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract AuctionStore {
address public owner;
struct Product {
uint256 productId;
string name;
string description;
uint256 auctionEndTime;
uint256 highestBid;
address highestBidder;
}
Product[] public products;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Only the owner can call this function");
_;
}
modifier productExists(uint256 productId) {
require(productId < products.length, "Product does not exist");
_;
}
modifier auctionEnded(uint256 productId) {
require(block.timestamp >= products[productId].auctionEndTime, "Auction has not ended yet");
_;
}
event NewBid(uint256 indexed productId, address indexed bidder, uint256 amount);
event AuctionEnded(uint256 indexed productId, address winner, uint256 amount);
function createProduct(string memory _name, string memory _description, uint256 _durationMinutes) public onlyOwner {
Product memory newProduct = Product({
productId: products.length,
name: _name,
description: _description,
auctionEndTime: block.timestamp + (_durationMinutes * 1 minutes),
highestBid: 0,
highestBidder: address(0)
});
products.push(newProduct);
}
function placeBid(uint256 productId) public payable productExists(productId) {
require(block.timestamp < products[productId].auctionEndTime, "Auction has ended");
require(msg.value > products[productId].highestBid, "Bid must be higher than the current highest bid");
if (products[productId].highestBidder != address(0)) {
// Refund the previous highest bidder
payable(products[productId].highestBidder).transfer(products[productId].highestBid);
}
products[productId].highestBidder = msg.sender;
products[productId].highestBid = msg.value;
emit NewBid(productId, msg.sender, msg.value);
}
function endAuction(uint256 productId) public onlyOwner productExists(productId) auctionEnded(productId) {
require(products[productId].highestBidder != address(0), "No valid bids");
payable(owner).transfer(products[productId].highestBid);
emit AuctionEnded(productId, products[productId].highestBidder, products[productId].highestBid);
}
// Fallback function to receive ETH
receive() external payable {
revert("Fallback function is disabled. Use placeBid.");
}
// Withdraw any remaining funds in the contract
function withdraw() public onlyOwner {
uint256 contractBalance = address(this).balance;
require(contractBalance > 0, "No balance to withdraw");
payable(owner).transfer(contractBalance);
}
// Get the number of products listed
function getNumberOfProducts() public view returns (uint256) {
return products.length;
}
function listAllProducts() public view returns (Product[] memory) {
return products;
}
}
Here is the ReactJs code:
import React, { useState } from "react";
import "./ProductComp.css";
const { ethers } = require("ethers");
const ProductComp = ({
productId,
name,
description,
auctionEndTime,
highestBid,
highestBidder,
state,
}) => {
const { contract } = state;
const [bidAmount, setBidAmount] = useState("");
const makeBid = async () => {
try {
const tx = await contract.placeBid(productId, { value: 2 });
let receipt = await tx.wait();
} catch (error) {
console.log(error);
}
};
return (
<div className="prod-comp">
<h4>{name}</h4>
<h4>{description}</h4>
<h4>Current Highest Bid : {highestBid.toString()} ETH</h4>
<h4>Auction End Time : {auctionEndTime.toString()}</h4>
<h4>Highest Bidder : {highestBidder}</h4>
<input
type="number"
placeholder="Enter bid amount (in Ether)"
value={bidAmount}
onChange={(e) => setBidAmount(e.target.value)}
/>
<button onClick={makeBid}>Place Bid</button>
</div>
);
};
export default ProductComp;