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

doc: guide for extending a deployed contract's Wasm code TTL #812

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
35 changes: 0 additions & 35 deletions docs/build/guides/cli/extend-contract-wasm.mdx

This file was deleted.

118 changes: 118 additions & 0 deletions docs/build/guides/cli/extend-deployed contract's Wasm Code TTL.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@

# Guide: Extending a Deployed Contract's Wasm Code TTL on Stellar

In this guide, we will walk you through the process of extending the Time-to-Live (TTL) of a deployed WebAssembly (Wasm) contract on the Stellar blockchain using the Stellar CLI.

## Introduction

Stellar contracts have a TTL (Time-to-Live) that dictates how long the contract's Wasm bytecode is retained on the network. You can use the Stellar CLI to extend this TTL, either by using the compiled contract locally or by referencing the Wasm hash of the deployed contract.

## Prerequisites

- **Stellar CLI**: Ensure you have the Stellar CLI installed and configured.
- **Access to the Stellar Network**: Ensure you have access to either the Stellar testnet or mainnet.

## Extending TTL with Local Binary

If you have the compiled Wasm binary on your local machine, you can extend the TTL using the following command:

```bash
stellar contract extend \
--source S... \
--network testnet \
--wasm ../relative/path/to/soroban_contract.wasm \
--ledgers-to-extend 535679 \
--durability persistent
```

### Explanation

- `--source`: The source account from which the transaction will be sent.
- `--network`: The network on which the transaction will be executed (`testnet` or `mainnet`).
- `--wasm`: The relative path to the local Wasm binary.
- `--ledgers-to-extend`: The number of ledgers to extend the TTL by (maximum allowable is 535,679).
- `--durability`: Specifies the durability of the extension (`persistent`).

This command sets the TTL to 535,679 ledgers, approximately 30 days based on an average ledger close time of 5 seconds.

## Extending TTL without Local Binary

If you do not have the compiled Wasm binary on your local machine, you can extend the TTL using the Wasm hash of the installed contract:

```bash
stellar contract extend \
--source S... \
--network testnet \
--wasm-hash <hex-encoded-wasm-hash> \
--ledgers-to-extend 535679 \
--durability persistent
```

### Explanation

- `--wasm-hash`: The hex-encoded hash of the Wasm bytecode.

## Finding the Wasm Hash

To extend the TTL without the local binary, you'll need the Wasm hash of the contract. You can retrieve this using Stellar's RPC interface. Refer to the following documentation to find the Wasm hash:

- [Retrieve Contract Code using JavaScript](../rpc/retrieve-contract-code-js.mdx)
- [Retrieve Contract Code using Python](../rpc/retrieve-contract-code-python.mdx)

## Example Contract for Extending TTL

Here's an example of a simple Soroban contract that includes TTL extension functionality:

### Contract Code

```rust
#![cfg_attr(not(feature = "std"), no_std)]

use soroban_sdk::{contractimpl, contracttype, Env, Symbol, BytesN};

pub struct Contract;

#[contractimpl]
impl Contract {
pub fn extend_ttl(env: Env, extra_time: u64) -> u64 {
// Extend the contract's TTL by the specified amount of time
let current_ttl = env.get_ledger().seq();
let new_ttl = current_ttl + extra_time;
env.set_contract_data(&Symbol::from("ttl"), &new_ttl);
new_ttl
}

pub fn get_ttl(env: Env) -> u64 {
// Retrieve the current TTL
env.get_contract_data(&Symbol::from("ttl")).unwrap_or(0)
}
}
```

### Explanation

- **extend_ttl**: This function extends the contract's TTL by the specified amount of time.
- **get_ttl**: This function retrieves the current TTL of the contract.

## Building and Deploying the Contract

1. **Build the Contract**:

```sh
cargo build --target wasm32-unknown-unknown --release
```

2. **Deploy the Contract**:

Use Stellar CLI or a Stellar SDK to deploy the contract to the network.

3. **Interact with the Contract**:

Use Stellar CLI to call the `extend_ttl` and `get_ttl` functions as needed.

## Conclusion

By following this guide, you can extend the TTL of a deployed Wasm contract on the Stellar network using the Stellar CLI. Make sure to test thoroughly on the testnet before deploying changes to the mainnet.



Loading