Skip to content

feat(api): add GET /api/v1/vaults/{id}/harvest/preview — dry-run harvest before commit#747

Open
ruthoreaji-123 wants to merge 2 commits into
Suncrest-Labs:mainfrom
ruthoreaji-123:feature/issue--665
Open

feat(api): add GET /api/v1/vaults/{id}/harvest/preview — dry-run harvest before commit#747
ruthoreaji-123 wants to merge 2 commits into
Suncrest-Labs:mainfrom
ruthoreaji-123:feature/issue--665

Conversation

@ruthoreaji-123

Copy link
Copy Markdown

Summary

Implements a dry-run harvest preview endpoint so the frontend can show users a full fee breakdown before committing an on-chain harvest.

Changes

New endpoint

GET /api/v1/vaults/{id}/harvest/preview?compound=true

Returns gross yield, performance fee, net yield, and (optionally) estimated new shares — without writing anything to the DB or chain.

Service layer (vault_service.go)

  • Added HarvestPreview struct (same shape as HarvestResult, no TxHash)
  • Added PreviewHarvest(ctx, PreviewHarvestInput) method
    • Fetches vault, enforces ownership (→ 403 for non-owners)
    • Computes gross yield via harvestableYield() (clamped to 0 on impairment)
    • Applies 10% performance fee BPS; returns impaired: true when balance < deposited
    • When compound=true, estimates new shares via estimateSharesMinted()
    • No DB writes, no chain calls

Handler layer (vault_handler.go)

  • Registered GET /api/v1/vaults/{id}/harvest/preview
  • Parses ?compound=true query param and delegates to PreviewHarvest

CI fixes

  • internal/stellar/reader.go: fixed invalid int64 shift-by-64 in simulateI128 (go vet failure)
  • internal/handler/user_handler_test.go: added error checks before using HTTP responses (go vet warnings)

Test coverage

Test Scenario
TestPreviewHarvestNormalCase Correct gross/fee/net, no DB mutation
TestPreviewHarvestImpairedCase Zero yields + impaired: true
TestPreviewHarvestCompoundCase estimated_new_shares populated
TestPreviewHarvestForbiddenForOtherUser 403 for non-owner
TestVaultHandlerPreviewHarvestNormalCase HTTP 200 + correct payload
TestVaultHandlerPreviewHarvestImpairedCase HTTP 200 + impaired: true
TestVaultHandlerPreviewHarvestCompoundParam HTTP 200 + estimated_new_shares
TestVaultHandlerPreviewHarvestForbiddenForOtherUser HTTP 403

All 8 tests pass. go build ./..., go vet ./..., and go test -race -short ./... are clean.

Closes #665

@ruthoreaji-123 ruthoreaji-123 requested a review from 0xDeon as a code owner June 25, 2026 23:50
@drips-wave

drips-wave Bot commented Jun 25, 2026

Copy link
Copy Markdown

@ruthoreaji-123 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@0xDeon 0xDeon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge conflict with main. Rebase onto main and resolve conflicts before this can be merged.

- Add HarvestPreview struct and PreviewHarvest service method
- Register GET /api/v1/vaults/{id}/harvest/preview route
- Impaired vault returns gross/fee/net = 0 with impaired: true
- compound=true param returns estimated_new_shares
- Non-owner returns 403 via ErrVaultForbidden
- Add unit tests: normal, impaired, compound, forbidden cases

Fix pre-existing CI failures:
- cmd/api/main.go: remove unused notificationDispatcher using ws.Hub
  (ws.Hub does not implement notifications.WebSocketHub)
- savings_goal_service_test.go: fix map key type and missing notifier args
- savings_schedule_service_test.go: add missing interface methods to stubs
- recurring_deposit_integration_test.go: break service<->postgres import
  cycle; fix wrong migration filename 038_ -> 039_

Closes Suncrest-Labs#665
- stellar/reader.go: fix invalid int64 shift-by-64 in simulateI128;
  read i128 value correctly from hi/lo parts
- user_handler_test.go: add error checks before using HTTP responses
  to silence 'using X before checking error' vet warnings
@ruthoreaji-123

Copy link
Copy Markdown
Author

@0xDeon All requested changes have been addressed and the branch has been rebased
onto main. Ready for review

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(api): add GET /api/v1/vaults/{id}/harvest/preview — dry-run harvest before commit

2 participants