Skip to content

Commit 7e6e7dd

Browse files
authored
Add Scrollycode (#72)
* Update open-in-llm.tsx * add scrollycode
1 parent fb78564 commit 7e6e7dd

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

content/docs/tutorial/4-component.mdx

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ description: evm-price-oracle component walkthrough
44
---
55
import { HoverContainer } from "@/components/hover-container";
66
import { Callout } from 'fumadocs-ui/components/callout';
7+
import { Scrollycoding } from "@/components/scrollycoding";
78
import { link } from "@/components/link.tsx";
89

910
The core logic of the price oracle in this example is located in the [`/evm-price-oracle/src/lib.rs` file](https://github.com/Lay3rLabs/wavs-foundry-template/tree/main/components/evm-price-oracle/src/lib.rs). Scroll down to follow a walkthrough of the code for the oracle component.
1011

11-
## trigger.rs
12+
<HoverContainer>
13+
<Scrollycoding>
1214

13-
The [trigger.rs](https://github.com/Lay3rLabs/wavs-foundry-template/tree/main/components/evm-price-oracle/src/trigger.rs) file handles the decoding of incoming trigger data from the trigger event emitted by the trigger contract. The component uses `decode_event_log_data!()` from the [wavs-wasi-utils crate](https://docs.rs/wavs-wasi-utils/latest/wavs_wasi_utils/) to decode the event log data and prepares it for processing within the WAVS component. Trigger.rs handles both ABI encoded data for trigger and submission data and raw data for local testing. For more information on different trigger types, visit the [Triggers page](../handbook/triggers). To learn more about trigger input handling, visit the [Component page](../handbook/components/component#trigger-inputs).
15+
## !!steps trigger.rs
1416

15-
```rust trigger.rs
17+
The [trigger.rs](https://github.com/Lay3rLabs/wavs-foundry-template/tree/main/components/evm-price-oracle/src/trigger.rs) file handles the decoding of incoming trigger data from the trigger event emitted by the trigger contract. The component uses the `decode_event_log_data!()` macro from the [wavs-wasi-utils crate](https://docs.rs/wavs-wasi-utils/latest/wavs_wasi_utils/) to decode the event log data and prepares it for processing within the WAVS component. The `Destination` enum handles both ABI encoded data for trigger and submission data and raw data for local testing. For more information on different trigger types, visit the [Triggers page](../handbook/triggers). To learn more about trigger input handling, visit the [Component page](../handbook/components/component#trigger-inputs).
18+
19+
```rust ! trigger.rs
1620
use crate::bindings::wavs::worker::layer_types::{
1721
TriggerData, TriggerDataEvmContractEvent, WasmResponse,
1822
};
@@ -73,11 +77,12 @@ pub mod solidity {
7377

7478
Visit the [Blockchain interactions page](../handbook/components/blockchain-interactions) for more information on the `sol!` macro and how to use it to generate Rust types from Solidity interfaces.
7579

76-
## Oracle component logic
80+
## !!steps Oracle component logic
7781

7882
The [`lib.rs`](https://github.com/Lay3rLabs/wavs-foundry-template/tree/main/components/evm-price-oracle/src/lib.rs) file contains the main component logic for the oracle. The first section of the code imports the required modules for requests, serialization, and bindings, defines the component struct, and exports the component for execution within the WAVS runtime.
7983

80-
```rust lib.rs
84+
```rust ! lib.rs
85+
// !focus(1:13)
8186
mod trigger;
8287
use trigger::{decode_trigger_event, encode_trigger_output, Destination};
8388
use wavs_wasi_utils::{
@@ -104,13 +109,14 @@ The `run` function:
104109
3. Fetches current price data from CoinMarketCap
105110
4. Returns the encoded response based on the destination
106111

107-
```rust lib.rs
108-
112+
```rust ! lib.rs
109113
struct Component;
110114
export!(Component with_types_in bindings);
111115

116+
// !focus(1:35)
112117
impl Guest for Component {
113118

119+
// !hover run_function
114120
fn run(action: TriggerAction) -> std::result::Result<Option<WasmResponse>, String> {
115121
let (trigger_id, req, dest) =
116122
decode_trigger_event(action.data).map_err(|e| e.to_string())?;
@@ -150,11 +156,12 @@ impl Guest for Component {
150156

151157
Visit the [Component page](../handbook/components/component) for more information on the `run` function and the main requirements for component structure.
152158

153-
## Fetching price data
159+
## !!steps Fetching price data
154160

155161
The `get_price_feed` function is responsible for fetching price data from CoinMarketCap's API. It takes the cryptocurrency ID passed from the trigger as input and returns a structured `PriceFeedData` containing the symbol, current price in USD, and server timestamp. For more information on making network requests in WAVS components, visit the [Network Requests page](../handbook/components/network-requests).
156162

157-
```rust lib.rs
163+
```rust ! lib.rs
164+
// !focus(1:30)
158165
async fn get_price_feed(id: u64) -> Result<PriceFeedData, String> {
159166
let url = format!(
160167
"https://api.coinmarketcap.com/data-api/v3/cryptocurrency/detail?id={}&range=1h",
@@ -184,11 +191,11 @@ async fn get_price_feed(id: u64) -> Result<PriceFeedData, String> {
184191
}
185192
```
186193

187-
## Handling the response
194+
## !!steps Handling the response
188195

189196
The processed price data is returned as a `WasmResponse` which contains the response payload. The response is formatted differently based on the destination.
190197

191-
```rust lib.rs
198+
```rust ! lib.rs
192199
let output = match dest {
193200
Destination::Ethereum => Some(encode_trigger_output(trigger_id, &res)),
194201
Destination::CliOutput => Some(WasmResponse { payload: res.into(), ordering: None }),
@@ -200,7 +207,7 @@ Ok(output)
200207

201208
In `trigger.rs`, the `WasmResponse` struct is used to standardize the format of data returned from components. The `payload` field contains the processed data from the component.
202209

203-
```rust trigger.rs
210+
```rust ! trigger.rs
204211
pub fn encode_trigger_output(trigger_id: u64, output: impl AsRef<[u8]>) -> WasmResponse {
205212
WasmResponse {
206213
payload: solidity::DataWithId {
@@ -217,6 +224,9 @@ For more information on component outputs, visit the [Component page](../handboo
217224

218225
The Service handbook contains more detailed information on each part of developing services and components. Visit the [Service handbook overview](../handbook/overview) to learn more.
219226

227+
</Scrollycoding>
228+
</HoverContainer>
229+
220230
## Next steps
221231

222232
Continue to the [next section](./5-build) to learn how to build and test your component.

0 commit comments

Comments
 (0)