Skip to main content
In order to test contracts that extend the contract data TTL via extend_ttl storage operations, you can use the TTL getter operation (get_ttl) in combination with manipulating the ledger sequence number. Note, that get_ttl function is only available for tests and only in Soroban SDK v21+.

Example

Follow along the example that tests TTL extensions. The example has extensive comments, this document just highlights the most important parts. We use a very simple contract that only extends an entry for every Soroban storage type:
The focus of the example is the tests, so the following code snippets come from test.rs. It’s a good idea to define the custom values of TTL-related network settings, since the defaults are defined by the SDK and aren’t immediately obvious for the reader of the tests:
You could also use the current network settings when setting up the tests, but keep in mind that these are subject to change, and the contract should be able to work with any values of these settings. Now we run a test scenario that verifies the TTL extension logic (see test_extend_ttl_behavior test for the full scenario). First, we setup the data and ensure that the initial TTL values correspond to the network settings we’ve defined above:
Notice, that we use env.as_contract(...) in order to access the contract’s storage. Then we call the TTL extension operations and verify that they behave as expected, for example:
In order to test the extension thresholds (i.e. maximum current TTL that requires extension), we need to increase the ledger sequence number:
Then we can extend the entries again and ensure that only entries that are below threshold have been extended (specifically, persistent and temporary entries in this example):
Soroban SDK also emulates the behavior for the entries that have their TTL expired. Temporary entries behave ‘as if’ they were deleted (see test_temp_entry_removal test for the full scenario):
Persistent entries are more subtle: when a transaction that is executed on-chain contains a persistent entry that has been archived (i.e., it has its TTL expired) in the footprint, then the entry will be automatically restored. Automatic restoration is mostly transparent, the main side effect is the increased fees. (see test_persistent_entry_auto_restored test for the full scenario):

Testing TTL extension for other contract instances

Sometimes a contract may want to extend TTL of another contracts and/or their Wasm entries (usually that would happen in factory contracts). This logic may be covered in a similar fashion to the example above using env.deployer().get_contract_instance_ttl(&contract) to get TTL of any contract’s instance, and env.deployer().get_contract_code_ttl(&contract) to get TTL of any contract’s Wasm entry. You can find an example of using these functions in the SDK test suite.