Documentation Index
Fetch the complete documentation index at: https://grat.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
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.
Connect to the Contract
Initialize the Grat client and your contract object.import { Grat } from '@grat-official-sdk/sdk';
import { Contract, Networks } from '@stellar/stellar-sdk';
const grat = Grat.testnet();
const contract = new Contract("CA...");
Build the Invocation
Build a transaction that calls a function on your contract.const tx = new TransactionBuilder(
{ publicKey: userKp.publicKey(), sequence: "123" },
{ fee: "100", networkPassphrase: Networks.TESTNET }
)
.addOperation(contract.call("hello", "world"))
.setTimeout(30)
.build();
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.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
);
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.readyTx.sign(userKp);
console.log("Sponsoring Soroban call...");
const result = await grat.sponsorContract(readyTx);
console.log("Success! Hash:", result.hash);
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.
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.
Use the estimate() method before sponsoring if you want to show the user exactly how much the application is paying to cover their transaction.
View on GitHub