For most blockchain gaming usages, we can think of the blockchain as a secure and permanent database.
This database is expensive to write, but relatively cheap to read.
We have seen we can generate proves, wether in OPZK or Zk only implementation of zkSpin. When writing the states to the blockchain, we'll need to provide those proves.
Reading Game State from the Chain
We are using WalletConnect's Web3Modal. The frontend wallet library does not matter. The example here is just trying to show what contracts and functions of the smart contracts are called.
import { decodeBytesToU64Array, GameStateStorageABI, SpinZKGameContractABI,} from"@zkspin/lib";asyncfunctiongetOnchainGameStates():Promise<bigint[]> {conststorageContractAddress= (awaitreadContract(config, { abi:SpinZKGameContractABI.abi, address:GAME_CONTRACT_ADDRESS, functionName:"getStorageContract", args: [], })) as`0x${string}`;constuserAccount=getAccount(config);constresult= (awaitreadContract(config, { abi:GameStateStorageABI.abi, address: storageContractAddress, functionName:"getStates", args: [userAccount.address], })) asstring;// result is in bytes, abi decode it, state is 2 u64 bigint// this is the same number as STATE_SIZE in definition.rsconstdecoded=decodeBytesToU64Array(result,2);return decoded;}