0

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;
1
  • There's no .then() in the JS snippet you provided, the error most likely comes from another bit of code
    – Foxxxey
    Sep 22 at 22:33

1 Answer 1

0

pls mention down the all packages and take a screenshot of your errors.

etherv6.0 is buggy as compare to ethers5.7.2

Edit: Here is the package.json

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.17.0",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "ethers": "^6.7.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.