Price Service: Add support for multiple price id in vaa request (#198)

* Add support for multiple price id in vaa request
This commit is contained in:
Ali Behjati 2022-05-04 12:30:24 +04:30 committed by GitHub
parent 5be5576f0c
commit ec7dcf7817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 9 deletions

View File

@ -56,25 +56,44 @@ export class RestAPI {
const latestVaaBytesInputSchema: schema = { const latestVaaBytesInputSchema: schema = {
query: Joi.object({ query: Joi.object({
id: Joi.string().regex(/^[a-f0-9]{64}$/) id: Joi.array().items(Joi.string().regex(/^[a-f0-9]{64}$/))
}) })
} }
app.get("/latest_vaa_bytes", validate(latestVaaBytesInputSchema), (req: Request, res: Response) => { app.get("/latest_vaa_bytes", validate(latestVaaBytesInputSchema), (req: Request, res: Response) => {
let priceId = req.query.id as string; let priceIds = req.query.id as string[];
let latestPriceInfo = this.priceFeedVaaInfo.getLatestPriceInfo(priceId); // Multiple price ids might share same vaa, we use sequence number as
// key of a vaa and deduplicate using a map of seqnum to vaa bytes.
let vaaMap = new Map<number, string>();
let notFoundIds: string[] = [];
for (let id of priceIds) {
let latestPriceInfo = this.priceFeedVaaInfo.getLatestPriceInfo(id);
if (latestPriceInfo === undefined) { if (latestPriceInfo === undefined) {
res.sendStatus(StatusCodes.BAD_REQUEST); notFoundIds.push(id);
return; continue;
} }
const freshness: DurationInSec = (new Date).getTime() / 1000 - latestPriceInfo.receiveTime; const freshness: DurationInSec = (new Date).getTime() / 1000 - latestPriceInfo.receiveTime;
this.promClient?.addApiRequestsPriceFreshness(req.path, priceId, freshness); this.promClient?.addApiRequestsPriceFreshness(req.path, id, freshness);
res.send(latestPriceInfo.vaaBytes); vaaMap.set(latestPriceInfo.seqNum, latestPriceInfo.vaaBytes);
}
if (notFoundIds.length > 0) {
res.status(StatusCodes.BAD_REQUEST).send(`Price Feeds with ids ${notFoundIds.join(', ')} not found`);
return;
}
const jsonResponse = Array.from(vaaMap.values(),
vaaBytes => Buffer.from(vaaBytes, 'binary').toString('base64')
);
res.json(jsonResponse);
}); });
endpoints.push("latest_vaa_bytes?id=<price_feed_id>"); endpoints.push("latest_vaa_bytes?id[]=<price_feed_id>&id[]=<price_feed_id_2>&..");
const latestPriceFeedInputSchema: schema = { const latestPriceFeedInputSchema: schema = {
query: Joi.object({ query: Joi.object({