Skip to content

Commit

Permalink
feat(price-service): transform v2 API responses to old format
Browse files Browse the repository at this point in the history
Co-Authored-By: Jayant Krishnamurthy <[email protected]>
  • Loading branch information
devin-ai-integration[bot] and Jayant Krishnamurthy committed Jan 17, 2025
1 parent 858c6c8 commit 56beca0
Showing 1 changed file with 96 additions and 8 deletions.
104 changes: 96 additions & 8 deletions price_service/client/js/src/PriceServiceConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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<string[]> {
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;
}

/**
Expand All @@ -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];
}

/**
Expand Down Expand Up @@ -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.
Expand All @@ -245,7 +327,13 @@ export class PriceServiceConnection {
*/
async getPriceFeedIds(): Promise<HexString[]> {
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);
}

/**
Expand Down

0 comments on commit 56beca0

Please sign in to comment.