Skip to content

Commit 2fb50ba

Browse files
committed
merge guides for extending ttl in js and rust
1 parent 9468be5 commit 2fb50ba

File tree

3 files changed

+172
-202
lines changed

3 files changed

+172
-202
lines changed

docs/build/guides/archival/extend-contract-instance-ttl.mdx

-92
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
title: Extend a deployed contract's TTL with code
3+
description: How to extend the TTL of a deployed contract's Wasm code
4+
---
5+
6+
import Tabs from "@theme/Tabs";
7+
import TabItem from "@theme/TabItem";
8+
9+
# Extending a deployed contract's TTL using code
10+
11+
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.
12+
13+
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.
14+
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
24+
25+
## Prerequisites
26+
27+
- Stellar SDK: `npm install @stellar/stellar-sdk` for Javascript
28+
- A Soroban RPC 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+
- 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+
- 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+
- 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: - 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 A resource fee and base fee are both charged in this operation. :::
48+
49+
<Tabs groupId="language" defaultValue="rust">
50+
<TabItem value="rust" label="Rust">
51+
```rust
52+
#![no_std]
53+
use soroban_sdk::{contract, contractimpl, Env, symbol_short};
54+
55+
#[contract]
56+
pub struct ExtendTTLContract;
57+
58+
#[contractimpl]
59+
impl ExtendTTLContract {
60+
// Self-extension
61+
pub fn extend_contract_ttl(env: Env, threshold: u32, extend_to: u32) {
62+
env.storage().instance().extend_ttl(threshold, extend_to);
63+
}
64+
65+
// External extension
66+
pub fn extend_other_contract_ttl(env: Env, contract_address: Address, threshold: u32, extend_to: u32) {
67+
let deployer = env.deployer();
68+
deployer.extend_ttl(
69+
contract_address,
70+
threshold,
71+
extend_to
72+
);
73+
}
74+
}
75+
```
76+
where:
77+
78+
- `env.storage().instance().extend_ttl(...)` is called to extend the TTL of the current contract instance.
79+
- `threshold` is a check that ensures that the current TTL of the contract instance is less than the set threshold value.
80+
- `extend_to` is the number of ledgers to be added to the current TTL.
81+
- `contract_address` is the address of the contract instance whose TTL we want to extend.
82+
- `env.deployer()` accesses the deployer, which has methods for managing the contract's lifecycle.
83+
- `deployer.extend_ttl(...)` extends the TTL of the specified contract instance.
84+
85+
</TabItem>
86+
87+
<TabItem value="js" label="JS">
88+
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).
89+
90+
```javascript
91+
import * as StellarSdk from "@stellar/stellar-sdk";
92+
93+
import { Server } from "@stellar/stellar-sdk/rpc";
94+
95+
async function extendContractWasmTTL(contractId, sourceKeypair) {
96+
const server = new Server("https://soroban-testnet.stellar.org");
97+
98+
// Create a new transaction builder
99+
const account = await server.getAccount(sourceKeypair.publicKey());
100+
const fee = "200100"; // Base fee plus resource fee
101+
102+
// Get the contract instance
103+
const contract = new StellarSdk.Contract(contractId);
104+
const instance = contract.getFootprint();
105+
106+
// Set the Soroban data and create an operation to extend the contract's TTL
107+
const sorobanData = new StellarSdk.SorobanDataBuilder()
108+
.setResourceFee(200_000)
109+
.setReadOnly([instance])
110+
.build();
111+
const transaction = new StellarSdk.TransactionBuilder(account, {
112+
fee,
113+
networkPassphrase: StellarSdk.Networks.TESTNET, // Use appropriate network
114+
})
115+
.setSorobanData(sorobanData)
116+
.addOperation(
117+
StellarSdk.Operation.extendFootprintTtl({
118+
extendTo: 500_000,
119+
}),
120+
)
121+
.setTimeout(30)
122+
.build();
123+
124+
// Sign and submit the transaction
125+
transaction.sign(sourceKeypair);
126+
const result = await server.sendTransaction(transaction);
127+
128+
console.log(
129+
"Transaction submitted. Result:",
130+
JSON.stringify(result, null, 2),
131+
);
132+
return result;
133+
}
134+
135+
// Usage
136+
const contractId = "CC6MWZMG2JPQEENRL7XVICAY5RNMHJ2OORMUHXKRDID6MNGXSSOJZLLF";
137+
const sourceKeypair = StellarSdk.Keypair.fromSecret(
138+
"SXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
139+
);
140+
141+
extendContractWasmTTL(contractId, sourceKeypair)
142+
.then(console.log)
143+
.catch(console.error);
144+
```
145+
### Breaking Down the Code
146+
147+
Let's walk through the key parts of this function:
148+
149+
1. Setting up the Soroban data: The `SorobanDataBuilder()` is where we prep the Soroban-specific info for our transaction.
150+
151+
- We set a resource fee with `setResourceFee(200_000)`. This covers the extra costs for storage and execution.
152+
- We use `setReadOnly([instance])` to tell the network which contract stuff we need to access. We're using `setReadOnly()` instead of `setReadWrite()` because we're just extending the TTL, not changing any data.
153+
154+
Why `setReadOnly()`? A few reasons:
155+
156+
- It's faster and uses fewer resources
157+
- It's safer - we can't accidentally change data we're not supposed to
158+
- The `ExtendFootprintTTLOp` operation needs it
159+
160+
2. Adding the operation: After setting up the Soroban data, we add the `extendFootprintTtl` operation to our transaction. We're telling it to extend the TTL to 500,000 ledgers with `extendTo: 500_000`.
161+
162+
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!"
163+
164+
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.
165+
166+
</TabItem>
167+
168+
</Tabs>
169+
170+
Learn how to test the TTL extension in this [guide](../archival/test-ttl-extension.mdx).
171+
172+
Want to dive deeper? Check out the docs on the [Extend Footprint TTL operation](../../../learn/encyclopedia/storage/state-archival.mdx#extendfootprintttlop).

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

-110
This file was deleted.

0 commit comments

Comments
 (0)