Smart contract based betting platform development for SocialBets
SocialBets provides a smart contract-based betting platform where users are able to place a wager in ETH on the outcome of any event they choose. Depending on the events’ results, the winner grabs his bet and money of his opponent as a winner prize.
What was the task ?
Blaize was involved in decentralized betting platform development from the initial project planning to the mainnet deployment. This project required complex solutions involving both front-end and contract developers teams.
The client request was such that neither back-end nor Subgraph should be used in order to simplify the systems’ work and keep it completely decentralized. Consequently, we need to organize smart contract architecture on the blockchain part which will interact with the front-end directly.
One of the main tasks before starting the development was the depiction of the bet status change. So we needed to design smart contracts’ scheme for their future implementation.
Ethereum blockchain was chosen due to its large toolset and a wide list of ready-made solutions that considerably faster the development. In addition, Ethereum is a trustable and well-know solution that helps to gain more credit from users in terms of delivering a new product to the market.
The Development Process
The project was held by the team of Solidity and front-end developers supported by QA and DevOps desks.
Primarily, we have divided the development process into three main milestones:
1. Smart contract development
2. Front-end development
- smart contracts integration
- Metamask integration
3. Project testing with contracts in the testnet
4. Mainnet deployment and project delivery
The first milestone was dedicated to the architecture planning and implementation of the smart contracts system.
While developing smart contract logic for the betting platform, the developers need to concentrate on providing the proper storage handling for betting and mediation flow as well as ensure safety in admin methods. We used Standard Ethereum development kit – Solidity smart contracts within Truffle Suite for SocialBets smart contracts set.
The next step was to develop the front-end parts and connect it with the contracts’ methods. The application layer is built on React with Web3 provider (for MetaMask) integration.
To ensure application interaction with Ethereum blockchain the platform needs MetaMask integration. This eliminates unwanted frictions and makes the performance process smoother.
The betting platform interface uses MetaMask for dApp logging, sending the transactions, staking ETH and checking info about the user’s holdings. Also, it stores the actual user account which is used for receiving the funds and rewards.
The absence of back-end in this case contributed to creation of a few interesting and challenging features that helps replacing automatic and time-dependent solutions.
For example, each bet has a voting timeout and a mediation timeout – time periods that should be followed by an automatic bet cancellation or bet finishing. Since there is no possibility to implement such timeout handlers on the blockchain part, we needed to translate this functionality to the front-end.
Such implementations allow users to manage certain events by themselves and process transactions (for instance send them for moderation or cancel a bet).
The last milestone was concentrated on platform testing and testnet/mainnet deployment. We used Ganache node for the local testing as a universal solution for smart contract based applications. We performed 100% unit-test coverage of smart contracts and the full set of manual tests for frontend and frontend-vs-contracts interactions.
Blaize’s team of QA engineers strongly recommend providing manual testing, even if the project has passed all automated tests and has shown great performance. Manual testing often helps to detect the bugs or defects connected to the user experience as a prior.
The automatic tools in the majority of cases are not able to recognize for ex. the size of the image on the pop-up messages or the rightfulness of text provided into it. That is why manual tests execution is highly appreciated.
Challenges we confronted
The absence of a backend surely simplifies the system but leads to storing an extensive amount of data on the blockchain part. Smart contracts are overloaded with information (like bet descriptions and metadata, extra number of events emitted etc.) that may considerably slow the application work and make them costly.
That is why we can define the optimization of communication between smart contracts and the web app interface (on the front end part) as one of the main challenges of this development. In further explanation, without the right front-end reorganization the dApp might have worked very slow and have difficulties with querying data from the blockchain.
In order to solve this problem, we needed to reorganize the process of data receiving. For the SocialBets case we organized the logic so that contracts communicate directly with the front-end and get the data from various events and getters. In order to perform this, we added a few extra storage structures and view methods in the contracts with an event emitted on every action.
We brought the main functions (supposed to be on the back-end part) to the user interface in the web app. We distributed them into tabs – for example for closed, created or funded bets – like on the picture above.
The implementation of blockchain represented by smart contracts allows for omitting the back-end and configure the software so that it communicates with the front-end. Thus, the end-user may interact with the contracts directly from Etherscan.
As a result we get a smart-contract based betting platform that allows its users to make bets for any events. The lack of back-end part enables the simplification of the system along with its full independence and decentralization. The usage of Ethereum framework ensures the proper work of smart contracts and allows creating bets in ETH.