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

問題

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

解決方案

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

運作方式

  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

程式碼範例

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

使用案例

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

安全注意事項

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

資源

手續費注意事項

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

提升您的 ErgoScript 技能

獲取新模式、教學和開發者資源的通知。

Follow for daily updates