tablet-screen-buttonCh 2: Core App Logic

In this chapter, you'll write the core logic to interact with the ClownBeatdown contract by creating an App class. This class will initialize player-specific wallet clients and contracts, and provide easy-to-use functions like hit, rob, and reset. Estimated time: ~20 minutes

Now, navigate to packages/cli/src/ and create a file called app.ts which will contain the core logic for the CLI:

# Assuming you are in packages/cli/lib
cd ../src
touch app.ts

Import required dependencies

Start by importing all the necessary modules and functions at the top of app.ts:

import {
  type ShieldedContract,
  type ShieldedWalletClient,
  createShieldedWalletClient,
} from "seismic-viem";
import { type Abi, type Address, type Chain, http } from 'viem'
import { privateKeyToAccount } from "viem/accounts";

import { getShieldedContractWithCheck } from "../lib/utils";

Define the app configuration

The AppConfig interface organizes all settings for the Clown Beatdown app, including player info, wallet setup, and contract details. It supports a multiplayer environment, with multiple players having distinct private keys and contract interactions.

Create the App class

The App class manages player-specific wallet clients and contract instances, providing an easy-to-use interface for multiplayer gameplay.

Add initialization logic to App

The init() method sets up individual wallet clients and contract instances for each player, enabling multiplayer interactions. Each player gets their own wallet client and a direct connection to the contract.

Add helper methods to App

These helper methods ensure that the app fetches the correct contract instance for a specific player, supporting multiplayer scenarios.

getPlayerContract:

Implement Contract Interaction Methods

reset

Resets the clown for the next round. The reset restores stamina and picks a new random secret.

hit

A player can hit the clown to reduce its stamina. Each hit is logged for the respective player.

rob

Reveals a secret for a specific player if they contributed to knocking out the clown. This ensures fairness in multiplayer gameplay. Uses signed reads.

Last updated