Beast Slayers is a simple yet addictive game built using Dojo on Telegram, where players need to spam click to defeat increasingly powerful beasts. The game integrates the cartridge Controller and the Dojo wasm bindings for syncing with the on-chain game state.
- We generate a local Stark key pair for the user and store the private key in Telegrams cloud storage.
- We open the session controller page, passing the user's public key.
- The controller registers the session public key and returns account information.
- We create a controller session account on the client.
- We store the account information in Telegrams cloud storage.
The useAccount
hook provides an easy way to integrate the controller into your Telegram Mini App.
-
Import the hook:
import { useAccount } from "./path/to/AccountProvider";
-
Use the hook in your component:
function MyComponent() { const { accountStorage, sessionSigner, account, openConnectionPage, clearSession, address, username, } = useAccount(); // Use the account information and functions as needed }
-
Available properties and functions:
accountStorage
: Contains user account information (username, address, ownerGuid)sessionSigner
: Contains the session's private and public keysaccount
: The CartridgeSessionAccount instanceopenConnectionPage()
: Function to open the connection page for account setupclearSession()
: Function to clear the current sessionaddress
: The user's account addressusername
: The user's username
-
Ensure your app is wrapped with the AccountProvider:
import { AccountProvider } from "./path/to/AccountProvider"; function App() { return <AccountProvider>{/* Your app components */}</AccountProvider>; }
-
Connecting to the controller
const { openConnectionPage } = useAccount(); openConnectionPage();
-
We create a Torii client in the main App component:
const [client, setClient] = useState<ToriiClient | undefined>(); useEffect(() => { createClient({ toriiUrl: TORII_URL, rpcUrl: RPC_URL, relayUrl: RELAY_URL, worldAddress: WORLD_ADDRESS, }).then(setClient); }, []);
-
We subscribe to our game entities
const entities = await client.getEntities({ limit: 1, offset: 0, clause: { Keys: { keys: ["0xfea4"], // or [address] for warrior models: ["beastslayers-Game"], // or ["beastslayers-Warrior"] pattern_matching: "FixedLen", }, }, }); subscription.current = await client.onEntityUpdated( [{ HashedKeys: Object.keys(entities) }], (_hashedKeys, models) => { // Update local state based on the new data } );
-
We refresh the react state to update the UI
const game = Object.values(entities)[0]["beastslayers-Game"]; updateBeast(game);
- Clone the repo
- Run
pnpm install
- Run
pnpm run dev