Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/lib/hooks/use-quiz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export function useQuiz(courseId: string) {
const [error, setError] = useState<string | null>(null);

useEffect(() => {
if (!courseId) return;
if (!courseId) {
setLoading(false);
return;
}
setLoading(true);
getQuiz(courseId, jwt ?? undefined)
.then(setQuiz)
Expand Down
9 changes: 6 additions & 3 deletions src/lib/stellar/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NetworkType } from "./wallet";
import { simulateContractCall, signAndSubmitTransaction } from "./transactions";
import type { TransactionResult } from "@/types/stellar";
import { scval } from "@stellar/stellar-sdk";
import { xdr, scValToNative } from "@stellar/stellar-sdk";

// Validate required environment variables at startup
function validateEnvironmentVariables() {
Expand Down Expand Up @@ -69,7 +69,10 @@ export async function readRewardBalance(
throw new Error("Invalid simulation result");
}

const resultArray = simResult.results || simResult;
const resultArray =
"results" in simResult && Array.isArray((simResult as Record<string, unknown>).results)
? (simResult as Record<string, unknown>).results
: simResult;
if (!Array.isArray(resultArray) || resultArray.length === 0) {
throw new Error("No results in simulation response");
}
Expand All @@ -79,7 +82,7 @@ export async function readRewardBalance(
throw new Error("Missing XDR in simulation result");
}

const scVal = scval.native.fromXDR(result.xdr, "base64");
const scVal = scValToNative(xdr.ScVal.fromXDR(result.xdr, "base64"));
const balance = scVal as unknown as bigint | number | string;
return String(balance);
}
Expand Down
13 changes: 3 additions & 10 deletions src/lib/stellar/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import freighterApi from "@stellar/freighter-api";
import { Contract, Address, TransactionBuilder, xdr } from "@stellar/stellar-sdk";
import { Contract, Account, TransactionBuilder, nativeToScVal } from "@stellar/stellar-sdk";
import type { NetworkType } from "./wallet";
import { getNetworkPassphrase, getRpcUrl } from "./wallet";
import type { TransactionResult } from "@/types/stellar";
Expand Down Expand Up @@ -81,21 +81,14 @@ export async function simulateContractCall(
const contract = new Contract(contractAddress);

const txBuilder = new TransactionBuilder(
new Address(contractAddress),
new Account(contractAddress, "0"),
{
fee: "100",
networkPassphrase: passphrase,
}
);

const sorobanArgs = args.map((arg) => {
if (typeof arg === "string") return xdr.ScVal.scvString(arg);
if (typeof arg === "number") return xdr.ScVal.scvU32(arg);
if (typeof arg === "bigint") return xdr.ScVal.scvU64(arg);
if (typeof arg === "boolean") return xdr.ScVal.scvBool(arg);
if (arg instanceof Uint8Array) return xdr.ScVal.scvBytes(arg);
return xdr.ScVal.scvString(JSON.stringify(arg));
});
const sorobanArgs = args.map((arg) => nativeToScVal(arg));

const tx = txBuilder
.addOperation(contract.call(method, ...sorobanArgs))
Expand Down