ERGO
Ergo-Native Features
Intermediate
4-8 hours

Crowdfunding / Assurance Contract Pattern

On-chain crowdfunding where contributors are refunded if target is not reached

問題

You want to raise funds for a project, but contributors need assurance they'll get refunds if the goal isn't met.

解決方案

Assurance contracts collect contributions into a shared box. If the funding goal is reached before deadline, funds go to the project. Otherwise, contributors can claim refunds.

運作方式

  1. 1Project creates campaign box with goal, deadline, and recipient
  2. 2Contributors add funds to campaign (tracked via tokens or registers)
  3. 3Each contribution creates a refund token for the contributor
  4. 4If goal reached: recipient can claim all funds
  5. 5If deadline passes without goal: contributors claim refunds with tokens
  6. 6Refund tokens are burned on claim

程式碼範例

{
  // Crowdfunding campaign box
  // R4: Funding goal (nanoERG)
  // R5: Deadline (block height)
  // R6: Project recipient address
  // R7: Refund token ID
  
  val fundingGoal = SELF.R4[Long].get
  val deadline = SELF.R5[Int].get
  val recipient = SELF.R6[Coll[Byte]].get
  val refundTokenId = SELF.R7[Coll[Byte]].get
  
  val currentFunds = SELF.value
  val goalReached = currentFunds >= fundingGoal
  val deadlinePassed = HEIGHT > deadline
  
  // Success: goal reached, recipient claims
  val successClaim = {
    goalReached &&
    OUTPUTS(0).propositionBytes == recipient &&
    OUTPUTS(0).value >= fundingGoal
  }
  
  // Add contribution (before deadline)
  val addContribution = {
    !deadlinePassed &&
    OUTPUTS(0).propositionBytes == SELF.propositionBytes &&
    OUTPUTS(0).value > SELF.value &&
    OUTPUTS(0).R4[Long].get == fundingGoal &&
    OUTPUTS(0).R5[Int].get == deadline
  }
  
  // Refund claim (deadline passed, goal not reached)
  val refundClaim = {
    deadlinePassed && !goalReached &&
    // Refund token burned in this transaction
    INPUTS.exists(i => 
      i.tokens.exists(t => t._1 == refundTokenId)
    )
  }
  
  successClaim || addContribution || refundClaim
}

Campaign box that accepts contributions, allows success claim if goal met, or refunds if deadline passes.

使用案例

  • Project fundraising
  • Community initiatives
  • Charity campaigns
  • Product pre-orders
  • DAO treasury bootstrapping

安全注意事項

  • !Set realistic deadlines and goals
  • !Test refund mechanism thoroughly
  • !Consider partial funding scenarios
  • !Protect against griefing attacks
  • !Audit token minting and burning logic

實際應用案例

Ergo Raffle

Raffle-style crowdfunding

資源

手續費注意事項

Contributors pay for contribution tx. Refund claims also require fees. Consider fee reserves.

提升您的 ErgoScript 技能

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

Follow for daily updates