Skip to content

Commit 9d63701

Browse files
committed
feat: Loading Toast for Contract Invocation & Bool value fixed
1 parent 97a9f1e commit 9d63701

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

packages/frontend/src/components/InvokeFunction.tsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { Fragment, useState } from "react";
3+
import { Fragment, useId, useState } from "react";
44
import { store } from "@/state";
55
import { useSelector } from "@xstate/store/react";
66
import { Button } from "./ui/button";
@@ -16,23 +16,36 @@ import { callContract } from "@/actions";
1616
import { networkRpc } from "@/lib/web3";
1717
import { Server } from "@stellar/stellar-sdk/rpc";
1818
import { Networks, scValToNative, xdr, rpc } from "@stellar/stellar-sdk";
19+
import { useToast } from "@/hooks/use-toast";
20+
21+
function transformValue(value: any) {
22+
const mapped = {
23+
string: `"${value}"`,
24+
bool: value ? "true" : "false",
25+
};
26+
27+
return mapped;
28+
}
1929

2030
function createLogSingnature(method: FunctionSpec, args: any, result: any) {
2131
const mappedArgs = method.inputs.map((arg) => {
2232
const value = args[arg.name]?.value;
33+
const mapped = transformValue(value);
2334
return {
2435
name: arg.name,
2536
type: arg.value.type,
26-
value: (arg as any)?.value?.type === "string" ? `"${value}"` : value,
37+
value: (mapped as any)[arg.value.type] || value,
2738
};
2839
});
2940
const output = method.outputs.at(0);
41+
const mapped = transformValue(result);
3042
return {
3143
name: method.name,
3244
args: mappedArgs,
3345
result: {
3446
type: output?.type as any,
35-
value: (output as any)?.type === "vec" ? `[${result}]` : result,
47+
// @ts-ignore
48+
value: mapped[output?.type] || result,
3649
},
3750
};
3851
}
@@ -46,6 +59,7 @@ function InvokeFunction({ method }: { method: FunctionSpec }) {
4659
const [args, setArgs] = useState<Record<string, { type: string; value: string; subType: string }>>({});
4760
const contractAddress = useSelector(store, (state) => state.context.contract?.address);
4861
const [logs, setLogs] = useState<string[]>(["Hello wrold", "Mango World"]);
62+
const toastId = useId();
4963

5064
const handleInputChange = (name: string, value: string, type: string, subType: string) => {
5165
setArgs((prev) => ({
@@ -70,11 +84,11 @@ function InvokeFunction({ method }: { method: FunctionSpec }) {
7084

7185
logger.info("Invoking Contract function...");
7286
logger.info(JSON.stringify(data, null, 2));
87+
toast.loading("Invoking function...", { id: toastId });
7388

7489
const result = await callContract(data);
7590

7691
let response;
77-
7892
while (true) {
7993
response = await server.getTransaction(result.hash);
8094
if (response.status !== "NOT_FOUND") {
@@ -115,11 +129,12 @@ function InvokeFunction({ method }: { method: FunctionSpec }) {
115129
const logSignature = createLogSingnature(method, args, scValToNative(response.returnValue));
116130
setSignature(logSignature);
117131
}
118-
toast.success(`Function invoked successfully`);
132+
toast.success(`Function invoked successfully`, { id: toastId });
119133
return response;
120134
} else {
121135
logger.error("Transaction failed.");
122136
logger.info(`TxId: ${result.hash}`);
137+
toast.error(`Transaction failed`, { id: toastId });
123138
throw new Error("Transaction failed");
124139
}
125140
console.log(result);
@@ -180,9 +195,9 @@ function InvokeFunction({ method }: { method: FunctionSpec }) {
180195
</Dialog>
181196
<Dialog open={Boolean(sg.name)} onOpenChange={(val) => setSignature(val ? sg : defaultState)}>
182197
<DialogContent>
183-
<DialogDescription className="sr-only">
198+
<DialogHeader className="sr-only">
184199
<DialogTitle>Invocation result</DialogTitle>
185-
</DialogDescription>
200+
</DialogHeader>
186201
<div className="">
187202
<p className="text-lg font-bold mb-2">Function Signature</p>
188203
<div className="w-full font-medium text-lg resize-none p-2 text-white bg-[rgb(31,31,31)] rounded min-h-16">

0 commit comments

Comments
 (0)