Skip to content

Commit 3c55193

Browse files
add guide for extending a contract instance's ttl (#1030)
* merge guides for extending ttl in rust and js * fix formatting * markdown formatting * more formatting; add a link --------- Co-authored-by: Elliot Voris <[email protected]>
1 parent 2e2cff5 commit 3c55193

File tree

1 file changed

+89
-17
lines changed

1 file changed

+89
-17
lines changed

docs/build/guides/conventions/extending-wasm-ttl.mdx

+89-17
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,105 @@
11
---
2-
title: Extend a deployed contracts TTL with code
3-
description: How to extend the TTL of a deployed contract's Wasm code using JavaScript SDK
2+
title: Extend a deployed contract's TTL with code
3+
description: How to extend the TTL of a deployed contract's Wasm code
44
---
55

6-
# Extending a deployed contract’s TTL using code
6+
import Tabs from "@theme/Tabs";
7+
import TabItem from "@theme/TabItem";
8+
9+
# Extending a deployed contract's TTL using code
710

811
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.
912

1013
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.
1114

12-
This guide will show you how to extend the TTL of a deployed contract's Wasm code using JavaScript.
15+
This guide will show you how to extend the TTL of a deployed contract's Wasm code using the Javascript and Rust SDKs.
16+
17+
## Understanding TTL in Soroban
18+
19+
Before we demonstrate the TTL extension methods, you should note that in Soroban:
20+
21+
- Contract instances and code are stored in the instance storage
22+
- TTL exists to prevent the blockchain from being cluttered with inactive contracts
23+
- TTL extension can be done for both the contract instance and the contract code
1324

1425
## Prerequisites
1526

16-
- Stellar SDK: `npm install @stellar/stellar-sdk`
17-
- A Stellar RPC endpoint (e.g., `https://soroban-testnet.stellar.org`)
18-
- Basic knowledge of Stellar SDK
27+
- Stellar SDK: `npm install @stellar/stellar-sdk` for Javascript
28+
- A [Stellar RPC](../../../data/rpc/README.mdx) endpoint (e.g., `https://soroban-testnet.stellar.org`)
29+
- Basic knowledge of the SDK in use
30+
31+
## Methods for Extending TTL
32+
33+
This guide will cover three ways to extend a contract's TTL:
34+
35+
1. Self-Extension: Extending the TTL from within the contract itself, in Rust.
36+
37+
- Use case: When a contract needs to manage its own lifetime
38+
- Process: Directly accessing the contract's instance storage to extend its TTL
39+
40+
2. External Extension: Extending the TTL from another contract (the deployer), in Rust.
41+
42+
- Use case: When managing multiple contract instances or implementing administrative control
43+
- Process: Using the deployer's authority to extend TTL for any contract it has deployed
44+
45+
3. Client Extension: Extending the TTL from an external client using Javascript.
46+
- Use Case: When you need to manage contract TTLs through an external application or automated system.
47+
- Process:
48+
- Get the contract's footprint
49+
- Set a reasonable resource fee (perhaps start at 10,000 stroops as this is a costly operation)
50+
- Set Soroban data as read-only
51+
- Set the desired ledger TTL to expire the contract
52+
- Create an operation `StellarSdk.Operation.extendFootprintTtl`
53+
54+
:::note
1955

20-
## Process overview
56+
A resource fee and base fee are both charged in this operation.
2157

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

29-
## JavaScript code
60+
<Tabs groupId="language" defaultValue="rust">
61+
<TabItem value="rust" label="Rust">
62+
63+
```rust
64+
#![no_std]
65+
use soroban_sdk::{contract, contractimpl, Env, symbol_short};
66+
67+
#[contract]
68+
pub struct ExtendTTLContract;
69+
70+
#[contractimpl]
71+
impl ExtendTTLContract {
72+
// Self-extension
73+
pub fn extend_contract_ttl(env: Env, threshold: u32, extend_to: u32) {
74+
env.storage().instance().extend_ttl(threshold, extend_to);
75+
}
76+
77+
// External extension
78+
pub fn extend_other_contract_ttl(env: Env, contract_address: Address, threshold: u32, extend_to: u32) {
79+
let deployer = env.deployer();
80+
deployer.extend_ttl(
81+
contract_address,
82+
threshold,
83+
extend_to
84+
);
85+
}
86+
}
87+
```
88+
89+
- `env.storage().instance().extend_ttl(...)` is called to extend the TTL of the current contract instance.
90+
- `threshold` is a check that ensures that the current TTL of the contract instance is less than the set threshold value.
91+
- `extend_to` is the number of ledgers to be added to the current TTL.
92+
- `contract_address` is the address of the contract instance whose TTL we want to extend.
93+
- `env.deployer()` accesses the deployer, which has methods for managing the contract's lifecycle.
94+
- `deployer.extend_ttl(...)` extends the TTL of the specified contract instance.
95+
96+
</TabItem>
97+
<TabItem value="js" label="JS">
3098

3199
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).
32100

33101
```javascript
34102
import * as StellarSdk from "@stellar/stellar-sdk";
35-
36103
import { Server } from "@stellar/stellar-sdk/rpc";
37104

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

106173
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!"
107174

108-
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.
175+
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.
176+
177+
</TabItem>
178+
</Tabs>
179+
180+
Learn how to test the TTL extension in this [guide](../archival/test-ttl-extension.mdx).
109181

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

0 commit comments

Comments
 (0)