get-update-fee.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. getLatestPriceFeed,
  8. solidity,
  9. ethersJS,
  10. } from "./common";
  11. import { ParameterType } from "../../components/EvmApi";
  12. import { InlineLink } from "../../components/InlineLink";
  13. export const getUpdateFee = readApi<"updateData">({
  14. name: "getUpdateFee",
  15. description: `
  16. Get the fee required to update the on-chain price feeds with the provided
  17. \`updateData\`. The returned number of wei should be sent as the transaction
  18. value when calling [updatePriceFeeds](update-price-feeds). The \`updateData\`
  19. can be retrieved from the [Hermes API](https://hermes.pyth.network/docs).
  20. `,
  21. parameters: [
  22. {
  23. name: "updateData",
  24. type: ParameterType.HexArray,
  25. description: (
  26. <>
  27. The price updates that you would like to submit to{" "}
  28. <InlineLink href="updatePriceFeeds">updatePriceFeeds</InlineLink>
  29. </>
  30. ),
  31. },
  32. ],
  33. examples: [
  34. {
  35. name: "Latest BTC/USD update data",
  36. icon: Btc,
  37. parameters: () => getParams(BTCUSD),
  38. },
  39. {
  40. name: "Latest ETH/USD update data",
  41. icon: Eth,
  42. parameters: () => getParams(ETHUSD),
  43. },
  44. ],
  45. code: [
  46. solidity(
  47. ({ updateData }) => `
  48. bytes[] memory updateData = new bytes[](1);
  49. updateData[0] = ${updateData ? `hex"${updateData}` : "/* <updateData> */"};
  50. uint feeAmount = pyth.getUpdateFee(updateData);
  51. `,
  52. ),
  53. ethersJS(
  54. ({ updateData }) => `
  55. const updateData = ${updateData ? `['${updateData}']` : "/* <updateData> */"};
  56. const [feeAmount] = await contract.getUpdateFee(updateData);
  57. `,
  58. ),
  59. ],
  60. });
  61. const getParams = async (feedId: string) => {
  62. const feed = await getLatestPriceFeed(feedId);
  63. return { updateData: feed.binary.data };
  64. };