3 - Prediction game on Alephium
In this series of articles, we'll explain how we developed a decentralized application on Alephium.
The third part will focus on the contract used by the bettors and the TxScript used to interact with the contracts on the chain. The two previous articles have dealt with the main PredictALPH contract and Round, which handles the rounds and stores information about them.
To test the application: ALPH.bet
A reminder of what Alephium is:
Alephium is the first scalable Layer 1 sharded blockchain that enhances the concepts of Proof of Work & UTXO. Decentralization, self-sovereignty and security meet energy efficiency in a developer-friendly network optimized for DeFi applications and smart contracts.
Contract Punter.ral
The "Punter" contract (slang for "bidder") is relatively simple and allows you to store the amount bet, the "Up" or "Down" choice, and when everyone can claim each other's winnings.
States
Error code
A single error code is implemented to check whether the address calling the contract functions is correct.
Functions
These various functions provide quick access to the status of the smart contract, and therefore of the person who placed the bet in a round.
The other function of this contract is destroy
, which destroys the bettor's contract and retrieves the ALPH used to bet on the round. Another function, destroyByOther
, is currently in place but will be replaced by a single function.
TxScript
TxScript are the functions that enable interaction with smart contracts. These scripts are not registered in the Alephium blockchain.
When a user interacts with the dapp, several TxScripts will be used, depending on the action. This is also part of the way in which interaction with the blockchain will be possible from TypeScript, for example, the SDK can be found here : Dapp SDK
The Bid
function allows you to place bets, and APS (Asset Permission System) will authorize only the desired amount to be sent. In this case, the person betting will authorize what's in amount
.
And here's how the interaction in TypeScript is done with the smart contract and therefore the blockchain. 1 ALPH is added, because the person placing the bet will have to create a new contract and therefore put in additional ALPH (which can be retrieved).
attoAlphAmount
is the amount approved (APS) and sent to the smart contract.
Another script for the gambler is Withdraw
, which will be useful for recovering winnings and destroying the associated contract.
And the TypeScript part for interacting with the string. The parameters in the image above can be found under initialFields
.
Again attoAlphAmount
is used, but only the fee needs to be approved, as no ALPH is sent to the contract, only retrieved.
For the round part, a TxScript is used to start and stop. The same function can be used to automatically restart a round. Destroying a round can also be done via a script
And in TypeScript, for example, using one of these functions looks like this:
End
This article concludes the series on ALPH.bet, a prediction game developed on Alephium. The development experience is really enjoyable. After grasping the various concepts such as the Asset Permission System and the need to rent space, the application could be set up quickly and securely. Fewer lines of code were required compared with the Ethereum version, which confirmed our decision to choose Alephium.
For those interested in exploring the subject further:
More articles will be added, the web interface will be updated, and with it, new developments. For example, the possibility of betting on other events, such as the price of bitcoin, or whether the Rhône update will be effective in March 2024.