Skip to content

Commit e146785

Browse files
committed
fix: readonly call in pipe console
1 parent 986a64d commit e146785

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

docs/app.js

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

609639
async 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({

docs/styles.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ button:hover {
147147
.terminal {
148148
margin: 0;
149149
padding: 0.85rem;
150+
max-width: 100%;
151+
min-width: 0;
150152
border-radius: 0.62rem;
151153
background: #15241a;
152154
color: #ccf3d7;
@@ -158,6 +160,12 @@ button:hover {
158160
line-height: 1.36;
159161
}
160162

163+
#log {
164+
white-space: pre-wrap;
165+
overflow-wrap: anywhere;
166+
word-break: break-word;
167+
}
168+
161169
#wallet-status.error {
162170
color: var(--danger);
163171
}

0 commit comments

Comments
 (0)