Using oracles
Use onchain price oracles to get accurate point-in-time USD prices of tokens, without needing to use any offchain price APIs.
Without Shadow, augmenting blockchain data with point-in-time accurate prices requires complex data pipelines and integrations with third-party price feed APIs. Shadow drastically simplifies this by allowing you to leverage existing oracle contracts, in <100 lines of code.
In this example, we’ll be updating the 1inch Aggregator contract to emit a shadow event called OrderFilledDetails
that contains additional metadata of a trade, including the point-in-time USD price of the trade. We’ll be fetching the point-in-time prices via a Chainlink oracle.
Step 1 – Edit the contract
Open the example on the Shadow Playground: https://app.shadow.xyz/demo?example=one_inch_oracle
This will open the playground editor at the 1inch Aggregation Router v5 contract on Ethereum at 0x1111111254eeb25477b68fb85ed929f73a960582
(as of Jul 2024).
At L4302, we’ve defined the OrderFilledDetails
event and a helper OrderDetails
struct:
At L4567, we call a new function called getOrderAmountUsd
and emit the OrderFilledDetails
event:
At the bottom of the contract at L4667, we define the getOrderAmountUsd
function and two other helper functions:
Notice that we also pasted Chainlink’s AggregatorV3Interface
into this file. This allows us to call the Chainlink oracle contract via its interface. You’ll have to do this any time you introduce shadow changes that interact with a contract interface that it doesn’t currently interact with.
Step 2 – Test run your changes
Click “Compile” > “Test Run” in the top right corner, and paste in this transaction hash 0x045bd6741a5ea38dc6da6b324c2446552bc0e38f6b53b79ada7fc53604135f75
(or simulate any other order filled transaction).
You should see your custom OrderFilledDetails
event in the output!
Step 3 – Deploy your changes
If you want to apply these changes on your shadow fork, click the button on the top right corner that says “Apply to your shadow fork”.
This will take you to the editor for your shadow fork, where you can deploy the changes by hitting “Compile > Deploy”
Last updated