Ergo-Native Features
Intermediate
2-4 hours

Storage-Rent Aware Contract (Long-Lived State)

Design contracts that survive storage rent: state refresh and rent safety

Problem

Ergo's storage rent can consume boxes after ~4 years if not maintained. Long-lived contracts need to survive rent collection.

Solution

Design contracts with rent awareness: sufficient ERG reserves, refresh mechanisms, and state recreation patterns. Anyone can refresh a box to reset the rent timer.

How It Works

  1. 1Ergo charges ~0.14 ERG/byte per 4 years for storage
  2. 2Boxes below minimum value can be collected by miners after rent period
  3. 3Refresh = spend and recreate box with same state (resets timer)
  4. 4Anyone can refresh (no signature needed if state preserved)
  5. 5Keep sufficient ERG reserves for multiple rent periods
  6. 6Design state to be recoverable if box is collected

Code Examples

{
  // State box that anyone can refresh
  val stateNFT = SELF.tokens(0)._1  // Unique identifier
  val stateData = SELF.R4[Coll[Byte]].get
  val lastRefresh = SELF.R5[Int].get
  
  // Minimum ERG to survive rent (conservative estimate)
  val minRentReserve = 10000000L  // 0.01 ERG
  
  // State operations by owner
  val ownerPK = SELF.R6[SigmaProp].get
  val ownerUpdate = {
    val newState = OUTPUTS(0)
    newState.tokens(0)._1 == stateNFT &&
    newState.value >= minRentReserve &&
    ownerPK
  }
  
  // Anyone can refresh (preserves state, resets rent timer)
  val anyoneRefresh = {
    val refreshedBox = OUTPUTS(0)
    
    // State must be preserved exactly
    val statePreserved = 
      refreshedBox.tokens(0)._1 == stateNFT &&
      refreshedBox.R4[Coll[Byte]].get == stateData &&
      refreshedBox.R6[SigmaProp].get == ownerPK &&
      refreshedBox.propositionBytes == SELF.propositionBytes
    
    // Value must be maintained or increased
    val valueOk = refreshedBox.value >= SELF.value
    
    // Update refresh timestamp
    val timestampUpdated = refreshedBox.R5[Int].get == HEIGHT
    
    statePreserved && valueOk && timestampUpdated
  }
  
  ownerUpdate || anyoneRefresh
}

State box that anyone can refresh without owner's signature. Preserves state exactly while resetting rent timer.

Use Cases

  • Long-lived protocol state
  • DAO treasuries and governance
  • Oracle pool boxes
  • DEX liquidity pools
  • NFT collections with metadata

Security Considerations

  • !Always keep sufficient ERG reserves
  • !Design state to be recoverable if collected
  • !Consider community-run refresh services
  • !Monitor box ages and refresh proactively
  • !Test rent scenarios on testnet

Resources

Fee Considerations

Refresh transactions cost standard fees. Budget for periodic refreshes (~1 per year minimum).

Level Up Your ErgoScript Skills

Get notified about new patterns, tutorials, and developer resources.

Follow for daily updates