From 56beca01dd9d40eda3158dd82a69fd7caafafadc Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Fri, 17 Jan 2025 22:37:37 +0000 Subject: [PATCH] feat(price-service): transform v2 API responses to old format Co-Authored-By: Jayant Krishnamurthy --- .../client/js/src/PriceServiceConnection.ts | 104 ++++++++++++++++-- 1 file changed, 96 insertions(+), 8 deletions(-) diff --git a/price_service/client/js/src/PriceServiceConnection.ts b/price_service/client/js/src/PriceServiceConnection.ts index 79ebfc569..d3fa32212 100644 --- a/price_service/client/js/src/PriceServiceConnection.ts +++ b/price_service/client/js/src/PriceServiceConnection.ts @@ -150,10 +150,31 @@ export class PriceServiceConnection { binary: this.priceFeedRequestConfig.binary, }, }); - const priceFeedsJson = response.data as any[]; - return priceFeedsJson.map((priceFeedJson) => - PriceFeed.fromJson(priceFeedJson) - ); + // Transform v2 API response to match old format + const transformedPriceFeeds = response.data.parsed.map((item: any) => { + const priceFeedJson: any = { + id: item.id, + price: item.price, + ema_price: item.ema_price, + }; + + // Include metadata if verbose flag is set + if (this.priceFeedRequestConfig.verbose && item.metadata) { + priceFeedJson.metadata = item.metadata; + } + + // Include VAA if binary flag is set + if (this.priceFeedRequestConfig.binary && response.data.binary?.data) { + const vaaIndex = response.data.parsed.findIndex((p: any) => p.id === item.id); + if (vaaIndex >= 0 && vaaIndex < response.data.binary.data.length) { + priceFeedJson.vaa = response.data.binary.data[vaaIndex]; + } + } + + return PriceFeed.fromJson(priceFeedJson); + }); + + return transformedPriceFeeds; } /** @@ -167,13 +188,31 @@ export class PriceServiceConnection { * @deprecated This method uses the v2 API endpoint which may return data in a different format. * Please verify the response format when using this method. */ + /** + * Fetch latest VAA of given price ids. + * This will throw an axios error if there is a network problem or the price service returns a non-ok response (e.g: Invalid price ids) + * + * This function is coupled to wormhole implementation. + * + * @param priceIds Array of hex-encoded price ids. + * @returns Array of base64 encoded VAAs. + * @deprecated This method uses the v2 API endpoint which may return data in a different format. + * Please verify the response format when using this method. + */ async getLatestVaas(priceIds: HexString[]): Promise { const response = await this.httpClient.get("/v2/updates/price/latest", { params: { ids: priceIds, + binary: true, // Always request binary data for VAAs }, }); - return response.data; + + // Extract VAAs from binary data array + if (!response.data.binary?.data || !Array.isArray(response.data.binary.data)) { + return []; + } + + return response.data.binary.data; } /** @@ -199,10 +238,24 @@ export class PriceServiceConnection { { params: { id: priceId, + binary: true, // Always request binary data for VAAs }, } ); - return [response.data.vaa, response.data.publishTime]; + + // Extract VAA and publishTime from response + if (!response.data.binary?.data?.[0] || !response.data.parsed?.[0]) { + throw new Error("No VAA data found for the given price id and publish time"); + } + + const vaa = response.data.binary.data[0]; + const actualPublishTime = response.data.parsed[0].price.publish_time; + + if (actualPublishTime < publishTime) { + throw new Error("No VAA found after the specified publish time"); + } + + return [vaa, actualPublishTime]; } /** @@ -232,9 +285,38 @@ export class PriceServiceConnection { } ); - return PriceFeed.fromJson(response.data); + // Extract price feed from response + if (!response.data.parsed?.[0]) { + throw new Error("No price feed data found for the given price id and publish time"); + } + + const priceFeedJson: any = { + id: response.data.parsed[0].id, + price: response.data.parsed[0].price, + ema_price: response.data.parsed[0].ema_price, + }; + + // Include metadata if verbose flag is set + if (this.priceFeedRequestConfig.verbose && response.data.parsed[0].metadata) { + priceFeedJson.metadata = response.data.parsed[0].metadata; + } + + // Include VAA if binary flag is set + if (this.priceFeedRequestConfig.binary && response.data.binary?.data?.[0]) { + priceFeedJson.vaa = response.data.binary.data[0]; + } + + return PriceFeed.fromJson(priceFeedJson); } + /** + * Fetch the list of available price feed ids. + * This will throw an axios error if there is a network problem or the price service returns a non-ok response. + * + * @returns Array of hex-encoded price ids. + * @deprecated This method uses the v2 API endpoint which may return data in a different format. + * Please verify the response format when using this method. + */ /** * Fetch the list of available price feed ids. * This will throw an axios error if there is a network problem or the price service returns a non-ok response. @@ -245,7 +327,13 @@ export class PriceServiceConnection { */ async getPriceFeedIds(): Promise { const response = await this.httpClient.get("/v2/price_feeds"); - return response.data; + + // Extract ids from response array + if (!Array.isArray(response.data)) { + return []; + } + + return response.data.map((item: any) => item.id); } /**