diff --git a/admin_frontend/demo.env b/admin_frontend/demo.env new file mode 100644 index 0000000..842ee73 --- /dev/null +++ b/admin_frontend/demo.env @@ -0,0 +1,2 @@ +REACT_APP_INDEXER_ENDPOINT=http://localhost:3003 +REACT_APP_SERVER_URL=http://localhost:5050 diff --git a/admin_frontend/src/components/ApiKeys.jsx b/admin_frontend/src/components/ApiKeys.jsx index ce4a8a7..37839fd 100644 --- a/admin_frontend/src/components/ApiKeys.jsx +++ b/admin_frontend/src/components/ApiKeys.jsx @@ -19,6 +19,7 @@ import FormControl from "@mui/material/FormControl"; import InputLabel from "@mui/material/InputLabel"; import Checkbox from "@mui/material/Checkbox"; import Header from "./Header"; +import { ENDPOINTS } from "../constants/common"; const ApiKeysPage = () => { const [keys, setKeys] = useState([]); @@ -45,14 +46,17 @@ const ApiKeysPage = () => { try { setLoading(true); const data = await ( - await fetch("http://localhost:5050/getKeys", { + await fetch(`${process.env.REACT_APP_SERVER_URL}${ENDPOINTS['getKeys']}`, { method: "GET", }) ).json(); setKeys(data); setLoading(false); } catch (err) { - toast.error("Check Backend Service for more info"); + if (err.message.includes("Falied to fetch")) + toast.error("Failed to connect. Please make sure that the backend is running") + else + toast.error(err.message) } }; @@ -87,7 +91,7 @@ const ApiKeysPage = () => { INDEXER_ENDPOINT: process.env.REACT_APP_INDEXER_ENDPOINT ?? "http://localhost:3003", }; - const data = await fetch("http://localhost:5050/saveKey", { + const data = await fetch(`${process.env.REACT_APP_SERVER_URL}${ENDPOINTS['saveKey']}`, { method: "POST", body: JSON.stringify(requestData), }); @@ -102,7 +106,10 @@ const ApiKeysPage = () => { toast.error("Could not save"); } } catch (err) { - toast.error("Check Backend Service for more info"); + if (err.message.includes("Falied to fetch")) + toast.error("Failed to connect. Please make sure that the backend is running") + else + toast.error(err.message) setLoading(false); } }; @@ -111,7 +118,7 @@ const ApiKeysPage = () => { try { setLoading(true); const data = await ( - await fetch("http://localhost:5050/deleteKey", { + await fetch(`${process.env.REACT_APP_SERVER_URL}${ENDPOINTS['deleteKey']}`, { method: "POST", body: JSON.stringify({ API_KEY: key }), }) @@ -124,8 +131,10 @@ const ApiKeysPage = () => { toast.error("Could not save"); } } catch (err) { - console.log("err: ", err); - toast.error("Check Backend Service for more info"); + if (err.message.includes("Falied to fetch")) + toast.error("Failed to connect. Please make sure that the backend is running") + else + toast.error(err.message) setLoading(false); } }; diff --git a/admin_frontend/src/constants/common.js b/admin_frontend/src/constants/common.js new file mode 100644 index 0000000..b5ab577 --- /dev/null +++ b/admin_frontend/src/constants/common.js @@ -0,0 +1,5 @@ +export const ENDPOINTS = { + 'getKeys': '/getKeys', + 'saveKey' : '/saveKey', + 'deleteKey': '/deleteKey', +} \ No newline at end of file diff --git a/backend/indexer/src/index.ts b/backend/indexer/src/index.ts index d3bb0ce..9326111 100644 --- a/backend/indexer/src/index.ts +++ b/backend/indexer/src/index.ts @@ -21,5 +21,6 @@ ponder.on("EtherspotPaymaster:SponsorSuccessful", async ({ event, context }) => }) } catch (err) { // caught err + console.log(err); } }); diff --git a/backend/src/routes/index.ts b/backend/src/routes/index.ts index f0f9c02..427cfb4 100644 --- a/backend/src/routes/index.ts +++ b/backend/src/routes/index.ts @@ -124,7 +124,7 @@ const routes: FastifyPluginAsync = async (server) => { server.log.info("Incomplete body data provided") return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.INVALID_DATA }); } - + if (server.config.SUPPORTED_NETWORKS == '' && !SupportedNetworks) { return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.UNSUPPORTED_NETWORK }); } @@ -144,7 +144,7 @@ const routes: FastifyPluginAsync = async (server) => { if (txnMode) { const signerAddress = await signer.getAddress(); const IndexerData = await getIndexerData(signerAddress, userOp.sender, date.getMonth(), date.getFullYear(), noOfTxns, indexerEndpoint); - if (IndexerData.length >= noOfTxns) return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.QUOTA_EXCEEDED}) + if (IndexerData.length >= noOfTxns) return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.QUOTA_EXCEEDED }) } const validUntil = context.validUntil ? new Date(context.validUntil) : date; const validAfter = context.validAfter ? new Date(context.validAfter) : date; @@ -286,7 +286,6 @@ const routes: FastifyPluginAsync = async (server) => { supportedNetworks = secrets['SUPPORTED_NETWORKS']; } else { const record: any = await getSQLdata(api_key); - console.log(record); if (!record) return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.INVALID_API_KEY }) privateKey = decode(record['PRIVATE_KEY']); supportedNetworks = record['SUPPORTED_NETWORKS']; @@ -347,7 +346,6 @@ const routes: FastifyPluginAsync = async (server) => { supportedNetworks = secrets['SUPPORTED_NETWORKS']; } else { const record: any = await getSQLdata(api_key); - console.log(record); if (!record) return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.INVALID_API_KEY }) privateKey = decode(record['PRIVATE_KEY']); supportedNetworks = record['SUPPORTED_NETWORKS']; @@ -407,7 +405,6 @@ const routes: FastifyPluginAsync = async (server) => { supportedNetworks = secrets['SUPPORTED_NETWORKS']; } else { const record: any = await getSQLdata(api_key); - console.log(record); if (!record) return reply.code(ReturnCode.FAILURE).send({ error: ErrorMessage.INVALID_API_KEY }) privateKey = decode(record['PRIVATE_KEY']); supportedNetworks = record['SUPPORTED_NETWORKS']; @@ -435,13 +432,18 @@ const routes: FastifyPluginAsync = async (server) => { ) async function getSQLdata(apiKey: string) { - const result: any[] = await new Promise((resolve, reject) => { - server.sqlite.db.get("SELECT * FROM api_keys WHERE API_KEY = ?", [apiKey], (err: any, rows: any[]) => { - if (err) reject(err); - resolve(rows); + try { + const result: any[] = await new Promise((resolve, reject) => { + server.sqlite.db.get("SELECT * FROM api_keys WHERE API_KEY = ?", [apiKey], (err: any, rows: any[]) => { + if (err) reject(err); + resolve(rows); + }) }) - }) - return result; + return result; + } catch (err) { + server.log.error(err); + return null; + } } async function getIndexerData(sponsor: string, sender: string, month: number, year: number, noOfTxns: number, endpoint: string): Promise { diff --git a/docker-compose.yml b/docker-compose.yml index 1158256..f813a79 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,6 +29,7 @@ services: admin_frontend: environment: - REACT_APP_INDEXER_ENDPOINT=http://localhost:3003 + - REACT_APP_SERVER_URL=http://localhost:5050 build: context: ./admin_frontend dockerfile: Dockerfile