{"_id":"@aliver/pyth-aptos-js","name":"@aliver/pyth-aptos-js","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.1":{"name":"@aliver/pyth-aptos-js","version":"1.0.1","description":"Pyth Network Aptos Utilities","homepage":"https://pyth.network","author":{"name":"Pyth Data Association"},"main":"lib/index.js","types":"lib/index.d.ts","repository":{"type":"git","url":"https://github.com/pyth-network/pyth-js/pyth-aptos-js"},"scripts":{"test":"jest --passWithNoTests","build":"tsc","example-relay":"npm run build && node lib/examples/AptosRelay.js","format":"prettier --write \"src/**/*.ts\"","lint":"eslint src/","prepare":"npm run build","prepublishOnly":"npm test && npm run lint","preversion":"npm run lint","version":"npm run format && git add -A src"},"keywords":["pyth","oracle"],"license":"Apache-2.0","devDependencies":{"@truffle/hdwallet-provider":"^2.0.8","@types/ethereum-protocol":"^1.0.2","@types/jest":"^27.4.1","@types/node":"^17.0.27","@types/web3":"~1.0.20","@types/web3-provider-engine":"^14.0.1","@types/yargs":"^17.0.10","@typescript-eslint/eslint-plugin":"^5.21.0","@typescript-eslint/parser":"^5.21.0","eslint":"^8.14.0","jest":"^27.5.1","prettier":"^2.6.2","ts-jest":"^27.1.4","typescript":"^4.6.3","web3":"^1.5.3","yargs":"^17.4.1"},"dependencies":{"@aliver/pyth-common-js":"^1.4.1","aptos":"^1.3.14","buffer":"^6.0.3"},"gitHead":"7a9875e667547b56e2353acd4a7e2bb39d650b91","_id":"@aliver/pyth-aptos-js@1.0.1","_nodeVersion":"18.12.1","_npmVersion":"8.19.2","dist":{"integrity":"sha512-56qirxyzgNxhJIURDOqYyxPo64ui/b1eEH09lmrz1Gm0VMMmYdDTVGWxC4T0HyWGq6qNpcNu5+hqqNkiruDI7g==","shasum":"78fa9f20e1c20be67da8df8972c28b720705b97f","tarball":"https://registry.npmjs.org/@aliver/pyth-aptos-js/-/pyth-aptos-js-1.0.1.tgz","fileCount":8,"unpackedSize":13178,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFXeBEYlk83vBgl2R1tomhRzjN2RDasSHSmPFgM76dNzAiB0/cwTkRzPatlWTCSs5v8rRVCTWeviZSmf7WdlRPEleQ=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJj4muFACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrzZBAAgVia9zprY20PdYSaYtsgUPlYzTt69flNyxGSSH8uCyHd8W3r\r\ndjQwGUHNRU3h8f6H2r3ekpgso8QthuzhYO4P8511TeNGfbKYUDkaeB29wv1h\r\nMopTc3kwTI666LSwXylKYVzimptV7cpn1yg+oK1sMrQiS2TqbojlA+++pAuO\r\nEhb4xOIIv1CViRvxV7PYGq5EHy0Yex4I2SR6PJYcb7436Juu8wYwf1qmUoWc\r\nmb5PgvW6SDSb9NKzDG8iHRiePGTPp5GdPop1r+U9B+NVLB3KcK/XWjEZyhk3\r\n6A3371ZrhSRsZeBl5zZhUxtp4WHg1KEDm5aab/GMa7FbQ/xV3c9VZLbByx8A\r\no1z90NR85dO5aHGtjtDJi3iF8+KoXrl6SHzb9p1xyqtfAVu1ebz/mPAQ6TkM\r\nW27MNC/aYqkB7ejAH4kDEvyOLGkPd9mvntu5KPC9dh5C345ENaVS9vwWMLGm\r\nJeQwrjTZL8yI5pIJk6N/hZxqZQZrV9wJ18gkbzR9/LMFODUdeXvne6mdrpkz\r\ngzC+DS1p3inqm8m9R2/8nujwttkNS20/q3zZPg/NZ7touBkZpJ/B2Qgvxm0n\r\n5jEoiyC2W8hCHvee+9h/ZdfC+cDUta1sm86rdRcddBQwX24lV5p9ea/6Uhi2\r\n8t1KiPUYgL/ptdhpqrLIPHVwknPTo3NgALA=\r\n=do+F\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"aliver","email":"aliver.len@outlook.com"},"directories":{},"maintainers":[{"name":"aliver","email":"aliver.len@outlook.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/pyth-aptos-js_1.0.1_1675783045579_0.07739631799868141"},"_hasShrinkwrap":false}},"time":{"created":"2023-02-07T15:17:25.506Z","1.0.1":"2023-02-07T15:17:25.749Z","modified":"2023-02-07T15:17:25.885Z"},"maintainers":[{"name":"aliver","email":"aliver.len@outlook.com"}],"description":"Pyth Network Aptos Utilities","homepage":"https://pyth.network","keywords":["pyth","oracle"],"repository":{"type":"git","url":"https://github.com/pyth-network/pyth-js/pyth-aptos-js"},"author":{"name":"Pyth Data Association"},"license":"Apache-2.0","readme":"# Pyth Aptos JS\n\n[Pyth](https://pyth.network/) provides real-time pricing data in a variety of asset classes, including cryptocurrency, equities, FX and commodities. This library allows you to use these real-time prices on Aptos networks.\n\n## Installation\n\n### npm\n\n```\n$ npm install --save @pythnetwork/pyth-aptos-js\n```\n\n### Yarn\n\n```\n$ yarn add @pythnetwork/pyth-aptos-js\n```\n\n## Quickstart\n\nPyth stores prices off-chain to minimize gas fees, which allows us to offer a wider selection of products and faster update times.\nSee [On-Demand Updates](https://docs.pyth.network/consume-data/on-demand) for more information about this approach.\nTo use Pyth prices on chain,\nthey must be fetched from an off-chain price service. The `AptosPriceServiceConnection` class can be used to interact with these services,\nproviding a way to fetch these prices directly in your code. The following example wraps an existing RPC provider and shows how to obtain\nPyth prices and submit them to the network:\n\n```typescript\nconst connection = new AptosPriceServiceConnection(\n  \"https://xc-testnet.pyth.network\"\n); // See Price Service endpoints section below for other endpoints\n\nconst priceIds = [\n  // You can find the ids of prices at https://pyth.network/developers/price-feed-ids#aptos-testnet\n  \"0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b\", // BTC/USD price id in testnet\n  \"0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6\", // ETH/USD price id in testnet\n];\n\n// In order to use Pyth prices in your protocol you need to submit the price update data to Pyth contract in your target\n// chain. `getPriceUpdateData` creates the update data which can be submitted to your contract. Then your contract should\n// call the Pyth Contract with this data.\nconst priceUpdateData = await connection.getPriceFeedsUpdateData(priceIds);\n\n// Create a transaction and submit to your contract using the price update data\nconst client = new AptosClient(endpoint);\nlet result = await client.generateSignSubmitWaitForTransaction(\n  sender,\n  new TxnBuilderTypes.TransactionPayloadEntryFunction(\n    TxnBuilderTypes.EntryFunction.natural(\n      \"0x..::your_module\",\n      \"do_something\",\n      [],\n      [priceUpdateData]\n    )\n  )\n);\n```\n\n`your_module::do_something` should then call `pyth::update_price_feeds` before querying the data using `pyth::get_price`:\n\n```move\nmodule example::your_module {\n    use pyth::pyth;\n    use pyth::price_identifier;\n    use aptos_framework::coin;\n\n    public fun do_something(user: &signer, pyth_update_data: vector<vector<u8>>) {\n        // First update the Pyth price feeds. The user pays the fee for the update.\n        let coins = coin::withdraw(user, pyth::get_update_fee(pyth_update_data));\n\n        pyth::update_price_feeds(pyth_update_data, coins);\n\n        // Now we can use the prices which we have just updated\n        let btc_usd_price_id = price_identifier::from_byte_vec(\n            x\"e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43\");\n        let price = pyth::get_price(btc_usd_price_id);\n\n    }\n}\n```\n\nWe strongly recommend reading our guide which explains [how to work with Pyth price feeds](https://docs.pyth.network/consume-data/best-practices).\n\n### Off-chain prices\n\nMany applications additionally need to display Pyth prices off-chain, for example, in their frontend application.\nThe `AptosPriceServiceConnection` provides two different ways to fetch the current Pyth price.\nThe code blocks below assume that the `connection` and `priceIds` objects have been initialized as shown above.\nThe first method is a single-shot query:\n\n```typescript\n// `getLatestPriceFeeds` returns a `PriceFeed` for each price id. It contains all information about a price and has\n// utility functions to get the current and exponentially-weighted moving average price, and other functionality.\nconst priceFeeds = await connection.getLatestPriceFeeds(priceIds);\n// Get the price if it is not older than 60 seconds from the current time.\nconsole.log(priceFeeds[0].getPriceNoOlderThan(60)); // Price { conf: '1234', expo: -8, price: '12345678' }\n// Get the exponentially-weighted moving average price if it is not older than 60 seconds from the current time.\nconsole.log(priceFeeds[1].getEmaPriceNoOlderThan(60));\n```\n\nThe object also supports a streaming websocket connection that allows you to subscribe to every new price update for a given feed.\nThis method is useful if you want to show continuously updating real-time prices in your frontend:\n\n```typescript\n// Subscribe to the price feeds given by `priceId`. The callback will be invoked every time the requested feed\n// gets a price update.\nconnection.subscribePriceFeedUpdates(priceIds, (priceFeed) => {\n  console.log(\n    `Received update for ${priceFeed.id}: ${priceFeed.getPriceNoOlderThan(60)}`\n  );\n});\n\n// When using the subscription, make sure to close the websocket upon termination to finish the process gracefully.\nsetTimeout(() => {\n  connection.closeWebSocket();\n}, 60000);\n```\n\n### Example\n\n[This example](./src/examples/AptosRelay.ts) shows how to update prices on an Aptos network. It does the following:\n\n1. Fetches update data from the Price Service for the given price feeds.\n2. Calls the Pyth Aptos contract with the update data.\n\nYou can run this example with `npm run example-relay`. A full command that updates BTC and ETH prices on the BNB Chain testnet network looks like this:\n\n```bash\nexport APTOS_KEY = \"0x...\";\nnpm run example-relay -- --endpoint https://xc-testnet.pyth.network --price-ids 0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b 0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6 --full-node https://fullnode.testnet.aptoslabs.com/v1 --pyth-contract 0xaa706d631cde8c634fe1876b0c93e4dec69d0c6ccac30a734e9e257042e81541\n```\n\n## Price Service endpoints\n\nWe provide public endpoints for the price service, although it is strongly recommended to host your own instance.\n\n| Aptos Network | Price Service URL               |\n| ------------- | ------------------------------- |\n| Testnet       | https://xc-testnet.pyth.network |\n| Mainnet       | https://xc-mainnet.pyth.network |\n","readmeFilename":"README.md"}