Skip to content

Commit

Permalink
add scan-asset endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
piyalbasu committed Jul 30, 2024
1 parent 138bd8e commit 7f850c9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/helper/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export const ERROR = {
INVALID_RUN_MODE: "invalid run mode",
UNABLE_TO_SCAN_SITE: "unable to scan site using blockaid",
UNABLE_TO_SCAN_TX: "unable to scan tx using blockaid",
UNABLE_TO_SCAN_ASSET: "unable to scan asset using blockaid",
};
28 changes: 28 additions & 0 deletions src/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,34 @@ export async function initApiServer(
},
});

instance.route({
method: "GET",
url: "/scan-asset",
schema: {
querystring: {
["address"]: {
type: "string",
},
},
},
handler: async (
request: FastifyRequest<{
Querystring: {
["address"]: string;
};
}>,
reply
) => {
const { address } = request.query;
try {
const { data, error } = await blockAidService.scanAsset(address);
return reply.code(error ? 400 : 200).send({ data, error });
} catch (error) {
return reply.code(500).send(ERROR.SERVER_ERROR);
}
},
});

instance.route({
method: "POST",
url: "/subscription/token",
Expand Down
19 changes: 19 additions & 0 deletions src/service/blockaid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,23 @@ export class BlockAidService {
return { data: null, error: ERROR.UNABLE_TO_SCAN_TX };
}
};

scanAsset = async (
address: string
): Promise<{
data: Blockaid.Token.TokenScanResponse | null;
error: string | null;
}> => {
try {
const data = await this.blockAidClient.token.scan({
address,
chain: "stellar",
});
return { data, error: null };
} catch (error) {
this.logger.error(error);
this.scanMissCounter.inc();
return { data: null, error: ERROR.UNABLE_TO_SCAN_ASSET };
}
};
}

0 comments on commit 7f850c9

Please sign in to comment.