How to pass Pyth updateData to smart contract?

I’m encountering an issue with fetching and formatting data from the Pyth SDK, specifically related to the update_price_data I obtain from it. In our Rust SDK, we’re using Vec<Bytes> to interact with contracts and parse responses from the Pyth service. However, when attempting to replicate this process in TypeScript, I keep running into an error message:
"FuelError: Expected array value."

To address this, I’ve explored two approaches:

#1. Making a GET request to https://xc-mainnet.pyth.network/api/ with specific parameters:

https://xc-mainnet.pyth.network/api/latest_vaas?ids[]=${ETH_USD_PRICE_FEED_ID}&ids[]=${USDC_USD_PRICE_FEED_ID}&ids[]=${BTC_USD_PRICE_FEED_ID}

This approach results in a response like:

[‘AQAAAAMNABzaqhF+LL16+qpe+MOn76zGy1A4+rRkH8cchQcv4D…lZ5L1AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=’, ‘AQAAAAMNAC96pJ62HNKYW/C2FZGRu2GYBsb0fQrRVREzd1DT0O…lZ5L1AAAAAGVnkvQAAAAABfW6fAAAAAAAAGwXAAAAAGVnkvQ=’, ‘AQAAAAMNABzLjUeoSQ3Az8fULTlxffpdLGpO+zzwWqCJGwfOOT…lZ5L1AAAAAGVnkvQAAAAAAHwPRgAAAAAAADNOAAAAAGVnkvQ=’]

#2. Using the Pyth SDK and the connection.getPriceFeedsUpdateData(priceIds) method:

[‘0x01000000030d00d87b4fe955a375b8affe2de70ec1c3fba9…0000000000000000000000000000000000000000000000000’, ‘0x01000000030d006dc127b609375e3ce7ab05f8406bf3f81c…100000000007c311600000000000025c60000000065679330’, ‘0x01000000030d00ccaeca9b71f377ed31e97999e6bb2a6376…10000000005f5b8680000000000006bb00000000065679331’, ‘0x01000000030d00e67af2a8984d0cffad22b77c619b1bbccf…1000000019faf1633000000000044c1110000000065679331’, ‘0x01000000030d00885058f5cfb4e8a5bbe37f395258a7b74b…f000000012ef0cbc500000000005f625e000000006567932b’]

I’ve attempted to parse both of these values using the following method:

const parsedUpdateData = data.map((v) => Uint8Array.from(v.match(/.{1,2}/g)!.map((byte) => parseInt(byte, 16))));

Afterwards, I’ve provided this parsed data to a contract method that expects arguments of this type:

withdraw_collateral: InvokeFunction<[amount: BigNumberish, price_update_data: Vec<Bytes>], void>;

However, this approach failed with the same error message: "FuelError: Expected array value."If you have any insights or examples on how to correctly format and provide this data, I would greatly appreciate it

Hi there!

You might try to use our built in helper “arrayify”, defined here.

import { arrayify } from "fuels";

That will give you a proper byte array.

Let me know if this works!

yep, it worked like this
const parsedUpdateData = updateData.map((v) => Array.from(arrayify(v)))

1 Like

I’m glad it worked!

I’ve opened an issue to make the Vector encode to support uInt8Array.

:palm_tree:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.