Betting via. the Blockchain

Xiangan He
6 min readFeb 1, 2021

Double or nothing is the game, losing all your Ether sure is lame.

Casinos pay a ton of money to continue operating at one location. For example, each brand new slot machine costs around $20k. Although these machines offer adults an in-person experience with food, drinks, flashing lights and party vibes, it can’t go anywhere else other than the place where it’s stationed.

Blockchain smart contracts has the power to not only automate two-party transactions, but distribute it across a global network. All the buyer and sellers of each transaction have to do with smart contracts is agree upon the preset conditions for the allocation of money at a given time, deposit money into the contract, and give the smart contract a signal to release the money when services have been provided. In this case, after depositing money into the contract, it can actually automate the process of determining if a user won or lost a bet by fetching a random number. Then, it will subsequently reward the user with money or keeping their money for itself, to be later deposited into the pockets of the game-maker who made the contract.

Smart contracts are series of conditionals deployed onto the blockchain most often used in cryptocurrency transactions. They’re just like the contracts we

have now, where each party declares what they want and what will happen, except that it’s all enforced by the computer. Current contracts require a third party to make sure both parties uphold their ends of the deal (for example, the buyer paying their deposits of good faith). However, smart contracts skip over the step of having to go to a third party. Instead, it keeps all transactions private between only the buyer and seller of each transaction. In this case, we are using a blockchain protocol called Chainlink to fetch a random number for betting. For example, if I wanted to bet $1000 on a coin flip, I can set up the win conditions using my smart contract, where if the number fetched from Chainlink is 0, I lose, and vice versa. Then, I can have my smart contract draw a random number (0 or 1) from Chainlink and input that number into the win conditions to have the smart contract release the amount of Ether corresponding to $1000.

Recently, I made that into a reality. I built a blockchain betting game by integrating Chainlink’s random number generator capabilities to prove that this is within the realm of possibility.

Credits to Dapp University for this image of how the web-app works

Firstly, the web-app connects to the user’s Metamask crypto-wallet using the following code:

async function loadWeb3() {if (window.ethereum) {     window.web3 = new Web3(window.ethereum);     window.ethereum.enable();

This gives the person a pop-up to connect to the site using Metamask, which is necessary for the site to load the information pertaining to your account balance. This is also necessary for actually transacting Ether into the smart contract. More will come regarding using web3 to integrate the frontend and the smart contract.

They are asked to predict the roll of a 6 sided die. They can either bet “high” or “low”, for numbers “4–6” or “1–3” respectively. Next, they would make a bet within the bounds of the minimum and maximum bets. The transaction processes, gets logged onto the blockchain, and goes through the Solidity smart contract to fetch a random number (0 or 1) from the Chainlink smart contracts. Depending on the result of the random number, the user would either receive twice the amount they have betted, up to a maximum of the current balance of the smart contract, or lose all their money to the smart contract (to be collected by the creator of the game).

The betting game’s interactive platform… as you can see, I lost my ETH.

Behind the Scenes Highlights: Smart Contract

In order to first get the betting smart contract running, we must first deposit Link tokens and Ether to have something to give to the gambler if they win. Link tokens are the fees that we need in order to use Chainlink’s random number generator, and the Ether that we deposit is the maximum amount of cash-out that the individual can get while they’re betting their money. I got the funds to do this with via. the Rinkeby test network and requested Link tokens and Ether from the testnet faucet.

The best part is, Chainlink offers documentation on their random number generation smart contract, so you can actually configure the blockchain betting app to apply to situations where the odds might not be so even.

The backend code that adjudicates random-number processes and payouts.

First, the smart contract fetches a random number from Chainlink using its smart contracts after paying a fee to Chainlink in Link tokens. Then, based on the number fetched from Chainlink, the smart contract would either keep the balance, or pay out two times the balance originally deposited by the caller of the smart contract (the person betting). The smart contract calls the variable “half” (which has been previously set to 0.5*uint256+1 to ensure 50/50 odds) to decide the victor.

Before the blockchain is able to pay out the balance, the blockchain will actually have to be able to take in and store the balance of the Ether that we pay to it with the following lines of code:

/* Allows this contract to receive payments */
receive() external payable {
emit Received(msg.sender, msg.value);
}

Behind the Scenes Highlights: Web3 integration

After the smart contract is set, it is necessary to download the dependencies necessary to actually develop the frontend integration. We’re using React.js and web3.js here.

After loading the necessary assets, we would update the balance of the smart contract (and your own crypto wallet) as seen below:

Next, we would send the bet to the verdict function from earlier to get a number that would determine if the gambler won or not. Here, just as in accordance with the smart contract, we represent a loss with 0 (you can also do 1, just make sure win conditions on both sides are consistent). After the registration of each bet, the web app would show whether if the user won or lost.

Before anything like the .send function takes place though, you’ll need to set the contract address and the account information. The below code should be placed as one of the first things you write on the js file that processes your webapp. You will need to declare such variables for the commands that I had shown above to run on. For example, the .send command that transfers a certain Ethereum value from your account to the smart contract requires that you have already loaded in information about both the smart contract and your account.

Calls your smart contract address and accounts to link both up to the webapp frontend. VERY IMPORTANT.

After all of that, and some other React programming of the frontend, we’ll end up with the blockchain betting game that can make or break your fortunes, all on a 50–50 chance. Not only that, but you’ve also found the way to mass-distributing a gaming experience that has the potential to earn you money!

I’m Xiangan — a 17 year old blockchain developer passionate for learning, and I appreciate you for reading! If you enjoyed the article, please feel free to follow me on my LinkedIn. Drop me a line and let me know your thoughts!

Check out the Github Repo for the project here!

--

--

Xiangan He

Blockchain guy. Public servant. Finance enthusiast. Son.