Skip to content

Commit

Permalink
feat(be/rpc): contract integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jagnani73 committed Sep 3, 2024
1 parent c56ca3b commit 14d5a42
Show file tree
Hide file tree
Showing 22 changed files with 895 additions and 1,164 deletions.
1 change: 1 addition & 0 deletions packages/backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ SUPABASE_PROJECT_ID
SUPABASE_ACCESS_TOKEN
SUPABASE_SERVICE_ROLE_KEY
JWT_SECRET_KEY
WALLET_PRIVATE_KEY
8 changes: 7 additions & 1 deletion packages/backend/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { playedGamesRouter } from "./microservices/played-games/played-games.rou
import { playerGameHistoryRouter } from "./microservices/player-game-history/player-game-history.routes";
import { playersRouter } from "./microservices/players/players.routes";
import { seasonsRouter } from "./microservices/seasons/seasons.routes";
import { RedisService, SupabaseService, WSService } from "./services";
import {
EthersService,
RedisService,
SupabaseService,
WSService,
} from "./services";
import cors from "cors";
import "dotenv/config";
import type { Express, NextFunction, Request, Response } from "express";
Expand Down Expand Up @@ -69,6 +74,7 @@ app.use(
SupabaseService.init(),
RedisService.init(),
WSService.init(server),
EthersService.init(),
]);
const env: string = process.env.NODE_ENV || "development";
if (env !== "test") {
Expand Down
144 changes: 0 additions & 144 deletions packages/backend/microservices/arcade-contract/arcade-game.service.ts

This file was deleted.

12 changes: 12 additions & 0 deletions packages/backend/microservices/game-tiers/game-tier.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { type MappedGameTier } from "../../utils/types/mappers.types";
import { type PartialYupSchema } from "../../utils/types/shared.types";
import * as yup from "yup";

export const getTierParams = yup
.object()
.shape<PartialYupSchema<MappedGameTier>>({
tier_id: yup.string().trim().required(),
})
.strict()
.noUnknown()
.required();
27 changes: 26 additions & 1 deletion packages/backend/microservices/game-tiers/game-tiers.routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { fetchGameTiers } from "./game-tiers.service";
import { validateQuery } from "../../middlewares/rest";
import { MappedGameTier } from "../../utils/types/mappers.types";
import { getTierParams } from "./game-tier.schema";
import { fetchGameTier, fetchGameTiers } from "./game-tiers.service";
import type { NextFunction, Request, Response } from "express";
import { Router } from "express";

Expand All @@ -20,4 +23,26 @@ const handleGetTiers = async (
}
};

const handleGetTier = async (
req: Request,
res: Response,
next: NextFunction,
) => {
try {
const { tier_id } = req.params as unknown as MappedGameTier;
const data = await fetchGameTier(tier_id);
return res.json({
success: true,
data,
});
} catch (error) {
next(error);
}
};

gameTiersRouter.get("/", handleGetTiers);
gameTiersRouter.get(
"/:tier_id",
validateQuery("params", getTierParams),
handleGetTier,
);
16 changes: 16 additions & 0 deletions packages/backend/microservices/game-tiers/game-tiers.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SupabaseService } from "../../services";
import { MappedGameTier } from "../../utils/types/mappers.types";

export const fetchGameTiers = async () => {
const { data, error } = await SupabaseService.getSupabase()
Expand All @@ -12,3 +13,18 @@ export const fetchGameTiers = async () => {

return data;
};

export const fetchGameTier = async (tier_id: MappedGameTier["tier_id"]) => {
const { data, error } = await SupabaseService.getSupabase()
.from("game_tiers")
.select()
.eq("tier_id", tier_id)
.single();

if (error) {
console.error(error);
throw error;
}

return data;
};
28 changes: 18 additions & 10 deletions packages/backend/microservices/played-games/played-games.routes.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import { validateQuery } from "../../middlewares/rest";
import { MappedPlayer } from "../../utils/types/mappers.types";
import { getUserGamesParams } from "./played-games.schema";
import { fetchAllUserGames } from "./played-games.service";
import { MappedPlayedGame } from "../../utils/types/mappers.types";
import {
postPlayerAttestationBody,
postPlayerAttestationParams,
} from "./played-games.schema";
import { setAttestationAndWithdrawRewards } from "./played-games.service";
import type { NextFunction, Request, Response } from "express";
import { Router } from "express";

export const playedGamesRouter = Router();

const handleAllUserGames = async (
const handleGameAttestation = async (
req: Request,
res: Response,
next: NextFunction,
) => {
try {
const { player_id } = req.params as MappedPlayer;
const data = await fetchAllUserGames(player_id);
const { played_game_id } = req.params as unknown as MappedPlayedGame;
const { attestation_hash } = req.body as unknown as MappedPlayedGame;
const data = await setAttestationAndWithdrawRewards(
played_game_id,
attestation_hash,
);
return res.json({
success: true,
data,
Expand All @@ -24,8 +31,9 @@ const handleAllUserGames = async (
}
};

playedGamesRouter.get(
"/:player_id",
validateQuery("params", getUserGamesParams),
handleAllUserGames,
playedGamesRouter.post(
"/:played_game_id/attestation",
validateQuery("params", postPlayerAttestationParams),
validateQuery("body", postPlayerAttestationBody),
handleGameAttestation,
);
17 changes: 13 additions & 4 deletions packages/backend/microservices/played-games/played-games.schema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { type MappedPlayer } from "../../utils/types/mappers.types";
import { type MappedPlayedGame } from "../../utils/types/mappers.types";
import { type PartialYupSchema } from "../../utils/types/shared.types";
import * as yup from "yup";

export const getUserGamesParams = yup
export const postPlayerAttestationParams = yup
.object()
.shape<PartialYupSchema<MappedPlayer>>({
player_id: yup.string().trim().required(),
.shape<PartialYupSchema<MappedPlayedGame>>({
played_game_id: yup.string().trim().required(),
})
.strict()
.noUnknown()
.required();

export const postPlayerAttestationBody = yup
.object()
.shape<PartialYupSchema<MappedPlayedGame>>({
attestation_hash: yup.string().trim().required(),
})
.strict()
.noUnknown()
Expand Down
Loading

0 comments on commit 14d5a42

Please sign in to comment.