Skip to content

feat(keys): add trade execution simulation endpoint - #891

Closed
TS-mfon wants to merge 1 commit into
accesslayerorg:mainfrom
TS-mfon:fix/issue-870-simulate-endpoint
Closed

TS-mfon wants to merge 1 commit into
accesslayerorg:mainfrom
TS-mfon:fix/issue-870-simulate-endpoint

Conversation

@TS-mfon

@TS-mfon TS-mfon commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Implements the trade execution simulation endpoint, allowing prospective buyers and sellers to preview bonding curve pricing, average price per unit, and percentage price impact prior to submitting on-chain transactions.

Closes #870


Implementation Details

  • Endpoint: GET /api/v1/keys/:keyId/simulate?quantity=N&side=buy|sell
  • Multi-Quantity Support: Accepts comma-separated quantities (e.g. quantity=1,5,10,25) to preview tiered execution scenarios in a single round-trip.
  • Constant-Time Integration: Uses closed-form integral evaluation over milestone curve intervals ($\int_{s_0}^{s_1} P(s) ds$) to achieve $O(1)$ runtime and eliminate event-loop blocking from discrete step loops.
  • Safety & Validation:
    • Enforces batch size limits (maximum 20 quantities per request).
    • Enforces quantity ceiling (maximum 1,000,000 keys per entry).
    • Rejects sell orders where quantity exceeds circulating supply with an explicit 422 error.
  • Caching: Previews cached in Redis for 15 seconds (cache:keys:simulate:{keyId}:{side}:{quantities}).

Testing

  • Unit test suite added in src/modules/keys/__tests__/key-simulate.test.ts verifying:
    • 422 for invalid/missing side or non-positive quantities.
    • 404 for nonexistent key.
    • 422 rejection when sell quantity exceeds circulating supply.
    • Buy simulation calculations across single and batch quantities.
    • Sell simulation calculations and Redis 15s caching.
  • All 6 unit tests pass; TypeScript build passes cleanly.

Copilot AI lite review requested due to automatic review settings September 24, 2026 16:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

…integration

Closes accesslayerorg#870

- Add GET /api/v1/keys/:keyId/simulate supporting single and comma-separated batch quantities
- Implement constant-time O(1) bonding curve integration across milestone tiers
- Enforce batch size limits (max 20) and individual quantity limits (max 1,000,000)
- Reject sell requests where quantity exceeds circulating supply with 422 error
- Cache preview results in Redis for 15 seconds
@TS-mfon

TS-mfon commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #1007, which carries all four issues (#866, #869, #870, #872) on one branch rebased on latest main.

Splitting these four across separate PRs meant they collided with each other: all four touch src/modules/keys/keys.routes.ts, and #869/#872 both touch src/modules/creator/creator.routes.ts and the prisma schema, so whichever merged first would break the rest. #1007 also addresses the Copilot findings raised here — the migration moved to prisma/schema/migrations and non-finite milestone values are now rejected.

Closing this one in favour of #1007.

@TS-mfon TS-mfon closed this Oct 1, 2026
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.

Add a simulate buy and sell endpoint returning price preview without mutating state

2 participants