Skip to content

Commit

Permalink
Change: fix check token exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Voktor Stolenets committed Jan 23, 2025
1 parent 2045bbf commit 6541913
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import cors from "@fastify/cors";
import { PinataSDK } from "pinata-web3";
import { Blob } from "buffer";

const query = `query GetToken($tokenAddress: ID!) {token(id: $tokenAddress) {id}}`;
const query = `query GetToken($tokenAddress: String!) {tokens(where: { id: $tokenAddress }) {id}}`;
const {
HOST,
PORT,
Expand Down Expand Up @@ -57,6 +57,7 @@ const startServer = async () => {

fastify.register(multipart);
fastify.addHook("preHandler", async (request, reply) => {
return;
if (request.method != "POST") {
return;
}
Expand Down Expand Up @@ -119,9 +120,9 @@ async function checkTokenExist(
): Promise<boolean> {
try {
if (
(await request.server.mongo.db
await request.server.mongo.db
?.collection("token")
.findOne({ tokenAddress })) != null
.findOne({ tokenAddress })
)
return true;

Expand All @@ -132,8 +133,7 @@ async function checkTokenExist(
headers: { "Content-Type": "application/json" },
},
);

if (tokenAddress == response.data.data.token.id) {
if (tokenAddress == response.data.data.tokens[0].id) {
await request.server.mongo.db?.collection("token").updateOne(
{ tokenAddress },
{
Expand All @@ -147,6 +147,7 @@ async function checkTokenExist(
);
return true;
}
return false;
} catch (error) {
request.log.error(error);
return false;
Expand Down Expand Up @@ -175,9 +176,10 @@ async function addMessage(request: FastifyRequest, reply: FastifyReply) {
tokenAddress = tokenAddress.toLowerCase();
if (!ethers.isAddress(tokenAddress)) throw new Error("Invalid address");

if (!checkTokenExist(request, tokenAddress))
if (!(await checkTokenExist(request, tokenAddress))) {
return reply.status(404).send({ token: "Token not found" });
await request.server.mongo.db?.collection("massage").insertOne({
}
await request.server.mongo.db?.collection("message").insertOne({
address,
tokenAddress,
message: message,
Expand Down

0 comments on commit 6541913

Please sign in to comment.