Skip to content

Commit

Permalink
docs(soroban): add extendInstanceTtl builtin documentation
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <tareknaser360@gmail.com>
  • Loading branch information
tareknaser committed Feb 16, 2025
1 parent 137022a commit 0020820
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
18 changes: 17 additions & 1 deletion docs/language/builtins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,6 @@ Assuming `arg1` is 512 and `arg2` is 196, the output to the log will be ``foo en
Be mindful this will increase the gas cost. Larger values will incur a higher gas cost.
Alternatively, use a hexadecimal ``{:x}`` format specifier to reduce the cost.

.. _extendPersistentTtl:

extendTtl(uint32 threshold, uint32 extend_to)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Expand All @@ -694,3 +693,20 @@ TTL = live_until_ledger_seq - current_ledger
For more details on managing contract data TTLs in Soroban, refer to the docs for `TTL <https://developers.stellar.org/docs/build/smart-contracts/getting-started/storing-data#managing-contract-data-ttls-with-extend_ttl>`_.

extendInstanceTtl(uint32 threshold, uint32 extend_to)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

The extendInstanceTtl() function extends the time-to-live (TTL) of contract instance storage.

If the TTL for the current contract instance and code (if applicable) is below threshold ledgers, this function extends ``live_until_ledger_seq`` such that TTL equals ``extend_to``.

.. note:: This is a global function, not a method, and is only available on the Soroban target

.. code-block:: solidity
/// Extends the TTL for the contract instance storage to 10000 ledgers
/// if the current TTL is smaller than 2000 ledgers
function extendInstanceTtl() public view returns (int64) {
return extendInstanceTtl(2000, 10000);
}
2 changes: 1 addition & 1 deletion src/sema/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub static BUILTIN_FUNCTIONS: Lazy<[Prototype; 28]> = Lazy::new(|| {
builtin: Builtin::ExtendInstanceTtl,
namespace: None,
method: vec![],
name: "extend_instance_ttl",
name: "extendInstanceTtl",
params: vec![Type::Uint(32), Type::Uint(32)],
ret: vec![Type::Int(64)],
target: vec![Target::Soroban],
Expand Down
22 changes: 11 additions & 11 deletions tests/soroban_testcases/ttl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn ttl_instance_wrong() {
r#"contract instance_counter {
uint64 instance instanceCount = 3;
function extend_instance_ttl() public view returns (int64) {
function extendInstanceTtl() public view returns (int64) {
return instanceCount.extendTtl(700, 3000);
}
}"#,
Expand All @@ -152,8 +152,8 @@ fn ttl_instance_correct() {
/// Extends the TTL for the instance storage to 10000 ledgers
/// if the current TTL is smaller than 2000 ledgers
function extend_instance_ttl() public view returns (int64) {
return extend_instance_ttl(2000, 10000);
function extendInstanceTtl() public view returns (int64) {
return extendInstanceTtl(2000, 10000);
}
}"#,
|env| {
Expand Down Expand Up @@ -185,7 +185,7 @@ fn ttl_instance_correct() {
});

// Extend instance TTL to 10000 ledgers
runtime.invoke_contract(addr, "extend_instance_ttl", vec![]);
runtime.invoke_contract(addr, "extendInstanceTtl", vec![]);
runtime.env.as_contract(addr, || {
assert_eq!(runtime.env.storage().instance().get_ttl(), 10000);
});
Expand All @@ -210,8 +210,8 @@ fn ttl_combined() {
return tCount.extendTtl(3000, 7000);
}
function extend_instance_ttl() public view returns (int64) {
return extend_instance_ttl(2000, 10000);
function extendInstanceTtl() public view returns (int64) {
return extendInstanceTtl(2000, 10000);
}
}"#,
|env| {
Expand Down Expand Up @@ -264,7 +264,7 @@ fn ttl_combined() {
});

// Extend instance storage TTL
runtime.invoke_contract(addr, "extend_instance_ttl", vec![]);
runtime.invoke_contract(addr, "extendInstanceTtl", vec![]);
runtime.env.as_contract(addr, || {
assert_eq!(runtime.env.storage().instance().get_ttl(), 10000);
});
Expand Down Expand Up @@ -313,7 +313,7 @@ fn ttl_combined() {

// Re-extend all TTLs
runtime.invoke_contract(addr, "extend_persistent_ttl", vec![]);
runtime.invoke_contract(addr, "extend_instance_ttl", vec![]);
runtime.invoke_contract(addr, "extendInstanceTtl", vec![]);
runtime.invoke_contract(addr, "extend_temp_ttl", vec![]);

// Final TTL verification
Expand Down Expand Up @@ -352,8 +352,8 @@ fn test_persistent_entry_archival() {
return pCount.extendTtl(1000, 10000);
}
function extend_instance_ttl() public view returns (int64) {
return extend_instance_ttl(2000, 10000);
function extendInstanceTtl() public view returns (int64) {
return extendInstanceTtl(2000, 10000);
}
}"#,
|env| {
Expand All @@ -369,7 +369,7 @@ fn test_persistent_entry_archival() {
let addr = runtime.contracts.last().unwrap();

// Extend instance TTL
runtime.invoke_contract(addr, "extend_instance_ttl", vec![]);
runtime.invoke_contract(addr, "extendInstanceTtl", vec![]);

// Bump ledger sequence by 10001 (one past persistent TTL)
runtime.env.ledger().with_mut(|li| {
Expand Down

0 comments on commit 0020820

Please sign in to comment.