I am trying to build a escrow smart contact between club promoter who wants to book my artist with ether but I am getting ParserError - smartcontracts

The error I am getting when I compile is: (ParserError: Expected primary expression.price = _price; * (1 ether);)
I would like to add a timestamp of 7 days for the promoter to add all funds to escrow in full not half to meet my conditions to book us. Is this even possible as a condition for us on our in? Do I need a multisig added to my smart contract to make it secure on both sides or is this code ok? Can anyone assist me in solving this problem?
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.6;
contract Escrow {
//VARIBALES
enum State { NOT_INITIATED, AWAITING_PAYMENT, AWAITING_DELIVERY, COMPLETE }
State public currState;
bool public isBuyerIn;
bool public isSellerIn;
uint public price;
address public buyer;
address payable public seller;
//MODIFIERS
modifier onlyBuyer() {
require(msg.sender == buyer, "Only buyer can call this function"); _;
}
modifier escrowNotStarted() {
require(currState == State.NOT_INITIATED);
_;
}
//FUCTIONS
constructor(address _buyer, address payable _seller, uint _price){
buyer =_buyer;
seller = _seller;
price = _price; * (1 ether);
}
function initContract() escrowNotStarted public{
if(msg.sender == buyer) {
isBuyerIn = true;
}
if (msg.sender == seller) {
isSellerIn = true;
}
if (isBuyerIn && isSellerIn) {
currState = State.AWAITING_PAYMENT;
}
}
function deposit() onlyBuyer public payable {
require(currState == State.AWAITING_PAYMENT, "Already paiid");
require(msg.value == price, "Wrong deposit amount");
currState = State.AWAITING_DELIVERY;
}
function confimDelivery() onlyBuyer payable public {
require(currState == State.AWAITING_DELIVERY, "Cannot confirm delivery");
seller.transfer(price);
currState = State.COMPLETE;
}
function withdraw() onlyBuyer payable public {
require(currState == State.AWAITING_DELIVERY, "Cannot withdraw at this stage");
payable(msg.sender).transfer(price);
currState = State.COMPLETE;
}
}

You have an extra semicolon on the price assigning line in the constructor.
Replace
// original code, extra semicolon
price = _price; * (1 ether);
with
// updated code, removed the semicolon
price = _price * (1 ether);

Related

problem in crowdfunding contract: ParserError: Expected primary expression

I am with a code but I have this problem:
ParserError: Expected primary expression. --> contracts/Crowfunding.sol/crowfundingstampe.sol:35:2: | 35 | } | ^
the code is:
> SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
contract Crowfunding {
uint256 public fundingGoal; //the target amount of funds that must be raised for the campaign to be considered successful
uint256 public deadline; // the deadline by which contributions must be received
uint256 public amountRaised; // the total amount of funds raised so far
mapping (address => uint256) public contributions; // tracks the amount contributed by each individual.
address [] public suppliers; //an array that keeps track of the addresses of all suppliers who have contributed to the campaign
bool public funded;
uint256 public fundsReturned;
constructor(uint256 _fundingGoal, uint256 _deadline) public {
fundingGoal = _fundingGoal;
deadline = _deadline;
}
function contribute() public payable {
require(msg.value > 0, "You must contribute a positive amount.");
require(now <= deadline, "The deadline for contributions has passed.");
contributions[msg.sender] += msg.value;
amountRaised += msg.value;
backers.push(msg.sender);
}
function returnFunds() public {
require(now > deadline, "The deadline for contributions has not passed yet.");
require(fundsReturned == 0, "The funds have already been returned.");
for (uint256 i = 0; i < backers.length; i++) {
backers[i].transfer(contributions[backers[i]]);
}
>
to compile but it pop o that error
First of all was missing array backers, two brackets } , keyword now deprecated, instead use block.timestamp, and in for() loop backers[i] must be payable.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Crowfunding {
uint256 public fundingGoal; //the target amount of funds that must be raised for
the campaign to be considered successful
uint256 public deadline; // the deadline by which contributions must be received
uint256 public amountRaised; // the total amount of funds raised so far
mapping (address => uint256) public contributions; // tracks the amount
contributed by each individual.
address [] public suppliers; //an array that keeps track of the addresses of all
suppliers who have contributed to the campaign
bool public funded;
uint256 public fundsReturned;
address[] public backers;
constructor(uint256 _fundingGoal, uint256 _deadline) {
fundingGoal = _fundingGoal;
deadline = _deadline;
}
function contribute() public payable {
require(msg.value > 0, "You must contribute a positive amount.");
require(block.timestamp <= deadline, "The deadline for contributions has
passed.");
contributions[msg.sender] += msg.value;
amountRaised += msg.value;
backers.push(msg.sender);
}
function returnFunds() public {
require(block.timestamp > deadline, "The deadline for contributions has not
passed yet.");
require(fundsReturned == 0, "The funds have already been returned.");
for (uint256 i = 0; i < backers.length; i++) {
payable(backers[i]).transfer(contributions[backers[i]]);
}
}
}

Why am I getting this error "Gas estimation errored with the following message (see below)"?

My remix keep alerting "Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? execution reverted". Can you guys help me find which part that im missing.
pragma solidity ^0.4.21;
contract GuessTheNewNumberChallenge {
function GuessTheNewNumberChallenge() public payable {
require(msg.value == 1 ether);
}
function isComplete() public view returns (bool) {
return address(this).balance == 0;
}
function guess(uint8 n) public payable {
require(msg.value == 1 ether);
uint8 answer = uint8(keccak256(block.blockhash(block.number - 1), now));
if (n == answer) {
msg.sender.transfer(2 ether);
}
}
}
contract Attack {
address vt = 0x2417929C9AE5884a754Cf1f77FA5FaBDDC9ce92A;
GuessTheNewNumberChallenge gn = GuessTheNewNumberChallenge(vt);
function attack() public {
uint8 answer = uint8(keccak256(block.blockhash(block.number - 1), now));
gn.guess(answer);
}
}
You must to specificy in Attack smart contract the GuessTheNewNumberChallenge
smart contract address for use its functions. To resolve this problem try this:
pragma solidity ^0.4.21;
contract GuessTheNewNumberChallenge {
function GuessTheNewNumberChallenge() public payable {
require(msg.value == 1 ether);
}
function isComplete() public view returns (bool) {
return address(this).balance == 0;
}
function guess(uint8 n) public payable {
require(msg.value == 1 ether);
uint8 answer = uint8(keccak256(block.blockhash(block.number - 1), now));
if (n == answer) {
msg.sender.transfer(2 ether);
}
}
}
contract Attack {
// I declare variable refers to GuessTheNewNumberChallenge
GuessTheNewNumberChallenge private gn;
// I set into constructor about this smart contract the address where GuessTheNewNumberChallenge deployed.
// And when the instance is complete, you can use the GuessTheNewNumberChallenge functions.
constructor(address gtncAddress) public {
gn = GuessTheNewNumberChallenge(gtncAddress);
}
function attack() public {
uint8 answer = uint8(keccak256(block.blockhash(block.number - 1), now));
gn.guess(answer);
}
}
I put some comments into smart contracts.

gas estimation errored with the following message - execution reverted

// SPDX-License-Identifier: MIT
pragma solidity >=0.6.6 < 0.9.0;
import "#chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract FundMe {
mapping(address => uint256) public addressToAmountFunded;
function fund() public payable {
uint256 minimumUSD = 30 * 10 ** 18;
require(getConversionRate(msg.value) >= minimumUSD, "You need to spend more ETH");
addressToAmountFunded[msg.sender] += msg.value;
}
function getVersion() public view returns (uint256) {
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
return priceFeed.version();
}
address public owner;
constructor() public {
owner = msg.sender;
}
function getPrice() public view returns (uint256) {
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(,int256 answer,,,) = priceFeed.latestRoundData();
return uint256(answer * 10000000000);
}
function getConversionRate(uint256 ethAmount) public view returns (uint256) {
uint256 ethPrice = getPrice();
uint256 ethAmountInUsd = (ethPrice * ethAmount)/1000000000000000000;
return ethAmountInUsd;
}
function withdraw() public payable {
require(msg.sender == owner);
payable(msg.sender).transfer(address(this).balance);
}
}
I write this piece of code. I get this message every time I deploy:
"gas estimation errored with the following message - execution reverted"
I know problem is in:
function fund() public payable {
uint256 minimumUSD = 30 * 10 ** 18;
require(getConversionRate(msg.value) >= minimumUSD, "You need to spend more ETH");
addressToAmountFunded[msg.sender] += msg.value;
}
In this function I try to avoid geting value below 30$.
Can anybody help me?

Type error: Member not found or visible in unit256

I have this crowdfunding platform that was initially using SafeMath library. And since is deprecated I'm looking for a workaround to make it compile and I'm getting a compilation error with remix in line 131:
contributions[msg.sender] = contributions[msg.sender].add(msg.value);
Here you have the whole contract code, it's a platform where you can create projects and raise funds:
// We will be using Solidity version 0.6.0
pragma solidity 0.6.0;
// Importing OpenZeppelin's SafeMath Implementation
//import 'https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/math/SafeMath.sol';
//import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/mocks/SafeMathMock.sol";
//import "#openzeppelin/contracts/utils/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,
uint deadline,
uint 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));
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');
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 {
// not 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;
}
}
The Using X for Y expression extends the Y datatype with functions of the X library.
In case of using SafeMath for uint256, it allows to use the functions defined in SafeMath (such as add()) on uint256 variables.
Specifically, function add() in SafeMath checks whether the addition would overflow the 256bit unsigned integer. If it did overflow, it throws an exception. If it didn't overflow, it returns the result of the addition.
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
You can achieve the same result with this snippet:
uint256 contribution = contributions[msg.sender] + msg.value; // c = a + b
require(contribution >= contributions[msg.sender]); // require(c >= a)
contributions[msg.sender] = contribution; // return c
If you switch to Solidity 0.8+, this is not needed, because since version 0.8.0, Solidity performs the overflow check automatically and reverts if an overflow/underflow would occur. So it would be sufficient to use just this snippet:
// safe in Solidity 0.8+, unsafe in older versions
contributions[msg.sender] += msg.value;
Arithmetic operations revert on underflow and overflow. You can use unchecked { ... } to use the previous wrapping behaviour.
Source: docs

Coding Question of Auction smart contract

This is Auction smart contract.
I want to make Auction like this. If someone registers his product for biding, anyone who want to buy can join on this bidding.
But When I test on Remix, it can't be complied even.
I think my coding is problem.
Please help me.
pragma solidity ^0.5.0;
contract Auction{
//product for auction
struct Product{
string name;
string description;
uint time;
}
//top bid
uint topMoney;
//presen owner of product
mapping (uint => address) productToOwner;
//owner of top bidder
mapping (uint => address) topMoneyOwner;
event Listed(uint id, string name, uint itme);
Product[] public products;
//register product
function listUp(string memory _name, string memory _description) public {
//time limit for bidding, 1 minutes;
uint time = now + 1 minutes;
//index of product
uint id = products.push(Product(_name, _description, time)) - 1;
//initial bid = 0
topMoney=0;
//initial owner of product
productToOwner[id] = msg.sender;
emit Listed(id, _name, time);
}
//bidding
function bidOn() payable public {
if ( topMoney < msg.value){
topMoney = msg.value;
topMoneyOwner[topMoney] = msg.sender;
} else {
msg.sender.transfer(msg.value);
}
}
//bidding end? return (bool)
function _end(uint _id) private view returns (bool) {
require(now >= products[_id].time);
return true;
}
//who is winner? Then, transfer money to owner of product.
function winner(uint _id) public {
require( true == _end(_id));
address(uint160(productToOwner[_id])).transfer(topMoney);
productToOwner[_id] = topMoneyOwner[topMoney];
}
}