ERGO
Ergo-Native Features
Intermediate
4-8 hours

Crowdfunding / Assurance Contract Pattern

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

Problem

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

Çözüm

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.

Nasıl Çalışır

  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

Kod Örnekleri

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

Kullanım Alanları

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

Güvenlik Hususları

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

Gerçek Dünya Uygulamaları

Ergo Raffle

Raffle-style crowdfunding

Kaynaklar

Ücret Hususları

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

ErgoScript Becerilerinizi Geliştirin

Yeni kalıplar, öğretici içerikler ve geliştirici kaynakları hakkında bildirim alın.

Follow for daily updates