Placing Bets
To place a bet, users interact with the BettingContract
through the MAGABET user interface. The BettingContract
is an Ethereum smart contract written in Solidity that manages the betting process. Here's a simplified version of the placeBet
function in the BettingContract
:
function placeBet(uint256 _itemId, uint256 _amount) external {
require(_amount > 0, "Bet amount must be greater than zero");
require(token.transferFrom(msg.sender, address(this), _amount), "Token transfer failed");
Bet memory newBet = Bet({
bettor: msg.sender,
itemId: _itemId,
amount: _amount,
claimed: false
});
bets.push(newBet);
emit BetPlaced(msg.sender, _itemId, _amount);
}
In this function, the user specifies the ID of the scorecard item they want to bet on (_itemId
) and the amount of $MAGABET tokens they wish to wager (_amount
). The function first checks that the bet amount is greater than zero and then attempts to transfer the specified amount of tokens from the user's wallet to the BettingContract
using the transferFrom
function of the ERC-20 token contract.
If the token transfer is successful, a new Bet
struct is created, containing the user's address (bettor
), the item ID (itemId
), the bet amount (amount
), and a boolean flag indicating whether the reward has been claimed (claimed
). The new bet is then added to the bets
array, and a BetPlaced
event is emitted to notify listeners of the newly placed bet.
Last updated