Skip to content

Commit

Permalink
changes as per feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
vignesha22 committed Feb 28, 2024
1 parent 361a8d8 commit 391ad3e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
2 changes: 2 additions & 0 deletions admin_frontend/demo.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REACT_APP_INDEXER_ENDPOINT=http://localhost:3003
REACT_APP_SERVER_URL=http://localhost:5050
23 changes: 16 additions & 7 deletions admin_frontend/src/components/ApiKeys.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Expand All @@ -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)
}
};

Expand Down Expand Up @@ -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),
});
Expand All @@ -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);
}
};
Expand All @@ -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 }),
})
Expand All @@ -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);
}
};
Expand Down
5 changes: 5 additions & 0 deletions admin_frontend/src/constants/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const ENDPOINTS = {
'getKeys': '/getKeys',
'saveKey' : '/saveKey',
'deleteKey': '/deleteKey',
}
1 change: 1 addition & 0 deletions backend/indexer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ ponder.on("EtherspotPaymaster:SponsorSuccessful", async ({ event, context }) =>
})
} catch (err) {
// caught err
console.log(err);
}
});
24 changes: 13 additions & 11 deletions backend/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand All @@ -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;
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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<any[]> {
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 391ad3e

Please sign in to comment.