Skip to content

Commit 56beca0

Browse files
devin-ai-integration[bot]Jayant Krishnamurthy
andcommitted
feat(price-service): transform v2 API responses to old format
Co-Authored-By: Jayant Krishnamurthy <[email protected]>
1 parent 858c6c8 commit 56beca0

File tree

1 file changed

+96
-8
lines changed

1 file changed

+96
-8
lines changed

price_service/client/js/src/PriceServiceConnection.ts

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,31 @@ export class PriceServiceConnection {
150150
binary: this.priceFeedRequestConfig.binary,
151151
},
152152
});
153-
const priceFeedsJson = response.data as any[];
154-
return priceFeedsJson.map((priceFeedJson) =>
155-
PriceFeed.fromJson(priceFeedJson)
156-
);
153+
// Transform v2 API response to match old format
154+
const transformedPriceFeeds = response.data.parsed.map((item: any) => {
155+
const priceFeedJson: any = {
156+
id: item.id,
157+
price: item.price,
158+
ema_price: item.ema_price,
159+
};
160+
161+
// Include metadata if verbose flag is set
162+
if (this.priceFeedRequestConfig.verbose && item.metadata) {
163+
priceFeedJson.metadata = item.metadata;
164+
}
165+
166+
// Include VAA if binary flag is set
167+
if (this.priceFeedRequestConfig.binary && response.data.binary?.data) {
168+
const vaaIndex = response.data.parsed.findIndex((p: any) => p.id === item.id);
169+
if (vaaIndex >= 0 && vaaIndex < response.data.binary.data.length) {
170+
priceFeedJson.vaa = response.data.binary.data[vaaIndex];
171+
}
172+
}
173+
174+
return PriceFeed.fromJson(priceFeedJson);
175+
});
176+
177+
return transformedPriceFeeds;
157178
}
158179

159180
/**
@@ -167,13 +188,31 @@ export class PriceServiceConnection {
167188
* @deprecated This method uses the v2 API endpoint which may return data in a different format.
168189
* Please verify the response format when using this method.
169190
*/
191+
/**
192+
* Fetch latest VAA of given price ids.
193+
* 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)
194+
*
195+
* This function is coupled to wormhole implementation.
196+
*
197+
* @param priceIds Array of hex-encoded price ids.
198+
* @returns Array of base64 encoded VAAs.
199+
* @deprecated This method uses the v2 API endpoint which may return data in a different format.
200+
* Please verify the response format when using this method.
201+
*/
170202
async getLatestVaas(priceIds: HexString[]): Promise<string[]> {
171203
const response = await this.httpClient.get("/v2/updates/price/latest", {
172204
params: {
173205
ids: priceIds,
206+
binary: true, // Always request binary data for VAAs
174207
},
175208
});
176-
return response.data;
209+
210+
// Extract VAAs from binary data array
211+
if (!response.data.binary?.data || !Array.isArray(response.data.binary.data)) {
212+
return [];
213+
}
214+
215+
return response.data.binary.data;
177216
}
178217

179218
/**
@@ -199,10 +238,24 @@ export class PriceServiceConnection {
199238
{
200239
params: {
201240
id: priceId,
241+
binary: true, // Always request binary data for VAAs
202242
},
203243
}
204244
);
205-
return [response.data.vaa, response.data.publishTime];
245+
246+
// Extract VAA and publishTime from response
247+
if (!response.data.binary?.data?.[0] || !response.data.parsed?.[0]) {
248+
throw new Error("No VAA data found for the given price id and publish time");
249+
}
250+
251+
const vaa = response.data.binary.data[0];
252+
const actualPublishTime = response.data.parsed[0].price.publish_time;
253+
254+
if (actualPublishTime < publishTime) {
255+
throw new Error("No VAA found after the specified publish time");
256+
}
257+
258+
return [vaa, actualPublishTime];
206259
}
207260

208261
/**
@@ -232,9 +285,38 @@ export class PriceServiceConnection {
232285
}
233286
);
234287

235-
return PriceFeed.fromJson(response.data);
288+
// Extract price feed from response
289+
if (!response.data.parsed?.[0]) {
290+
throw new Error("No price feed data found for the given price id and publish time");
291+
}
292+
293+
const priceFeedJson: any = {
294+
id: response.data.parsed[0].id,
295+
price: response.data.parsed[0].price,
296+
ema_price: response.data.parsed[0].ema_price,
297+
};
298+
299+
// Include metadata if verbose flag is set
300+
if (this.priceFeedRequestConfig.verbose && response.data.parsed[0].metadata) {
301+
priceFeedJson.metadata = response.data.parsed[0].metadata;
302+
}
303+
304+
// Include VAA if binary flag is set
305+
if (this.priceFeedRequestConfig.binary && response.data.binary?.data?.[0]) {
306+
priceFeedJson.vaa = response.data.binary.data[0];
307+
}
308+
309+
return PriceFeed.fromJson(priceFeedJson);
236310
}
237311

312+
/**
313+
* Fetch the list of available price feed ids.
314+
* This will throw an axios error if there is a network problem or the price service returns a non-ok response.
315+
*
316+
* @returns Array of hex-encoded price ids.
317+
* @deprecated This method uses the v2 API endpoint which may return data in a different format.
318+
* Please verify the response format when using this method.
319+
*/
238320
/**
239321
* Fetch the list of available price feed ids.
240322
* 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 {
245327
*/
246328
async getPriceFeedIds(): Promise<HexString[]> {
247329
const response = await this.httpClient.get("/v2/price_feeds");
248-
return response.data;
330+
331+
// Extract ids from response array
332+
if (!Array.isArray(response.data)) {
333+
return [];
334+
}
335+
336+
return response.data.map((item: any) => item.id);
249337
}
250338

251339
/**

0 commit comments

Comments
 (0)