5

How, for example, might you implement a simple "coin-flip" game as an on-chain Solana program?

The concept is that a player deposits a coin or token, and the onchain program generates a "win" or a "lose" with a 50/50 chance.

1
  • the coin flip example doesn't particularly elaborate the question. it would be better to discuss the properties of "randomness" that you desire
    – trent.sol
    Commented Aug 2, 2022 at 2:00

5 Answers 5

3

Coin flip would be really difficult to implement as a smart contract because it traditionally requires a source of entropy in order to create a random(-ish) result. The problem is that introducing randomness means the program will no longer be deterministic and that makes it impossible for validators to replicate the results of the program and agree about the outcome. This is why the rand crate is not available to on-chain programs.

Alternatively, you could take a non-traditional approach and implement a deterministic variant of coin-flip. However, since this result is calculable in a predictable manner it becomes very easy to cheat! Anyone can run the code themselves and only submit a coin to play when they know what the next result will be.

Another option would be to use an oracle or an off-chain resource that interacts with the on-chain contract after coins have been submitted.

Once you determine how to implement the actual "flip" mechanism, Paul wrote a very nice tutorial that illustrates how a smart contract might hold and later distribute tokens to a user.

2

Switchboard just recently carried out a Verifiable Random Function (VRF) workshop in their Discord, here are some links to help you.

Video

Implementation Walkthrough

vrf-flip game

vrf-flip open source repo

1

Switchboard has an oracle that you can use to achieve randomness

https://docs.switchboard.xyz/randomness

They have an example using anchor

https://github.com/switchboard-xyz/vrf-cpi-example

1
  • 1
    Thanks @vicyyn. docs.switchboard.xyz/randomness explains that Switchboard's solution is pseudorandom (deterministic, not truly random). Since producing the next pseudorandom result requires access to a private key, it needs to be done off chain and then published to an account so it can be verified & used. It looks like reading a value just checks the latest data from the on-chain account where it was already published. I don't think this is generated on-chain.
    – Nate
    Commented Jul 31, 2022 at 14:11
0

As you pointed out yourself, the answer is "No, you cannot achieve randomness on-chain with Solana".

All validators need to get to the same new state after each transaction, so Solana is functioning in a deterministic manner.

Oracles are a possibility, but then you have to trust some off-chain generator to be truly random.

1
  • Yeah thanks for chiming in @AllBlooming. I posted this because the original time this was asked got changed into something completely different after others found the answer helpful. There used to be a note on this Q to that effect but it was suggested to remove it for clarity.
    – Nate
    Commented Jul 31, 2022 at 1:11
0

Most of the coin-flip games you see use some form of off-chain random source (Oracle is just someone pushing into the chain something generated outside).

There are ways to generate pseudo random numbers purely in solana.

But the main issue is that you don't know their distribution and if you want to base a game on them, it can greatly affect it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.