Skip to content

Commit a649389

Browse files
committed
feat: Improved function signature
1 parent 4fa966e commit a649389

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

packages/frontend/src/components/InvokeFunction.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ import { networkRpc } from "@/lib/web3";
1717
import { Server } from "@stellar/stellar-sdk/rpc";
1818
import { Networks, scValToNative } from "@stellar/stellar-sdk";
1919

20+
function createLogSingnature(method: FunctionSpec, args: any, result: any) {
21+
const strArgs = method.inputs.map((arg) => {
22+
const value = args[arg.name]?.value;
23+
return `${arg.name}: ${arg.value.type} = ${value}`;
24+
});
25+
const output = method.outputs.at(0);
26+
const signature =
27+
`fn ${method.name}
28+
${strArgs.join(", ")}
29+
result: ${output?.type} = ${result}
30+
`;
31+
return signature;
32+
}
33+
2034
function InvokeFunction({ method }: { method: FunctionSpec }) {
2135
const [isOpen, setIsOpen] = useState("");
2236
const [args, setArgs] = useState<Record<string, { type: string; value: string; subType: string }>>({});
@@ -38,7 +52,7 @@ function InvokeFunction({ method }: { method: FunctionSpec }) {
3852
const server = new Server(networkRpc[Networks.TESTNET]);
3953
const argsArray = Object.values(args);
4054
const data = {
41-
contractId: contractAddress || "CBT42OCEG6ECN74FRMI4CBXOAZWXCQNJE2UC3RNNB3VNFIUHULJCP242",
55+
contractId: contractAddress!,
4256
method: method.name,
4357
args: argsArray,
4458
};
@@ -63,7 +77,8 @@ function InvokeFunction({ method }: { method: FunctionSpec }) {
6377
logger.info(`TxId: ${result.hash}`);
6478
if (response.returnValue) {
6579
logger.info(`TX Result: ${scValToNative(response.returnValue)}`);
66-
setIsOpen(JSON.stringify(response.returnValue, null, 2));
80+
const logSignature = createLogSingnature(method, args, scValToNative(response.returnValue));
81+
setIsOpen(logSignature);
6782
}
6883
toast.success(`Function invoked successfully`);
6984
return response;
@@ -131,11 +146,11 @@ function InvokeFunction({ method }: { method: FunctionSpec }) {
131146
<Dialog open={Boolean(isOpen)} onOpenChange={(val) => setIsOpen(val ? isOpen : "")}>
132147
<DialogContent>
133148
<DialogHeader>
134-
<DialogTitle>Function Returned Value</DialogTitle>
149+
<DialogTitle>Function Signature</DialogTitle>
135150
</DialogHeader>
136151

137152
<div className="">
138-
<textarea defaultValue={isOpen} rows={12} className="w-full resize-none p-2 text-white bg-black/10" />
153+
<textarea defaultValue={isOpen} rows={6} className="w-full font-medium text-lg resize-none p-2 text-white bg-black/10" />
139154
</div>
140155

141156
<DialogFooter>

0 commit comments

Comments
 (0)