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

# Estimate Fees

export const Method = ({children, className = ""}) => {
  return <code className={`spark-method not-prose inline-block px-2 py-1 mt-12 text-sm font-mono font-semibold rounded-md border ${className}`}>
      {children}
    </code>;
};

Fee breakdown for Lightning payments, cooperative exits, and swaps.

***

<br />

`For Bitcoin`

<div className="w-full overflow-x-auto">
  <table className="w-full border-collapse mt-0">
    <thead>
      <tr className="border-b border-gray-200 dark:border-gray-700">
        <th className="text-left py-2 pr-12 w-1/2">Transaction Type</th>
        <th className="text-left py-2 w-1/2">Fee structure</th>
      </tr>
    </thead>

    <tbody>
      <tr className="border-b border-gray-100 dark:border-gray-800">
        <td className="py-3 pr-12">L1 to Spark</td>
        <td className="py-3">On-chain fee paid by the user</td>
      </tr>

      <tr className="border-b border-gray-100 dark:border-gray-800">
        <td className="py-3 pr-12">Spark to Spark</td>
        <td className="py-3">Free. Small flat fee coming in 6-12 months</td>
      </tr>

      <tr className="border-b border-gray-100 dark:border-gray-800">
        <td className="py-3 pr-12">Spark to Lightning</td>
        <td className="py-3">0.25% + routing fee</td>
      </tr>

      <tr className="border-b border-gray-100 dark:border-gray-800">
        <td className="py-3 pr-12">Lightning to Spark</td>
        <td className="py-3">Free for the receiver. 0.15% for the sender (charged on routing nodes via route hints)</td>
      </tr>

      <tr className="border-b border-gray-100 dark:border-gray-800">
        <td className="py-3 pr-12">Send to L1</td>
        <td className="py-3">`250 × sats_per_vbyte + 750` where `sats_per_vbyte` is the fee rate based on exit speed (slow, medium, or fast)</td>
      </tr>

      <tr>
        <td className="py-3 pr-12">Unilateral Exit</td>
        <td className="py-3">On-chain fee paid by the user</td>
      </tr>
    </tbody>
  </table>
</div>

<br />

`For BTKN assets`

<div className="w-full overflow-x-auto">
  <table className="w-full border-collapse mt-0">
    <thead>
      <tr className="border-b border-gray-200 dark:border-gray-700">
        <th className="text-left py-2 pr-12 w-1/2">Transaction Type</th>
        <th className="text-left py-2 w-1/2">Fee structure</th>
      </tr>
    </thead>

    <tbody>
      <tr className="border-b border-gray-100 dark:border-gray-800">
        <td className="py-3 pr-12">Spark to Spark</td>
        <td className="py-3">Free</td>
      </tr>

      <tr>
        <td className="py-3 pr-12">Unilateral Exit</td>
        <td className="py-3">On-chain fee + bond locked by user</td>
      </tr>
    </tbody>
  </table>
</div>

<Note> Some of the fees are sourced directly from the Lightspark SSP, specifically for Spark–Lightning interactions. Lightspark is the first SSP on Spark, but the system is open. Anyone can run an SSP (SSP specs coming soon). If you're an SSP, reach out, and we'll include your fee structure. </Note>

***

## Fee Estimation Flow

The complete process for estimating fees before making transactions:

<Steps>
  <Step title="Choose Operation Type">
    Determine which type of operation you want to estimate fees for.

    ```typescript theme={null}
    // Lightning payment
    const lightningInvoice = "lnbc...";

    // Cooperative exit (withdrawal to L1)
    const withdrawalAmount = 10000;
    const withdrawalAddress = "bc1p...";

    // Swap operation
    const swapAmount = 5000;
    ```
  </Step>

  <Step title="Get Fee Estimate">
    Call the appropriate fee estimation method.

    ```typescript theme={null}
    // Lightning send fee estimate
    const lightningFee = await wallet.getLightningSendFeeEstimate({
      encodedInvoice: lightningInvoice
    });

    // Cooperative exit fee estimate
    const exitFee = await wallet.getWithdrawalFeeQuote({
      amountSats: withdrawalAmount,
      withdrawalAddress: withdrawalAddress
    });

    // Swap fee estimate
    const swapFee = await wallet.getSwapFeeEstimate(swapAmount);
    ```
  </Step>

  <Step title="Review Fee Details">
    Examine the fee estimate response to understand costs.

    ```typescript theme={null}
    console.log("Lightning fee estimate:", lightningFee, "sats");
    console.log("Exit fee estimate:", exitFee);
    console.log("Swap fee estimate:", swapFee);
    ```
  </Step>

  <Step title="Proceed with Transaction">
    Use the fee estimate to set appropriate limits for your transaction.

    ```typescript theme={null}
    // Use fee estimate for Lightning payment
    const payment = await wallet.payLightningInvoice({
      invoice: lightningInvoice,
      maxFeeSats: lightningFee + 5 // Add buffer to fee estimate
    });
    ```
  </Step>
</Steps>

***

## Lightning Send Fee Estimates

Estimate fees for sending Lightning payments before making the transaction.

<Method>getLightningSendFeeEstimate(params)</Method>

Gets an estimated fee for sending a Lightning payment. Note: the actual fee assessed may be different from the fee estimate as it will be determined by the actual Lightning node routing.

```typescript theme={null}
const feeEstimateSats = await wallet.getLightningSendFeeEstimate({
  encodedInvoice: "lnbcrt1u1pnm7ammpp4v84f05tl0kzt6g95g056athdpp8f8azvg6d7epz74z562ymer9jqsp5nc50gazvp0e98u42jlu653rw0eutcl067nqq924hf89q4la4kd9sxq9z0rgqnp4qdnmwu8v22cvq9xsv2l05cn9rre7xlcgdtntxawf8m0zxq3qemgzqrzjqtr2vd60g57hu63rdqk87u3clac6jlfhej4kldrrjvfcw3mphcw8sqqqqrj0q7ew45qqqqqqqqqqqqqq9qcqzpgdq5w3jhxapqd9h8vmmfvdjs9qyyssqj7lf2w4m587g04n4t0ferdv0vnwftzca0xuc9yxycng78cnhrvmyw2mzaa8t76jskpypqnwqhp9xh0vnwxz90jytd34vrmhcngsnl8qplz7ylk"
});

console.log("Lightning send fee estimate:", feeEstimateSats, "sats");
```

<Expandable title="Parameters">
  <ResponseField name="encodedInvoice" type="string" required>
    The BOLT11-encoded Lightning invoice
  </ResponseField>
</Expandable>

<Expandable title="Returns">
  <ResponseField name="feeEstimate" type="number" required>
    Estimated fee in satoshis
  </ResponseField>
</Expandable>

### Example Response

```typescript theme={null}
100 // fee estimate in satoshis
```

***

## Sending on L1 Fee Estimates

Estimate fees for sending funds on the Bitcoin network.

<Method>getWithdrawalFeeQuote(params)</Method>

Gets a fee quote for a cooperative exit (on-chain withdrawal). The quote includes options for different speeds and an expiry time and must be passed to `withdraw` before it expires.

```typescript theme={null}
const exitFeeEstimate = await wallet.getWithdrawalFeeQuote({
  amountSats: 10000,
  withdrawalAddress: "bc1p5d7rjq7g6rdk2yhzks9smtbqtedr4dekq08ge8ztwac72sfr9rusxg3297"
});

console.log("Cooperative exit fee estimate:", exitFeeEstimate);
```

<Expandable title="Parameters">
  <ResponseField name="amountSats" type="number" required>
    The amount in satoshis to withdraw
  </ResponseField>

  <ResponseField name="withdrawalAddress" type="string" required>
    The Bitcoin address where the funds should be sent
  </ResponseField>
</Expandable>

<Expandable title="Returns">
  <ResponseField name="exitFeeEstimate" type="CoopExitFeeQuote | null" required>
    A fee quote for the withdrawal, or null if not available
  </ResponseField>
</Expandable>

### Example Response

```typescript theme={null}
{
  feeEstimate: {
    originalValue: 1000,
    originalUnit: 'SATOSHI',
    preferredCurrencyUnit: 'USD',
    preferredCurrencyValueRounded: 0.50,
    preferredCurrencyValueApprox: 0.483
  }
}
```

***

## Swap Fee Estimates

Estimate fees for leaves swap operations. Leaves swaps are internal wallet optimization operations that consolidate or split your sats leaves.

<Method>getSwapFeeEstimate(amountSats)</Method>

Gets the estimated fee for a swap of leaves.

```typescript theme={null}
const swapFeeEstimate = await wallet.getSwapFeeEstimate(5000);
console.log("Swap fee estimate:", swapFeeEstimate);
```

<Expandable title="Parameters">
  <ResponseField name="amountSats" type="number" required>
    The amount of sats to swap
  </ResponseField>
</Expandable>

<Expandable title="Returns">
  <ResponseField name="swapFeeEstimate" type="LeavesSwapFeeEstimateOutput" required>
    The estimated fee for the swap operation
  </ResponseField>
</Expandable>

***

## Fee Deduction Options

For cooperative exits, you can choose how fees are handled:

### Deduct Fee from Withdrawal Amount

When `deductFeeFromWithdrawalAmount` is set to `true`:

```typescript theme={null}
import { ExitSpeed } from "@buildonspark/spark-sdk";

const withdrawal = await wallet.withdraw({
  onchainAddress: "bc1p...",
  exitSpeed: ExitSpeed.MEDIUM,
  amountSats: 10000, // This is the net amount you'll receive
  feeQuote: feeQuote,
  deductFeeFromWithdrawalAmount: true // Fee deducted from amount
});
// You receive: 10000 sats - fee
```

### Pay Fee Separately

When `deductFeeFromWithdrawalAmount` is set to `false`:

```typescript theme={null}
import { ExitSpeed } from "@buildonspark/spark-sdk";

const withdrawal = await wallet.withdraw({
  onchainAddress: "bc1p...",
  exitSpeed: ExitSpeed.MEDIUM,
  amountSats: 10000, // This is the gross amount you'll receive
  feeQuote: feeQuote,
  deductFeeFromWithdrawalAmount: false // Fee paid separately
});
// You receive: 10000 sats (fee paid from wallet balance)
```
