{"version":3,"file":"inspect.mjs","names":[],"sources":["../../../src/predict/reads/inspect.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\nimport type { ClientWithCoreApi } from '@mysten/sui/client';\nimport type { Transaction } from '@mysten/sui/transactions';\nimport { normalizeSuiAddress } from '@mysten/sui/utils';\nimport { decodeMoveAbort, type MoveAbortError } from '../errors.js';\n\n// The one network seam every read sits on: any client exposing the core API\n// (`client.core.simulateTransaction`, plus the object reads positions.ts needs).\n// Callers pass a real SuiGrpcClient/SuiGraphQLClient; tests inject a structural mock.\nexport type ReadClient = ClientWithCoreApi;\n\n// The abort payload a failed simulate result carries: the structured gRPC `MoveAbort`\n// arm (`{ abortCode, location: { module }, cleverError: { constantName } }`) plus an\n// optional display `message`. Read structurally so mocks and API-shape drift satisfy it.\ntype SimAbortError = MoveAbortError & { message?: string };\n\n// Run a read-only PTB through the gRPC `simulateTransaction` (the devInspect\n// replacement) and return each command's BCS return values, indexed\n// [commandIndex][returnValueIndex].\n//\n// `checksEnabled: false` disables validation so we may inspect non-entry public\n// funs — see SimulateTransactionOptions.checksEnabled in\n// node_modules/@mysten/sui/dist/client/types.d.mts:385-396. Per-command outputs\n// live under `commandResults[i].returnValues[j].bcs` — see\n// SimulateTransactionResult / CommandResult / CommandOutput in that same file at\n// lines 309-348. `commandResults` is only populated when `include.commandResults`\n// is set, so we request it explicitly.\n//\n// A sender is required to simulate; callers rarely have one for a pure read, so we\n// default to the zero address. On abort the result's `$kind` is `FailedTransaction`;\n// we decode the carried abort into a typed PredictMoveError, falling back to a plain\n// Error with the failure message when it isn't a decodable Move abort.\n// Shared failure handling: decode the abort carried by a FailedTransaction\n// into a typed PredictMoveError, else throw a plain Error.\nfunction throwSimFailure(failed: unknown): never {\n\tconst status = (failed as { status?: { success: boolean; error?: SimAbortError } })?.status;\n\tconst error = status && status.success === false ? status.error : undefined;\n\tconst decoded = error ? decodeMoveAbort(error) : null;\n\tif (decoded) throw decoded;\n\tthrow new Error(error?.message ?? 'simulateTransaction aborted (FailedTransaction)');\n}\n\n// Simulate a full transaction (typically one built by a tx.* builder) and return\n// the events it would emit — the quote path: dry-run the real action, decode the\n// receipt. Sender must be the acting owner so account auth resolves. Throws the\n// same typed errors the real execution would surface.\nexport async function simulateWithEvents(\n\tclient: ReadClient,\n\ttx: Transaction,\n\tsender: string,\n): Promise<{ eventType?: string; bcs?: Uint8Array | string }[]> {\n\ttx.setSender(sender);\n\tconst result = await client.core.simulateTransaction({\n\t\ttransaction: tx,\n\t\tchecksEnabled: false,\n\t\tinclude: { events: true },\n\t});\n\tif (result.$kind === 'FailedTransaction') throwSimFailure(result.FailedTransaction);\n\treturn (result.Transaction?.events ?? []) as { eventType?: string; bcs?: Uint8Array }[];\n}\n\nexport async function inspectReturns(\n\tclient: ReadClient,\n\ttx: Transaction,\n\tsender: string = normalizeSuiAddress('0x0'),\n): Promise<Uint8Array[][]> {\n\ttx.setSender(sender);\n\tconst result = await client.core.simulateTransaction({\n\t\ttransaction: tx,\n\t\tchecksEnabled: false,\n\t\tinclude: { commandResults: true },\n\t});\n\tif (result.$kind === 'FailedTransaction') throwSimFailure(result.FailedTransaction);\n\tconst commands = result.commandResults;\n\tif (!commands) throw new Error('simulateTransaction returned no commandResults');\n\treturn commands.map((c) => c.returnValues.map((rv) => rv.bcs));\n}\n"],"mappings":";;;;AAmCA,SAAS,gBAAgB,QAAwB;CAChD,MAAM,SAAU,QAAqE;CACrF,MAAM,QAAQ,UAAU,OAAO,YAAY,QAAQ,OAAO,QAAQ;CAClE,MAAM,UAAU,QAAQ,gBAAgB,MAAM,GAAG;AACjD,KAAI,QAAS,OAAM;AACnB,OAAM,IAAI,MAAM,OAAO,WAAW,kDAAkD;;AAOrF,eAAsB,mBACrB,QACA,IACA,QAC+D;AAC/D,IAAG,UAAU,OAAO;CACpB,MAAM,SAAS,MAAM,OAAO,KAAK,oBAAoB;EACpD,aAAa;EACb,eAAe;EACf,SAAS,EAAE,QAAQ,MAAM;EACzB,CAAC;AACF,KAAI,OAAO,UAAU,oBAAqB,iBAAgB,OAAO,kBAAkB;AACnF,QAAQ,OAAO,aAAa,UAAU,EAAE;;AAGzC,eAAsB,eACrB,QACA,IACA,SAAiB,oBAAoB,MAAM,EACjB;AAC1B,IAAG,UAAU,OAAO;CACpB,MAAM,SAAS,MAAM,OAAO,KAAK,oBAAoB;EACpD,aAAa;EACb,eAAe;EACf,SAAS,EAAE,gBAAgB,MAAM;EACjC,CAAC;AACF,KAAI,OAAO,UAAU,oBAAqB,iBAAgB,OAAO,kBAAkB;CACnF,MAAM,WAAW,OAAO;AACxB,KAAI,CAAC,SAAU,OAAM,IAAI,MAAM,iDAAiD;AAChF,QAAO,SAAS,KAAK,MAAM,EAAE,aAAa,KAAK,OAAO,GAAG,IAAI,CAAC"}