Skip to content

Fix allowance check for deposits#1901

Merged
hardyjosh merged 3 commits intomainfrom
1900-getapprovalcalldata-should-return-an-error-ie-no-approval-required-if-deposit-allowance
May 30, 2025
Merged

Fix allowance check for deposits#1901
hardyjosh merged 3 commits intomainfrom
1900-getapprovalcalldata-should-return-an-error-ie-no-approval-required-if-deposit-allowance

Conversation

@hardingjam
Copy link
Copy Markdown
Contributor

@hardingjam hardingjam commented May 28, 2025

Motivation

Fixes #1900

Solution

Checks

By submitting this for review, I'm confirming I've done the following:

  • made this PR as small as possible
  • unit-tested any new functionality
  • linked any relevant issues or PRs
  • included screenshots (if this involves a front-end change)

Summary by CodeRabbit

  • Bug Fixes

    • Improved validation for vault approval requests to ensure approval is only generated when the requested amount is strictly greater than the current allowance.
  • Tests

    • Added test cases to verify that approval requests for amounts equal to or less than the current allowance are correctly rejected.

@hardingjam hardingjam requested review from findolor and hardyjosh May 28, 2025 09:35
@hardingjam hardingjam self-assigned this May 28, 2025
@hardingjam hardingjam added the bug Something isn't working label May 28, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 28, 2025

Walkthrough

The condition in the get_vault_approval_calldata function was changed from requiring the allowance to be strictly greater than the deposit amount to requiring it to be greater than or equal. Corresponding test cases were added and updated to verify that the function returns an error when the allowance is equal to or greater than the deposit amount.

Changes

Files/Paths Change Summary
crates/js_api/src/subgraph/vault.rs Modified the conditional check in get_vault_approval_calldata from allowance > deposit_amount to allowance >= deposit_amount; added a related test case.
packages/orderbook/test/js_api/vault.test.ts Added two new test cases to verify error handling when allowance is equal to or greater than the deposit amount.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant JS_API
    participant Vault

    User->>JS_API: getVaultApprovalCalldata(rpc_url, vault, deposit_amount)
    JS_API->>Vault: read_allowance(owner, transaction_args)
    Vault-->>JS_API: allowance
    alt allowance >= deposit_amount
        JS_API-->>User: Error (InvalidAmount)
    else
        JS_API->>Vault: get_approve_calldata(transaction_args)
        Vault-->>JS_API: calldata
        JS_API-->>User: VaultCalldataResult(calldata)
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure getApprovalCalldata returns an error if deposit >= allowance (#1900)

Suggested labels

test

Suggested reviewers

  • hardyjosh
  • findolor

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

npm warn config production Use --omit=dev instead.
npm error Exit handler never called!
npm error This is an error with npm itself. Please report this error at:
npm error https://github.com/npm/cli/issues
npm error A complete log of this run can be found in: /.npm/_logs/2025-05-28T09_37_29_946Z-debug-0.log

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 55ac48e and 68282d8.

📒 Files selected for processing (2)
  • crates/js_api/src/subgraph/vault.rs (2 hunks)
  • packages/orderbook/test/js_api/vault.test.ts (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
packages/orderbook/test/js_api/vault.test.ts

[error] 324-324: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)


[error] 340-340: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)

🔇 Additional comments (3)
crates/js_api/src/subgraph/vault.rs (2)

143-143: LGTM! Correct fix for the allowance check logic.

The change from allowance > deposit_amount to allowance >= deposit_amount properly handles the edge case where the allowance exactly equals the deposit amount. When the allowance is sufficient (greater than or equal to the deposit amount), no approval calldata should be generated.


576-579: Good test coverage for the edge case.

The new test case properly verifies that when the allowance equals the deposit amount (both 100), the function correctly returns an InvalidAmount error, ensuring no unnecessary approval calldata is generated.

packages/orderbook/test/js_api/vault.test.ts (1)

313-327: Well-designed test case for equal allowance scenario.

This test properly verifies that when the requested approval amount equals the current allowance (both 100), the function correctly rejects the request with an "Invalid amount" error, consistent with the updated logic in the Rust implementation.

🧰 Tools
🪛 Biome (1.9.4)

[error] 324-324: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)

Comment on lines +329 to +343
it('should handle approval amount less than allowance', async () => {
// Allowance is 100, and user tries to approve 90, so there should be approval calldata
await mockServer.forPost('/rpc').thenReply(
200,
JSON.stringify({
jsonrpc: '2.0',
id: 1,
result: '0x0000000000000000000000000000000000000000000000000000000000000064'
})
);

const res = await getVaultApprovalCalldata(mockServer.url + '/rpc', vault1, '90');
if (!res.error) assert.fail('expected to reject, but resolved');
assert.equal(res.error.msg, 'Invalid amount');
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Good test coverage for insufficient approval amount.

The test correctly verifies that when the requested approval amount (90) is less than the current allowance (100), the function rejects the request. However, there's a minor style issue to address.

Consider using template literals for better readability:

-		const res = await getVaultApprovalCalldata(mockServer.url + '/rpc', vault1, '90');
+		const res = await getVaultApprovalCalldata(`${mockServer.url}/rpc`, vault1, '90');

Also apply the same fix to line 324:

-		const res = await getVaultApprovalCalldata(mockServer.url + '/rpc', vault1, '100');
+		const res = await getVaultApprovalCalldata(`${mockServer.url}/rpc`, vault1, '100');
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it('should handle approval amount less than allowance', async () => {
// Allowance is 100, and user tries to approve 90, so there should be approval calldata
await mockServer.forPost('/rpc').thenReply(
200,
JSON.stringify({
jsonrpc: '2.0',
id: 1,
result: '0x0000000000000000000000000000000000000000000000000000000000000064'
})
);
const res = await getVaultApprovalCalldata(mockServer.url + '/rpc', vault1, '90');
if (!res.error) assert.fail('expected to reject, but resolved');
assert.equal(res.error.msg, 'Invalid amount');
});
it('should handle approval amount less than allowance', async () => {
// Allowance is 100, and user tries to approve 90, so there should be approval calldata
await mockServer.forPost('/rpc').thenReply(
200,
JSON.stringify({
jsonrpc: '2.0',
id: 1,
result: '0x0000000000000000000000000000000000000000000000000000000000000064'
})
);
const res = await getVaultApprovalCalldata(`${mockServer.url}/rpc`, vault1, '90');
if (!res.error) assert.fail('expected to reject, but resolved');
assert.equal(res.error.msg, 'Invalid amount');
});
🧰 Tools
🪛 Biome (1.9.4)

[error] 340-340: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)

🤖 Prompt for AI Agents
In packages/orderbook/test/js_api/vault.test.ts around lines 324 and 329 to 343,
the test uses string concatenation or plain strings where template literals
would improve readability. Update the code to use template literals for
constructing strings, especially in the getVaultApprovalCalldata call and any
related string usage, to enhance clarity and maintain consistency.

@hardyjosh hardyjosh enabled auto-merge May 28, 2025 17:44
hardyjosh and others added 2 commits May 28, 2025 18:44
…ror-ie-no-approval-required-if-deposit-allowance
…ror-ie-no-approval-required-if-deposit-allowance
@hardyjosh hardyjosh merged commit 1d2fa8c into main May 30, 2025
17 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Sep 6, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getApprovalCalldata should return an error (i.e. no approval required) if deposit >= allowance

3 participants