💻
mx-sdk-as
  • MultiversX WASM AssemblyScript (Proof of concept)
  • The Crowdfunding Smart Contract
    • The Crowdfunding Smart Contract (part 1)
    • The Crowdfunding Smart Contract (part 2)
  • The Flip Smart Contract
    • The Flip Smart Contract : Introduction
    • The Flip Smart Contract part 1 : Setting up the development environment
    • The Flip Smart Contract part 2 : Think about the contract and the game mechanisms
    • The Flip Smart Contract part 3 : Setting up the project
    • The Flip Smart Contract part 4 : Writing the contract storage
    • The Flip Smart Contract part 5 : Contract administration
    • The Flip Smart Contract part 6 : Bet logic
    • The Flip Smart Contract part 7 : Make the flip
    • The Flip Smart Contract part 8 : Bounty endpoint
    • The Flip Smart Contract part 9 : Testing the contract
  • 💡How to
    • Model classes
    • Enums
    • Modules
Powered by GitBook
On this page
  • Modifying settings the storage
  • Admin actions
  • Next up
  1. The Flip Smart Contract

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 🎲

PreviousThe Flip Smart Contract part 4 : Writing the contract storageNextThe Flip Smart Contract part 6 : Bet logic

Last updated 1 year ago