0

Acutally I create a smart contract that the user create an auctions for things, so whenever some buyer come in website and see the acutions if he want to buy then he will create an offer. Same as if another buyer will come in our website if he like the auctions then he will create an offer if his offer price is greater than the previous buyer so auctions thing will be send to him. When I try to test smart contract,this error occurs I aslo run the local server of hardhat can anybody help me to fix this error Error: CONNECTION ERROR: Couldn't connect to node http://localhost:8545 Iam using hardhat for test

Here is my Smart Contract

//SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.5.0 <0.9.0;

contract Ebay {
  
  struct Auction {
      uint id;
      address payable seller;
      string name;
      string description;
      uint min;
      uint bestOfferId;
      uint[] offerIds;
  }

  struct Offer{
      uint id;
      uint auctionId;
      address payable buyer;
      uint price;
  }

mapping(uint=>Auction) private auctions;
mapping(uint=>Offer) private offers;
mapping(address=>uint[]) private auctionList;
mapping(address=>uint[]) private offerList;

uint private newAuctionId =1;
uint private newOfferId = 1;

function createAuction(string calldata _name,string calldata _description, uint _min) external{
 require(_min>0,"minimum must be greater than 0");
 uint[] memory offerIds = new uint[](0);

 auctions[newAuctionId] = Auction(newAuctionId,payable(msg.sender),_name,_description,_min,0,offerIds);
 auctionList[msg.sender].push(newAuctionId);
 newAuctionId++;
}

function createOffer(uint _auctionId) external payable auctionExists(_auctionId) {
    Auction storage auction = auctions[_auctionId];
    Offer storage bestOffer = offers[auction.bestOfferId];

    require(msg.value>=auction.min && msg.value>bestOffer.price,"msg.value must be greater than the minimum and the best offer");
    auction.bestOfferId = newOfferId;
    auction.offerIds.push(newOfferId);

    offers[newOfferId]=Offer(newOfferId,_auctionId,payable(msg.sender),msg.value);
    offerList[msg.sender].push(newOfferId);
    newOfferId++;

}

function transaction(uint _auctionId) external auctionExists(_auctionId){
    Auction storage auction  = auctions[_auctionId];
    Offer storage bestOffer = offers[auction.bestOfferId];

    for(uint i=0;i<auction.offerIds.length;i++){
        uint offerId = auction.offerIds[i];

        if(offerId!=auction.bestOfferId){
            Offer storage offer = offers[offerId];
            offer.buyer.transfer(offer.price); //contract -> b ether transfer
        }
    }
    auction.seller.transfer(bestOffer.price); //contract-> a (seller)
}

function getAuctions() external view returns(Auction[] memory){
    Auction[] memory _auctions = new Auction[](newAuctionId-1);

    for(uint i=1;i<newAuctionId;i++){
        _auctions[i-1]=auctions[i];
    }
    return _auctions;
}

function getUserAuctions(address _user) external view returns(Auction[] memory){
    uint[] storage userAuctionIds = auctionList[_user];
    Auction[] memory _auctions = new Auction[](userAuctionIds.length);
    for(uint i=0; i<userAuctionIds.length;i++){
        uint auctionId = userAuctionIds[i];
        _auctions[i]=auctions[auctionId];
    }
    return _auctions;
}

function getUserOffers(address _user) external view returns(Offer[] memory){
    uint[] storage userOfferIds = offerList[_user];
    Offer[] memory _offers = new Offer[](userOfferIds.length);

    for(uint i=0;i<userOfferIds.length;i++){
        uint offerId = userOfferIds[i];
        _offers[i]=offers[offerId];
    }
    return _offers;
}

modifier auctionExists(uint _auctionId){
    require(_auctionId>0 && _auctionId <newAuctionId,"Auction does not exist");
    _;
}
}

Here is my hardhat config.js

const { task } = require("hardhat/config");

require("@nomicfoundation/hardhat-toolbox");
task("accounts","Print the list of accounts",async(taskArgs,hre)=>{

  const accounts=await hre.ethers.getSigners();

  for(let account of accounts){

    console.log(account.address);
  }

});
module.exports = {
  solidity: "0.8.18",
  defaultNetwork:"hardhat",
  networks:{
    hardhat:{},
  }
};

Here is my Test Code.

const { ethers } = require("hardhat");
const {expectRevert}=require("@openzeppelin/test-helpers");


describe("Ebay",()=>{
    let ebay;
    beforeEach(async()=>{
        const Ebay=await ethers.getContractFactory("Ebay");
        ebay=await Ebay.deploy();
        await ebay.deployed();
    });
    const Auction={
        name:"auction1",
        description:"item sold",
        min:10
    }
    let auctions;
    it("should create an auction",async()=>{
        await ebay.createAuction(Auction.name,Auction.description,Auction.min);
        auctions=await ebay.getAuctions();
        assert(auctions.length===1);
        assert(auctions[0].name==Auction.name);
        assert(auctions[0].description===Auction.description);
        assert(parseInt(auctions[0].min)===Auction.min);
    });

The problem is coming in this test

    it("should not create an offer if auction is not exists",async()=>{

        const [seller1,buyer1,buyer2]=await ethers.getSigners();
        await expectRevert(
            ebay.createOffer(1,{from: buyer1 ,value:Auction.min+10}),"Auction does not exist"
        );
    });
});```
2
  • Try to be a little more specific with your question - what are you trying to achieve, what language you are using, what development framework, etc. May 22, 2023 at 8:54
  • Can you Check now please May 22, 2023 at 9:05

0

Your Answer

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