get-update-fee.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import Btc from "cryptocurrency-icons/svg/color/btc.svg";
  2. import Eth from "cryptocurrency-icons/svg/color/eth.svg";
  3. import {
  4. readApi,
  5. BTCUSD,
  6. ETHUSD,
  7. getLatestPriceUpdate,
  8. solidity,
  9. ethersJS,
  10. } from "./common";
  11. import { ParameterType } from "../../components/EvmApi";
  12. export const getUpdateFee = readApi<"updateData">({
  13. name: "getUpdateFee",
  14. summary:
  15. "Get the fee required to update the on-chain price feeds with the provided `updateData`.",
  16. description: `
  17. This method returns the fee required to update the on-chain price feeds for the given \`updateData\`.
  18. The fee returned is in **wei**.
  19. The caller should send the returned fee amount as the transaction value when calling [updatePriceFeeds](update-price-feeds).
  20. The \`updateData\` can be retrieved from the [Hermes API](https://hermes.pyth.network/docs).
  21. `,
  22. parameters: [
  23. {
  24. name: "updateData",
  25. type: ParameterType.HexArray,
  26. description:
  27. "The price updates that you would like to submit to [updatePriceFeeds](updatePriceFeeds). Fetch this data from [Hermes API](https://hermes.pyth.network/docs/#/rest/latest_price_updates).",
  28. },
  29. ],
  30. examples: [
  31. {
  32. name: "Latest BTC/USD update data",
  33. icon: Btc,
  34. parameters: () => getParams(BTCUSD),
  35. },
  36. {
  37. name: "Latest ETH/USD update data",
  38. icon: Eth,
  39. parameters: () => getParams(ETHUSD),
  40. },
  41. ],
  42. code: [
  43. solidity(
  44. ({ updateData }) => `
  45. bytes[] memory updateData = new bytes[](1);
  46. updateData[0] = ${updateData ? `hex"${updateData}` : "/* <updateData> */"};
  47. uint feeAmount = pyth.getUpdateFee(updateData);
  48. `,
  49. ),
  50. ethersJS(
  51. ({ updateData }) => `
  52. const updateData = ${updateData ? `['${updateData}']` : "/* <updateData> */"};
  53. const [feeAmount] = await contract.getUpdateFee(updateData);
  54. `,
  55. ),
  56. ],
  57. });
  58. const getParams = async (feedId: string) => {
  59. const feed = await getLatestPriceUpdate(feedId);
  60. return { updateData: feed.binary.data };
  61. };