2 - Prediction game on Alephium

2 - Prediction game on Alephium

In this series of articles, we'll explain how we developed a decentralized application on Alephium.


4 min read

This article is the second part of "Building on Alephium". To read the previous article explaining how the dApp works and the PredictALPH main contract, click here.

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.

In this article, we'll be focusing on the Round contract. It will store bets, ALPH, number of participants, winnings at the end of a round, etc.

Contract Round.ral

For each new round, a new contract is created, and Round.ral is used as a template for this. This is how a new contract and round are initialized in PredictALPH.ral.

The full code can be found on Github.

Lines 5 to 7 will encode the states, transforming them into ByteVec so that they can be used in CopyCreateSubContract!. The first part, from selfContract!to operator, represents immutable values, i.e. they cannot be changed after creation.

And so the new round is created on line 9 with the previously created variables.



The first state, prediction, contains the address of the PredictALPH contract. This allows only calls from this contract to be authorized, and not from any other. It is therefore not possible to call Round directly.



This series of functions is useful for retrieving information on various states, such as when a round ends or the total amount of fees collected.


This function updates the number of ALPH bet in the round and transfers the bettor's ALPH to the round contract.

Line 3, checkCaller, validates that the call only comes from the PredictALPH smart contract.

The variable totalAmount (l. 6) is used to optimize smart contract costs. Although the tokenRemaining!function can be used, it requires more gas. Therefore, the total contract amount is stored in totalAmount to make it more efficient.


The calculateRewards function will calculate the outcome of a round and the associated winnings.

The variable rewardBaseCalAmount is used to store the total amount of those who have bet correctly and calculate how much a person has won when claiming their winnings.

treasuryAmount contains the total amount of fees taken by the contract, currently 1% (in 100 basis points).

Finally, rewardAmount is the actual amount that will be shared between all winners.

If the final price is the same as the initial price, the "house wins".


A function that simply "boosts" a round, i.e. adds ALPH for the winning side. This encourages people to play, for example.


This function destroys a smart contract on Alephium, freeing up space on the chain. It is called from PredictALPH.ral and cannot be executed completely, i.e. the contract can only be destroyed if all players have claimed their winnings (l. 4).


The last function is userClaimRewards, which allows participants to claim their winnings. Line 10 calculates the number of ALPH earned:

$$(amountBid * rewardAmount) / rewardBaseCalAmount$$

  • amountBid : number of ALPH bet by a participant

  • rewardAmount : total amount played (less fees)

  • rewardBaseCalAmount : total number of ALPH bet for side

If a person bets 100 ALPH on "Up", the total bet amount is 1500 ALPH, with 900 ALPH for "Up", and the final outcome is correct, then the payout calculation would be as follows:

$$\frac{{100 \times (1500 - (1500*(100/10000))}}{{900}}$$

  • amountBid: 100 ALPH

  • rewardAmount : 1500 - 1% = 1485 ALPH

  • rewardBaseCalAmount : 900 ALPH

So the person would win a total of 165 ALPH, which represents a net gain of 65 ALPH over his initial stake.


This article concludes part two on ALPH.bet. The Round contract is used to calculate winnings, contains ALPH played and redistributes them.

The third and final part will focus on the Punter contract, which is used to store the information of a person taking part in a round, and on TxScript, which is a special feature of Alephium.

To reread the first article in the series: **https://blog.notrustverify.ch/**1-alephiums-prediction-game

About No Trust Verify

We are deeply experimentation-oriented, with a particular focus on technologies related to privacy, self-sovereignty and trustless organizational structures. Our proactive commitment is reflected in a number of projects. In particular, we are contributors to Nym, supporting the existing infrastructure, developing applications and spreading essential knowledge related to privacy issues. In addition, we are active contributors to the Alephium community, offering decisive components such as full nodes, useful and exploratory services, Bridge guardian while participating in the development of dApps.

We are the developers of pastenym.ch, alph.bet and walph.io

We look forward to hearing from you:

| Website | Twitter | Telegram | Linkedin | Matrix |