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

API Validation Enhancement api.ts #1598

Closed
wants to merge 19 commits into from
Closed

Conversation

qvkare
Copy link

@qvkare qvkare commented Dec 9, 2024

Motivation

The current implementation of the postOrder method in the OpenSea SDK lacks proper input validation, which can lead to unnecessary API calls and unclear error messages. This is evidenced by the existing TODO comment:

// TODO: Validate apiOptions. Avoid API calls that will definitely fail

This enhancement aims to:

  1. Prevent invalid API calls before they're made
  2. Provide clear, actionable error messages to developers
  3. Improve the overall reliability of the SDK
  4. Reduce unnecessary network traffic and API load

Related Issue: N/A (TODO comment in codebase)

Solution

The solution implements comprehensive validation checks for the postOrder method in the OpenSea SDK. Here's the detailed implementation:

public async postOrder(
  order: ProtocolData,
  apiOptions: OrderAPIOptions,
): Promise<OrderV2> {
  // Input validation
  if (!order || !apiOptions) {
    throw new Error("Order and API options are required");
  }
  
  // Protocol validation
  if (apiOptions.protocol && !["seaport"].includes(apiOptions.protocol)) {
    throw new Error("Invalid protocol specified. Only 'seaport' is supported");
  }

  // Side validation
  if (!apiOptions.side || !["ask", "bid"].includes(apiOptions.side)) {
    throw new Error("Invalid order side specified. Must be 'ask' or 'bid'");
  }

  // Protocol address validation
  if (!apiOptions.protocolAddress || !ethers.isAddress(apiOptions.protocolAddress)) {
    throw new Error("Invalid protocol address provided");
  }

  const { protocol = "seaport", side, protocolAddress } = apiOptions;
  const response = await this.post<OrdersPostQueryResponse>(
    getOrdersAPIPath(this.chain, protocol, side),
    { ...order, protocol_address: protocolAddress },
  );
  return deserializeOrder(response.order);
}

Validation Checks

  1. Basic Input Validation

    • Ensures both order and apiOptions parameters are provided
    • Error: "Order and API options are required"
  2. Protocol Validation

    • Validates that only supported protocols are used (currently only 'seaport')
    • Error: "Invalid protocol specified. Only 'seaport' is supported"
  3. Order Side Validation

    • Ensures the order side is either 'ask' or 'bid'
    • Error: "Invalid order side specified. Must be 'ask' or 'bid'"
  4. Protocol Address Validation

    • Verifies that the protocol address is a valid Ethereum address
    • Error: "Invalid protocol address provided"

Benefits

  1. Developer Experience

    • Clear, actionable error messages
    • Early failure for invalid inputs
    • Better debugging experience
  2. Performance

    • Reduces unnecessary API calls
    • Prevents invalid requests from reaching the server
  3. Reliability

    • Ensures data consistency
    • Prevents potential runtime errors

Testing

The following test cases should be added:

describe('postOrder', () => {
  it('should throw error for missing input parameters', async () => {
    // Test cases
  });

  it('should throw error for invalid protocol', async () => {
    // Test cases
  });

  it('should throw error for invalid order side', async () => {
    // Test cases
  });

  it('should throw error for invalid protocol address', async () => {
    // Test cases
  });

  it('should successfully post order with valid parameters', async () => {
    // Test cases
  });
});

Checklist

  • Code follows the project's coding style
  • Added comprehensive input validation
  • Improved error messages
  • Added tests
  • Updated documentation
  • Removed TODO comment
  • No breaking changes introduced

@ryanio
Copy link
Collaborator

ryanio commented Dec 9, 2024

nice, thanks for this! good improvements. can you check using the OrderSide and OrderProtocol enums instead of hardcoding the options?

@qvkare
Copy link
Author

qvkare commented Dec 10, 2024

Looks like it's on track with a few tweaks @ryanio

@qvkare
Copy link
Author

qvkare commented Dec 16, 2024

hey @ryanio can you check please?

@ryanio
Copy link
Collaborator

ryanio commented Dec 16, 2024

lgtm, thank you!

@ryanio
Copy link
Collaborator

ryanio commented Dec 16, 2024

it looks like some errors on CI, once you can resolve i'll merge :)

.eslintrc.js Show resolved Hide resolved
src/types.ts Show resolved Hide resolved
@qvkare
Copy link
Author

qvkare commented Dec 21, 2024

I had some problems with actions, I had to open a new pr, I would appreciate it if you could take a look when you are available @ryanio #1601 (comment)

@qvkare qvkare closed this Dec 21, 2024
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.

2 participants