|
| 1 | +## Chainlink Migration Example |
| 2 | + |
| 3 | +This example demonstrates how to deploy a Chainlink-compatible application to Pyth Network price feeds. |
| 4 | +The application `src/ChainlinkApp.sol` is designed to use Chainlink price feeds. |
| 5 | +The script `script/PythAggregatorV3Deployment.sol` deploys this application with the [`PythAggregatorV3`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/sdk/solidity/PythAggregatorV3.sol) adapter contract, such that it uses Pyth price feeds. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +This example uses [Foundry](https://book.getfoundry.sh/getting-started/installation), `npm` and `jq`. |
| 10 | +Please install these tools if you don't have them already. |
| 11 | + |
| 12 | +Install the solidity dependencies by running: |
| 13 | + |
| 14 | +```bash copy |
| 15 | +npm install |
| 16 | +forge install foundry-rs/ [email protected] --no-git --no-commit |
| 17 | +``` |
| 18 | + |
| 19 | +## Deploy |
| 20 | + |
| 21 | +To run the deployment script, you will need a blockchain with a Pyth contract, a wallet with some tokens and an RPC node. |
| 22 | +Export the value of your wallet's private key, the RPC URL, and the [Pyth contract address](https://docs.pyth.network/price-feeds/contract-addresses/evm) as environment variables: |
| 23 | + |
| 24 | +```bash copy |
| 25 | +export PRIVATE_KEY=0x... |
| 26 | +export RPC_URL=https://sepolia.optimism.io/ |
| 27 | +export PYTH_ADDRESS=0x0708325268dF9F66270F1401206434524814508b |
| 28 | +``` |
| 29 | + |
| 30 | +Then, deploy the contracts by running: |
| 31 | + |
| 32 | +```bash copy |
| 33 | +forge script script/PythAggregatorV3Deployment.s.sol --rpc-url $RPC_URL --broadcast |
| 34 | +``` |
| 35 | + |
| 36 | +This command will print something like: |
| 37 | + |
| 38 | +``` |
| 39 | +##### optimism-sepolia |
| 40 | +✅ [Success]Hash: 0xbca3b74534042646899091ee6298e42b491e565dc676691291b144d59974fe93 |
| 41 | +Contract Address: 0xbac361910F2a8cEB96c9207302acb692B4708a8b |
| 42 | +Block: 10873047 |
| 43 | +Paid: 0.001974369167163242 ETH (658123 gas * 3.000000254 gwei) |
| 44 | +
|
| 45 | +
|
| 46 | +##### optimism-sepolia |
| 47 | +✅ [Success]Hash: 0x45f41d30c1e971d3e2bbc6de4586a5da3633e5051218808a475e75938e78e66b |
| 48 | +Contract Address: 0x481F16240c55631c78fBF5B2B4191Aa98087C355 |
| 49 | +Block: 10873047 |
| 50 | +Paid: 0.001974369167163242 ETH (658123 gas * 3.000000254 gwei) |
| 51 | +
|
| 52 | +
|
| 53 | +##### optimism-sepolia |
| 54 | +✅ [Success]Hash: 0xfea9cea348ea62d66305ca46181b4a261afff6ea744a50ba8d6a5766b2fe254f |
| 55 | +Contract Address: 0x1e8ad7B210EA49e2B910C0D74fa259f83f76D64b |
| 56 | +Block: 10873047 |
| 57 | +Paid: 0.000676959057315862 ETH (225653 gas * 3.000000254 gwei) |
| 58 | +``` |
| 59 | + |
| 60 | +The first two contracts are instances of `PythAggregatorV3` and the final address is the `ChainlinkApp` contract. |
| 61 | +Set the address of the deployed `ChainlinkApp` contract as an environment variable for later reference: |
| 62 | + |
| 63 | +``` |
| 64 | +export CONTRACT_ADDR=0x7E1Bf7047200dB58A0eD8002552e1243B0DB4bfC |
| 65 | +``` |
| 66 | + |
| 67 | +## Try it out |
| 68 | + |
| 69 | +The repo provides two scripts for interacting with the deployed application. |
| 70 | + |
| 71 | +First, `script/UpdatePrice.s.sol` updates the on-chain Pyth price. |
| 72 | +Pyth is a pull oracle, so it has permissionless price updates. |
| 73 | +Retrieve a price update and post it on-chain by running the following commands: |
| 74 | + |
| 75 | +```bash |
| 76 | +curl 'https://hermes.pyth.network/v2/updates/price/latest?ids[]=0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace&ids[]=0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d' | jq '.binary.data[0]' -r > price_update.txt |
| 77 | +export PRICE_UPDATE="0x`cat price_update.txt`" |
| 78 | +forge script script/UpdatePrice.s.sol --rpc-url $RPC_URL --broadcast |
| 79 | +``` |
| 80 | + |
| 81 | +Please see [Schedule Price Updates](https://docs.pyth.network/price-feeds/schedule-price-updates) for ways to schedule on-chain updates to Pyth price feeds. |
| 82 | + |
| 83 | +Second, `script/GetEthSolPrice.s.sol` interacts with a method on the `ChainlinkApp` contract: |
| 84 | + |
| 85 | +```bash |
| 86 | +forge script script/GetEthSolPrice.s.sol --rpc-url $RPC_URL --broadcast |
| 87 | +``` |
| 88 | + |
0 commit comments