Building a Sport Betting DApp
How I created my first DApp from scratch
Sports Betting
Many avid sport fans enjoy placing bets on their favorite teams in an attempt to make profits. However, one problem is that many betting websites can be insecure and require a third party to handle and redistribute their money. But why place our trust in these intermediaries? This is where DApps come in.
What is a DApp?
A decentralized application, or DApp, is an app that contains a smart contract and runs on a distributed network such as the blockchain. A smart contract is executable code on the blockchain that runs when certain conditions are met.
Smart contracts remove the need for intermediaries and allow for transactions to be governed and carried out solely by code. This means that all your money is handled by code rather than a potentially unreliable third party.
There are many protocols that can be used to build DApps but the main network and the one I used is Ethereum.
In depth article I wrote on smart contracts:
Contract Structure
Before we breakdown the contract code, let’s list out functions/variables we may need and data we will need to store.
Events
event NewBet
: stores a new bet on the blockchain with address, amount bet, and team
Structs
struct Bet
: contains name, address of user, amount they bet, and team they bet onstruct Team
: contains name of the team and the total amount of ETH placed on their team
Variables/Mappings/Arrays
Bet[]
: array containing all betsTeam[]
: array containing all teamsaddress payable
: address of the owner of the contractuint totalBetMoney
: total bets placed on both teamsmapping numBetsAddress
: links address to a bet to ensure that each user only places one bet until a winner is chosen
Functions
create Team(_name)
: can be called by owner to create a new team that bets can be placed ongetTotalBetAmount(_teamId)
: can be called to get total bet amount placed on a teamcreate Bet(_name, _teamId)
: can be called by a user with msg.value to place a bet on a certain team (ETH transferrs to contract)teamWinDistribution(_teamId)
: can be called by owner to make a team win and distribute ETH to winners accordingly
Essentially, 2 teams that are preset (Warriors/Nets in this case) can be betted on. Each team has a unique ID (0/1) that a user can use to place a bet. Whenever a user places a bet, create Bet
is called and whenever the owner of the contract sets a team as a winner, team WinDistribution
is called.
My contract also utilized ownable.sol, ATM.sol, and console.sol. Ownable allows me to add function modifiers such as only Owner
which only allows the contract owner to call a function. ATM is what I used to facilitate the transferring of ETH between accounts. Console is a library that helped with debugging and allowed me to view variables throughout the contract execution.
Code Walkthrough
https://medium.com/media/7aceb034a8cb3db21fc22824056d010d
Line one is for the compiler and specifies what version of Solidity we aim to use. The import statements import all the external contracts/libaries we will use.
In Solidity, an event is called whenever information needs to be stored on the blockchain. In this case, we wish to store all new bets on the blockchain with the user’s address, the amount of ether they bet, and the team that they placed a bet on.