Skip to content

Commit 325dd7d

Browse files
committed
fix: Argument Type Conversion
1 parent a64a941 commit 325dd7d

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

packages/frontend/src/actions/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
import { invokeContract } from "@/lib/invoke-contract";
44

5-
export async function callContract({ contractId, method, args }: { method: string; contractId: string; args: any[] }) {
5+
export async function callContract({
6+
contractId,
7+
method,
8+
args,
9+
}: {
10+
method: string;
11+
contractId: string;
12+
args: { type: string; value: string }[];
13+
}) {
614
const result = await invokeContract({ contractId, method, args });
715
return result;
816
}

packages/frontend/src/components/InvokeFunction.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { toast } from "sonner";
1111
import { FunctionSpec } from "@/types/idl";
1212
import { DialogTrigger } from "@radix-ui/react-dialog";
1313
import { ChevronsLeftRightEllipsis } from "lucide-react";
14-
import { invokeContract } from "@/lib/invoke-contract";
1514
import { logger } from "@/state/utils";
1615
import { callContract } from "@/actions";
1716
import { networkRpc } from "@/lib/web3";
@@ -20,13 +19,16 @@ import { Networks } from "@stellar/stellar-sdk";
2019

2120
function InvokeFunction({ method }: { method: FunctionSpec }) {
2221
const [isOpen, setIsOpen] = useState(false);
23-
const [args, setArgs] = useState<Record<string, string>>({});
22+
const [args, setArgs] = useState<Record<string, { type: string; value: string }>>({});
2423
const contractAddress = useSelector(store, (state) => state.context.contract?.address);
2524

26-
const handleInputChange = (name: string, value: string) => {
25+
const handleInputChange = (name: string, value: string, type: string) => {
2726
setArgs((prev) => ({
2827
...prev,
29-
[name]: value,
28+
[name]: {
29+
type,
30+
value,
31+
},
3032
}));
3133
};
3234

@@ -95,8 +97,8 @@ function InvokeFunction({ method }: { method: FunctionSpec }) {
9597
</Label>
9698
<Input
9799
id={arg.name}
98-
value={args[arg.name] || ""}
99-
onChange={(e) => handleInputChange(arg.name, e.target.value)}
100+
value={args[arg.name]?.value || ""}
101+
onChange={(e) => handleInputChange(arg.name, e.target.value, arg.value.type)}
100102
className="col-span-3"
101103
placeholder={`Enter '${arg.name}' value`}
102104
/>

packages/frontend/src/lib/invoke-contract.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
import { Address, BASE_FEE, Contract, Keypair, Networks, TransactionBuilder, xdr } from "@stellar/stellar-sdk";
2-
import { assembleTransaction, Server } from "@stellar/stellar-sdk/rpc";
1+
import { BASE_FEE, Contract, Keypair, nativeToScVal, Networks, TransactionBuilder } from "@stellar/stellar-sdk";
32
import { networkRpc } from "./web3";
4-
import { buildAndSendTransaction } from "./deploy-steller";
3+
import { Server } from "@stellar/stellar-sdk/rpc";
54

6-
function buildOperation({ contractId, method, args }: { method: string; contractId: string; args: any[] }) {
5+
function buildOperation({
6+
contractId,
7+
method,
8+
args,
9+
}: {
10+
method: string;
11+
contractId: string;
12+
args: { type: string; value: string }[];
13+
}) {
714
const contract = new Contract(contractId);
8-
const scArgs = args.map((arg) => {
9-
if (typeof arg === "string" && arg.startsWith("G")) {
10-
return Address.fromString(arg).toScVal(); // Address
11-
} else if (typeof arg === "number") {
12-
return xdr.ScVal.scvI32(arg); // Integer
13-
} else {
14-
return xdr.ScVal.scvString(arg); // String (adjust as needed)
15-
}
15+
const scArgs = args.map(({ type, value }) => {
16+
return nativeToScVal(value, { type });
1617
});
1718
const operation = contract.call(method, ...scArgs);
1819
return operation;
@@ -25,7 +26,7 @@ export async function invokeContract({
2526
}: {
2627
method: string;
2728
contractId: string;
28-
args: any[];
29+
args: { type: string; value: string }[];
2930
}) {
3031
const sourceKeypair = Keypair.random();
3132
const rpcUrl = networkRpc[Networks.TESTNET];

0 commit comments

Comments
 (0)