Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add guide for extending a contract instance's ttl #1030

Merged
merged 4 commits into from
Jan 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 89 additions & 17 deletions docs/build/guides/conventions/extending-wasm-ttl.mdx
Original file line number Diff line number Diff line change
@@ -1,38 +1,105 @@
---
title: Extend a deployed contracts TTL with code
description: How to extend the TTL of a deployed contract's Wasm code using JavaScript SDK
title: Extend a deployed contract's TTL with code
description: How to extend the TTL of a deployed contract's Wasm code
---

# Extending a deployed contract’s TTL using code
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

# Extending a deployed contract's TTL using code

When a smart contract is deployed on the Stellar network, its WebAssembly (Wasm) code has a [Time To Live (TTL)](../../../learn/encyclopedia/storage/state-archival.mdx#ttl) that determines how long it remains accessible.

The TTL is the number of ledgers between the current ledger and the final ledger for which the contract data can still be accessed. If the TTL expires, the contract's code becomes archived and inaccessible. To prevent this, you need to periodically extend the TTL of the contract's Wasm code.

This guide will show you how to extend the TTL of a deployed contract's Wasm code using JavaScript.
This guide will show you how to extend the TTL of a deployed contract's Wasm code using the Javascript and Rust SDKs.

## Understanding TTL in Soroban

Before we demonstrate the TTL extension methods, you should note that in Soroban:

- Contract instances and code are stored in the instance storage
- TTL exists to prevent the blockchain from being cluttered with inactive contracts
- TTL extension can be done for both the contract instance and the contract code

## Prerequisites

- Stellar SDK: `npm install @stellar/stellar-sdk`
- A Stellar RPC endpoint (e.g., `https://soroban-testnet.stellar.org`)
- Basic knowledge of Stellar SDK
- Stellar SDK: `npm install @stellar/stellar-sdk` for Javascript
- A [Stellar RPC](../../../data/rpc/README.mdx) endpoint (e.g., `https://soroban-testnet.stellar.org`)
- Basic knowledge of the SDK in use

## Methods for Extending TTL

This guide will cover three ways to extend a contract's TTL:

1. 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

2. 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

3. Client Extension: Extending the TTL from an external client using Javascript.
- Use Case: When you need to manage contract TTLs through an external application or automated system.
- Process:
- Get the contract's footprint
- Set a reasonable resource fee (perhaps start at 10,000 stroops as this is a costly operation)
- Set Soroban data as read-only
- Set the desired ledger TTL to expire the contract
- Create an operation `StellarSdk.Operation.extendFootprintTtl`

:::note

## Process overview
A resource fee and base fee are both charged in this operation.

- Get the contract's footprint,
- Set a reasonable resource fee (perhaps start at 10,000 stroops as this is a costly operation),
- Set Soroban data as read-only,
- Set the desired ledger TTL to expire the contract,
- Create an operation `StellarSdk.Operation.extendFootprintTtl`,
- Note that a resource fee and base fee are both charged in this operation.
:::

## JavaScript code
<Tabs groupId="language" defaultValue="rust">
<TabItem value="rust" label="Rust">

```rust
#![no_std]
use soroban_sdk::{contract, contractimpl, Env, symbol_short};

#[contract]
pub struct ExtendTTLContract;

#[contractimpl]
impl ExtendTTLContract {
// Self-extension
pub fn extend_contract_ttl(env: Env, threshold: u32, extend_to: u32) {
env.storage().instance().extend_ttl(threshold, extend_to);
}

// External extension
pub fn extend_other_contract_ttl(env: Env, contract_address: Address, threshold: u32, extend_to: u32) {
let deployer = env.deployer();
deployer.extend_ttl(
contract_address,
threshold,
extend_to
);
}
}
```

- `env.storage().instance().extend_ttl(...)` is called to extend the TTL of the current contract instance.
- `threshold` is a check that ensures that the current TTL of the contract instance is less than the set threshold value.
- `extend_to` is the number of ledgers to be added to the current TTL.
- `contract_address` is 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.

</TabItem>
<TabItem value="js" label="JS">

The code below uses Nodejs environment but same concept can also be applied in the browser using Freighter wallet or using any other [Stellar SDK](../../../tools/sdks/library.mdx).

```javascript
import * as StellarSdk from "@stellar/stellar-sdk";

import { Server } from "@stellar/stellar-sdk/rpc";

async function extendContractWasmTTL(contractId, sourceKeypair) {
Expand Down Expand Up @@ -105,6 +172,11 @@ Why `setReadOnly()`? A few reasons:

3. What's the point? This whole process is about keeping our contract's data alive in the ledger. It's like renewing a lease - we're telling the network "Hey, keep this stuff around longer, we're still using it!"

This is super important for contracts that need to stick around for a while. Without extending the TTL, the contract's data could expire and disappear from the ledger.
This is super important for contracts that need to stick around for a while. Without extending the TTL, the contract's data could expire and disappear from the ledger.

</TabItem>
</Tabs>

Learn how to test the TTL extension in this [guide](../archival/test-ttl-extension.mdx).

Want to dive deeper? Check out the docs on the [Extend Footprint TTL operation](../../../learn/encyclopedia/storage/state-archival.mdx#extendfootprintttlop).
Loading