I signed up for a "introduction to Blockchain" course in my exchange semester in South Korea and I feel like it's not just an introduction because for half of the final grade we get we have to create our own simple dApp and I never really coded before.
So I copied and tried to understand this code for a simple auction app but it's not working because of this error:
transact to Auction.placeBid errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.
It took me a while to go through the code and I still don't understand all of it clearly so maybe I am missing something very simple since im a coding novice. Here's the code:
// We are using version 0.6.6 of Solidity
pragma solidity 0.6.6;
// We import OpenZeppelins SafeMath library to avoid bugs
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol";
contract AuctionBox{
Auction[] public auctions;
function createAuction (
string memory _title,
uint _startPrice,
string memory _description
) public {
// we set a new instance
Auction newAuction = new Auction(msg.sender, _title, _startPrice, _description);
// we add auction address to the auctions array
auctions.push(newAuction);
}
function returnAllAuctions() public view returns(Auction[] memory){
return auctions;
}
}
contract Auction {
using SafeMath for uint256;
address payable private owner;
string title;
uint startPrice;
string description;
enum State{Default, Running, Finalized}
State public auctionState;
uint public highestPrice;
address payable public highestBidder;
mapping(address => uint) public bids;
//constructor to creat an auction when the owner calls createAuction() in AuctionBox contract
constructor(
address payable _owner,
string memory _title,
uint _startPrice,
string memory _description
//_title is the title of the auction
// _startPrice is the start price of the auction
// _description is the description of the auction
) public {
// here we initialize auction
owner = _owner;
title = _title;
startPrice = _startPrice;
description = _description;
auctionState = State.Running;
}
modifier notOwner(){
require(msg.sender != owner);
_;
}
//the function to place a bid
function placeBid() public payable notOwner returns(bool) {
//to check the if the auctionState is running and if the bid is more than 1
require(auctionState == State.Running, "error: auctionState is not running");
require(msg.value >= 0, "error: bid value is lower than 0");
// to update the currentBid
uint currentBid = bids[msg.sender].add(msg.value);
// uint currentBid = bids[msg.sender] + msg.value;
require(currentBid > highestPrice);
// link the currentBid with msg.sender
bids[msg.sender] = currentBid;
// to update the highestPrice
highestPrice = currentBid;
highestBidder = msg.sender;
return true;
}
function finalizeAuction() public{
//to finalize the auction by owner or bidders
require(msg.sender == owner || bids[msg.sender] > 0);
address payable recipiant;
uint value;
// for the owner to get highestPrice
if(msg.sender == owner){
recipiant = owner;
value = highestPrice;
}
// for the highestBidder to not get the money back
else if (msg.sender == highestBidder){
recipiant = highestBidder;
value = 0;
}
// for the other bidders to get the money back
else {
recipiant = msg.sender;
value = bids[msg.sender];
}
// initialize the value
bids[msg.sender] = 0;
recipiant.transfer(value);
auctionState = State.Finalized;
}
//the function to show the auction content
function returnContent() public view returns(
string memory,
uint,
string memory,
State
) {
return (
title,
startPrice,
description,
auctionState
);
}
}
The "place bid" function always fails with the error message named in the title.
I tried to fix it with the help of the following threads but without success since I don't know how to apply it on my code:
https://medium.com/linum-labs/error-vm-exception-while-processing-transaction-revert-8cd856633793
Gas estimation errored with the following message
"The transaction execution will likely fail. Do you want to force sending? VM Exception while processing transaction: out of gas"
Transaction revert in Remix
solidity