# 2 - Prediction game on Alephium

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](https://blog.notrustverify.ch/1-alephiums-prediction-game).

To test the application: [ALPH.bet](http://ALPH.bet)

A reminder of what [Alephium](https://alephium.org/) 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**](https://github.com/notrustverify/predictalph-contracts/blob/fd94d87b7f7fa7555d70b4bead8a9330713daf31/contracts/round.ral)**.**

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1706620984978/e7df96e1-35a6-4b23-9b38-04599bf47402.avif align="center")

[**Github**](https://github.com/notrustverify/predictalph-contracts/blob/fd94d87b7f7fa7555d70b4bead8a9330713daf31/contracts/predict.ral#L87)

### **States**

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1706621100809/cad88c54-9b42-4b1d-8606-1a12cc7a6ae4.avif align="center")

[**Github**](https://github.com/notrustverify/predictalph-contracts/blob/fd94d87b7f7fa7555d70b4bead8a9330713daf31/contracts/round.ral#L1-L19)

### **Functions**

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1706621154981/477b2f3a-a295-4815-b17b-6b0d060a64b5.avif align="center")

[**Github**](https://github.com/notrustverify/predictalph-contracts/blob/fd94d87b7f7fa7555d70b4bead8a9330713daf31/contracts/round.ral#L31-L46)

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](https://docs.alephium.org/glossary/#gas-price). Therefore, the total contract amount is stored in `totalAmount` to make it more efficient.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1706621350349/daccff33-8f8f-4eb3-97f8-3aeebbaf328b.avif align="center")

[**Github**](https://github.com/notrustverify/predictalph-contracts/blob/fd94d87b7f7fa7555d70b4bead8a9330713daf31/contracts/round.ral#L49-L65)

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".

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1706621486411/2662202d-0157-40e3-bdad-5800c3824dfb.avif align="center")

[**Github**](https://github.com/notrustverify/predictalph-contracts/blob/fd94d87b7f7fa7555d70b4bead8a9330713daf31/contracts/round.ral#L67-L90)

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1706621548202/8d42b42c-875f-47f7-95e4-fa11dfa0c97f.avif align="center")

[**Github**](https://github.com/notrustverify/predictalph-contracts/blob/fd94d87b7f7fa7555d70b4bead8a9330713daf31/contracts/round.ral#L94-L103)

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).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1706621625114/e9ed6a57-925a-42a9-b747-861346b5609c.avif align="center")

[**Github**](https://github.com/notrustverify/predictalph-contracts/blob/fd94d87b7f7fa7555d70b4bead8a9330713daf31/contracts/round.ral#L105-L111)

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1706621908192/d663ba96-867c-4dfc-bcbc-d0592eb94cb1.avif align="center")

[**Github**](https://github.com/notrustverify/predictalph-contracts/blob/fd94d87b7f7fa7555d70b4bead8a9330713daf31/contracts/round.ral#L113-L129)

This article concludes part two on [ALPH.bet](http://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**](https://docs.alephium.org/ralph/getting-started/#txscript), which is a special feature of [**Alephium**](https://alephium.org/).

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

---

%%[footer-english]
