The Flip Smart Contract part 5 : Contract administration

Some of previous storage values should be changed only by us, this is what we will do here. We will also add some admins actions.

Modifying settings the storage

Three storages properties should be changed only by us :

  • maximum_bet

  • maximum_bet_percent

  • minimum_block_bounty

For each of them we create an @onlyOwner endpoint :

@contract
abstract class FlipContract extends ContractBase {

    @onlyOwner
    setMaximumBet(
        tokenIdentifier: TokenIdentifier,
        nonce: ManagedU64,
        amount: BigUint
    ): void {
        this.require(
            amount > BigUint.zero(),
            "amount zero"
        )

        this.maximumBet(tokenIdentifier, nonce).set(amount)
    }

    @onlyOwner
    setMaximumBetPercent(
        tokenIdentifier: TokenIdentifier,
        nonce: ManagedU64,
        percent: ManagedU64
    ): void {
        this.require(
            percent > ManagedU64.zero(),
            "percent zero"
        )

        this.maximumBetPercent(tokenIdentifier, nonce).set(percent)
    }

    @onlyOwner
    setMinimumBlockBounty(
        minimumBlockBounty: ManagedU64
    ): void {
        this.require(
            minimumBlockBounty > ManagedU64.zero(),
            "minimum block bounty zero"
        )

        this.minimumBlockBounty().set(minimumBlockBounty)
    }
    
    //--- previous code ---
}

The @onlyOwner annotation ensure that only contract owner can call an endpoint. This is critical and should not be forgot.

Instead of TypeScript, you should always specify return type in AssemblyScript... even when it's void !

Admin actions

Beside changing settings, the contract's owner need to do the two following actions :

  • increasing the contract's reserve of a token by sending token

  • withdrawing token reserve

Let's code these two endpoints :

@contract
abstract class FlipContract extends ContractBase {

    @onlyOwner
    increaseReserve(): void {
        const payment = this.callValue.singlePayment

        this.require(
            payment.amount > BigUint.zero(),
            "no payment"
        )

        const oldTokenReserve = this.tokenReserve(payment.tokenIdentifier, payment.tokenNonce).get()

        this.tokenReserve(payment.tokenIdentifier, payment.tokenNonce).set(oldTokenReserve + payment.amount)
    }

    @onlyOwner
    withdrawReserve(
        tokenIdentifier: TokenIdentifier,
        nonce: ManagedU64,
        amount: BigUint
    ): void {
        const owner = this.blockchain.caller

        const tokenReserve = this.tokenReserve(tokenIdentifier, nonce).get()

        this.require(
            tokenReserve <= amount,
            "amount too high"
        )
        
        this.tokenReserve(tokenIdentifier, nonce).set(tokenReserve - amount)
        
        this.send.direct(
            owner,
            tokenIdentifier,
            nonce,
            amount
        )
    }
}

Nothing new here, this is similar to previous onlyOwner endpoints.

Next up

Owner can now administrate the contract, next thing to code is the bet logic 🎲

Last updated