Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhanced gas and amount validation #951

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

qvkare
Copy link

@qvkare qvkare commented Dec 15, 2024

Enhances the validation logic for gas and amount parameters in the Quote API, ensuring robust handling of edge cases and preventing potential overflow issues.

1. Amount Validation (quote-schema.ts)

  • Added BigInt extension for amount validation
  • Implemented checks for:
    • Negative amounts (rejected)
    • Zero amounts (rejected)
    • Decimal values (rejected)
    • Values exceeding uint256 (rejected)
    • Maximum uint256 value (accepted)
// Added MAX_UINT256 constant
export const MAX_UINT256 = BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')

// Enhanced QuoteQueryParamsJoi schema
amount: Joi.string()
  .pattern(/^[0-9]+$/)
  .custom((value, helpers) => {
    try {
      const amount = BigInt(value)
      if (amount <= 0n) {
        return helpers.error('Amount must be greater than 0')
      }
      if (amount > MAX_UINT256) {
        return helpers.error('Amount must not exceed maximum uint256 value')
      }
      return value
    } catch {
      return helpers.error('Invalid amount format')
    }
  })

2. Test Coverage (quote.test.ts)

Added comprehensive test cases for amount validation:

// Test cases for amount validation
it('rejects negative amounts')
it('rejects zero amounts')
it('rejects decimal values')
it('rejects amounts larger than uint256')
it('accepts maximum uint256 value')

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.

1 participant