1- import { TriggerAction , WasmResponse } from "./out/wavs:worker@0.4.0" ;
1+ import { TriggerAction , WasmResponse , } from "./out/wavs:worker@0.4.0" ;
2+ import { TriggerSource , TriggerSourceManual } from "./out/interfaces/wavs-worker-layer-types" ;
23import { decodeTriggerEvent , encodeOutput , Destination } from "./trigger" ;
4+ import { AbiCoder } from "ethers" ;
35
46async function run ( triggerAction : TriggerAction ) : Promise < WasmResponse > {
57 let event = decodeTriggerEvent ( triggerAction . data ) ;
68 let triggerId = event [ 0 ] . triggerId ;
79
8- let result = await compute ( event [ 0 ] . data ) ;
9-
10-
10+ let num = processInput ( event [ 0 ] . data , triggerAction . config . triggerSource ) ;
11+ let result = await compute ( num ) ;
1112
1213 switch ( event [ 1 ] ) {
1314 case Destination . Cli :
@@ -29,15 +30,56 @@ async function run(triggerAction: TriggerAction): Promise<WasmResponse> {
2930 ) ;
3031}
3132
32- async function compute ( input : Uint8Array ) : Promise < Uint8Array > {
33- const num = new TextDecoder ( ) . decode ( input ) ;
34-
35- const priceFeed = await fetchCryptoPrice ( parseInt ( num ) ) ;
33+ async function compute ( num : number ) : Promise < Uint8Array > {
34+ const priceFeed = await fetchCryptoPrice ( num ) ;
3635 const priceJson = priceFeedToJson ( priceFeed ) ;
3736
3837 return new TextEncoder ( ) . encode ( priceJson ) ;
3938}
4039
40+ function processInput ( input : Uint8Array , triggerSource : { tag : string } ) : number {
41+ // Prepare the input data based on trigger type
42+ const processedInput = prepareInputData ( input , triggerSource . tag ) ;
43+
44+ // Single ABI decoding step
45+ const abiCoder = new AbiCoder ( ) ;
46+ const res = abiCoder . decode ( [ "string" ] , processedInput ) ;
47+ const decodedString = res [ 0 ] as string ;
48+
49+ console . log ( "Decoded input:" , decodedString , "triggerSource.tag:" , triggerSource . tag ) ;
50+
51+ // Validate the decoded string is a valid number
52+ const num = decodedString . trim ( ) ;
53+ if ( isNaN ( parseInt ( num ) ) ) {
54+ throw new Error ( `Input is not a valid number: ${ num } ` ) ;
55+ }
56+
57+ return parseInt ( num ) ; // Return the validated number
58+ }
59+
60+
61+ function prepareInputData ( input : Uint8Array , triggerTag : string ) : Uint8Array {
62+ if ( triggerTag === "manual" ) {
63+ return input ; // Use input directly for manual triggers
64+ }
65+
66+ // For evm-contract-event: handle potential hex string conversion
67+ try {
68+ const inputStr = new TextDecoder ( ) . decode ( input ) ;
69+ if ( ! inputStr . startsWith ( "0x" ) ) {
70+ throw new Error ( "Input is not a valid hex string: " + inputStr ) ;
71+ }
72+
73+ // Convert hex string to bytes
74+ const hexString = inputStr . slice ( 2 ) ; // Remove "0x" prefix
75+ return new Uint8Array (
76+ hexString . match ( / .{ 1 , 2 } / g) ! . map ( byte => parseInt ( byte , 16 ) )
77+ ) ;
78+ } catch {
79+ return input ; // If UTF-8 decode fails, assume it's already binary
80+ }
81+ }
82+
4183// ======================== CMC ========================
4284
4385// Define the types for the CMC API response
0 commit comments