Skip to content

Commit

Permalink
TicTacToe game using ERC7715 permissions (#676)
Browse files Browse the repository at this point in the history
* initial version

* refactor WalletConnectCosignerUtils

* updated UserOpBuilderServiceUtils

* Update .gitignore

* add deps

* add provider component

* updated configs

* refactor code to handle wait for tx on client side

* remove api/game/board route

* move const to ConstantsUtil

* chores:run prettier

* handle app privatekey

* remove NEXT_PUBLIC_APPLICATION_PRIVATE_KEY

* chores: increase timeout duration

* chores:run prettier
  • Loading branch information
KannuSingh authored Aug 31, 2024
1 parent efa0ddd commit a027cf0
Show file tree
Hide file tree
Showing 51 changed files with 9,987 additions and 0 deletions.
3 changes: 3 additions & 0 deletions advanced/dapps/tic-tac-toe/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
37 changes: 37 additions & 0 deletions advanced/dapps/tic-tac-toe/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local
.env

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
36 changes: 36 additions & 0 deletions advanced/dapps/tic-tac-toe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
17 changes: 17 additions & 0 deletions advanced/dapps/tic-tac-toe/components.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "default",
"rsc": true,
"tsx": true,
"tailwind": {
"config": "tailwind.config.ts",
"css": "src/app/globals.css",
"baseColor": "slate",
"cssVariables": true,
"prefix": ""
},
"aliases": {
"components": "@/components",
"utils": "@/lib/utils"
}
}
13 changes: 13 additions & 0 deletions advanced/dapps/tic-tac-toe/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";
import pluginReact from "eslint-plugin-react";


export default [
{files: ["**/*.{js,mjs,cjs,ts,jsx,tsx}"]},
{languageOptions: { globals: globals.browser }},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
pluginReact.configs.flat.recommended,
];
4 changes: 4 additions & 0 deletions advanced/dapps/tic-tac-toe/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default nextConfig;
67 changes: 67 additions & 0 deletions advanced/dapps/tic-tac-toe/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"name": "tic-tac-toe",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,css,md}\""
},
"dependencies": {
"@radix-ui/colors": "^3.0.0",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-progress": "^1.1.0",
"@radix-ui/react-select": "^2.1.1",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-tabs": "^1.1.0",
"@radix-ui/react-toast": "^1.2.1",
"@radix-ui/themes": "^3.1.3",
"@shadcn/ui": "^0.0.4",
"@tanstack/react-query": "5.24.8",
"@wagmi/connectors": "5.1.2",
"@wagmi/core": "2.13.1",
"@walletconnect/ethereum-provider": "2.14.0",
"@walletconnect/utils": "2.14.0",
"@web3modal/wagmi": "5.1.0",
"@web3modal/wallet": "5.1.0",
"autoprefixer": "10.4.18",
"axios": "1.7.2",
"bs58": "^6.0.0",
"class-variance-authority": "^0.7.0",
"clsx": "2.1.0",
"lucide-react": "^0.427.0",
"next": "14.2.5",
"next-themes": "^0.3.0",
"permissionless": "0.1.31",
"pino-pretty": "^11.2.2",
"react": "^18",
"react-dom": "^18",
"react-toastify": "^10.0.5",
"sonner": "1.4.3",
"tailwind-merge": "2.2.1",
"tailwindcss-animate": "^1.0.7",
"vaul": "0.9.0",
"viem": "2.17.8",
"wagmi": "2.12.2"
},
"devDependencies": {
"@eslint/js": "^9.9.0",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^9.9.0",
"eslint-config-next": "14.2.5",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-react": "^7.35.0",
"globals": "^15.9.0",
"postcss": "^8",
"prettier": "^3.3.3",
"tailwindcss": "^3.4.1",
"typescript": "^5",
"typescript-eslint": "^8.2.0"
}
}
8 changes: 8 additions & 0 deletions advanced/dapps/tic-tac-toe/postcss.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('postcss-load-config').Config} */
const config = {
plugins: {
tailwindcss: {},
},
};

export default config;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions advanced/dapps/tic-tac-toe/public/next.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions advanced/dapps/tic-tac-toe/public/vercel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions advanced/dapps/tic-tac-toe/src/app/api/game/create/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { createGame } from "@/utils/TicTacToeUtils";
import { NextResponse } from "next/server";
import { isAddress } from "viem";
import { GrantPermissionsReturnType } from "viem/experimental";

export async function POST(request: Request) {
try {
const APPLICATION_PRIVATE_KEY = process.env.APPLICATION_PRIVATE_KEY;
if (!APPLICATION_PRIVATE_KEY) {
return NextResponse.json(
{ message: "Missing required environment variables" },
{ status: 400 },
);
}
const { permissions, pci } = await request.json();

if (!permissions) {
return NextResponse.json(
{ message: "No permissions provided" },
{ status: 400 },
);
}
if (!pci) {
return NextResponse.json({ message: "No PCI provided" }, { status: 400 });
}

const playerOAddress = (permissions as GrantPermissionsReturnType)
.signerData?.submitToAddress;
if (!playerOAddress || !isAddress(playerOAddress))
throw new Error("Invalid playerO address");

const txHash = await createGame(APPLICATION_PRIVATE_KEY, playerOAddress);

return NextResponse.json({ transactionHash: txHash });
} catch (e) {
console.error("Error:", e);
return NextResponse.json(
{ message: "An error occurred", error: (e as Error).message },
{ status: 500 },
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
findRandomEmptyPosition,
getBoardState,
makeComputerMove,
} from "@/utils/TicTacToeUtils";
import { NextResponse } from "next/server";

export async function POST(request: Request) {
try {
const { gameId } = await request.json();

if (typeof gameId !== "string") {
return NextResponse.json({ message: "Invalid game ID" }, { status: 400 });
}
const board = await getBoardState(gameId);
const computerPosition = findRandomEmptyPosition(board);
if (computerPosition === null) {
return NextResponse.json({ message: "Game is over" }, { status: 400 });
}
const APPLICATION_PRIVATE_KEY = process.env.APPLICATION_PRIVATE_KEY;
if (!APPLICATION_PRIVATE_KEY) {
return NextResponse.json(
{ message: "Missing required environment variables" },
{ status: 400 },
);
}
const systemMoveTxHash = await makeComputerMove(
APPLICATION_PRIVATE_KEY,
gameId,
computerPosition,
);

return NextResponse.json({ transactionHash: systemMoveTxHash });
} catch (e) {
console.error("Error:", e);
const errorMessage = (e as Error)?.message || "Error making move";
return NextResponse.json(
{ message: "Error making move", error: errorMessage },
{ status: 500 },
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { makeUserMove } from "@/utils/TicTacToeUtils";
import { NextResponse } from "next/server";

export async function POST(request: Request) {
try {
const { gameId, position, permissions, pci } = await request.json();

if (typeof gameId !== "string") {
return NextResponse.json({ message: "Invalid game ID" }, { status: 400 });
}
if (typeof position !== "number" || position < 0 || position > 8) {
return NextResponse.json(
{ message: "Invalid position" },
{ status: 400 },
);
}
const APPLICATION_PRIVATE_KEY = process.env.APPLICATION_PRIVATE_KEY;
if (!APPLICATION_PRIVATE_KEY) {
return NextResponse.json(
{ message: "Missing required environment variables" },
{ status: 400 },
);
}

const userTxHash = await makeUserMove(
APPLICATION_PRIVATE_KEY,
gameId,
position,
permissions,
pci,
);
return NextResponse.json({ userOpIdentifier: userTxHash });
} catch (e) {
console.error("Error:", e);
const errorMessage = (e as Error)?.message || "Error making move";
return NextResponse.json(
{ message: "Error making move", error: errorMessage },
{ status: 500 },
);
}
}
19 changes: 19 additions & 0 deletions advanced/dapps/tic-tac-toe/src/app/api/signer/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { NextResponse } from "next/server";
import { privateKeyToAccount } from "viem/accounts";

export function GET() {
try {
const APPLICATION_PRIVATE_KEY = process.env
.APPLICATION_PRIVATE_KEY as `0x${string}`;
const account = privateKeyToAccount(APPLICATION_PRIVATE_KEY);

return NextResponse.json({ key: account.publicKey });
} catch (e) {
console.warn("Error getting signer:", e);

return NextResponse.json(
{ message: "Error getting signer", error: (e as Error).message },
{ status: 500 },
);
}
}
Binary file added advanced/dapps/tic-tac-toe/src/app/favicon.ico
Binary file not shown.
Loading

0 comments on commit a027cf0

Please sign in to comment.