> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sorokit.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Next.js Server Actions

> Optimize performance by simulating transactions on the server.

While signing must happen on the client (where the wallet lives), you can significantly speed up your app by performing the simulation and argument building on the server. This reduces the number of RPC calls the browser has to make and keeps your contract specifications off the main thread.

#### The Workflow

* 1. Server: Fetch the contract spec and build the transaction.
* 2. Server: Run the simulation to calculate fees.
* 3. Client: Receive the "ready-to-sign" transaction from the server.
* 4. Client: Sign with the user's wallet and broadcast.

#### The Server Action

```ts theme={null}
"use server";

import { AssembledTransaction } from "@stellar/stellar-sdk/contract";
import { fetchContractSpec } from "@sorokit/contract";
import { resolveSorokitConfig } from "@sorokit/core";
import { QueryClient } from "@tanstack/react-query";

export async function prepareMintAction(address: string) {
  const config = resolveSorokitConfig({ network: "TESTNET" });
  const queryClient = new QueryClient();

  const spec = await fetchContractSpec("C...", config, queryClient);

  const tx = await AssembledTransaction.build({
    contractId: "C...",
    method: "mint",
    args: spec.funcArgsToScVals("mint", { to: address }),
    networkPassphrase: config.networkPassphrase,
    rpcUrl: config.rpcUrl,
  });

  return tx.toJson();
}
```

#### The Client Component

```tsx theme={null}
"use client";

import { useWallet } from "@sorokit/wallet-adapter";
import { prepareMintAction } from "./actions";

export function MintButton() {
  const { signTransaction } = useWallet();

  const handleAction = async () => {
    // Fetch the prepared transaction from the server
    const json = await prepareMintAction();

    // Reconstruct and sign
    const tx = AssembledTransaction.fromJson({...}, JSON.parse(json));
    await tx.signAndSend({ signTransaction });
  };

  return <button onClick={handleAction}>Server-Side Mint</button>;
}
```

#### Performance Gains

* Reduced Client Latency: The browser doesn't need to fetch the WASM spec or run heavy simulations.
* Security: You can validate the user's input on the server before the transaction is even sent to the wallet.
* Shared Logic: If you are already fetching data for SEO (SSR), you can reuse that contract state for the subsequent user actions.
