0

I'm encountering a Parse Error when creating an Event.

In the below createEvent Function, I'm getting an error on this line: enter image description here

Error: Parse Error: Expected primary expression.

Here's the whole function code:

// Function to create a new prediction event
function createEvent(
    bytes32 eventId,
    uint256 startTime,
    uint256 endTime,
    bytes32 result
)
    public
    onlyOwner
    returns (bool success)
{
    require(events[eventId].totalBet == 0, "Event already exists");

    // Create the new prediction event
    events[eventId] = Event({
        eventId: eventId,
        startTime: startTime,
        endTime: endTime,
        result: result,
        totalBet: 0,
        numBets: 0,
        bets: mapping(bytes32 => uint256)new(0)
    });

    return true;
}

And here's the events struct:

// Struct to represent a prediction event
struct Event {
    bytes32 eventId;
    uint256 startTime;
    uint256 endTime;
    bytes32 result;
    uint256 totalBet;
    uint256 numBets;
    mapping(bytes32 => uint256) bets;
}   

I can provide more code if this is not sufficient.

EDIT: Providing the whole smart contract for prediction betting:

pragma solidity ^0.8.0;

import "@openzeppelin/solidity/math/SafeMath.sol";
import "@openzeppelin/solidity/access/AccessControl.sol";

// Market Prediction Smart Contract

contract MarketPrediction is Upgradeable, AccessControl {
    using SafeMath for uint256;

    // Mapping of prediction events to their details
    mapping(bytes32 => Event) public events;

    // Mapping of users to their balances
    mapping(address => Balances) public userBalances;

    // Struct to represent a user's balances for various currencies
    struct Balances {
        uint256 eth;
        uint256 bnb;
        uint256 dai;
        uint256 usdt;
        uint256 usdc;
        uint256 busd;
    }

    // Struct to represent a prediction event
    struct Event {
        bytes32 eventId;
        uint256 startTime;
        uint256 endTime;
        bytes32 result;
        uint256 totalBet;
        uint256 numBets;
        mapping(bytes32 => uint256) bets;
    }   
    // Event emitted when a user makes a bet on a prediction event
    event BetPlaced(
        bytes32 indexed eventId,
        address indexed user,
        bytes32 indexed prediction,
        uint256 betAmount,
        string currency
    );

    // Event emitted when the result of a prediction event is revealed
    event ResultRevealed(
        bytes32 indexed eventId,
        bytes32 indexed result
    );

    // Event emitted when a user withdraws their balance
    event Withdrawal(
        address indexed user,
        uint256 amount,
        string currency
    );

    // Constructor sets the owner of the contract
    constructor() public {
        setOwner(msg.sender);
    }

    // Function to create a new prediction event
    function createEvent(
        bytes32 eventId,
        uint256 startTime,
        uint256 endTime,
        bytes32 result
    )
        public
        onlyOwner
        returns (bool success)
    {
        require(events[eventId].totalBet == 0, "Event already exists");

        // Create the new prediction event
        events[eventId] = Event({
            eventId: eventId,
            startTime: startTime,
            endTime: endTime,
            result: result,
            totalBet: 0,
            numBets: 0,
            bets: mapping(bytes32 => uint256)new(0)
        });

        return true;
    }

    // Function to place a bet on a prediction event
    function placeBet(
        bytes32 eventId,
        bytes32 prediction,
        uint256 betAmount,
        string currency
    )
        public
        payable
        returns (bool success)
    {
        // Check if event exists and is active
        Event storage event = events[eventId];
        require(event.totalBet > 0, "Event does not exist or has ended");
        require(now >= event.startTime && now <= event.endTime, "Event is not active");

        // Check if user has sufficient balance
        require(userBalances[msg.sender][currency] >= betAmount, "Insufficient balance");

        // Place bet and update event and user balance details
        event.totalBet = event.totalBet.add(betAmount);
        event.numBets = event.numBets.add(1);
        event.bets[prediction] = event.bets[prediction].add(betAmount);

        if (currency == "eth") {
            userBalances[msg.sender].eth = userBalances[msg.sender].eth.sub(betAmount);
        } else if (currency == "bnb") {
            userBalances[msg.sender].bnb = userBalances[msg.sender].bnb.sub(betAmount);
        } else if (currency == "dai") {
            userBalances[msg.sender].dai = userBalances[msg.sender].dai.sub(betAmount);
        } else if (currency == "usdt") {
            userBalances[msg.sender].usdt = userBalances[msg.sender].usdt.sub(betAmount);
        } else if (currency == "usdc") {
            userBalances[msg.sender].usdc = userBalances[msg.sender].usdc.sub(betAmount);
        } else if (currency == "busd") {
            userBalances[msg.sender].busd = userBalances[msg.sender].busd.sub(betAmount);
        }

        // Emit event
        emit BetPlaced(eventId, msg.sender, prediction, betAmount, currency);

        return true;
    }

     // Function to reveal the result of a prediction event
    function revealResult(
        bytes32 eventId,
        bytes32 result
    )
        public
        onlyOwner
        returns (bool success)
    {
        Event storage event = events[eventId];

        // Check if event exists and is inactive
        require(event.totalBet > 0, "Event does not exist or is still active");
        require(now > event.endTime, "Event is still active");

        // Set result and emit event
        event.result = result;
        emit ResultRevealed(eventId, result);

        return true;
    }

    // Function to withdraw a user's balance
    function withdraw(string currency)
        public
        returns (bool success)
    {
        // Check if user has sufficient balance
        require(userBalances[msg.sender][currency] > 0, "Insufficient balance");

        // Withdraw balance and emit event
        uint256 amount = userBalances[msg.sender][currency];
        if (currency == "eth") {
            msg.sender.transfer(amount);
        } else if (currency == "bnb") {
            // TODO: Implement BNB withdrawal
        } else if (currency == "dai") {
            // TODO: Implement DAI withdrawal
        } else if (currency == "usdt") {
            // TODO: Implement USDT withdrawal
        } else if (currency == "usdc") {
            // TODO: Implement USDC withdrawal
        } else if (currency == "busd") {
            // TODO: Implement BUSD withdrawal
        }

        userBalances[msg.sender][currency] = 0;
        emit Withdrawal(msg.sender, amount, currency);

        return true;
    }
}
2
  • Can you edit your post with entire smart contract code? Jan 7, 2023 at 15:23
  • Edited the post to include the whole smart contract code @AntonioCarito
    – SpiderW3B
    Jan 9, 2023 at 13:44

2 Answers 2

0

you can do this by using

function createEvent(
    bytes32 eventId,
    uint256 startTime,
    uint256 endTime,
    bytes32 result
)
    public
    returns (bool success)
{
    require(events[eventId].totalBet == 0, "Event already exists");

    // Create the new prediction event
        Event storage temp= events[eventId];
        temp.eventId= eventId;
        temp.startTime=startTime;
        temp.endTime=endTime;
        temp.result= result;
        temp.totalBet=0;
        temp.numBets= 0;

    return true;
}
1
  • Can you try help me understand why this is the answer? the bets was not included in creating the event. how will I add details to the bets?
    – SpiderW3B
    Jan 9, 2023 at 13:45
0
// Function to create a new prediction event
function createEvent(
    bytes32 eventId,
    uint256 startTime,
    uint256 endTime,
    bytes32 result
)
    public
    onlyOwner
    returns (bool success)
{
    require(events[eventId].totalBet == 0, "Event already exists");

    // Create the new prediction event
    Event storage event = events[eventId];

    event.eventId = eventId;
    event.startTime = startTime;
    event.endTime = endTime;
    event.result = result;
    event.totalBet = 0; // not strictly necessary, since it's 0 by default
    event.numBets = 0;

    return true;
}

Mappings are not created with new (inside a struct or not). You simply assign a value given a key. Structs that contain mappings are special, since they cannot be created (and stored in memory). You'll have to pretend that the struct has already been created like above (avoiding memory costs less gas anyhow). Alternatively, you could also move the mapping outside the struct.

And later when you want to add bets:

    Event storage event = events[eventId];
    event.bets[hash] = 123;

Edit: You might actually want to change the require check to something else, like checking the startTime, since in the above example, event.totalBet is still 0 after creation.

1
  • If I understand it correctly, removing the bets when creating the event would solve this? does that mean the certain event would not include the bets mapping meaning the event would not have any details about bets once sent to the blockchain?
    – SpiderW3B
    Jan 9, 2023 at 13:48

Your Answer

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

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