Skip to content

Commit f1c0a9a

Browse files
committed
working wasi-exec for rust component
1 parent 2bdbaf2 commit f1c0a9a

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ wasi-build:
3535
wasi-exec: pull-image
3636
@$(WAVS_CMD) exec --log-level=info --data /data/.docker --home /data \
3737
--component "/data/compiled/$(COMPONENT_FILENAME)" \
38-
--input `cast format-bytes32-string $(TRIGGER_DATA_INPUT)`
38+
--input `cast abi-encode "f(string)" "$(TRIGGER_DATA_INPUT)"`
3939

4040
## clean: cleaning the project files
4141
clean: clean-docker

components/evm-price-oracle/src/lib.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
mod trigger;
2+
use alloy_sol_types::{SolType, SolValue};
23
use trigger::{decode_trigger_event, encode_trigger_output, Destination};
34
use wavs_wasi_utils::http::{fetch_json, http_request_get};
45
pub mod bindings;
56
use crate::bindings::{export, Guest, TriggerAction, WasmResponse};
67
use serde::{Deserialize, Serialize};
78
use wstd::{http::HeaderValue, runtime::block_on};
9+
// import solidity from triggers
10+
use trigger::solidity;
811

912
struct Component;
1013
export!(Component with_types_in bindings);
@@ -30,11 +33,24 @@ impl Guest for Component {
3033
let (trigger_id, req, dest) =
3134
decode_trigger_event(action.data).map_err(|e| e.to_string())?;
3235

33-
// Convert bytes to string and parse first char as u64
34-
let input = std::str::from_utf8(&req).map_err(|e| e.to_string())?;
35-
println!("input id: {}", input);
36-
37-
let id = input.chars().next().ok_or("Empty input")?;
36+
// Decode the string using proper ABI decoding
37+
let string_data: String =
38+
if let Ok(decoded) = <solidity::DataWithId as SolType>::abi_decode(&req) {
39+
// If it has a function selector (from cast abi-encode "f(string)" format)
40+
decoded.data.to_string()
41+
} else {
42+
// Fallback: try decoding just as a string parameter (no function selector)
43+
match String::abi_decode(&req) {
44+
Ok(s) => s,
45+
Err(e) => return Err(format!("Failed to decode input as ABI string: {}", e)),
46+
}
47+
// alloy_primitives::Bytes::from(s.into_bytes())
48+
};
49+
50+
println!("Decoded string input: {}", string_data);
51+
52+
// Parse the first character as a hex digit for the ID
53+
let id = string_data.chars().next().ok_or("Empty input")?;
3854
let id = id.to_digit(16).ok_or("Invalid hex digit")? as u64;
3955

4056
let res = block_on(async move {

components/evm-price-oracle/src/trigger.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ pub fn decode_trigger_event(trigger_data: TriggerData) -> Result<(u64, Vec<u8>,
3535
match trigger_data {
3636
TriggerData::EvmContractEvent(TriggerDataEvmContractEvent { log, .. }) => {
3737
let event: solidity::NewTrigger = decode_event_log_data!(log)?;
38-
let trigger_info = solidity::TriggerInfo::abi_decode(&event._triggerInfo)?;
38+
// let trigger_info = solidity::TriggerInfo::abi_decode(&event._triggerInfo)?;
39+
let trigger_info =
40+
<solidity::TriggerInfo as SolValue>::abi_decode(&event._triggerInfo)?;
3941
Ok((trigger_info.triggerId, trigger_info.data.to_vec(), Destination::Ethereum))
4042
}
4143
TriggerData::Raw(data) => Ok((0, data.clone(), Destination::CliOutput)),
@@ -75,7 +77,7 @@ pub fn encode_trigger_output(trigger_id: u64, output: impl AsRef<[u8]>) -> WasmR
7577
/// Documentation:
7678
/// - <https://docs.rs/alloy-sol-macro/latest/alloy_sol_macro/macro.sol.html>
7779
/// (You can also just sol! arbitrary solidity types like `event` or `struct` too)
78-
mod solidity {
80+
pub mod solidity {
7981
use alloy_sol_macro::sol;
8082
pub use ITypes::*;
8183

0 commit comments

Comments
 (0)