Skip to content

feat(auto-drive): send requestedBytes in createPaymentIntent and handle credit cap rejection - #694

Open
yeziR4 wants to merge 3 commits into
autonomys:mainfrom
yeziR4:feat/create-payment-intent-requested-bytes
Open

yeziR4 wants to merge 3 commits into
autonomys:mainfrom
yeziR4:feat/create-payment-intent-requested-bytes

Conversation

@yeziR4

@yeziR4 yeziR4 commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Resolves #691

Overview

This PR updates createPaymentIntent in @autonomys/auto-drive according to the specifications in #691:

  1. Input Validation: Added positive safe integer validation for sizeBytes, rejecting non-integers, <= 0, NaN, and Infinity with a clean TypeError instead of leaking internal BigInt errors.
  2. requestedBytes Payload: Updated createPaymentIntent to send { requestedBytes: sizeBytes.toString() } in the POST /intents request body so server-side credit-cap checks can be performed prior to on-chain payment.
  3. Structured Error Handling (CreditCapExceededError): Created and exported CreditCapExceededError (with code 'CREDIT_CAP_EXCEEDED') allowing callers to programmatically catch and branch on over-cap rejections. Parses JSON error response bodies to surface clean error messages rather than raw JSON strings.
  4. Documentation: Updated docstrings to accurately document the request body and credit-cap error behavior.
  5. Unit Tests: Added 7 unit tests in packages/auto-drive/__test__/payment.test.ts covering validation rules, payload structure, credit cap errors, and general error handling.

Verification

  • yarn workspace @autonomys/auto-drive build compiles with 0 errors.
  • Jest unit tests pass 7/7 green (packages/auto-drive/__test__/payment.test.ts).
  • Formatted with Prettier per repository config.

@yeziR4

yeziR4 commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

Hi @EmilFattakhov

I've opened this PR to resolve #691 per your specifications:

  • Enforced positive safe integer validation for sizeBytes, rejecting non-integers, <= 0, NaN, and Infinity with a clean TypeError.
  • Updated createPaymentIntent to forward { requestedBytes: sizeBytes.toString() } with Content-Type: application/json in POST /intents so backend credit-cap checks are performed prior to on-chain payment.
  • Created and exported CreditCapExceededError with code 'CREDIT_CAP_EXCEEDED' allowing callers to cleanly catch and branch on over-cap rejections, and parsing JSON error messages rather than leaking raw JSON strings.
  • Updated docstrings to document the payload and error behavior.
  • Added comprehensive unit test suite in packages/auto-drive/__test__/payment.test.ts (7/7 tests passing green).

Ready for review whenever you have time. Thank you

@yeziR4

yeziR4 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

@jim-counter

@EmilFattakhov EmilFattakhov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for your contribution @yeziR4! A couple of blocking comments that should be relatively easy to address.

throw new CreditCapExceededError(parsed.message || 'Credit cap exceeded')
}

const errorMessage = parsed?.message || body

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This only covers only covers the coded errors. Auto Drive's HttpError.handleResponse sends { error: <code>, message } when an error carries a machine-readable code, but { error: <the message> } — no message key — for every uncoded one.

| 'EXPIRED'
| 'FAILED'
| 'OVER_CAP'
'PENDING' | 'CONFIRMED' | 'COMPLETED' | 'EXPIRED' | 'FAILED' | 'OVER_CAP'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unrelated reformat — please restore the one-member-per-line union.

Comment thread packages/auto-drive/jest.config.js Outdated
@@ -0,0 +1,13 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Deviates from convention, what's this module is used for? Please drop it unless there's a clear reason

@EmilFattakhov

Copy link
Copy Markdown
Member

What would a nice addition: since #691 POST /intents also gained paymentMethod (usdc_eth), that would be great if you could include this as a part of that PR as well. Optional though, could be addressed separately.

@yeziR4

yeziR4 commented Sep 20, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review @EmilFattakhov! All review comments have been addressed in commit \575ab5eb:

  1. Uncoded Error Response Handling: Updated error message extraction to check \parsed?.message || (typeof parsed?.error === 'string' && parsed.error !== 'CREDIT_CAP_EXCEEDED' ? parsed.error : body). This seamlessly supports both coded errors ({ error: , message }) and uncoded errors ({ error: }) from Auto Drive's \HttpError.handleResponse. Added an explicit unit test for uncoded responses.
  2. Restored Union Formatting: Restored the one-member-per-line formatting for \PaymentIntentStatus.
  3. Dropped \jest.config.js: Removed \packages/auto-drive/jest.config.js\ and dropped the package-level test script from \package.json\ to adhere to repo conventions.

Ready for review, thank you!

This branch has not been deployed

No deployments
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.

Feature: createPaymentIntent doesn't send requestedBytes, so SDK users skip the server-side credit-cap check

2 participants