feat(marketplace): x402 micro-payment flow for skill invocation - #442
Open
Peolite001 wants to merge 9 commits into
Open
feat(marketplace): x402 micro-payment flow for skill invocation#442Peolite001 wants to merge 9 commits into
Peolite001 wants to merge 9 commits into
Conversation
- Add x402 payment middleware for agent-to-agent skill calls - Implement POST /api/marketplace/skills/[skillId]/invoke with payment validation - Add invocation ledger tracking txHash, amountXLM, agentId, skillId - Handle 402 -> payment -> retry flow with Stellar settlement - Support insufficient_balance error propagation - Add unit tests with mocked x402 flow
Added invocation recording and status updating for payment processing.
Added mock restoration to ensure subsequent tests use mock path.
Restores mock-mode to true before each test and removes redundant mock-mode restoration after the insufficient balance test.
|
Contributor
Author
|
done, pls review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Closes #304
feat(marketplace): x402 micro-payment flow for skill invocation
Summary
Implements the x402 micro-payment flow for the skills marketplace. When an agent calls a skill from another agent, the x402 HTTP payment protocol handles the micro-payment automatically before the skill runs.
Changes
New Files
lib/marketplace/x402-middleware.ts— Core payment middlewareinvokeSkillWithPayment(): Attempts skill call → captures 402 → generates Stellar payment → retries withX-PaymentheadergenerateStellarPayment(): Builds and submits payment tx via existing/api/stellar/build-tx+/api/stellar/submit-txMOCK_MODEfor local developmentlib/marketplace/invocation-ledger.ts— Payment ledger store{ agentId, skillId, amountXLM, txHash, invokedAt, status }listInvocations(),getInvocationStats()lib/marketplace/skill-registry.ts— Bridge to PR#304 skill listingsregisterSkill(),getSkill(),listSkills(),incrementSkillInvocation()app/api/marketplace/skills/[skillId]/invoke/route.ts— Invoke endpoint{ agentId, payload, payerWallet? }{ ok, data, paymentProof, invocationId }on success402 { error: 'insufficient_balance' }on underfunded walletapp/api/marketplace/invocations/route.ts— Ledger query endpoint?agentId=&skillId=&txHash=&status=&since=&limit=lib/marketplace/__tests__/x402-middleware.test.ts— Unit testsArchitecture
Agent A ──POST /api/marketplace/skills/skill_123/invoke──┐
{ agentId: "A", payload: {...} } │
▼
┌─────────────────────┐
│ x402-middleware.ts │
│ 1. attemptSkillRequest
│ 2. if 402 → create quote
│ 3. generateStellarPayment
│ 4. settleX402 on-chain
│ 5. retry w/ X-Payment header
└──────────┬──────────┘
│
┌──────────▼──────────┐
│ invocation-ledger │
│ record → update │
│ (txHash, amountXLM) │
└─────────────────────┘
Acceptance Criteria
{ error: 'insufficient_balance' }Testing