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

# Sponsor Soroban Contracts

> Zero-gas interactions for smart contracts on Stellar.

Soroban smart contracts involve complex resource fees for CPU, memory, and ledger footprints. Grat simplifies this by providing built-in simulation and sponsorship for all Soroban fees.

### Prerequisites

* A running Grat relay.
* A deployed Soroban contract on Testnet.

<Steps>
  <Step title="Connect to the Contract">
    Initialize the Grat client and your contract object.

    ```typescript theme={null}
    import { Grat } from '@grat-official-sdk/sdk';
    import { Contract, Networks } from '@stellar/stellar-sdk';

    const grat = Grat.testnet();
    const contract = new Contract("CA...");
    ```
  </Step>

  <Step title="Build the Invocation">
    Build a transaction that calls a function on your contract.

    ```typescript theme={null}
    const tx = new TransactionBuilder(
      { publicKey: userKp.publicKey(), sequence: "123" },
      { fee: "100", networkPassphrase: Networks.TESTNET }
    )
    .addOperation(contract.call("hello", "world"))
    .setTimeout(30)
    .build();
    ```
  </Step>

  <Step title="Simulate the Transaction">
    Soroban transactions require resource estimates before they can be submitted. Use Grat to simulate the call and retrieve the necessary footprint data.

    ```typescript theme={null}
    console.log("Simulating contract call...");
    const simulation = await grat.simulate(tx);

    // The simulation returns the transaction with 
    // appropriate footprints and resource fees attached.
    const readyTx = TransactionBuilder.fromXDR(
      simulation.transactionData, 
      Networks.TESTNET
    );
    ```
  </Step>

  <Step title="Sponsor the Call">
    Sign the transaction with the user's keypair and send it to Grat for sponsorship. Grat will pay both the base fee and the resource fees.

    ```typescript theme={null}
    readyTx.sign(userKp);

    console.log("Sponsoring Soroban call...");
    const result = await grat.sponsorContract(readyTx);

    console.log("Success! Hash:", result.hash);
    ```
  </Step>

  <Step title="Read the Result">
    After the transaction is included in a ledger, you can parse the result XDR to read the return value of your contract function.
  </Step>
</Steps>

### Resource Fees vs Base Fees

In Soroban, every transaction has two fee components:

* **Base Fee**: The cost of including the transaction in a ledger (Classic).
* **Resource Fee**: The cost of the computation and storage used by the contract.

Grat automatically handles the calculation and payment of both components during the sponsorship process.

<Tip>
  Use the `estimate()` method before sponsoring if you want to show the user exactly how much the application is paying to cover their transaction.
</Tip>

[View on GitHub](https://github.com/gratnetwork/grat)
