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

# useTransactionSimulation

> Use transaction simulation without Dynamic's UI

<Card title="Recommended: JavaScript SDK with React Hooks" icon="react" color="#4779FE">
  For new React apps, we recommend the JavaScript SDK with React Hooks (`@dynamic-labs-sdk/react-hooks`) instead of the legacy React SDK documented here. The JS SDK comes with many benefits such as a much smaller bundle size and other optimizations. Use the [React quickstart (JavaScript SDK)](/javascript/reference/react-quickstart) to get started.
</Card>

Dynamic provides React hooks to simulate transactions programmatically without using the built-in UI. These hooks allow you to validate transactions before sending them to the network.

## useEVMTransactionSimulation

Use this hook to simulate EVM transactions and get detailed information about the transaction's expected outcome, including asset transfers, balance changes, and security validations.

### Usage

```jsx theme={"system"}
import { useEVMTransactionSimulation } from '@dynamic-labs/sdk-react-core';

const { simulateEVMTransaction, isLoading, error } = useEVMTransactionSimulation();
```

### Parameters

The `simulateEVMTransaction` function accepts an object with the following parameters:

| Parameter   | Type   | Description                                                   |
| :---------- | :----- | :------------------------------------------------------------ |
| transaction | object | The transaction object containing from, to, data, value, etc. |
| type        | string | The transaction type (e.g., 'SignTransaction')                |

The `transaction` object supports the following fields:

| Field    | Type   | Description                             |
| :------- | :----- | :-------------------------------------- |
| from     | string | The sender's address                    |
| to       | string | The recipient's address                 |
| data     | string | The transaction data (optional)         |
| value    | bigint | The transaction value in wei (optional) |
| gas      | string | The gas limit (optional)                |
| gasPrice | string | The gas price in wei (optional)         |

**Note:** The `chainId` is automatically fetched from the wallet connector and should not be included in the parameters.

### Response

The simulation returns a `SimulateTransactionResponse` object containing:

* `assetTransfers` - Array of asset transfers that will occur
* `assetDiffs` - Balance changes for each asset
* `blockaidValidation` - Security validation results
* `priceData` - Price information for assets involved

### Example

```jsx theme={"system"}
import { useEVMTransactionSimulation } from '@dynamic-labs/sdk-react-core';
import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
import { parseEther } from 'viem';

const MyComponent = () => {
  const { primaryWallet } = useDynamicContext();
  const { simulateEVMTransaction, isLoading, error } = useEVMTransactionSimulation();

  const handleSimulate = async () => {
    if (!primaryWallet) return;

    try {
      const result = await simulateEVMTransaction({
        transaction: {
          from: primaryWallet.address,
          to: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
          value: parseEther('1'), // 1 ETH
        },
        type: 'SignTransaction',
      });

      console.log('Simulation result:', result);
      console.log('Asset transfers:', result.assetTransfers);
      console.log('Security validation:', result.blockaidValidation);
    } catch (err) {
      console.error('Simulation failed:', err);
    }
  };

  return (
    <button onClick={handleSimulate} disabled={isLoading}>
      {isLoading ? 'Simulating...' : 'Simulate Transaction'}
    </button>
  );
};
```

### Simulating contract interactions

```jsx theme={"system"}
import { useEVMTransactionSimulation } from '@dynamic-labs/sdk-react-core';
import { encodeFunctionData, parseEther } from 'viem';

const MyComponent = () => {
  const { simulateEVMTransaction } = useEVMTransactionSimulation();

  const handleSimulateContractCall = async () => {
    const data = encodeFunctionData({
      abi: contractAbi,
      functionName: 'transfer',
      args: [recipientAddress, amount],
    });

    const result = await simulateEVMTransaction({
      transaction: {
        from: walletAddress,
        to: contractAddress,
        data,
        value: parseEther(amount),
      },
      type: 'SignTransaction',
    });

    console.log('Contract call simulation:', result);
  };

  return <button onClick={handleSimulateContractCall}>Simulate Contract Call</button>;
};
```

### Simulating Account Abstraction transactions

For ZeroDev wallets and other account abstraction implementations, use the `simulateEVMTransactionAA` function:

```jsx theme={"system"}
import { useEVMTransactionSimulation } from '@dynamic-labs/sdk-react-core';
import { parseEther } from 'viem';

const MyComponent = () => {
  const { simulateEVMTransactionAA } = useEVMTransactionSimulation();

  const handleSimulateAA = async () => {
    const result = await simulateEVMTransactionAA({
      transaction: {
        from: smartWalletAddress,
        to: recipientAddress,
        value: parseEther('0.1'),
      },
      type: 'SignTransaction',
    });

    console.log('AA simulation result:', result);
  };

  return <button onClick={handleSimulateAA}>Simulate AA Transaction</button>;
};
```

## useSVMTransactionSimulation

Use this hook to simulate Solana (SVM) transactions and validate them before sending to the network.

### Usage

```jsx theme={"system"}
import { useSVMTransactionSimulation } from '@dynamic-labs/sdk-react-core';

const { simulateSVMTransaction, isLoading, error } = useSVMTransactionSimulation();
```

### Parameters

The `simulateSVMTransaction` function accepts an object with the following parameters:

| Parameter   | Type        | Description                                    |
| :---------- | :---------- | :--------------------------------------------- |
| transaction | Transaction | The Solana Transaction object (not serialized) |
| type        | string      | The transaction type (e.g., 'SignTransaction') |

**Note:** The hook accepts the Transaction object directly and handles bs58 encoding internally. Do not serialize the transaction to base64.

### Response

The simulation returns a `SimulateTransactionResponse` object containing:

* `assetTransfers` - Array of asset transfers that will occur
* `assetDiffs` - Balance changes for each asset
* `blockaidValidation` - Security validation results
* `priceData` - Price information for assets involved

### Example

```jsx theme={"system"}
import { useSVMTransactionSimulation } from '@dynamic-labs/sdk-react-core';
import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
import { Transaction, SystemProgram, PublicKey } from '@solana/web3.js';

const MyComponent = () => {
  const { primaryWallet } = useDynamicContext();
  const { simulateSVMTransaction, isLoading, error } = useSVMTransactionSimulation();

  const handleSimulate = async () => {
    if (!primaryWallet) return;

    // Create a Solana transaction
    const solTransaction = new Transaction().add(
      SystemProgram.transfer({
        fromPubkey: new PublicKey(primaryWallet.address),
        toPubkey: new PublicKey('recipient-address'),
        lamports: 1000000000, // 1 SOL
      })
    );

    try {
      const result = await simulateSVMTransaction({
        transaction: solTransaction, // Pass Transaction object directly
        type: 'SignTransaction',
      });

      console.log('Simulation result:', result);
      console.log('Asset transfers:', result.assetTransfers);
      console.log('Security validation:', result.blockaidValidation);
    } catch (err) {
      console.error('Simulation failed:', err);
    }
  };

  return (
    <button onClick={handleSimulate} disabled={isLoading}>
      {isLoading ? 'Simulating...' : 'Simulate Transaction'}
    </button>
  );
};
```

### Simulating versioned transactions

```jsx theme={"system"}
import { useSVMTransactionSimulation } from '@dynamic-labs/sdk-react-core';
import { VersionedTransaction, TransactionMessage, PublicKey } from '@solana/web3.js';

const MyComponent = () => {
  const { simulateSVMTransaction } = useSVMTransactionSimulation();

  const handleSimulateVersioned = async () => {
    // Create a versioned transaction
    const messageV0 = new TransactionMessage({
      payerKey: new PublicKey(walletAddress),
      recentBlockhash: blockhash,
      instructions: [instruction],
    }).compileToV0Message();

    const versionedTx = new VersionedTransaction(messageV0);

    const result = await simulateSVMTransaction({
      transaction: versionedTx, // Pass VersionedTransaction object directly
      type: 'SignTransaction',
    });

    console.log('Versioned transaction simulation:', result);
  };

  return <button onClick={handleSimulateVersioned}>Simulate Versioned Transaction</button>;
};
```

## Error handling

Both hooks provide error states that you can use to handle simulation failures:

```jsx theme={"system"}
const { simulateEVMTransaction, isLoading, error } = useEVMTransactionSimulation();

if (error) {
  console.error('Simulation error:', error);
  // Handle error appropriately
}
```

## Security validation

The simulation response includes security validation from Blockaid, which helps identify potentially malicious transactions:

```jsx theme={"system"}
const result = await simulateEVMTransaction({
  transaction: {
    from: walletAddress,
    to: contractAddress,
    data,
  },
  type: 'SignTransaction',
});

if (result.blockaidValidation?.result === 'malicious') {
  console.warn('Warning: This transaction may be malicious');
  console.log('Reason:', result.blockaidValidation.reason);
  // Show warning to user
}
```
