Reading L2 Data
Creating the parser
Now that you've defined you grammar in the previous section, you can generate your parse like this:
import { PaimaParser } from '@paima/sdk/concise';
const myParser = new PaimaParser(myGrammar, parserCommands);
function parse(s: string): ParsedSubmittedInput {
try {
const parsed = myParser.start(s);
return { input: parsed.command, ...parsed.args } as any;
} catch (e) {
console.log(e, 'Parsing error');
return { input: 'invalidString' };
}
}
Calling the parser from your STF
Paima works by updating your state machine whenever happens onchain - the most common case being that somebody interacted with your Paima L2 contract. The set of actions your state machine can react to are called the Paima Primitives.
Your parser can then be used in the stf (state transition function) of your application
import type { type STFSubmittedData } from '@paima/sdk/chain-types';
import type Prando from '@paima/sdk/prando';
import type { Pool } from 'pg';
import type { PreExecutionBlockHeader } from '@paima/sdk/chain-types';
export default async function (
inputData: STFSubmittedData,
blockHeader: PreExecutionBlockHeader,
randomnessGenerator: Prando,
dbConn: Pool
): Promise<{ stateTransitions: SQLUpdate[]; events: Events }> {
console.log(inputData, 'parsing input data');
const user = inputData.userAddress.toLowerCase();
// use your parser from the previous section
const parsed = parse(inputData.inputData);
console.log(`Processing input string: ${inputData.inputData}`);
console.log(`Input string parsed as: ${parsed.input}`);
switch (parsed.input) {
case 'createLobby':
// handle this input however you need (but needs to be deterministic)
default:
return { stateTransitions: [], events: [] };
}
}
Reading data from an external source
(Hardhat) Read Paima game input
Paima comes with a Hardhat plugin as part of @paima/evm-contracts for this.
To use it, make sure you have import @paima/evm-contracts/plugin
at the top of your hardhat.config.ts
For reading inputs submitted to your L2 contract directly (instead of implicitly through another Paima Primitive), you can run npx hardhat paima PaimaL2Contract:recentInputs
.
This command helps you see recent inputs to your L2 contract (not the best tool for getting all historic interactions).
(Explorer) See historical game inputs
You can, of course, also see all interactions with your smart contract on any blockchain explorer for the network you deployed to (recall: you can find your L2 contract address in anytime in ./contracts/evm/ignition/deployments/chain-XXX/deployed_addresses.json
)