@@ -581,29 +581,59 @@ async function handleConnectWallet() {
581581 }
582582}
583583
584- async function fetchReadOnly ( functionName , functionArgs , sender ) {
585- const { contractAddress, contractName } = parseContractId ( ) ;
586- const apiBase = getStacksApiBase ( ) ;
587- const url = `${ apiBase } /v2/contracts/call-read/${ contractAddress } /${ contractName } /${ functionName } ` ;
584+ function toClarityPrincipalLiteral ( principal ) {
585+ return `'${ normalizedText ( principal ) } ` ;
586+ }
587+
588+ function toOptionalPrincipalLiteral ( principalOrNull ) {
589+ const text = normalizedText ( principalOrNull ) ;
590+ return text ? `(some '${ text } )` : "none" ;
591+ }
592+
593+ async function postReadOnlyCall ( url , sender , encodedArgs ) {
588594 const response = await fetch ( url , {
589595 method : "POST" ,
590596 headers : { "content-type" : "application/json" } ,
591597 body : JSON . stringify ( {
592598 sender,
593- arguments : functionArgs . map ( ( cv ) => cvHex ( cv ) ) ,
599+ arguments : encodedArgs ,
594600 } ) ,
595601 } ) ;
596602 const body = await response . json ( ) . catch ( ( ) => null ) ;
597- if ( ! response . ok ) {
598- throw new Error ( `Read-only call failed (${ response . status } )` ) ;
599- }
603+ return { response, body } ;
604+ }
605+
606+ function readOnlyErrorMessage ( status , body ) {
600607 if ( ! body || typeof body !== "object" ) {
601- throw new Error ( " Read-only response was not JSON" ) ;
608+ return ` Read-only call failed ( ${ status } )` ;
602609 }
603610 if ( body . okay === false ) {
604- throw new Error ( `Read-only call returned error: ${ body . cause || "unknown" } ` ) ;
611+ return `Read-only call returned error: ${ body . cause || "unknown" } ` ;
605612 }
606- return body . result ;
613+ return `Read-only call failed (${ status } )` ;
614+ }
615+
616+ async function fetchReadOnly ( functionName , functionArgs , sender , options = { } ) {
617+ const { contractAddress, contractName } = parseContractId ( ) ;
618+ const apiBase = getStacksApiBase ( ) ;
619+ const url = `${ apiBase } /v2/contracts/call-read/${ contractAddress } /${ contractName } /${ functionName } ` ;
620+ const hexArgs = functionArgs . map ( ( cv ) => cvHex ( cv ) ) ;
621+ const firstAttempt = await postReadOnlyCall ( url , sender , hexArgs ) ;
622+ if ( firstAttempt . response . ok && firstAttempt . body && firstAttempt . body . okay !== false ) {
623+ return firstAttempt . body . result ;
624+ }
625+
626+ const clarityArgs = Array . isArray ( options . clarityArgs ) ? options . clarityArgs : null ;
627+ if ( clarityArgs && clarityArgs . length === functionArgs . length ) {
628+ const secondAttempt = await postReadOnlyCall ( url , sender , clarityArgs ) ;
629+ if ( secondAttempt . response . ok && secondAttempt . body && secondAttempt . body . okay !== false ) {
630+ appendLog ( "Read-only call retried with Clarity literal arguments." ) ;
631+ return secondAttempt . body . result ;
632+ }
633+ throw new Error ( readOnlyErrorMessage ( secondAttempt . response . status , secondAttempt . body ) ) ;
634+ }
635+
636+ throw new Error ( readOnlyErrorMessage ( firstAttempt . response . status , firstAttempt . body ) ) ;
607637}
608638
609639async function handleGetPipe ( ) {
@@ -616,12 +646,18 @@ async function handleGetPipe() {
616646 "For Principal" ,
617647 elements . forPrincipal . value || state . connectedAddress ,
618648 ) ;
619- const { cv : tokenCV } = parseOptionalTokenCV ( ) ;
649+ const { cv : tokenCV , tokenText } = parseOptionalTokenCV ( ) ;
620650
621651 const resultHex = await fetchReadOnly (
622652 "get-pipe" ,
623653 [ tokenCV , Cl . principal ( withPrincipal ) ] ,
624654 forPrincipal ,
655+ {
656+ clarityArgs : [
657+ toOptionalPrincipalLiteral ( tokenText ) ,
658+ toClarityPrincipalLiteral ( withPrincipal ) ,
659+ ] ,
660+ } ,
625661 ) ;
626662 const decoded = decodeReadOnlyResult ( resultHex ) ;
627663 setOutput ( {
0 commit comments