Understanding TTL in Soroban
Before we demonstrate the TTL extension methods, you should note that in Soroban:- The contract instance and code are two separate, persistent storage entries
- TTL exists to prevent the blockchain from being cluttered with unused storage entries
- TTL extension can be done for both the contract instance and the contract code
Prerequisites
- Stellar SDK:
npm install @stellar/stellar-sdkfor Javascript - Install the Stellar CLI
- A Stellar RPC endpoint (e.g.,
https://soroban-testnet.stellar.org) - Basic knowledge of the SDK in use
Extending Contract Code and Instance TTL with a Contract
-
Self-Extension: Extending the TTL from within the contract itself, in Rust.
- Use case: When a contract needs to manage its own lifetime
- Process: Directly accessing the contract’s instance storage to extend its TTL
-
External Extension: Extending the TTL from another contract (the deployer), in Rust.
- Use case: When managing multiple contract instances or implementing administrative control
- Process: Using the deployer’s authority to extend TTL for any contract it has deployed
env.storage().instance().extend_ttl(...)is called to extend the TTL of the current contract instance and the code entries.thresholdis a check that ensures that the current TTL of the contract instance is less than the set threshold value.extend_tois the minimum number of ledgers from the current ledger that the TTL should be extended to (if the current TTL is already greater, this call is a no-op).contract_addressis the address of the contract instance whose TTL we want to extend.env.deployer()accesses the deployer, which has methods for managing the contract’s lifecycle.deployer.extend_ttl(...)extends the TTL of the specified contract instance and code entries.
Extending Contract Code and Instance TTL with a Transaction
- Use Case: When you need to manage contract TTLs through an external application or automated system.
- Process:
- Get the contract’s footprint
- Set the entries you want to extend as read-only in the Soroban Data
- Create an operation
StellarSdk.Operation.extendFootprintTtlwith the new TTL value (extendTo), this will ensure the entries’ TTL will be at least extendTo ledgers from now - Simulate to determine the resource fee needed to extend the TTL
- Sign and submit the transaction
A resource fee and inclusion fee are both charged in this transaction.
- CLI
- JS
The CLI has separate commands to extend contract code and contract instance ledger entries. The Contract Instance:
--ledgers-to-extend argument is the new entries TTL value.Contract Code: