@@ -2,6 +2,17 @@ use crate::errors::ContractError;
22use crate :: types:: { Config , DataKey , LoanRecord } ;
33use soroban_sdk:: { token, Address , Env , String , Vec } ;
44
5+ /// Ledgers to live for persistent storage entries (~1 year at ~5s/ledger).
6+ const PERSISTENT_TTL_LEDGERS : u32 = 6_307_200 ;
7+
8+ /// Extend the TTL of a persistent storage entry after every write.
9+ /// Call this immediately after `env.storage().persistent().set(key, ...)`.
10+ pub fn extend_ttl ( env : & Env , key : & DataKey ) {
11+ env. storage ( )
12+ . persistent ( )
13+ . extend_ttl ( key, PERSISTENT_TTL_LEDGERS , PERSISTENT_TTL_LEDGERS ) ;
14+ }
15+
516/// Returns true if the address is the all-zeros account or contract address.
617pub fn is_zero_address ( env : & Env , addr : & Address ) -> bool {
718 // Stellar zero account: all-zero 32-byte ed25519 key
@@ -223,3 +234,34 @@ pub fn validate_admin_config(
223234
224235 Ok ( ( ) )
225236}
237+
238+ #[ cfg( test) ]
239+ mod ttl_tests {
240+ use super :: * ;
241+ use crate :: { QuorumCreditContract , QuorumCreditContractClient } ;
242+ use soroban_sdk:: { testutils:: Address as _, Address , Env , Vec } ;
243+
244+ /// Verify extend_ttl does not panic when called on an existing persistent key.
245+ #[ test]
246+ fn test_extend_ttl_does_not_panic ( ) {
247+ let env = Env :: default ( ) ;
248+ env. mock_all_auths ( ) ;
249+
250+ let contract_id = env. register_contract ( None , QuorumCreditContract ) ;
251+ let client = QuorumCreditContractClient :: new ( & env, & contract_id) ;
252+
253+ let deployer = Address :: generate ( & env) ;
254+ let admin = Address :: generate ( & env) ;
255+ let admins = Vec :: from_array ( & env, [ admin. clone ( ) ] ) ;
256+ let token = Address :: generate ( & env) ;
257+
258+ client. initialize ( & deployer, & admins, & 1 , & token) ;
259+
260+ // Write a persistent key then call extend_ttl — must not panic.
261+ env. as_contract ( & contract_id, || {
262+ let key = DataKey :: LoanCount ( deployer. clone ( ) ) ;
263+ env. storage ( ) . persistent ( ) . set ( & key, & 42u32 ) ;
264+ extend_ttl ( & env, & key) ;
265+ } ) ;
266+ }
267+ }
0 commit comments