ERGO
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

Problema

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

Solución

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

Cómo funciona

  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

Ejemplos de código

{
  // 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.

Casos de uso

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

Consideraciones de seguridad

  • !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

Recursos

Consideraciones de comisiones

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

Mejora tus habilidades en ErgoScript

Recibe notificaciones sobre nuevos patrones, tutoriales y recursos para desarrolladores.

Follow for daily updates