transact to Auction.placeBid errored: VM error: revert. revert The transaction has been reverted to the initial state - solidity

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
);
}
}
enter image description here
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
Why am I getting this error ? "Gas estimation errored with the following message (see below). The transaction > execution will likely fail"
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

Related

What to do to solve unpredictable gas fees from a function in a smart contract using boolean?

The smart contract I have written seems to have an error where the gas fees are unpredictable and even if I increase the max limit, the transaction fails and gets reverted. This specific function uses a bool and am wondering if it is the cause.
I was trying to pass the function goodsDelivered with the exchangeID which is the transaction hash from executing function openExchange, however when I try changing the goodsDelivered to 'true', I get a warning from metamask stating: unpredictable_gas_limit and gets reverted. Initially I thought it was an error from the client side where I wrote js code to be able to interact with the smart contract but when I tried writing on etherscan directly there was also the transaction failed. I have also tried debugging on Remix but it functions fine on remix.
pragma solidity ^0.8.0;
contract Exchange{
struct exchange{
uint amount;
address buyer;
address seller;
address agent;
bool goodsDelivered;
}
constructor() {
}
uint nonce;
mapping (bytes32 => exchange) public exchangeRegistry;
function openExchange(address _seller, address _agent) external payable returns (bytes32 _exchangeId) {
address _buyer = msg.sender;
uint _amount = msg.value;
_exchangeId = keccak256(abi.encodePacked(_buyer, nonce));
exchangeRegistry[_exchangeId].amount = _amount;
exchangeRegistry[_exchangeId].buyer = _buyer;
exchangeRegistry[_exchangeId].seller = _seller;
exchangeRegistry[_exchangeId].agent = _agent;
nonce += 1;
}
function withdrawAmount(bytes32 _exchangeId) external payable returns (bool){
require((exchangeRegistry[_exchangeId].goodsDelivered == true && msg.sender == exchangeRegistry[_exchangeId].seller)
|| (exchangeRegistry[_exchangeId].goodsDelivered == false && msg.sender == exchangeRegistry[_exchangeId].buyer));
payable(msg.sender).transfer(exchangeRegistry[_exchangeId].amount);
return (true);
}
function goodsDelivered(bytes32 _exchangeId, bool _goodsDelivered) external returns (bool){
require(msg.sender == exchangeRegistry[_exchangeId].agent);
exchangeRegistry[_exchangeId].goodsDelivered = _goodsDelivered;
}
}

DeclarationError: Identifier not found or not unique

I'm trying to compile my sol file, but it's giving me this error. Why am I getting this error?
DeclarationError: Identifier not found or not unique. -->
project:/contracts/MemoTokenSale.sol:11:5: | 11 | MemoToken
public token; | ^^^^^^^^^
Compilation failed. See above. Truffle v5.4.23 (core: 5.4.23) Node
v16.13.0
The following is my code
// SPX-License-Identifier: MIT
pragma solidity ^0.8.0;
import './MemoToken.sol';
import "#chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract MemoTokenSale {
address payable public admin;
address payable private ethFunds = payable(0x15b22B37679eF92672dA117F4357e048319008AD); // Web3 Account
MemoToken public token;
uint256 public tokensSold;
int public tokenPriceUSD;
AggregatorV3Interface internal priceFeed;
uint256 public transactionCount;
event Sell(address _buyer, uint256 _amount);
struct Transaction {
address buyer;
uint256 amount;
}
mapping(uint256 => Transaction) public transaction;
constructor(MemoToken _token) {
priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331); // ChainLink Contracts
tokenPriceUSD = 50;
token = _token;
admin = payable(msg.sender);
}
function getETHPrice() public view returns(int) {
(, int price, , , ) = priceFeed.latestRoundData();
return (price / 10^8);
}
function memoTokenPriceInETH() public view returns(int) {
int ethPrice = getETHPrice();
return tokenPriceUSD / ethPrice;
}
function buyToken(uint256 _amount) public payable {
int memoTokenPriceETH = memoTokenPriceInETH();
// Check that the buyer sends the enough ETH
require(int(msg.value) >= memoTokenPriceETH * int(_amount));
// Check that the sale contract provides the enough ETH to make this transaction.
require(token.balanceOf(address(this)) >= _amount);
// Make the transaction inside of the require
// transfer returns a boolean value.
require(token.transfer(msg.sender, _amount));
// Transfer the ETH of the buyer to us
ethFunds.transfer(msg.value);
// Increase the amount of tokens sold
tokensSold += _amount;
// Increase the amount of transactions
transaction[transactionCount] = Transaction(msg.sender, _amount);
transactionCount++;
// Emit the Sell event
emit Sell(msg.sender, _amount);
}
function endSale() public {
require(msg.sender == admin);
// Return the tokens that were left inside of the sale contract
uint256 amount = token.balanceOf(address(this));
require(token.transfer(admin, amount));
selfdestruct(payable(admin));
}
}

solidity - Invalid type for argument in function call. Invalid implicit conversion from address to address payable requested

I am trying to give address as payable but I am getting error at msg.sender and address(_author).transfer(msg.value) . it was showing like Invalid type for argument in function call. Invalid implicit conversion from address to address payable requested. i tried many ways to solve every time i replace the same error. before adding payable to author it was fine, but when added payable to author then it started getting error. In both, msg.sender and msg.value
pragma solidity >=0.4.0 <0.9.0;
contract SocialNetwork {
string public name;
uint public postCount = 0;
mapping(uint => Post) public posts;
struct Post {
uint id;
string content;
uint tipAmount;
address payable author;
}
event PostCreated(
uint id,
string content,
uint tipAmount,
address payable author
);
event PostTipped(
uint id,
string content,
uint tipAmount,
address payable author
);
constructor() public {
name = "Yash university Social Network";
}
function createPost(string memory _content) public {
//REquire Valid content
require(bytes(_content).length > 0);
// InCREMENT the post count
postCount ++;
// Create the post
posts[postCount] = Post(postCount, _content, 0, msg.sender);
// Trigger event
emit PostCreated(postCount, _content, 0, msg.sender);
}
function tipPost(uint _id) public payable {
//fetch the post
Post memory _post = posts[_id];
//fetch the author
address payable _author = _post.author;
//pay the author
address(_author).transfer(msg.value);
//increment the tip post
_post.tipAmount = _post.tipAmount + msg.value;
//update the post
posts[_id] = _post;
//Trigger an event
emit PostTipped(postCount, _post.content, _post.tipAmount, _author);
}
}
You have a few issues in your code:
1- in the Post struct, you defined address as payable:
struct Post {
uint id;
string content;
uint tipAmount;
address payable author;
}
But when you are creating a post, you are passing msg.sender which has address type. Before v0.8.0 msg.sender was payable but since than you have to cast it as payable(msgs.sender). it should be:
function createPost(string memory _content) public {
require(bytes(_content).length > 0);
postCount ++;
posts[postCount] = Post(postCount, _content, 0, payable(msg.sender));
emit PostCreated(postCount, _content, 0, payable(msg.sender));
}
2- in tipPost function you are getting payable address
address payable _author = _post.author;
but then you are casting it with address. In solidity address and payable address are two different things. send and transfer are only available to the payable address type. You dont need this:
address(_author).transfer(msg.value);
instead just
_author.transfer(msg.value);

How can I multiply dates in Solidity after Safemath.sol library is deprecated

I had this piece of code compiling in Solidity 0.6.0 without errors(I'm using Remix). But suddenly SafeMath library got deprecated because it wasn't necessary to import it anymore and I don't know how to fix this line:
uint raiseUntil = now.add(durationInDays.mul(1 days));
where I calculate a future date using the ¨mul¨ function.
I let you all code below. It's the backend of a crowdfunding platform. The raisedUntil variable is the date where the deadline of the crowdfunding project ends.
pragma solidity 0.6.0;
// Importing OpenZeppelin's SafeMath Implementation
import 'https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/math/SafeMath.sol';
contract Crowdfunding {
using SafeMath for uint256;
// List of existing projects
Project[] private projects;
// Event that will be emitted whenever a new project is started
event ProjectStarted(
address contractAddress,
address projectStarter,
string projectTitle,
string projectDesc,
uint256 deadline,
uint256 goalAmount
);
/** #dev Function to start a new project.
* #param title Title of the project to be created
* #param description Brief description about the project
* #param durationInDays Project deadline in days
* #param amountToRaise Project goal in wei
*/
function startProject(
string calldata title,
string calldata description,
uint durationInDays,
uint amountToRaise
) external {
uint raiseUntil = now.add(durationInDays.mul(1 days));
Project newProject = new Project(msg.sender, title, description, raiseUntil, amountToRaise);
projects.push(newProject);
emit ProjectStarted(
address(newProject),
msg.sender,
title,
description,
raiseUntil,
amountToRaise
);
}
/** #dev Function to get all projects' contract addresses.
* #return A list of all projects' contract addreses
*/
function returnAllProjects() external view returns(Project[] memory){
return projects;
}
}
contract Project {
using SafeMath for uint256;
// Data structures
enum State {
Fundraising,
Expired,
Successful
}
// State variables
address payable public creator;
uint public amountGoal; // required to reach at least this much, else everyone gets refund
uint public completeAt;
uint256 public currentBalance;
uint public raiseBy;
string public title;
string public description;
State public state = State.Fundraising; // initialize on create
mapping (address => uint) public contributions;
// Event that will be emitted whenever funding will be received
event FundingReceived(address contributor, uint amount, uint currentTotal);
// Event that will be emitted whenever the project starter has received the funds
event CreatorPaid(address recipient);
// Modifier to check current state
modifier inState(State _state) {
require(state == _state);
_;
}
// Modifier to check if the function caller is the project creator
modifier isCreator() {
require(msg.sender == creator);
_;
}
constructor
(
address payable projectStarter,
string memory projectTitle,
string memory projectDesc,
uint fundRaisingDeadline,
uint goalAmount
) public {
creator = projectStarter;
title = projectTitle;
description = projectDesc;
amountGoal = goalAmount;
raiseBy = fundRaisingDeadline;
currentBalance = 0;
}
/** #dev Function to fund a certain project.
*/
function contribute() external inState(State.Fundraising) payable {
require(msg.sender != creator);
contributions[msg.sender] = contributions[msg.sender].add(msg.value);
currentBalance = currentBalance.add(msg.value);
emit FundingReceived(msg.sender, msg.value, currentBalance);
checkIfFundingCompleteOrExpired();
}
/** #dev Function to change the project state depending on conditions.
*/
function checkIfFundingCompleteOrExpired() public {
if (currentBalance >= amountGoal) {
state = State.Successful;
payOut();
} else if (now > raiseBy) {
state = State.Expired;
}
completeAt = now;
}
/** #dev Function to give the received funds to project starter.
*/
function payOut() internal inState(State.Successful) returns (bool) {
uint256 totalRaised = currentBalance;
currentBalance = 0;
if (creator.send(totalRaised)) {
emit CreatorPaid(creator);
return true;
} else {
currentBalance = totalRaised;
state = State.Successful;
}
return false;
}
/** #dev Function to retrieve donated amount when a project expires.
*/
function getRefund() public inState(State.Expired) returns (bool) {
require(contributions[msg.sender] > 0);
uint amountToRefund = contributions[msg.sender];
contributions[msg.sender] = 0;
if (!msg.sender.send(amountToRefund)) {
contributions[msg.sender] = amountToRefund;
return false;
} else {
currentBalance = currentBalance.sub(amountToRefund);
}
return true;
}
/** #dev Function to get specific information about the project.
* #return Returns all the project's details
*/
function getDetails() public view returns
(
address payable projectStarter,
string memory projectTitle,
string memory projectDesc,
uint256 deadline,
State currentState,
uint256 currentAmount,
uint256 goalAmount
) {
projectStarter = creator;
projectTitle = title;
projectDesc = description;
deadline = raiseBy;
currentState = state;
currentAmount = currentBalance;
goalAmount = amountGoal;
}
}
You might decide to switch to a newer version of Solidity (current is 0.8.4), where now is deprecated so I already used block.timestamp instead. It's the same thing - now was just its alias.
The SafeMath library checks whether an arithmetic operation (such as multiplication or addition) on two unsigned integers would overflow/underflow. If it would, it throws an exception. If it wouldn't, it performs the arithmetic operation. Since Solidity 0.8 this is done automatically and SafeMath is not needed, so another reason to use the current version.
In Solidity 0.8 and newer, if this would overflow, it would throw an exception automatically
uint raiseUntil = block.timestamp + durationInDays * 1 days;
In older versions, you'll need to check for the possible overflow yourself and throw the exception from your code (the same behavior as SafeMath would do).
uint durationInSeconds = durationInDays * 1 days;
// Check that the result of division (inverse operation to multiplication) is the original number.
// If it's not, throw an exception, because the multiplication overflowed.
require(durationInSeconds / durationInDays == 1 days, 'Multiplication overflow');
uint raiseUntil = block.timestamp + durationInSeconds;
// Check that the result of subtraction (inverse operation to addition) is the original number.
// If it's not, throw an exception, because the addition overflowed.
require(raiseUntil - block.timestamp == durationInSeconds, 'Addition overflow');
Integer overflow/underflow is one of the most common security vulnerabilities in smart contracts. If your startProject() function was vulnerable, it would only allow to start a project with a deadline in the past. But in other implementations it can have more serious consequences. For example if an attacker exploits this vulnerability on a token contract, it could allow them to spend more tokens than they own.

Transfer ownership web3

I am creating a dapp to transfer ownership of the contract from one address to another using testrpc. However,I keep encountering this problem. I have tried using sentransaction method to do perform this ownership change.Perhaps I'm calling the exchange in a wrong manner.
Solidity version 0.4.4
web3 "version": "0.20.2"
web3.js:3127 Uncaught Error: VM Exception while processing transaction: invalid opcode
at Object.InvalidResponse (web3.js:3127)
at RequestManager.send (web3.js:6332)
at Eth.send [as sendTransaction] (web3.js:5066)
at SolidityFunction.sendTransaction (web3.js:4122)
at SolidityFunction.execute (web3.js:4208)
at transferOwnership (luxcure_manu.html:309)
at HTMLButtonElement.onclick (luxcure_manu.html:378
Full solidity contract as of yet.
pragma solidity ^0.4.4;
// TODO: Hash of the cert through IPFS Hash
// Transfer ownership of smart contract
contract LuxSecure {
address public contract_owner; //Manufacturer/owner
//string public current_owner; //Current Owner of good
bytes32 public model; //Model
mapping(uint => address) public owners; //list of owners
uint256 public owners_count;
bytes32 public status; // (Public(Owned by no one), Private(Bought by another entity),stolen(Stolen from public or private))
bytes32 public date_manufactured; //Time
// Set manufacturer of the Good RUN ONCE ONLY
function manufacturer() public{
if(owners_count == 0){
contract_owner = msg.sender;
}
}
//Modifier that only allows owner of the bag to Smart Contract AKA Good to use the function
modifier onlyOwner(){
require(msg.sender == contract_owner);
_;
}
// Add a new product to the blockchain with a new serial
function addNewGoods(bytes32 _model,bytes32 _status, bytes32 _date_manufactured) public returns(bool made) {//Declare Goods struct
setOwner(msg.sender);
model = _model;
status = _status;
date_manufactured = _date_manufactured;
return true;
}
//This function transfer ownership of contract from one entity to another
function transferOwnership(address _newOwner) public onlyOwner(){
require(_newOwner != address(0));
contract_owner = _newOwner;
}
//Set the KEY to uint256 and VALUE owner Ethereum Address
function setOwner(address owner)public{
owners_count += 1 ;
owners[owners_count] = owner;
}
//Get the previous owner in the mappings
function previousOwner()constant public returns(address){
if(owners_count != 0){
uint256 previous_owner = owners_count - 1;
return owners[previous_owner];
}
}
// Getter Methods
function getManufacturer() constant public returns(address){
return contract_owner;
}
function getCurrentOwner() constant public returns(address){
return owners[owners_count] ;
}
function getOwnerCount() constant public returns(uint256){
return owners_count;
}
function getModel() constant public returns(bytes32){
return model;
}
function getStatus() constant public returns(bytes32){
return status;
}
function getDateManufactured() constant public returns(bytes32){
return date_manufactured;
}
}// end of LuxSecure
Javascript to perform the transfer of ownership
function transferOwnership(){
var account_to_transfer = document.getElementById("ethereumaddress").value;
contract.transferOwnership(account_to_transfer,{
from:web3.eth.accounts[0],
gas:4000000
});
}
I don't see any particular mistake in your code. Maybe a bad formatting on the front-side, but can't guess for sure as we have partial front here.
I don't know if it will be some help but sometimes, using truffle, it happened to me to have some functions that returned bad opcode from testrpc/ganache-cli while no apparent error was in the code.
Deleting the ABI, recompiling the smart-contracts to get a brand new ABI and then redeploying the contracts solved the problem.