Chapter 2: Writing the core app
In this chapter, you’ll write the core logic to interact with the Walnut contract by creating an App class. This class will initialize player-specific wallet clients and contracts, and provide easy-to-use functions like hit, shake, reset, and look. 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.tsImport required dependencies
Start by importing all the necessary modules and functions at the top of app.ts:
import {
type ShieldedContract,
type ShieldedWalletClient,
createShieldedWalletClient,
getShieldedContract,
} from 'seismic-viem'
import { Abi, Address, 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 Walnut 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 wallet client or contract instance for a specific player, supporting multiplayer scenarios.
getWalletClient :
getPlayerContract :
Implement Contract Interaction Methods
reset
Resets the Walnut for the next round. The reset is player-specific and resets the shell and kernel values.
shake
Allows a player to shake the Walnut, incrementing the kernel. This supports multiplayer scenarios where each player’s shakes impact the Walnut. Uses signed writes.
hit :
A player can hit the Walnut to reduce the shell’s strength. Each hit is logged for the respective player.
look :
Reveals the kernel for a specific player if they contributed to cracking the shell. This ensures fairness in multiplayer gameplay. Uses signed reads.
Last updated

