<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: milaabl</title>
    <description>The latest articles on DEV Community by milaabl (@milaabl).</description>
    <link>https://dev.to/milaabl</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F721402%2Fdf0eaf38-7c38-4e18-b85a-326664e943ce.jpg</url>
      <title>DEV Community: milaabl</title>
      <link>https://dev.to/milaabl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/milaabl"/>
    <language>en</language>
    <item>
      <title>On-Chain dApp Game with Solidity, React, Typescript &amp; Wagmi/Viem (Commit-reveal keccak256, contract factory patterns)</title>
      <dc:creator>milaabl</dc:creator>
      <pubDate>Fri, 11 Aug 2023 08:39:42 +0000</pubDate>
      <link>https://dev.to/milaabl/on-chain-dapp-game-with-solidity-react-typescript-wagmiviem-commit-reveal-keccak256-contract-factory-patterns-1c11</link>
      <guid>https://dev.to/milaabl/on-chain-dapp-game-with-solidity-react-typescript-wagmiviem-commit-reveal-keccak256-contract-factory-patterns-1c11</guid>
      <description>&lt;p&gt;This dApp is an on-chain rock paper scissors lizard spock game built with React, Typescript, Wagmi/Viem, Sepolia &amp;amp; integrated with Solidity smart contracts (contract factory, commit/reveal patterns).&lt;/p&gt;

&lt;p&gt;We’ll utilize the best practices, follow the Solidity security patterns &amp;amp; the reusable components approach to React and Typescript development.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The final version of the dApp we’ll be building in this tutorial looks like this: &lt;a href="https://rpsls-medium-tutorial.vercel.app" rel="noopener noreferrer"&gt;https://rpsls-medium-tutorial.vercel.app&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can try it out yourself before going deeper in the implementation details, because it’ll help you with better understanding the user flows &amp;amp; logic of the game.&lt;/p&gt;

&lt;p&gt;I’ll also show you how to implement common Solidity patterns such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;commit-reveal;&lt;/li&gt;
&lt;li&gt;contract factory;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We’ll focus on security, and also adhere to old-fashioned Solidity practices such as the checks -&amp;gt; effects -&amp;gt; interactions pattern.&lt;/p&gt;

&lt;p&gt;I’ll start off by scaffolding a new React &amp;amp; Typescript project:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;yarn create react-app rpsls-game --template typescript&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next we need to install the necessary NPM dependencies:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd rpsls-game&lt;br&gt;
yarn add @mui/material @mui/styled-engine-sc styled-components @fontsource/red-hat-display viem wagmi react-router-dom react-timer-hook&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now let’s build a Solidity contract that we’ll be using as main game juror &amp;amp; that will lock the rewards for the players:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: Unlicensed

pragma solidity ^0.8.12;

contract RPSLS {
    enum Move {
        Null, Rock, Paper, Scissors, Lizard, Spock
    }
    address public player1;
    address public player2;

    bytes32 move1Hash;

    Move public move1;
    Move public move2;

    uint256 public stake;

    uint256 public TIMEOUT_IN_MS = 5 minutes;
    uint256 public lastTimePlayed;

    modifier onlyOwner() {
        require(msg.sender == player1);
        _;
    }

    event Player2Played(address indexed _player2, Move indexed _move2);
    event GameSolved(address indexed winner);
    event GameTied();
    event GameTimedOut(address indexed fallbackWinner);

    constructor(bytes32 _move1Hash, address _player1, address _player2) payable {
        stake = msg.value;
        move1Hash = _move1Hash;
        player1 = _player1;
        player2 = _player2;
        lastTimePlayed = block.timestamp;
    }

    function play (Move _move2) external payable {
        require(msg.value == stake, "Insufficient funds for move. Make sure you stake the required amount of ETH for the transaction to succeed.");
        require(msg.sender == player2);
        require(move2 == Move.Null, "Move already played");

        move2 = _move2;
        lastTimePlayed = block.timestamp;

        emit Player2Played(player2, _move2);
    }

    function solve(Move _move1, string calldata _salt) onlyOwner external {
        require(player2 != address(0), "Player 2 should make his move in order to solve the round.");
        require(move2 != Move.Null, "Player 2 should move first.");
        require(keccak256(abi.encodePacked(_move1, _salt)) == move1Hash, "The exposed value is not the hashed one!");
        require(stake &amp;gt; 0, "Winner is already determined.");

        move1 = _move1;

        uint256 _stake = stake;

        if (win(move1, move2)) {
            stake = 0;
            (bool _success) = payable(player1).send(2 * _stake);
            if (!_success) {
                stake = _stake;
            }
            else {
                emit GameSolved(player1);
            }
        }
        else if (win(move1, move2)) {
            stake = 0;
            (bool _success) = payable(player2).send(2 * _stake);
            if (!_success) {
                stake = _stake;
            }
            else {
                emit GameSolved(player2);
            }
        }
        else {
            stake = 0;
            (bool _success1) = payable(player2).send(_stake);
            (bool _success2) = payable(player1).send(_stake);
            if (!(_success1 || _success2)) {
                stake = _stake;
            }
            else {
                emit GameTied();
            }
        }
    }

    function win(Move _move1, Move _move2) public pure returns (bool) {
        if (_move1 == _move2)
            return false; // They played the same so no winner.
        else if (_move1 == Move.Null)
            return false; // They did not play.
        else if (uint(_move1) % 2 == uint(_move2) % 2) 
            return (_move1 &amp;lt; _move2);
        else
            return (_move1 &amp;gt; _move2);
    }

    function claimTimeout() external {
        require(msg.sender == player1 || msg.sender == player2, "You're not a player of this game.");

        require(block.timestamp &amp;gt; lastTimePlayed + TIMEOUT_IN_MS, "Time has not run out yet.");

        uint256 _stake = stake;
        stake = 0;

        if (player2 == address(0)) {
            (bool _success) = payable(player1).send(_stake);
            if (!_success) {
                stake = _stake;
            }
            else {
                emit GameTimedOut(player1);
            }
        }
        else if (move2 != Move.Null) {
         (bool _success) = payable(player2).send(_stake * 2);
            if (!_success) {
                stake = _stake;
            }
            else {
                emit GameTimedOut(player2);
            }   
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make sure the game cannot be cheated by a front-running attack or block explorer attack, I’m using the commit-reveal startegy pattern.&lt;/p&gt;

&lt;p&gt;The first player’s move will remain hashed until the second player makes his move &amp;amp; the player 1 solves the game by revealing his move. There’s a good &lt;a href="https://www.oreilly.com/library/view/mastering-blockchain-programming/9781839218262/3cca669b-430c-4cc7-9019-4562ecad87b5.xhtml" rel="noopener noreferrer"&gt;article&lt;/a&gt; about that pattern from the O’Reilly Team. The algoritgm uses the keccak256 hasher implementation.&lt;/p&gt;

&lt;p&gt;Now take a look at our contract’s code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Did you notice what’s missing?
&lt;/h2&gt;

&lt;p&gt;Currently the main game contract can only be deployed with a method like viem’s publicClient.deployContract().&lt;/p&gt;

&lt;p&gt;However, the address of the deployed contract will not be stored anywhere, and the only way the user can retrieve the deployed contract’s address after further leaving the page is by exloring the transactions tab of his Metamask wallet.&lt;/p&gt;

&lt;p&gt;It’s definitely not user-friendly.&lt;/p&gt;

&lt;p&gt;Let’s write an additional contract that will act as a factory producing &amp;amp; storing new game session contracts, each attached to the players’ addresses.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: Unlicensed

pragma solidity ^0.8.12;
import "./RPSLS.sol"

contract RPSLSFactory {
    RPSLS[] private gameSessions;
    mapping (address =&amp;gt; RPSLS[]) private userGameSessions;

    event NewGameSession(address indexed gameSession);
    function createGameSession(
        bytes32 _move1Hash,
        address _player2
    ) external payable {
        RPSLS gameSession = (new RPSLS){value: msg.value}(
            _move1Hash,
            msg.sender,
            _player2
        );
        gameSessions.push(gameSession);
        userGameSessions[msg.sender].push(gameSession);
        userGameSessions[_player2].push(gameSession);

        emit NewGameSession(address(gameSession));
    }
    function getGameSessions()
        external
        view
        returns (RPSLS[] memory _gameSessions)
    {
        return userGameSessions[msg.sender];
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ I’ll be using the Sepolia 🐬testnet for the further development &amp;amp; contract deployment.&lt;/p&gt;

&lt;h2&gt;
  
  
  👉 OK, now when we have implemented the necessary Solidity code, we can continue with our React app.
&lt;/h2&gt;

&lt;p&gt;❗Make sure you save the deployed address of the RPSLSFactory.sol contract &amp;amp; add it to your .env file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// .env

REACT_APP_PUBLIC_RPSLS_FACTORY_ADDRESS=&amp;lt;your factory's deployed address&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import './App.css';
import { WagmiConfig, configureChains, createConfig } from 'wagmi'
import { sepolia } from 'wagmi/chains'
import { publicProvider } from 'wagmi/providers/public'
import { InjectedConnector } from 'wagmi/connectors/injected'

const { chains, publicClient } = configureChains(
  [sepolia],
  [publicProvider()]
)

const connector = new InjectedConnector({
  chains,
})

const config = createConfig({
  publicClient,
  connectors: [connector],
  autoConnect: true,
});

function App() {
  return (
    &amp;lt;WagmiConfig config={config}&amp;gt;

    &amp;lt;/WagmiConfig&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The basic config for your &lt;code&gt;App.tsx&lt;/code&gt; file should look like this 👆.&lt;/p&gt;

&lt;p&gt;I figured out that using the &lt;code&gt;publicProvider&lt;/code&gt; with a testnet might sometimes be a &lt;a href="https://github.com/wagmi-dev/viem/issues/923" rel="noopener noreferrer"&gt;culprit&lt;/a&gt; when fetching pending transaction details, so I switched to using the &lt;code&gt;alchemyProvider&lt;/code&gt; &amp;amp; separated the wagmi config to a new file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// wagmi.ts ~ App.tsx

import { configureChains, createConfig } from 'wagmi'
import { sepolia } from 'wagmi/chains'
import { alchemyProvider } from 'wagmi/providers/alchemy'
import { MetaMaskConnector } from 'wagmi/connectors/metaMask'

const { chains, publicClient, webSocketPublicClient } = configureChains(
  [sepolia],
  [alchemyProvider({
    apiKey: process.env.REACT_APP_PUBLIC_ALCHEMY_API_KEY
  })]
)

const connector = new MetaMaskConnector({
  chains,
})

export const wagmiConfig = createConfig({
  publicClient,
  webSocketPublicClient,
  connectors: [connector]
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 Let’s create a &lt;code&gt;contracts.ts&lt;/code&gt; file in the src/ directory. It’ll be used to store the address of the factory contract, as well as its ABI — so that we connect the contract the reusable way, so we don’t reference the &lt;code&gt;process.env.REACT_APP_PUBLIC_RPSLS_FACTORY_ADDRESS&lt;/code&gt; variable each time, as this project is using Typescript, we’ll also need to use some type-casting like &lt;code&gt;as Address&lt;/code&gt;, where &lt;code&gt;Address&lt;/code&gt; is a type provided by Wagmi.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// contracts.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Address&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;wagmi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;contracts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;REACT_APP_PUBLIC_RPSLS_FACTORY_ADDRESS&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;abi&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
          &lt;span class="p"&gt;...&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="na"&gt;rpslsGame&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;abi&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[...]&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;💎 I’m using &lt;code&gt;@mui/material&lt;/code&gt; &amp;amp; &lt;code&gt;styled-components&lt;/code&gt; for my project, you can use any components library you wish. For the purposes of this tutorial, I’ll mainly omit the stylization code so that it doesn’t mess with the Web3 integration part.&lt;/p&gt;

&lt;h2&gt;
  
  
  Web3 Front-end integration
&lt;/h2&gt;

&lt;p&gt;We’ll need the &lt;code&gt;useAccount&lt;/code&gt;, &lt;code&gt;useSwitchNetwork&lt;/code&gt;, &lt;code&gt;useConnect&lt;/code&gt;, &lt;code&gt;useNetwork&lt;/code&gt;, &lt;code&gt;useDisconnect&lt;/code&gt;, &lt;code&gt;useContractWrite&lt;/code&gt;, &lt;code&gt;useContractRead&lt;/code&gt;, &lt;code&gt;useContractReads&lt;/code&gt; &amp;amp; &lt;code&gt;useWaitForTransaction&lt;/code&gt; hooks provided by the &lt;code&gt;wagmi-dev/wagmi&lt;/code&gt; package.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxmni1knlw9yds3lilp8h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxmni1knlw9yds3lilp8h.png" alt="@mui/material, styled-components, React, Typescript basic layout"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftyosuctjnrgt02tcgvwh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftyosuctjnrgt02tcgvwh.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fugk46lp5b3gk5bwxwtql.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fugk46lp5b3gk5bwxwtql.png" alt="Network switch component &amp;amp; indicator"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Let’s first build the wallet connection functionality &amp;amp; UI like the above 👆👆👆. The header will have:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;a connect button&lt;/li&gt;
&lt;li&gt;a loading indicator&lt;/li&gt;
&lt;li&gt;a connected account label.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The best practice is to also have a &lt;code&gt;&amp;lt;SwitchNetwork /&amp;gt;&lt;/code&gt; component that will offer the user the ability to switch his current network to one of the dApp’s supported networks.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Header.tsx&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Header.tsx

import React from "react";
import { useAccount, useConnect, useDisconnect } from "wagmi";
import CancelIcon from '@mui/icons-material/Cancel';
import * as S from "./Header.styles";
import logoIcon from 'assets/icons/logo.svg';
import AccountCircleIcon from '@mui/icons-material/AccountCircle';
import SwitchNetwork from './SwitchNetwork';
import { IconButton } from "@mui/material";

function Header () {
    const { isLoading : isConnectingWallet, connectors, connect } = useConnect();
    const { address } = useAccount();
    const { disconnect } = useDisconnect();
    return &amp;lt;&amp;gt;
        &amp;lt;S.Header&amp;gt;
            &amp;lt;S.Logo alt="Logo" src={logoIcon} /&amp;gt;
            { !address ? &amp;lt;S.Button loading={isConnectingWallet} onClick={() =&amp;gt; connect({
                connector: connectors[0]
            })} startIcon={&amp;lt;AccountCircleIcon /&amp;gt;}&amp;gt;Connect wallet&amp;lt;/S.Button&amp;gt; : &amp;lt;S.AccountAddress&amp;gt;
                &amp;lt;IconButton size="small" onClick={() =&amp;gt; disconnect()}&amp;gt;
                    &amp;lt;CancelIcon color="error" fontSize="inherit" /&amp;gt;
                &amp;lt;/IconButton&amp;gt;
                &amp;lt;span&amp;gt;{address}&amp;lt;/span&amp;gt;&amp;lt;/S.AccountAddress&amp;gt; }
        &amp;lt;/S.Header&amp;gt;
        &amp;lt;SwitchNetwork /&amp;gt;
    &amp;lt;/&amp;gt;
};

export default Header;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;SwitchNetwork.tsx&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SwitchNetwork.tsx

import React from "react";
import { useSwitchNetwork, useNetwork, Chain } from "wagmi";
import * as S from './SwitchNetwork.styles';

function SwitchNetwork () {
    const { switchNetwork, chains } = useSwitchNetwork();
    const { chain } = useNetwork();
    return &amp;lt;S.Container&amp;gt;
        {chains &amp;amp;&amp;amp; switchNetwork &amp;amp;&amp;amp; !chains.find((supportedChain : Chain) =&amp;gt; supportedChain.id === chain?.id) ? &amp;lt;S.SwitchButton onClick={
            () =&amp;gt; switchNetwork(chains[0]?.id)
        }&amp;gt;Switch to {chains[0]?.name}&amp;lt;/S.SwitchButton&amp;gt; : &amp;lt;&amp;gt;&amp;lt;/&amp;gt;}
    &amp;lt;/S.Container&amp;gt;
}

export default SwitchNetwork;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;OK. The user can now connect his Metamask wallet, disconnect it if he wants, switch the network of his Metamask account if the network he’s using is not on the dApp’s supported networks list.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Let’s now start the work on our front-end &lt;code&gt;RPSLSFactory.sol&lt;/code&gt; integration by building the components for the &lt;code&gt;new-game&lt;/code&gt; page.&lt;/strong&gt; The user will be able to set a game’s bid, select his first move, hash it &amp;amp; create a new instance of game by inviting the 2nd player.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsixzpbqa11d4fys8octq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsixzpbqa11d4fys8octq.png" alt="Create a new rock paper scissars lizard spock game session React + Typescript + Web3 (Wagmi/Viem) page 🎮"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The request to write a contract looks like the following in Wagmi:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const {
    error,
    isLoading: isNewGameSessionLoading,
    write: createNewGameSession,
    data: createNewGameSessionData,
  } = useContractWrite({
    ...contracts.factory,
    functionName: "createGameSession",
    value: parseEther(bid),
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that I’m not passing the “args” param in the useContractWrite hook because I’ll pass it later when making a call. As the move hash is generated asynchronously, I found that it’s a more convenient way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🦉 It’s a good practice to let the user paste the opponent’s address clicking on the button.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s also add the move icons so the user can selected his move:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0s3v2gg9wvmq6qimtv7g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0s3v2gg9wvmq6qimtv7g.png" alt="Decentralized rock paper scissors lizard spock game in React, Typescript, Web3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔐 Now I’ll show you how to securely encrypt the user’s move. As our Solidity contract is using the &lt;code&gt;keccak256&lt;/code&gt; function, we’ll be using an equivalent function provided by &lt;code&gt;viem&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;🦉 I used to reference the &lt;code&gt;ethers&lt;/code&gt;’ &lt;code&gt;solidityKeccak256&lt;/code&gt; function, but viem has its own alternative. Let’s find out how to generate a secret key &amp;amp; hash it.&lt;/p&gt;

&lt;p&gt;That’s basically the main commitment encryption code we’ll need in order to call the factory contract’s function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randomUUID&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;_move1Hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;keccak256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nf"&gt;encodePacked&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;uint8&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;selectedMove&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;_salt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;createNewGameSession&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;_move1Hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;player2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here I’m using the &lt;strong&gt;Browser Subtle Crypto&lt;/strong&gt; API &amp;amp; the &lt;code&gt;keccak256&lt;/code&gt; from the &lt;code&gt;viem&lt;/code&gt; library.&lt;/p&gt;

&lt;p&gt;But first let’s make sure that the user signs our actions &amp;amp; verifies his commitment. We’ll need to use the &lt;code&gt;useSignMessage&lt;/code&gt; hook to verify that the user is OK with using a randomly generated salt to initiate a new game.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;signMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;signData&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useSignMessage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="p"&gt;...&lt;/span&gt;

&lt;span class="nf"&gt;signMessage&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`Your game move is: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;selectedMove&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;. Your game salt is: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;_salt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;. Keep it private! It'll automatically be stored in your local storage.`&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final code of the “Create new game” page looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// pages/new-game/index.tsx

import React, {
  ChangeEvent,
  useContext,
  useEffect,
  useRef,
  useState,
} from "react";
import {
  useContractWrite,
  useAccount,
  useSignMessage,
} from "wagmi";
import { useLocalStorage } from "hooks/useLocalStorage";
import * as S from "./styles";
import { moves, moveIcons, Move } from "moves";
import { contracts } from "contracts";
import { Hash, encodePacked, keccak256, parseEther } from "viem";
import { AppContext } from "context/AppContext";
import TransactionHistory from "components/TransactionHistory/TransactionHistory";
import { validateAddress } from "utils/validators";

function NewGamePage() {
  const { address } = useAccount();

  const [player2, setPlayer2] = useState&amp;lt;string | undefined&amp;gt;();
  const [bid, setBid] = useState&amp;lt;string&amp;gt;("0");

  const [, setSalt] = useLocalStorage("salt");
  const [, setMove1] = useLocalStorage("move");

  const [selectedMove, setSelectedMove] = useState&amp;lt;Move&amp;gt;(Move.Null);

  const [isMoveCommitted, setIsMoveCommitted] = useState&amp;lt;boolean&amp;gt;(false);

  const {
    error,
    isLoading: isNewGameSessionLoading,
    write: createNewGameSession,
    data: createNewGameSessionData,
  } = useContractWrite({
    ...contracts.factory,
    functionName: "createGameSession",
    value: parseEther(bid),
  });

  const _salt = useRef&amp;lt;string | undefined&amp;gt;();
  const _move1Hash = useRef&amp;lt;string | undefined&amp;gt;();

  const { signMessage, data: signData } = useSignMessage();

  useEffect(() =&amp;gt; {
    if (createNewGameSessionData?.hash &amp;amp;&amp;amp; !error) {
      setIsMoveCommitted(true);
    }
  }, [createNewGameSessionData?.hash]);

  const [gameSessionHash, setGameSessionHash] = useState&amp;lt;Hash&amp;gt;();

  useEffect(() =&amp;gt; {
    if (!createNewGameSessionData &amp;amp;&amp;amp; signData) createNewGameSession({
      args: [_move1Hash.current, player2],
    });
  }, [signData]);

  useEffect(() =&amp;gt; {
    if (gameSessionHash &amp;amp;&amp;amp; _salt.current) {
      setSalt(_salt.current, `salt-${gameSessionHash}`);
      setMove1(String(selectedMove), `move-${gameSessionHash}`);
    }
  }, [
    gameSessionHash
  ]);

  const { setErrorMessage, setIsLoading } = useContext(AppContext);

  useEffect(() =&amp;gt; {
    error?.message &amp;amp;&amp;amp; setErrorMessage?.(error.message);
  }, [error?.message]);

  useEffect(() =&amp;gt; {
    setIsLoading?.(isNewGameSessionLoading);
  }, [isNewGameSessionLoading]);

  return !isMoveCommitted ? (
    &amp;lt;S.Container&amp;gt;
      &amp;lt;S.MovesContainer&amp;gt;
        {moves.map((move: Move) =&amp;gt; (
          &amp;lt;S.MoveItem
            className={selectedMove === move ? "selected" : ""}
            onClick={() =&amp;gt; setSelectedMove(move)}
          &amp;gt;
            &amp;lt;img src={moveIcons[move - 1]} alt={`Move №${move}`} /&amp;gt;
          &amp;lt;/S.MoveItem&amp;gt;
        ))}
      &amp;lt;/S.MovesContainer&amp;gt;
      &amp;lt;S.Heading&amp;gt;Create a new game session 🎮&amp;lt;/S.Heading&amp;gt;
      &amp;lt;S.Form&amp;gt;
        &amp;lt;S.Input&amp;gt;
          &amp;lt;S.TextField
            inputProps={{
              maxLength: 42,
            }}
            InputLabelProps={{ shrink: true }}
            label="Address of player2's wallet"
            helperText="Invite your opponent 🪖"
            value={player2}
            onChange={({ target: { value } }: ChangeEvent&amp;lt;HTMLInputElement&amp;gt;) =&amp;gt;
              setPlayer2(value)
            }
          /&amp;gt;
          &amp;lt;S.PasteWalletAddressButton
            label="Paste"
            onClick={() =&amp;gt; {
              navigator.clipboard.readText().then((value) =&amp;gt; setPlayer2(value));
            }}
          /&amp;gt;
        &amp;lt;/S.Input&amp;gt;
        &amp;lt;S.Input&amp;gt;
          &amp;lt;S.TextField
            inputProps={{
              step: "0.01",
            }}
            type="number"
            label="Bid (in ETH)"
            helperText="Please enter the bid 🎲 for the game"
            value={bid}
            onChange={({ target: { value } }: ChangeEvent&amp;lt;HTMLInputElement&amp;gt;) =&amp;gt;
              setBid(value)
            }
          /&amp;gt;
        &amp;lt;/S.Input&amp;gt;
      &amp;lt;/S.Form&amp;gt;
      &amp;lt;S.SubmitButton
        disabled={
          !(
            address &amp;amp;&amp;amp;
            selectedMove !== Move.Null &amp;amp;&amp;amp;
            Number(bid) &amp;gt; 0 &amp;amp;&amp;amp;
            validateAddress(player2)
          )
        }
        onClick={() =&amp;gt; {
          const salt = crypto.randomUUID();

          _move1Hash.current = keccak256(
            encodePacked(["uint8", "string"], [selectedMove, salt]),
          );

          _salt.current = salt;

          signMessage({ message: `Your game move is: ${selectedMove}. Your game salt is: ${_salt.current}. Keep it private! It'll automatically be stored in your local storage.` });
        }}
      &amp;gt;
        Submit session ✅
      &amp;lt;/S.SubmitButton&amp;gt;
    &amp;lt;/S.Container&amp;gt;
  ) : createNewGameSessionData?.hash ? (
    &amp;lt;TransactionHistory
      setGameSessionHash={setGameSessionHash}
      transactionHash={createNewGameSessionData?.hash}
    /&amp;gt;
  ) : (
    &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
  );
}

export default NewGamePage;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⚠️ We are storing the salt &amp;amp; player’s move in the localStorage. Currently the auth stuff is a bit complicated in the Web3, but the use of localStorage in case of our game helps us further solve the game by revealing the first player’s commitment, so that he doesn’t need to store it himself.&lt;/p&gt;

&lt;p&gt;Optionally, you can incorporate Metamask’s recent feature that allows storing passwords in the local self-custodial wallet the safe way: &lt;a href="https://github.com/ritave/snap-passwordManager" rel="noopener noreferrer"&gt;https://github.com/ritave/snap-passwordManager&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I thought of implementing it in this tutorial application, but not every user has the development version of the Metamask extension installed, so I might leave it for the next article. 😉&lt;/p&gt;

&lt;p&gt;You can checkout the Metamask’s Snap edition: &lt;a href="https://metamask.io/news/developers/invisible-keys-snap-multi-cloud-private-key-storage" rel="noopener noreferrer"&gt;https://metamask.io/news/developers/invisible-keys-snap-multi-cloud-private-key-storage&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;❗Notice that as the dApp alllows multiple game session at a time, we have to store the salt &amp;amp; move values attached to more specific keys like “salt” -&amp;gt; “salt-0x3800429c6cFB510602eb9545D133b046B9d19535”, “move” -&amp;gt; “move-0x3800429c6cFB510602eb9545D133b046B9d19535”. Otherwise, it’ll be hard to figure out which salt &amp;amp; move are related to a particular game session.&lt;/p&gt;

&lt;p&gt;💁‍♀️ I also created a context with the React Context API in order to have the loading spinner functionality in the app, for better UX.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft66ewqm2ttopscdztr1m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft66ewqm2ttopscdztr1m.png" alt="Wagmi useContractWrite, useWaitForTransaction, usePrepareContractWrite loader"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s also add a transaction history component.&lt;/p&gt;

&lt;p&gt;It’ll wait for the transaction to complete &amp;amp; show the details.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// components/TransactionHistory/TransactionHistory.tsx

import React, { Dispatch, useContext, useEffect, useState } from "react";
import * as S from "./TransactionHistory.styles";
import { Address, useWaitForTransaction } from "wagmi";
import { Hash, decodeAbiParameters } from "viem";
import { useTheme } from "@mui/material";
import successTickIcon from "assets/icons/success-tick.svg";
import { AppContext } from "context/AppContext";
import { useNavigate } from "react-router-dom";

interface TransactionHistoryProps {
  transactionHash: Hash;
  setGameSessionHash: Dispatch&amp;lt;Hash&amp;gt;;
}

function TransactionHistory({
  setGameSessionHash: _setGameSessionHash,
  transactionHash: _transactionHash,
}: TransactionHistoryProps) {
  const theme = useTheme();

  const [transactionHash, setTransactionHash] =
    useState&amp;lt;Hash&amp;gt;(_transactionHash);

  const { setIsLoading, setErrorMessage } = useContext(AppContext);

  const {
    error,
    data: transactionData,
    isLoading: isTransactionDataLoading,
  } = useWaitForTransaction({
    hash: transactionHash,
    enabled: !!transactionHash,
    onSuccess: (data) =&amp;gt; {
      console.log(data);
    },
    onReplaced: (replacement) =&amp;gt; {
      console.log({ replacement });
      if (replacement.reason === "cancelled") {
        setErrorMessage?.(`Transaction ${transactionHash} was cancelled`);
        return;
      } else {
        setTransactionHash(replacement.transactionReceipt.transactionHash);
      }
    },
    onError: (err) =&amp;gt; setErrorMessage?.(err.message),
    confirmations: 1,
  });

  const [gameSessionHash, setGameSessionHash] = useState&amp;lt;Hash&amp;gt;();

  useEffect(() =&amp;gt; {
    if (!transactionData?.logs[0]?.data) return;

    const _gameSessionHash = String(
      decodeAbiParameters(
        [
          {
            type: "address",
            name: "gameSession",
          },
        ],
        transactionData.logs[0].topics[1] as Address,
      ),
    ) as Hash;

    setGameSessionHash(_gameSessionHash);

    _setGameSessionHash(_gameSessionHash);
  }, [transactionData]);

  const navigate = useNavigate();

  useEffect(() =&amp;gt; {
    setIsLoading?.(isTransactionDataLoading);
  }, [isTransactionDataLoading]);

  return isTransactionDataLoading ? (
    &amp;lt;&amp;gt;
      &amp;lt;S.Container&amp;gt;
        &amp;lt;S.Details&amp;gt;
          &amp;lt;S.Heading&amp;gt;Transaction pending&amp;lt;/S.Heading&amp;gt;
          &amp;lt;S.DetailsItem&amp;gt;
            &amp;lt;strong&amp;gt;Transaction address: &amp;lt;/strong&amp;gt;
            {transactionHash}
          &amp;lt;/S.DetailsItem&amp;gt;
          &amp;lt;S.DetailsItem&amp;gt;
            &amp;lt;strong&amp;gt;Status: &amp;lt;/strong&amp;gt;
            Pending
          &amp;lt;/S.DetailsItem&amp;gt;
          &amp;lt;S.LoadingIconComponent variant="indeterminate" /&amp;gt;
        &amp;lt;/S.Details&amp;gt;
        &amp;lt;S.CutOffBorder /&amp;gt;
      &amp;lt;/S.Container&amp;gt;
    &amp;lt;/&amp;gt;
  ) : transactionData?.status === "success" &amp;amp;&amp;amp; !error ? (
    &amp;lt;&amp;gt;
      &amp;lt;S.Container&amp;gt;
        &amp;lt;S.Details&amp;gt;
          &amp;lt;S.SuccessIndicator
            height={theme.spacing(4)}
            src={successTickIcon}
            alt="Success"
          /&amp;gt;
          &amp;lt;S.Heading&amp;gt;Transaction details&amp;lt;/S.Heading&amp;gt;
          &amp;lt;S.DetailsItem&amp;gt;
            &amp;lt;strong&amp;gt;Transaction address: &amp;lt;/strong&amp;gt;
            {transactionData?.transactionHash}
          &amp;lt;/S.DetailsItem&amp;gt;
          &amp;lt;S.DetailsItem&amp;gt;
            &amp;lt;strong&amp;gt;Gas used: &amp;lt;/strong&amp;gt;
            {String(transactionData.gasUsed)}
          &amp;lt;/S.DetailsItem&amp;gt;
          &amp;lt;S.DetailsItem&amp;gt;
            &amp;lt;strong&amp;gt;Gas price: &amp;lt;/strong&amp;gt;
            {String(transactionData.effectiveGasPrice)} WEI
          &amp;lt;/S.DetailsItem&amp;gt;
          {gameSessionHash ? (
            &amp;lt;S.DetailsItem&amp;gt;
              &amp;lt;strong&amp;gt;Game session hash: &amp;lt;/strong&amp;gt;
              {gameSessionHash}
            &amp;lt;/S.DetailsItem&amp;gt;
          ) : (
            &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
          )}
          &amp;lt;S.DetailsItem&amp;gt;
            &amp;lt;strong&amp;gt;Status: &amp;lt;/strong&amp;gt;
            {transactionData.status.charAt(0).toUpperCase()}
            {transactionData.status.slice(1)}
          &amp;lt;/S.DetailsItem&amp;gt;
        &amp;lt;/S.Details&amp;gt;
        &amp;lt;S.CutOffBorder /&amp;gt;
      &amp;lt;/S.Container&amp;gt;
      {gameSessionHash ? (
        &amp;lt;S.GameButtonsContainer&amp;gt;
          &amp;lt;S.InviteOpponentButton
            onClick={() =&amp;gt; {
              navigator.clipboard.writeText(
                `${window.location.hostname}/game-session/${gameSessionHash}`,
              );
            }}
          &amp;gt;
            Copy opponent's invitation link
          &amp;lt;/S.InviteOpponentButton&amp;gt;
          &amp;lt;S.GoToSolveGameButton
            onClick={() =&amp;gt; navigate(`/game-session/${gameSessionHash}`)}
          &amp;gt;
            Go to game session
          &amp;lt;/S.GoToSolveGameButton&amp;gt;
        &amp;lt;/S.GameButtonsContainer&amp;gt;
      ) : (
        &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
      )}
    &amp;lt;/&amp;gt;
  ) : (
    &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
  );
}

export default TransactionHistory;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onReplaced: (replacement) =&amp;gt; {
      console.log({ replacement });
      if (replacement.reason === "cancelled") {
        setErrorMessage?.(`Transaction ${transactionHash} was cancelled`);
        return;
      } else {
        setTransactionHash(replacement.transactionReceipt.transactionHash);
      }
    },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;👆 This particular lines allow us to seamlessly replace the transaction hash &amp;amp; continue fetching the data if the transaction gets replaced with a higher fee to speed up.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bonus!&lt;/strong&gt; Some utils🧹 worth mentioning:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// hooks/useLocalStorage.ts

import React, { useEffect, useSyncExternalStore } from "react";

export const useLocalStorage = (
  key: string,
): [string | null, (value: string, key?: string) =&amp;gt; void] =&amp;gt; {
  const subscribe = (listener: () =&amp;gt; void) =&amp;gt; {
    window.addEventListener("storage", listener);
    return () =&amp;gt; {
      window.removeEventListener("storage", listener);
    };
  };

  const getSnapShot = (): string | null =&amp;gt; {
    return localStorage.getItem(key);
  };

  const value = useSyncExternalStore(subscribe, getSnapShot);

  const setValue = (newValue: string, _key?: string) =&amp;gt; {
    localStorage.setItem(_key || key, newValue);
  };

  useEffect(() =&amp;gt; {
    if (value) {
      setValue(value, key);
    }
  }, [key]);

  return [value, setValue];
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// utils/validators.ts

import { isAddress } from "viem";

export const validateAddress = (address : string | undefined) =&amp;gt; address &amp;amp;&amp;amp; isAddress(address);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;There’re a lot of libraries that provide the &lt;code&gt;useLocalStorage&lt;/code&gt; hook, but they don’t have support for updating the key of the storage item. As our salt &amp;amp; move storage secrets are attached to a gameSession address for further decryption, we need the key to be a dynamically updating variable.&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The second page will be the index welcome page where all of the games available to the user will be displayed, either a game where the user is the 1st player or has an invite to join the game as the 2nd player.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It’s a pretty simple page except for one nuance.&lt;/p&gt;

&lt;p&gt;We have to make sure that if the game is gets further solved, it won’t be displayed in the available game sessions list, as it’s not active anymore.&lt;/p&gt;

&lt;p&gt;I used the &lt;code&gt;useContractRead&lt;/code&gt; hook to fetch the user’s available game sessions from the &lt;strong&gt;factory&lt;/strong&gt; 👩‍🏭contract.&lt;/p&gt;

&lt;p&gt;Then I mapped through the returned array to fetch the values of stakes of each game session. I used the &lt;code&gt;useContractReads&lt;/code&gt; hook to read from a list of contracts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { AppContext } from "context/AppContext";
import * as S from "./styles";
import { contracts } from "contracts";
import React, { useContext, useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { Abi, Hash, MulticallResult } from "viem";
import { useWalletClient, useContractRead, useContractReads } from "wagmi";

function WelcomePage() {
  const { data: walletClient } = useWalletClient();
  const { isLoading, data: availableGameSessions } = useContractRead({
    ...contracts.factory,
    functionName: "getGameSessions",
    account: walletClient?.account,
    watch: true,
    select: (data: any) =&amp;gt; {
      return data as Array&amp;lt;Hash&amp;gt;;
    },
  });

  const [activeGameSessions, setActiveSessions] = useState&amp;lt;Array&amp;lt;Hash&amp;gt;&amp;gt;([]);

  const { isLoading: isGameStakesLoading } = useContractReads({
    contracts: (availableGameSessions as Array&amp;lt;Hash&amp;gt;)?.map(
      (gameSession: Hash) =&amp;gt; {
        return {
          address: gameSession,
          functionName: "stake",
          abi: contracts.rpslsGame.abi as Abi,
        };
      },
    ),
    onSuccess: (data) =&amp;gt; {
      const contractStakes = (
        data as unknown as Array&amp;lt;MulticallResult&amp;lt;bigint&amp;gt;&amp;gt;
      ).map((result: MulticallResult&amp;lt;bigint&amp;gt;) =&amp;gt; result.result);
      contractStakes.forEach(
        (contractStake: bigint | undefined, index: number) =&amp;gt; {
          if (
            Number(contractStake as unknown as bigint) &amp;gt; 0 &amp;amp;&amp;amp;
            availableGameSessions?.[index]
          ) {
            setActiveSessions((prev: Array&amp;lt;Hash&amp;gt;) =&amp;gt;
              Array.from(new Set([...prev, availableGameSessions[index]])),
            );
          }
        },
      );
    },
    watch: true,
  });

  const navigate = useNavigate();

  const { setIsLoading } = useContext(AppContext);

  useEffect(() =&amp;gt; {
    setIsLoading?.(isLoading || isGameStakesLoading);
  }, [isLoading, isGameStakesLoading]);

  return activeGameSessions &amp;amp;&amp;amp;
    (activeGameSessions as Array&amp;lt;Hash&amp;gt;)?.length &amp;gt;= 1 ? (
    &amp;lt;S.Container&amp;gt;
      {(activeGameSessions as Array&amp;lt;Hash&amp;gt;).map((hash: Hash) =&amp;gt; (
        &amp;lt;S.LinkToSession onClick={() =&amp;gt; navigate(`/game-session/${hash}`)}&amp;gt;
          &amp;lt;span&amp;gt;{hash}&amp;lt;/span&amp;gt;
          &amp;lt;S.ArrowRightButton /&amp;gt;
        &amp;lt;/S.LinkToSession&amp;gt;
      ))}
      &amp;lt;S.NewGameSessionLink onClick={() =&amp;gt; navigate("/new-game")}&amp;gt;
        Propose new game session
      &amp;lt;/S.NewGameSessionLink&amp;gt;
    &amp;lt;/S.Container&amp;gt;
  ) : (
    &amp;lt;S.NoAvailableGameSessions&amp;gt;
      &amp;lt;S.NoAvailableGameSessionsLabel&amp;gt;
        There're no available active game sessions for you yet. Propose a new
        game session or get invited to join one!
      &amp;lt;/S.NoAvailableGameSessionsLabel&amp;gt;
      &amp;lt;S.NewGameSessionLink onClick={() =&amp;gt; navigate("/new-game")}&amp;gt;
        Propose new game session
      &amp;lt;/S.NewGameSessionLink&amp;gt;
    &amp;lt;/S.NoAvailableGameSessions&amp;gt;
  );
}

export default WelcomePage;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The last page is the game session page. It’ll be accessible to the players of the same game session, for the 2nd user to join the game &amp;amp; make his move, the first player can then reveal his commitment, as well there’s timeout functionality incorporated. It’s a shareable link, thanks to the &lt;code&gt;react-router-dom&lt;/code&gt;'s route template path we can reference the unique gameSession hash from the &lt;code&gt;/game-session/:gameSession&lt;/code&gt; location parameter.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;The game session page is probably the most complex page of this app.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let me show you the full code &amp;amp; then I’ll explain it step-by-step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// pages/game-session/index.tsx

import { contracts } from "contracts";
import * as S from "./styles";
import hiddenMoveIcon from "assets/icons/moves/hidden-move.gif";
import React, { useContext, useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import {
  Address,
  useAccount,
  useContractRead,
  useContractWrite,
  useWaitForTransaction,
} from "wagmi";
import { Move, moveIcons, moves } from "moves";
import { formatEther } from "viem";
import { useTimer } from "react-timer-hook";
import { AppContext } from "context/AppContext";
import { useLocalStorage } from "hooks/useLocalStorage";
import { Typography } from "@mui/material";

interface ContractData {
  abi: any;
  address: Address;
}

const formatTime = (time: number): string =&amp;gt;
  time &amp;lt; 10 ? `0${time}` : `${time}`;

function GameSessionPage() {
  const { hash } = useParams();

  const rpslsGameContract: ContractData = {
    abi: contracts.rpslsGame.abi,
    address: hash as Address,
  };

  const { data: move2 } = useContractRead({
    ...rpslsGameContract,
    functionName: "move2",
    watch: true,
  });

  const { data: stake } = useContractRead({
    ...rpslsGameContract,
    functionName: "stake",
    watch: true,
  });

  const { data: player1 } = useContractRead({
    ...rpslsGameContract,
    functionName: "player1",
    watch: true,
  });

  const { data: lastTimePlayed } = useContractRead({
    ...rpslsGameContract,
    functionName: "lastTimePlayed",
    watch: true,
  });

  const { data: TIMEOUT_IN_MS } = useContractRead({
    ...rpslsGameContract,
    functionName: "TIMEOUT_IN_MS",
  });

  const { data: player2 } = useContractRead({
    ...rpslsGameContract,
    functionName: "player2",
    watch: true,
  });

  const { setIsLoading } = useContext(AppContext);

  const [isEligibleForTimeout, setIsEligibleForTimeout] =
    useState&amp;lt;boolean&amp;gt;(false);

  const {
    isLoading: claimTimeoutLoading,
    write: claimTimeout,
    data: claimTimeoutTransactionData,
  } = useContractWrite({
    ...rpslsGameContract,
    functionName: "claimTimeout",
  });

  const { address } = useAccount();

  const [successMessage, setSuccessMessage] = useState&amp;lt;string | undefined&amp;gt;();

  const { isLoading: claimTimeoutTransactionLoading } = useWaitForTransaction({
    hash: claimTimeoutTransactionData?.hash,
    onSuccess: () =&amp;gt; setSuccessMessage("Timeout claimed successfully"),
  });

  useEffect(() =&amp;gt; {
    setIsLoading?.(claimTimeoutLoading || claimTimeoutTransactionLoading);
  }, [claimTimeoutLoading, claimTimeoutTransactionLoading]);

  const { seconds, minutes, restart } = useTimer({
    expiryTimestamp: new Date(
      ((Number(lastTimePlayed || 0) as unknown as number) +
        (Number(TIMEOUT_IN_MS || 0) as unknown as number)) *
        1000,
    ),
    autoStart: true,
    onExpire: () =&amp;gt; setIsEligibleForTimeout(true),
  });

  const [selectedMove, setSelectedMove] = useState&amp;lt;Move&amp;gt;(Move.Null);

  const {
    write: submitMove,
    isLoading: isSubmitMoveLoading,
    data: submitMoveData,
  } = useContractWrite({
    ...rpslsGameContract,
    functionName: "play",
    args: [selectedMove],
    value: stake as unknown as bigint,
  });

  const { isLoading: isSubmitMoveTransactionLoading } = useWaitForTransaction({
    hash: submitMoveData?.hash,
    onSuccess: () =&amp;gt; setSuccessMessage("Move submitted successfully!"),
  });

  const [salt] = useLocalStorage(`salt-${hash}`);
  const [move1] = useLocalStorage(`move-${hash}`);

  const {
    write: solveGame,
    isLoading: isSolveGameLoading,
    data: solveGameData,
  } = useContractWrite({
    ...rpslsGameContract,
    functionName: "solve",
    args: [Number(move1), salt],
  });

  const { isLoading: isSolveGameTransactionLoading } = useWaitForTransaction({
    hash: solveGameData?.hash,
    onSuccess: () =&amp;gt;
      setSuccessMessage("Game solved successfully. See the winner! 🎊🎉"),
  });

  const { data: isPlayer1Winner } = useContractRead({
    ...rpslsGameContract,
    functionName: "win",
    args: [move1, move2],
    enabled: Number(move1) !== Move.Null &amp;amp;&amp;amp; Number(move2) !== Move.Null,
  });

  const { data: isPlayer2Winner } = useContractRead({
    ...rpslsGameContract,
    functionName: "win",
    args: [move2, move1],
    enabled: Number(move1) !== Move.Null &amp;amp;&amp;amp; Number(move2) !== Move.Null,
  });

  useEffect(() =&amp;gt; {
    if (!TIMEOUT_IN_MS || !lastTimePlayed) return;

    restart(
      new Date(
        ((Number(lastTimePlayed || 0) as unknown as number) +
          (Number(TIMEOUT_IN_MS || 0) as unknown as number)) *
          1000,
      ),
    );
  }, [TIMEOUT_IN_MS, lastTimePlayed]);

  return player1 &amp;amp;&amp;amp; (stake || !move2) ? (
    &amp;lt;S.Container&amp;gt;
      {player2 === address ? (
        &amp;lt;S.MovesContainer&amp;gt;
          {moves.map((move: Move) =&amp;gt; (
            &amp;lt;S.MoveItem
              className={move === selectedMove ? "selected" : ""}
              onClick={() =&amp;gt; setSelectedMove(move)}
            &amp;gt;
              &amp;lt;img src={moveIcons[move - 1]} alt={`Move №${move}`} /&amp;gt;
            &amp;lt;/S.MoveItem&amp;gt;
          ))}
        &amp;lt;/S.MovesContainer&amp;gt;
      ) : (
        &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
      )}
      &amp;lt;S.PlayerContainer&amp;gt;
        &amp;lt;S.DetailsItem&amp;gt;
          &amp;lt;strong&amp;gt;Player 1: &amp;lt;/strong&amp;gt;
          &amp;lt;span&amp;gt;{player1 as unknown as Address}&amp;lt;/span&amp;gt;
        &amp;lt;/S.DetailsItem&amp;gt;
        &amp;lt;S.DetailsItem&amp;gt;
          &amp;lt;strong&amp;gt;Player 2: &amp;lt;/strong&amp;gt;
          &amp;lt;span&amp;gt;{player2 as unknown as Address}&amp;lt;/span&amp;gt;
        &amp;lt;/S.DetailsItem&amp;gt;
        &amp;lt;S.DetailsItem&amp;gt;
          &amp;lt;strong&amp;gt;Stake details: &amp;lt;/strong&amp;gt;
          &amp;lt;span&amp;gt;{formatEther(stake as unknown as bigint)} ETH&amp;lt;/span&amp;gt;
        &amp;lt;/S.DetailsItem&amp;gt;
        &amp;lt;S.DetailsItem&amp;gt;
          &amp;lt;strong&amp;gt;Time until timeout: &amp;lt;/strong&amp;gt;
          &amp;lt;span&amp;gt;
            {formatTime(minutes)}::{formatTime(seconds)}
          &amp;lt;/span&amp;gt;
        &amp;lt;/S.DetailsItem&amp;gt;
        &amp;lt;S.DetailsItem&amp;gt;
          &amp;lt;strong&amp;gt;Player 1's move: &amp;lt;/strong&amp;gt;
          &amp;lt;S.HiddenMoveImage src={hiddenMoveIcon} alt="?" /&amp;gt;
        &amp;lt;/S.DetailsItem&amp;gt;
        &amp;lt;S.DetailsItem&amp;gt;
          &amp;lt;strong&amp;gt;Player 2's move: &amp;lt;/strong&amp;gt;
          {player2 === address &amp;amp;&amp;amp; !move2 ? (
            &amp;lt;S.SubmitMoveButton
              disabled={selectedMove === Move.Null}
              onClick={() =&amp;gt; submitMove?.()}
              loading={isSubmitMoveLoading || isSubmitMoveTransactionLoading}
            &amp;gt;
              Submit move
            &amp;lt;/S.SubmitMoveButton&amp;gt;
          ) : (move2 as unknown as Move) === Move.Null ? (
            &amp;lt;span&amp;gt;Move not submitted&amp;lt;/span&amp;gt;
          ) : (
            &amp;lt;S.MoveImage
              alt="Player 2's move"
              src={moveIcons[(move2 as unknown as Move) - 1]}
            /&amp;gt;
          )}
        &amp;lt;/S.DetailsItem&amp;gt;
      &amp;lt;/S.PlayerContainer&amp;gt;
      {((player2 as unknown as Address) === address &amp;amp;&amp;amp;
        move2 &amp;amp;&amp;amp;
        isEligibleForTimeout) ||
      (isEligibleForTimeout &amp;amp;&amp;amp;
        (player1 as unknown as Address) === address &amp;amp;&amp;amp;
        !move2) ? (
        &amp;lt;S.TimeoutButton
          loading={claimTimeoutLoading || claimTimeoutTransactionLoading}
          onClick={() =&amp;gt; claimTimeout?.()}
        &amp;gt;
          Claim timeout
        &amp;lt;/S.TimeoutButton&amp;gt;
      ) : (
        &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
      )}
      {(player1 as unknown as Address) === address &amp;amp;&amp;amp; move2 ? (
        &amp;lt;S.SolveButton
          loading={isSolveGameTransactionLoading || isSolveGameLoading}
          onClick={() =&amp;gt; solveGame?.()}
        &amp;gt;
          Solve game
        &amp;lt;/S.SolveButton&amp;gt;
      ) : (
        &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
      )}
      {successMessage ? &amp;lt;S.SuccessBox&amp;gt;{successMessage}&amp;lt;/S.SuccessBox&amp;gt; : &amp;lt;&amp;gt;&amp;lt;/&amp;gt;}
    &amp;lt;/S.Container&amp;gt;
  ) : player1 &amp;amp;&amp;amp; move2 ? (
    &amp;lt;S.GameSolvedContainer&amp;gt;
      &amp;lt;Typography variant="h3"&amp;gt;Game solved successfully!&amp;lt;/Typography&amp;gt;
      &amp;lt;S.GameSolvedTitle variant="h4"&amp;gt;
        &amp;lt;S.HighlightContainer&amp;gt;The winner is:&amp;lt;/S.HighlightContainer&amp;gt;
      &amp;lt;/S.GameSolvedTitle&amp;gt;
      &amp;lt;Typography variant="h6"&amp;gt;
        {(isPlayer1Winner as unknown as boolean) === false ? (
          &amp;lt;strong&amp;gt;Player2: {player2 as unknown as Address}&amp;lt;/strong&amp;gt;
        ) : (isPlayer2Winner as unknown as boolean) === false ? (
          &amp;lt;strong&amp;gt;Player 1: {player1 as unknown as Address}&amp;lt;/strong&amp;gt;
        ) : (
          &amp;lt;strong&amp;gt;Everyone winned. Game tied! 🪢&amp;lt;/strong&amp;gt;
        )}
      &amp;lt;/Typography&amp;gt;
    &amp;lt;/S.GameSolvedContainer&amp;gt;
  ) : (
    &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
  );
}

export default GameSessionPage;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the first player (the creator of the game) visits the game session page, he’ll see whether the second player submitted his move already or not yet, as well as how much time is left until the game is eligible for timeout.&lt;/p&gt;

&lt;p&gt;When the second player visits the game session page, he’ll be able to submit his move &amp;amp; wait for the first player to reveal his commitment &amp;amp; solve the game.&lt;/p&gt;

&lt;p&gt;Similarly, he can claim a timeout if the 1st player went unresponsive.&lt;/p&gt;

&lt;p&gt;I’m not using &lt;code&gt;useContractEvent&lt;/code&gt; for this page because it only watches the events in real-time, but I want to display the correct state even after the game is finished, if the user wants to visit the page later.&lt;/p&gt;

&lt;p&gt;The final dApp from the first player’s view came out to this:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/U3CrcnErQfw"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F76mxpjiilfyh1s3xk3pv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F76mxpjiilfyh1s3xk3pv.png" alt="/ — Welcome page with all active game sessions displayed with useContractRead &amp;amp; useContractReads wagmi viem"&gt;&lt;/a&gt;&lt;br&gt;
The active game sessions page 👆&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7jezcq6kvo4mxydc9pol.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7jezcq6kvo4mxydc9pol.png" alt="Metamask useSignMessage request result | Wagmi, React, Viem, Browser Subtle Crypto API"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F68xq3ivfjsavv93t1f6q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F68xq3ivfjsavv93t1f6q.png" alt="Metamask useContractWrite | Solidity Factory pattern"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The prompts from my Metamask were not recorded on the video due to my video recorder’s security limitations. These looks like 👆👆👆&lt;/p&gt;

&lt;p&gt;⌚ When the time runs out, the 1st player can claim the timeout and get his stake back before the 2nd player can join the game. The game session will get disactivated automatically. 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxh96e0yigoyy34uc22vk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxh96e0yigoyy34uc22vk.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the second player, the game session page allows him to select his move &amp;amp; submit it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgor71zx9s5g9oxrmohob.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgor71zx9s5g9oxrmohob.png" alt="Rock paper scissors lizard spock Solidity &amp;amp; React game: Player2, submit your move!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fert5rx7zqbms1cqdpsou.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fert5rx7zqbms1cqdpsou.png" alt="useContractWrite: “play”() function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The last thing for the player 1 is to solve the game by revealing his commitment:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvj3t9avt68wfieptg60r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvj3t9avt68wfieptg60r.png" alt="Both players submitted their moves of choice"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faxmiw8sgkdso02ld1jx5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faxmiw8sgkdso02ld1jx5.png" alt="Solve game — rock paper scissors lizard spock!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ctmoa0l78diyar89y4b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ctmoa0l78diyar89y4b.png" alt="Wagmi, React &amp;amp; Typescript: Game solved successfully! Contract write (useContractWrite) completed."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The game is finished &amp;amp; the stake was distributed fairly.&lt;/p&gt;

&lt;p&gt;If the user claims a timeout considering there really is no time left for the opponent to make his move, the result is:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7k159q24etqn81xk6zc9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7k159q24etqn81xk6zc9.png" alt="block.timestamp best practices Timeout claimed Solidity claimTimeout() game smart contract | React Context API"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let me know if this tutorial was helpful! © Built with love and a pinch of posture health. 🙂🙆‍♀️&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you have any questions or ideas, make sure to ask those in the comments below. 🗨️&lt;/p&gt;

</description>
      <category>wagmi</category>
      <category>react</category>
      <category>web3</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
