From c17f5d3a48b10c988302d65fe3f223a93c7b3ec0 Mon Sep 17 00:00:00 2001 From: Jayant Krishnamurthy Date: Thu, 11 Apr 2024 10:08:43 -0700 Subject: [PATCH 1/4] migration guide --- pages/price-feeds/_meta.json | 1 + pages/price-feeds/migrate-an-app-to-pyth.mdx | 5 +++ .../migrate-an-app-to-pyth/_meta.json | 3 ++ .../migrate-an-app-to-pyth/chainlink.md | 43 +++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 pages/price-feeds/migrate-an-app-to-pyth.mdx create mode 100644 pages/price-feeds/migrate-an-app-to-pyth/_meta.json create mode 100644 pages/price-feeds/migrate-an-app-to-pyth/chainlink.md diff --git a/pages/price-feeds/_meta.json b/pages/price-feeds/_meta.json index 276936e1..dd1d24a1 100644 --- a/pages/price-feeds/_meta.json +++ b/pages/price-feeds/_meta.json @@ -25,6 +25,7 @@ "use-real-time-data": "Use Real-Time Price Data", "schedule-price-updates": "Schedule Price Updates", + "migrate-an-app-to-pyth": "Migrate an App to Pyth", "publish-data": "Publish Data", "-- Reference Material": { diff --git a/pages/price-feeds/migrate-an-app-to-pyth.mdx b/pages/price-feeds/migrate-an-app-to-pyth.mdx new file mode 100644 index 00000000..dfa8b342 --- /dev/null +++ b/pages/price-feeds/migrate-an-app-to-pyth.mdx @@ -0,0 +1,5 @@ +# Migrate to Pyth from Another Oracle + +The guides in this section are designed to help developers migrate an application from another oracle to use Pyth price feeds. + +- [Chainlink](migrate-an-app-to-pyth/chainlink.md) diff --git a/pages/price-feeds/migrate-an-app-to-pyth/_meta.json b/pages/price-feeds/migrate-an-app-to-pyth/_meta.json new file mode 100644 index 00000000..ff1a61c5 --- /dev/null +++ b/pages/price-feeds/migrate-an-app-to-pyth/_meta.json @@ -0,0 +1,3 @@ +{ + "chainlink": "from Chainlink" +} diff --git a/pages/price-feeds/migrate-an-app-to-pyth/chainlink.md b/pages/price-feeds/migrate-an-app-to-pyth/chainlink.md new file mode 100644 index 00000000..6fc1f267 --- /dev/null +++ b/pages/price-feeds/migrate-an-app-to-pyth/chainlink.md @@ -0,0 +1,43 @@ +# Migrate from Chainlink to Pyth + +This guide explains how to migrate an EVM application that uses Chainlink price feeds to use Pyth price feeds. + +## Install Pyth SDKs + +Add the [Pyth Price Feeds Solidity SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ethereum/sdk/solidity) to the dependencies of your EVM contract. +This SDK provides the `IPyth` interface for working with the Pyth Price Feeds Contract. + +**Truffle/Hardhat** + +If you are using Truffle or Hardhat, simply install the NPM package: + +```bash +npm install @pythnetwork/pyth-sdk-solidity +``` + +**Foundry** + +If you are using Foundry, you will need to create an NPM project if you don't already have one. +From the root directory of your project, run: + +```bash +npm init -y +npm install @pythnetwork/pyth-sdk-solidity +``` + +Then add the following line to your `remappings.txt` file: + +```text +@pythnetwork/pyth-sdk-solidity/=node_modules/@pythnetwork/pyth-sdk-solidity +``` + +## Deploy Chainlink Adapter + +Pyth provides an adapter contract that implements the Chainlink AggregatorV3 interface. +Developers can deploy an instance of this adapter for each price feed they wish to use in their app. + +``` +forge +``` + +## Schedule Updates From d1447b66e5a4cf28d3c104f5d3d13935e215673d Mon Sep 17 00:00:00 2001 From: Jayant Krishnamurthy Date: Tue, 16 Apr 2024 06:40:02 -0700 Subject: [PATCH 2/4] hm --- .../migrate-an-app-to-pyth/chainlink.md | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/pages/price-feeds/migrate-an-app-to-pyth/chainlink.md b/pages/price-feeds/migrate-an-app-to-pyth/chainlink.md index 6fc1f267..8159cee1 100644 --- a/pages/price-feeds/migrate-an-app-to-pyth/chainlink.md +++ b/pages/price-feeds/migrate-an-app-to-pyth/chainlink.md @@ -1,11 +1,16 @@ # Migrate from Chainlink to Pyth This guide explains how to migrate an EVM application that uses Chainlink price feeds to use Pyth price feeds. +Pyth provides a Chainlink-compatible interface for its price feeds + +There are two main steps: + +1. Deploy an instance of the `PythAggregatorV3` contract to provide a Chainlink-compatible feed interface. +2. ## Install Pyth SDKs Add the [Pyth Price Feeds Solidity SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ethereum/sdk/solidity) to the dependencies of your EVM contract. -This SDK provides the `IPyth` interface for working with the Pyth Price Feeds Contract. **Truffle/Hardhat** @@ -33,11 +38,32 @@ Then add the following line to your `remappings.txt` file: ## Deploy Chainlink Adapter -Pyth provides an adapter contract that implements the Chainlink AggregatorV3 interface. -Developers can deploy an instance of this adapter for each price feed they wish to use in their app. +`PythAggregatorV3` in `pyth-sdk-solidity` is an adapter contract that wraps the Pyth contract and implements Chainlink's `AggregatorInterface`. +One important difference between Pyth and Chainlink is that the Pyth contract holds data for all price feeds, whereas Chainlink has separate instances of `AggregatorInterface` for each feed. +The adapter contract addresses this discrepancy by wrapping a single Pyth price feed. +Users should deploy an instance of this adapter for every required price feed: ``` -forge +forge create PythAggregatorV3 --private-key $PRIVATE_KEY --rpc-url $RPC_URL --constructor-args $PYTH_ADDRESS $ETH_USD_ID +``` + +Then simply point your existing Chainlink-compatible app at the address of the newly deployed contracts. + +The following solidity snippet shows how to use this adapter contract: + +```solidity +import { AggregatorInterface } from "chainlink/AggregatorInterface.sol"; + ``` +Pyth provides an adapter contract that implements the Chainlink AggregatorV3 interface. +Developers can deploy an instance of this adapter for each price feed they wish to use in their app. + ## Schedule Updates + +Pyth is a pull oracle, meaning that users are responsible for updating on-chain prices as needed. +See [What is a pull oracle?](TODO) to learn more about pull oracles. +However, + +The simplest way to solve this problem is to schedule price updates for the feeds your app needs. +See the guide on [Scheduling Updates](TODO) for the various ways to solve this problem. From b4bd36403d4b2f6d9bee996c87331371aed9344d Mon Sep 17 00:00:00 2001 From: Jayant Krishnamurthy Date: Tue, 16 Apr 2024 10:44:27 -0700 Subject: [PATCH 3/4] hm --- next.config.js | 4 + .../migrate-an-app-to-pyth/chainlink.md | 99 ++++++++++++------- 2 files changed, 69 insertions(+), 34 deletions(-) diff --git a/next.config.js b/next.config.js index 4115988b..30ff6434 100644 --- a/next.config.js +++ b/next.config.js @@ -55,6 +55,10 @@ const permanentRedirectArray = [ "/price-feeds/pythnet-price-feeds/best-practices", "/price-feeds/best-practices", ], + [ + "/price-feeds/pythnet-price-feeds/pull-updates", + "/price-feeds/pull-updates", + ], [ "/price-feeds/solana-price-feeds/best-practices", "/price-feeds/best-practices", diff --git a/pages/price-feeds/migrate-an-app-to-pyth/chainlink.md b/pages/price-feeds/migrate-an-app-to-pyth/chainlink.md index 8159cee1..283edafc 100644 --- a/pages/price-feeds/migrate-an-app-to-pyth/chainlink.md +++ b/pages/price-feeds/migrate-an-app-to-pyth/chainlink.md @@ -1,22 +1,22 @@ # Migrate from Chainlink to Pyth This guide explains how to migrate an EVM application that uses Chainlink price feeds to use Pyth price feeds. -Pyth provides a Chainlink-compatible interface for its price feeds +Pyth provides a Chainlink-compatible interface for its price feeds to make this process simple. +There are two main steps to the migration: -There are two main steps: - -1. Deploy an instance of the `PythAggregatorV3` contract to provide a Chainlink-compatible feed interface. -2. +1. Deploy the `PythAggregatorV3` contract to provide a Chainlink-compatible feed interface. +2. Schedule price updates for the feeds required by your app. ## Install Pyth SDKs -Add the [Pyth Price Feeds Solidity SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ethereum/sdk/solidity) to the dependencies of your EVM contract. +The `PythAggregatorV3` contract is provided in the [Pyth Price Feeds Solidity SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ethereum/sdk/solidity). +Add this SDK to the dependencies of your EVM contract. **Truffle/Hardhat** If you are using Truffle or Hardhat, simply install the NPM package: -```bash +```bash copy npm install @pythnetwork/pyth-sdk-solidity ``` @@ -25,45 +25,76 @@ npm install @pythnetwork/pyth-sdk-solidity If you are using Foundry, you will need to create an NPM project if you don't already have one. From the root directory of your project, run: -```bash +```bash copy npm init -y npm install @pythnetwork/pyth-sdk-solidity ``` Then add the following line to your `remappings.txt` file: -```text +```text copy @pythnetwork/pyth-sdk-solidity/=node_modules/@pythnetwork/pyth-sdk-solidity ``` -## Deploy Chainlink Adapter - -`PythAggregatorV3` in `pyth-sdk-solidity` is an adapter contract that wraps the Pyth contract and implements Chainlink's `AggregatorInterface`. -One important difference between Pyth and Chainlink is that the Pyth contract holds data for all price feeds, whereas Chainlink has separate instances of `AggregatorInterface` for each feed. -The adapter contract addresses this discrepancy by wrapping a single Pyth price feed. -Users should deploy an instance of this adapter for every required price feed: +## Deploy Adapter Contract + +Chainlink-compatible applications can deploy and use `PythAggregatorV3` in `@pythnetwork/pyth-sdk-solidity` as a replacement for their Chainlink price feeds. +`PythAggregatorV3` is an adapter contract that wraps the Pyth contract and implements Chainlink's `AggregatorV3Interface`. + +One important difference between Pyth and Chainlink is that the Pyth contract holds data for all price feeds, whereas Chainlink has separate instances of `AggregatorV3Interface` for each feed. +The adapter contract resolves this discrepancy by wrapping a single Pyth price feed. +Users should deploy an instance of this adapter for every required price feed, then point their existing app to the addresses of the deployed adapter contracts. + +The following `forge` deployment script demonstrates the expected deployment process: + +```solidity copy +// SPDX-License-Identifier: Apache 2 +pragma solidity ^0.8.0; + +import "forge-std/Script.sol"; +import { PythAggregatorV3 } from "@pythnetwork/pyth-sdk-solidity/PythAggregatorV3.sol"; +import { ChainlinkApp } from "./ChainlinkApp.sol"; + +contract PythAggregatorV3Deployment is Script { + function run() external { + uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); + vm.startBroadcast(deployerPrivateKey); + + // Get the address for your ecosystem from: + // https://docs.pyth.network/price-feeds/contract-addresses/evm + address pythPriceFeedsContract = 0xff1a0f4744e8582DF1aE09D5611b887B6a12925C; + // Get the price feed ids from: + // https://docs.pyth.network/price-feeds/price-feed-ids + bytes32 ethFeedId = 0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace; + bytes32 solFeedId = 0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d; + + // Deploy an instance of PythAggregatorV3 for every feed. + PythAggregatorV3 ethAggregator = new PythAggregatorV3( + pythPriceFeedsContract, + ethFeedId + ); + PythAggregatorV3 solAggregator = new PythAggregatorV3( + pythPriceFeedsContract, + solFeedId + ); + + // Pass the address of the PythAggregatorV3 contract to your chainlink-compatible app. + ChainlinkApp app = new ChainlinkApp( + address(ethAggregator), + address(solAggregator) + ); + + vm.stopBroadcast(); + } +} ``` -forge create PythAggregatorV3 --private-key $PRIVATE_KEY --rpc-url $RPC_URL --constructor-args $PYTH_ADDRESS $ETH_USD_ID -``` - -Then simply point your existing Chainlink-compatible app at the address of the newly deployed contracts. - -The following solidity snippet shows how to use this adapter contract: - -```solidity -import { AggregatorInterface } from "chainlink/AggregatorInterface.sol"; - -``` - -Pyth provides an adapter contract that implements the Chainlink AggregatorV3 interface. -Developers can deploy an instance of this adapter for each price feed they wish to use in their app. ## Schedule Updates -Pyth is a pull oracle, meaning that users are responsible for updating on-chain prices as needed. -See [What is a pull oracle?](TODO) to learn more about pull oracles. -However, +Chainlink-compatible applications typically expect on-chain price feeds to update on a schedule. +When migrating to Pyth, apps may need to schedule these price updates themselves. +This step is required because Pyth is a pull oracle; see [What is a pull oracle?](/price-feeds/pull-updates.mdx) to learn more about this topic. -The simplest way to solve this problem is to schedule price updates for the feeds your app needs. -See the guide on [Scheduling Updates](TODO) for the various ways to solve this problem. +The [Sponsored Feeds](/price-feeds/sponsored-feeds.mdx) page shows a list of feeds that have scheduled on-chain updates. +If the feeds your application needs are not on this list, see [Schedule Price Updates](/price-feeds/schedule-price-updates) for several options to solve this problem. From 0b4de3a7ce393be3400139f0ed5eef3f40176ade Mon Sep 17 00:00:00 2001 From: Jayant Krishnamurthy Date: Wed, 17 Apr 2024 06:44:22 -0700 Subject: [PATCH 4/4] fix docs --- pages/price-feeds/migrate-an-app-to-pyth/chainlink.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pages/price-feeds/migrate-an-app-to-pyth/chainlink.md b/pages/price-feeds/migrate-an-app-to-pyth/chainlink.md index 283edafc..cd84d42a 100644 --- a/pages/price-feeds/migrate-an-app-to-pyth/chainlink.md +++ b/pages/price-feeds/migrate-an-app-to-pyth/chainlink.md @@ -1,10 +1,10 @@ # Migrate from Chainlink to Pyth -This guide explains how to migrate an EVM application that uses Chainlink price feeds to use Pyth price feeds. +This guide explains how to migrate an EVM application that uses Chainlink price feeds to Pyth price feeds. Pyth provides a Chainlink-compatible interface for its price feeds to make this process simple. There are two main steps to the migration: -1. Deploy the `PythAggregatorV3` contract to provide a Chainlink-compatible feed interface. +1. Deploy the [`PythAggregatorV3`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/sdk/solidity/PythAggregatorV3.sol) contract to provide a Chainlink-compatible feed interface. 2. Schedule price updates for the feeds required by your app. ## Install Pyth SDKs @@ -38,10 +38,10 @@ Then add the following line to your `remappings.txt` file: ## Deploy Adapter Contract -Chainlink-compatible applications can deploy and use `PythAggregatorV3` in `@pythnetwork/pyth-sdk-solidity` as a replacement for their Chainlink price feeds. +First, deploy the `PythAggregatorV3` contract from `@pythnetwork/pyth-sdk-solidity` as a replacement for your application's Chainlink price feeds. `PythAggregatorV3` is an adapter contract that wraps the Pyth contract and implements Chainlink's `AggregatorV3Interface`. -One important difference between Pyth and Chainlink is that the Pyth contract holds data for all price feeds, whereas Chainlink has separate instances of `AggregatorV3Interface` for each feed. +One important difference between Pyth and Chainlink is that the Pyth contract holds data for all price feeds; in contrast, Chainlink has separate instances of `AggregatorV3Interface` for each feed. The adapter contract resolves this discrepancy by wrapping a single Pyth price feed. Users should deploy an instance of this adapter for every required price feed, then point their existing app to the addresses of the deployed adapter contracts.