The Flip Smart Contract part 7 : Make the flip
Flips are initialised, time to make them
Function's signature
private makeFlip(
bountyAddress: ManagedAddress,
flip: Flip
): void {
// Function code goes here
}Function's body
private makeFlip(
bountyAddress: ManagedAddress,
flip: Flip
): void {
const randomNumber = RandomnessSource.nextU8InRange(ManagedU8.fromValue(0), ManagedU8.fromValue(2))
const isWin = randomNumber == ManagedU8.fromValue(1)
this.send
.direct(
bountyAddress,
flip.tokenIdentifier,
flip.tokenNonce,
flip.bounty
)
const profitIfWin = flip.amount * BigUint.fromU64(2)
if (isWin) {
this.send
.direct(
flip.playerAddress,
flip.tokenIdentifier,
flip.tokenNonce,
profitIfWin
)
} else {
let oldTokenReserve = this.tokenReserve(
flip.tokenIdentifier,
flip.tokenNonce
).get()
this.tokenReserve(
flip.tokenIdentifier,
flip.tokenNonce
).set(oldTokenReserve + profitIfWin)
}
this.flipForId(flip.id).clear()
}Next up
PreviousThe Flip Smart Contract part 6 : Bet logicNextThe Flip Smart Contract part 8 : Bounty endpoint
Last updated