Skip to content

MaintenancePoolService.deposit creates a second escrow when it loses the first-deposit race, which is the orphaned escrow #275 was meant to prevent #781

Closed

Description

@chonilius

The race-loser branch (src/maintenance-pool/maintenance-pool.service.ts:89-100):

      if (updateResult.affected === 0) {
        // Another request won the race — top up the winner's escrow instead.
        const existingPool = await this.findOne(id);
        await this.escrowService.fund({ amount, asset: pool.asset, funderAddress, maintenancePoolId: pool.id });
        await this.poolRepo.increment({ id }, 'balance', Number(amount));
        return this.findOne(id);
      }

The comment says "top up the winner's escrow instead" — and the code calls escrowService.fund again, which creates a new Escrow row (and a new on-chain fund) rather than depositing into the existing one. existingPool is fetched and never used, so the winner's escrowId is read and discarded.

Why it matters: the loser has already created one escrow before the conditional update ran (:77-82), so this branch creates a second. The pool ends up pointing at the winner's escrow while two further escrow rows exist with maintenancePoolId set and nothing referencing them — orphans, which is precisely what #275's lock was added to avoid. pool.balance is incremented for money that is locked in an escrow the pool will never withdraw from.

The non-race path has the same shape: subsequent deposits call escrowService.fund (:103-108), creating a new escrow per deposit, while the contract's model is repeated deposit() calls against one pool. So a pool with twelve monthly deposits has twelve escrow rows and pool.escrowId pointing at the first.

Fix: add a deposit path to EscrowService that invokes the pool contract's deposit(pool_id, sponsor, token, amount) against the existing escrow row instead of calling fund, and have both branches here use it. Then pool.escrowId is the single escrow for the pool's lifetime, which is what poolWithdraw already assumes.

Activity

  1. added
    bug
    on Nov 12, 2026
  2. guptakumarranjeet150 commented on Nov 12, 2026

    @guptakumarranjeet150

    Hi team,

    I analyzed "MaintenancePoolService.deposit creates a second escrow when it loses the first-deposit race, which is the orphaned escrow #275 was meant to prevent" in MergeFi/backend and have a concrete technical roadmap to resolve it.

    I have reviewed the problem description and acceptance criteria for this issue. I have a clear, tested implementation ready with defensive error handling and 100% green CI proof.

    I will deliver a clean, well-tested, and production-ready PR promptly upon assignment. Kindly assign this issue to me.

    Thank you!

  3. added
    Stellar Wave
    on Nov 19, 2026
  4. Umubioghenemaro commented on Nov 19, 2026

    @Umubioghenemaro

    @Umubioghenemaro has applied to work on this issue as part of the Stellar Wave Program's 11th wave.

    I’m interested in contributing to this issue. I’ll begin by reviewing the existing architecture and related code, then implement the requested changes while following the project’s existing conventions. I’ll include appropriate tests and keep the PR focused, documented, and easy to review.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @Umubioghenemaro to this issue.

  5. maikNeta commented on Nov 19, 2026

    @maikNeta
    Contributor

    @maikNeta has applied to work on this issue as part of the Stellar Wave Program's 11th wave.

    hi maintainer let me fix this please

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @maikNeta to this issue.

  6. drips-wave commented on Nov 19, 2026

    @drips-wave

    Congratulations, @maikNeta! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on November 25, 2026.

    🧑‍💻 @maikNeta: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

    Warning

    When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

    🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

    🌊 Happy Wave 🌊

  7. added a commit that references this issue on Nov 20, 2026
    c29a86d
  8. drips-wave commented on Nov 20, 2026

    @drips-wave

    This issue has been completed by @maikNeta as part of the Stellar Wave Program's 11th Wave 🥳

    😎 @maikNeta: You earned 200 Points for completing this issue! Once the Wave's rewards are finalized, you'll be eligible for a percentage of the Wave's reward pool based on the percentage of total points you've earned. Learn more here. You can also Leave a review to share your experience working on this issue.

    🧑‍💻 Repo maintainers: How'd the contributor do? Leave a review to share your experience working with them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Stellar Wavebug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions