@@ -90,25 +90,26 @@ function callResultToScriptResult(callResult: CallResult): ScriptResult {
9090 return scriptResult ;
9191}
9292
93+ type DecodeCallResultParams < TResult > = {
94+ callResult : CallResult ;
95+ scriptResultDecoder : ( scriptResult : ScriptResult ) => TResult ;
96+ logs ?: DecodedLogs [ 'logs' ] ;
97+ groupedLogs ?: DecodedLogs [ 'groupedLogs' ] ;
98+ abis ?: JsonAbisFromAllCalls ;
99+ } ;
100+
93101/**
94- * Decodes a CallResult using the provided decoder function .
102+ * Decodes a CallResult using the provided options .
95103 *
96- * @param callResult - The CallResult to decode.
97- * @param decoder - The decoding function to apply on the ScriptResult.
98- * @param logs - Optional logs associated with the decoding.
104+ * @param params - The options to decode the CallResult.
99105 * @returns The decoded result.
100106 * @throws Throws an error if decoding fails.
101107 */
102- export function decodeCallResult < TResult > (
103- callResult : CallResult ,
104- decoder : ( scriptResult : ScriptResult ) => TResult ,
105- logs : DecodedLogs [ 'logs' ] = [ ] ,
106- groupedLogs : DecodedLogs [ 'groupedLogs' ] = { } ,
107- abis ?: JsonAbisFromAllCalls
108- ) : TResult {
108+ export function decodeCallResult < TResult > ( params : DecodeCallResultParams < TResult > ) : TResult {
109+ const { callResult, scriptResultDecoder, logs = [ ] , groupedLogs = { } , abis } = params ;
109110 try {
110111 const scriptResult = callResultToScriptResult ( callResult ) ;
111- return decoder ( scriptResult ) ;
112+ return scriptResultDecoder ( scriptResult ) ;
112113 } catch ( error ) {
113114 if ( ( < FuelError > error ) . code === ErrorCode . SCRIPT_REVERTED ) {
114115 const statusReason = ( < DryRunFailureStatusFragment > callResult ?. dryRunStatus ) ?. reason ;
@@ -125,24 +126,27 @@ export function decodeCallResult<TResult>(
125126 }
126127}
127128
129+ export type CallResultToInvocationResultParams = {
130+ callResult : CallResult ;
131+ call : CallConfig ;
132+ logs ?: DecodedLogs < unknown > [ 'logs' ] ;
133+ groupedLogs ?: DecodedLogs < unknown > [ 'groupedLogs' ] ;
134+ abis ?: JsonAbisFromAllCalls ;
135+ } ;
136+
128137/**
129- * Converts a CallResult to an invocation result based on the provided call configuration .
138+ * Decodes a CallResult using the provided options .
130139 *
131- * @param callResult - The CallResult from the script call.
132- * @param call - The call configuration.
133- * @param logs - Optional logs associated with the decoding.
140+ * @param params - The parameters to decode the CallResult.
134141 * @returns The decoded invocation result.
135142 */
136143export function callResultToInvocationResult < TReturn > (
137- callResult : CallResult ,
138- call : CallConfig ,
139- logs ?: DecodedLogs < unknown > [ 'logs' ] ,
140- groupedLogs ?: DecodedLogs < unknown > [ 'groupedLogs' ] ,
141- abis ?: JsonAbisFromAllCalls
144+ params : CallResultToInvocationResultParams
142145) : TReturn {
143- return decodeCallResult (
146+ const { callResult, call, logs = [ ] , groupedLogs = { } , abis } = params ;
147+ return decodeCallResult ( {
144148 callResult,
145- ( scriptResult : ScriptResult ) => {
149+ scriptResultDecoder : ( scriptResult : ScriptResult ) => {
146150 if ( scriptResult . returnReceipt . type === ReceiptType . Revert ) {
147151 throw new FuelError (
148152 ErrorCode . SCRIPT_REVERTED ,
@@ -178,8 +182,8 @@ export function callResultToInvocationResult<TReturn>(
178182 } ,
179183 logs,
180184 groupedLogs,
181- abis
182- ) ;
185+ abis,
186+ } ) ;
183187}
184188
185189export type EncodedScriptCall = Uint8Array | { data : Uint8Array ; script : Uint8Array } ;
@@ -266,11 +270,58 @@ export class ScriptRequest<TData = void, TResult = void> {
266270 /**
267271 * Decodes the result of a script call.
268272 *
269- * @param callResult - The CallResult from the script call.
273+ * @param callResultOrParams - The CallResult from the script call.
270274 * @param logs - Optional logs associated with the decoding.
275+ * @param groupedLogs - Optional grouped logs associated with the decoding.
276+ * @param abis - Optional abis associated with the decoding.
277+ * @returns The decoded result.
278+ *
279+ * @deprecated Use the object-based approach for parameters instead.
280+ */
281+ decodeCallResult (
282+ callResult : CallResult ,
283+ logs ?: Array < any > ,
284+ groupedLogs ?: DecodedLogs [ 'groupedLogs' ] ,
285+ abis ?: JsonAbisFromAllCalls
286+ ) : TResult ;
287+
288+ /**
289+ * Decodes the result of a script call.
290+ *
291+ * @param params - The parameters to decode the CallResult.
271292 * @returns The decoded result.
272293 */
273- decodeCallResult ( callResult : CallResult , logs : Array < any > = [ ] ) : TResult {
274- return decodeCallResult ( callResult , this . scriptResultDecoder , logs ) ;
294+ decodeCallResult ( params : Omit < DecodeCallResultParams < TResult > , 'scriptResultDecoder' > ) : TResult ;
295+
296+ decodeCallResult (
297+ callResultOrParams : CallResult | Omit < DecodeCallResultParams < TResult > , 'scriptResultDecoder' > ,
298+ _logs ?: Array < any > ,
299+ _groupedLogs ?: DecodedLogs [ 'groupedLogs' ] ,
300+ _abis ?: JsonAbisFromAllCalls
301+ ) : TResult {
302+ let callResult : CallResult ;
303+ let logs : Array < any > ;
304+ let groupedLogs : DecodedLogs [ 'groupedLogs' ] ;
305+ let abis : JsonAbisFromAllCalls | undefined ;
306+
307+ if ( typeof callResultOrParams === 'object' && 'callResult' in callResultOrParams ) {
308+ callResult = callResultOrParams . callResult as CallResult ;
309+ logs = callResultOrParams . logs ?? [ ] ;
310+ groupedLogs = callResultOrParams . groupedLogs ?? { } ;
311+ abis = callResultOrParams . abis ;
312+ } else {
313+ callResult = callResultOrParams as CallResult ;
314+ logs = _logs ?? [ ] ;
315+ groupedLogs = _groupedLogs ?? { } ;
316+ abis = _abis ;
317+ }
318+
319+ return decodeCallResult ( {
320+ callResult,
321+ scriptResultDecoder : this . scriptResultDecoder ,
322+ logs,
323+ groupedLogs,
324+ abis,
325+ } ) ;
275326 }
276327}
0 commit comments