11package main
22
33import (
4- "encoding/hex"
54 "encoding/json"
65 "fmt"
76 "io"
@@ -26,7 +25,7 @@ func init() {
2625 wavs .Exports .Run = func (triggerAction wavs.TriggerAction ) types.TriggerResult {
2726 triggerID , requestInput , dest := decodeTriggerEvent (triggerAction .Data )
2827
29- result , err := compute (requestInput .Slice (), dest )
28+ result , err := compute (requestInput .Slice ())
3029 if err != nil {
3130 return cm.Err [types.TriggerResult ](err .Error ())
3231 }
@@ -37,7 +36,7 @@ func init() {
3736}
3837
3938// compute is the main function that computes the price of the crypto currency
40- func compute (input []uint8 , dest types. Destination ) ([]byte , error ) {
39+ func compute (input []uint8 ) ([]byte , error ) {
4140 // Input is now properly decoded by decodeTriggerEvent, so we can parse it directly
4241 inputStr := string (input )
4342 // Clean any remaining control characters and whitespace
@@ -48,7 +47,7 @@ func compute(input []uint8, dest types.Destination) ([]byte, error) {
4847 return - 1 // Remove control characters
4948 }, inputStr )
5049 cleanStr = strings .TrimSpace (cleanStr )
51-
50+
5251 fmt .Printf ("Compute input (cleaned): '%s'\n " , cleanStr )
5352
5453 id , err := strconv .Atoi (cleanStr )
@@ -86,74 +85,56 @@ func routeResult(triggerID uint64, result []byte, dest types.Destination) types.
8685
8786// decodeABIInput decodes ABI-encoded input data using go-eth library
8887func decodeABIInput (input []byte ) (string , error ) {
89- // First, convert the input bytes to a string to check if it's a hex string
90- inputStr := string (input )
88+ fmt .Printf ("Attempting ABI decode on %d bytes: %x\n " , len (input ), input )
9189
92- var hexData []byte
93- var err error
90+ // The data is just the parameters (string), not a full method call
91+ // Try to decode as a tuple containing a single string parameter
92+ inputTuple := abi .NewTupleType (abi.TupleTypeElem {
93+ Name : "data" ,
94+ Type : abi .NewStringType (),
95+ })
9496
95- // Check if it's a hex string (starts with "0x")
96- if strings .HasPrefix (inputStr , "0x" ) {
97- // Decode the hex string to bytes
98- hexData , err = hex .DecodeString (inputStr [2 :])
99- if err != nil {
100- return "" , fmt .Errorf ("failed to decode hex string: %w" , err )
101- }
102- } else {
103- // If it's not a hex string, assume the input is already binary data
104- hexData = input
97+ // Decode into a struct
98+ var result struct {
99+ Data string `abi:"data"`
100+ }
101+ err := abi .DecodeValue (inputTuple , input , & result )
102+ if err == nil {
103+ fmt .Printf ("Successfully decoded tuple as string: '%s'\n " , result .Data )
104+ return result .Data , nil
105105 }
106+ fmt .Printf ("Tuple decode failed: %v\n " , err )
106107
107- // Use go-eth library for generalized ABI decoding
108- // Try to decode as string first (most common case)
108+ // Fallback: try to decode as a simple string parameter directly
109109 stringType := abi .NewStringType ()
110- var result string
111- err = abi .DecodeValue (stringType , hexData , & result )
110+ var data string
111+ err = abi .DecodeValue (stringType , input , & data )
112112 if err == nil {
113- return result , nil
113+ fmt .Printf ("Successfully decoded as string: '%s'\n " , data )
114+ return data , nil
114115 }
116+ fmt .Printf ("String decode failed: %v\n " , err )
115117
116- // If string decoding fails, try other common types
117- // Try uint256 (common for numeric inputs)
118+ // Try uint256 as fallback
118119 uintType := abi .NewUintType (256 )
119120 var uintResult * big.Int
120- err = abi .DecodeValue (uintType , hexData , & uintResult )
121- if err == nil {
122- return uintResult .String (), nil
123- }
124-
125- // Try bytes (fallback for arbitrary data)
126- bytesType := abi .NewBytesType ()
127- var bytesResult []byte
128- err = abi .DecodeValue (bytesType , hexData , & bytesResult )
121+ err = abi .DecodeValue (uintType , input , & uintResult )
129122 if err == nil {
130- return string (bytesResult ), nil
123+ result := uintResult .String ()
124+ fmt .Printf ("Successfully decoded as uint256: '%s'\n " , result )
125+ return result , nil
131126 }
127+ fmt .Printf ("Uint256 decode failed: %v\n " , err )
132128
133- // If all ABI decoding attempts fail, return the raw input as string
134- return string (input ), nil
129+ return "" , fmt .Errorf ("all ABI decoding attempts failed" )
135130}
136131
137132// decodeTriggerEvent is the function that decodes the trigger event from the chain event to Go.
138133func decodeTriggerEvent (triggerAction trigger.TriggerData ) (trigger_id uint64 , req cm.List [uint8 ], dest types.Destination ) {
139134 // Handle CLI input case
140135 if triggerAction .Raw () != nil {
141136 raw := * triggerAction .Raw ()
142- fmt .Printf ("Raw input: %s\n " , string (raw .Slice ()))
143-
144- // For CLI input, just use the raw string directly (no ABI decoding needed)
145- // CLI inputs are simple strings like "1", "2", etc.
146- rawString := string (raw .Slice ())
147- // Remove null bytes and trim whitespace
148- cleanString := strings .ReplaceAll (rawString , "\x00 " , "" )
149- trimmedString := strings .TrimSpace (cleanString )
150- fmt .Printf ("CLI input (cleaned): %s\n " , trimmedString )
151-
152- // Convert the trimmed string back to bytes for processing
153- inputBytes := []byte (trimmedString )
154- inputList := cm .NewList (& inputBytes [0 ], len (inputBytes ))
155-
156- return 0 , inputList , types .CliOutput
137+ return decodeHexInput (raw .Slice (), types .CliOutput )
157138 }
158139
159140 // Handle Ethereum event case
@@ -162,26 +143,43 @@ func decodeTriggerEvent(triggerAction trigger.TriggerData) (trigger_id uint64, r
162143 panic ("triggerAction.EthContractEvent() is nil" )
163144 }
164145
165- // Use generalized ABI decoding for Ethereum event data
166- decodedString , err := decodeABIInput (ethEvent .Log .Data .Slice ())
167- if err != nil {
168- fmt .Printf ("Failed to decode Ethereum event ABI input: %v, using raw data\n " , err )
169- // Fallback to original method if generalized decoding fails
170- triggerInfo := types .DecodeTriggerInfo (ethEvent .Log .Data .Slice ())
171- fmt .Printf ("Trigger ID: %v\n " , triggerInfo .TriggerID )
172- fmt .Printf ("Creator: %s\n " , triggerInfo .Creator .String ())
173- fmt .Printf ("Input Data: %v\n " , string (triggerInfo .Data ))
174- return triggerInfo .TriggerID , cm .NewList (& triggerInfo .Data [0 ], len (triggerInfo .Data )), types .Ethereum
146+ // Always use generalized ABI decoding for hex data
147+ return decodeHexInput (ethEvent .Log .Data .Slice (), types .Ethereum )
148+ }
149+
150+ // decodeHexInput handles hex data decoding for both CLI and Ethereum inputs
151+ func decodeHexInput (input []byte , dest types.Destination ) (uint64 , cm.List [uint8 ], types.Destination ) {
152+ fmt .Printf ("Raw input: %s\n " , string (input ))
153+
154+ var decodedString string
155+
156+ if dest == types .Ethereum {
157+ // For Ethereum events, try ABI decoding first
158+ decoded , err := decodeABIInput (input )
159+ if err != nil {
160+ fmt .Printf ("ABI decode failed: %v, using raw input\n " , err )
161+ decodedString = string (input )
162+ } else {
163+ decodedString = decoded
164+ }
165+ } else {
166+ // For CLI input, just clean the raw input
167+ cleanString := strings .Map (func (r rune ) rune {
168+ if r >= 32 && r < 127 {
169+ return r
170+ }
171+ return - 1
172+ }, string (input ))
173+ decodedString = strings .TrimSpace (cleanString )
175174 }
176175
177- fmt .Printf ("Decoded Ethereum event input: %s\n " , decodedString )
176+ fmt .Printf ("Decoded input: %s\n " , decodedString )
178177
179- // For now, use trigger ID 0 and convert decoded string to bytes
180- // In a real implementation, you might need to extract trigger ID from the event differently
178+ // Convert decoded string to bytes for processing
181179 decodedBytes := []byte (decodedString )
182180 decodedList := cm .NewList (& decodedBytes [0 ], len (decodedBytes ))
183181
184- return 0 , decodedList , types . Ethereum
182+ return 0 , decodedList , dest
185183}
186184
187185// fetchCryptoPrice fetches the price of the crypto currency from the CoinMarketCap API by their ID.
0 commit comments